Java将Live Video InputStream发送到Red5媒体服务器

时间:2013-01-12 13:54:18

标签: java inputstream h.264 red5 rtmp

我有来自Java的Socket的InputStreamInputStream是H.264视频直播。我想将此视频InputStream发送到red5媒体服务器,以便其他客户端可以观看。

我找到了这个例子

package com.ryong21.example.publisher;import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.red5.server.messaging.IMessage;
import org.red5.server.stream.message.RTMPMessage;
import org.red5.server.stream.provider.FileProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Publisher {
            public static void main(String[] args) throws IOException, InterruptedException {

            Logger log = LoggerFactory.getLogger(Publisher.class);

            String publishName = "test";
            String localFile = "2.mp3";
            String host = "rtmpxn.91kge.com";
            int port = 1935;
            String app = "live";

            IMessage msg = null;
            int timestamp = 0;
            int lastTS = 0;

            PublishClient client = new PublishClient();

            client.setHost(host);
            client.setPort(port);
            client.setApp(app);                    

            client.start(publishName, "live", null);

            while(client.getState() != PublishClient.PUBLISHED){
                    Thread.sleep(500);
            }

            FileProvider fp = new FileProvider(new File(localFile));        

            Date begin = new Date();
            log.debug(begin.toString());

            while(true){
                    msg = fp.pullMessage(null);

                    if(msg == null){
                            log.debug("done!");
                            break;
                    }
                    timestamp = ((RTMPMessage)msg).getBody().getTimestamp();
                    Thread.sleep(timestamp - lastTS);
                    lastTS = timestamp;
                    client.pushMessage( msg);
            }      
            Date end = new Date();
            log.debug(end.toString());
            client.stop();
    }

}`

我的问题是如何使用套接字InputStream,而不是FileProvider中使用的文件。 InputStream已经采用H.264编码。

1 个答案:

答案 0 :(得分:4)

好的,在深入研究视频流后,我发现我首先必须将InputStream转换为容器格式文件。可能的容器格式为.mp4或.flv

创建容器文件后,我现在可以将其发布到red5媒体服务器。

相关问题