开源项目 webRTC 转成 RTMP输出

这个项目基于kurento tutorial的hello world构建。

项目地址:https://github.com/godka/kurento-rtmp  

sdp参考:https://www.cnblogs.com/bigben0123/p/14229018.html

sdp是个文本文件,vlc拿到后可以直接直播。 

 

使用技术包括c编译生成的kurento webrtc服务;nodejs充当web服务接收websocket的sdp交换;nodejs同时启动了node内置的流媒体服务node_media_server。

原理是摄像头采集webRTC的,发送到kurento服务器。nodejs server充当kurento的客户端,接收sdp offer,根据offer生成自定义得sdp,ffmpeg将sdp和rtp://xxx内容用rtmp推到流媒体服务器node_media_server.

(

sdp是描述RTP包的信息包文件,需要手动加入时间戳等信息,并且在打包rtp时填写。代码实现是,你调用ffmpeg库的函数接口,打包sdp头信息,并将视频数据打包称rtp,然后发送到http://xxxxxx

udp:
推送:ffmpeg -re -i test.264 -vcodec copy -f h264 udp://10.0.192.82:6970

拉取:ffplay -f h264 udp://10.0.192.82:6970

rtp:
ffmpeg -re -i test.264 -vcodec copy -f rtp rtp://10.0.192.82:6970>test.sdp  
ffplay test.sdp

  )

从页面采集摄像头数据用webRTC,通过ws发送到服务端。在 webRtcEndpoint.processOffer处理客户端发来得sdp offer。我们自己生成 rtp的sdpRtpOfferString,将它传递给rtp的

rtpEndpoint.processOffer(sdpRtpOfferString处理。成功后启动ffmpeg,对这个sdpRtpOfferString作为 -i的参数做发送到rtmp服务,即nodejs的Node-Media-Server。这样就可以用http flv播放了。 
  1. 构建:

1.install node && npm  需要nodejs npm安装:https://www.cnblogs.com/schips/p/12402412.html
2.git clone https://github.com/godka/kurento-rtmp
3.cd kurento-rtmp
4.npm install
5.node server.js
6.Open https://yourhost:8443 on Chrome or Firefox
7.Click Start button and have fun!

也可以参考kurento tutorial的hello world安装:

Be sure to install Bower and Node.js version 8.x in your system. In an Ubuntu machine, you can install both as follows:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g bower

Also, Node.js should already include NPM, the Node.js package manager.

To launch the application, you need to clone the GitHub project where this demo is hosted, install it and run it:

git clone https://github.com/Kurento/kurento-tutorial-node.git
cd kurento-tutorial-node/kurento-hello-world
git checkout master
npm install
cd static
bower install --allow-root
cd ..
npm start

If you have problems installing any of the dependencies, please remove them and clean the npm cache, and try to install them again:

rm -r node_modules
npm cache clean

Access the application connecting to the URL https://localhost:8443/ in a WebRTC capable browser (Chrome, Firefox).

Note

These instructions work only if Kurento Media Server is up and running in the same machine as the tutorial. However, it is possible to connect to a remote KMS in other machine, simply adding the argument ws_uri to the npm execution command, as follows:

npm start -- --ws_uri=ws://{KMS_HOST}:8888/kurento

In this case you need to use npm version 2. To update it you can use this command:

sudo npm install npm -g

 

观看rmtp地址:

启动后,推webrtc地址:https://localhost:8443/.

如果不能回显,去https://ossrs.net/players/srs_player.html,在srs播放器里面地址输入:rtmp://192.168.16.133/live/127.0.0.1_55002,可以播放。地址是从下面log中找到的:

修改server.js,推流地址变成rtmp://192.168.16.133/live/55002,去掉127.0.0.1_

不用搭建srs,直接用播放器访问http flv地址,即可播放:http://192.168.16.133:8000/live/55002.flv 

server.js是服务器. 这个开源用了Node-Media-Server。它是支持rtmp,http flv等的流媒体服务器。

ffmpeg推流用的命令  用sdp生成rtp命令

/*ffmpeg 
-protocol_whitelist "file,udp,rtp" 
-i test.sdp 
-vcodec copy 
-f flv 
rtmp://localhost/live/stream
*/
/*
SDP:
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 57.71.100
m=video 55000 RTP/AVP 96
b=AS:200
a=rtpmap:96 H264/90000
*/

 实现回显:webrtc连接成功后,rtp再去回连webrtc。这样就回显。

function connectMediaElements(webRtcEndpoint, rtpEndpoint, callback) {
    webRtcEndpoint.connect(rtpEndpoint, function (error) {
        if (error) {
            return callback(error);
        }
        rtpEndpoint.connect(webRtcEndpoint, function (error) {//rtpEndPoint又连接回去webRTC
            if (error) {
                return callback(error);
            }
            return callback(null);
        });
    });
}

配置sdpStream类型, H264,这里时为了生成 rtp

function generateSdpStreamConfig(nodeStreamIp, port, callback) {
... ...
    var sdpRtpOfferString = 'v=0\n';
    sdpRtpOfferString += 'o=- 0 0 IN IP4 ' + nodeStreamIp + '\n';
    sdpRtpOfferString += 's=KMS\n';
    sdpRtpOfferString += 'c=IN IP4 ' + nodeStreamIp + '\n';
    sdpRtpOfferString += 't=0 0\n';
    sdpRtpOfferString += 'm=video ' + port + ' RTP/AVP 96\n';
    sdpRtpOfferString += 'a=rtpmap:96 H264/90000\n';
    sdpRtpOfferString += 'a=fmtp:96 packetization-mode=1\n';
    return callback(null, sdpRtpOfferString);

增加音频处理部分

    sdpRtpOfferString += 'm=audio 49170 RTP/AVP 97\n' ;
              sdpRtpOfferString += 'a=recvonly\n' ;
              sdpRtpOfferString += 'a=rtpmap:97 PCMU/8000\n' ;
              sdpRtpOfferString += 'a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1508\n' ;

 

在rtpEndpoint.processOffer去使用:

webrtc连接成功 => rtc.processOffer => 成功后按照sdp config 调用 rtp.processOffer =>callback(sdpanswer):

                     
webRtcEndpoint.processOffer(sdpOffer, function (error, sdpAnswer) { if (error) { pipeline.release(); return callback(error); } sessions[sessionId] = { 'pipeline': pipeline, 'webRtcEndpoint': webRtcEndpoint } var streamPort = 55000;
//每个新session会调用这里一次
var streamIp = '127.0.0.1';//Test ip generateSdpStreamConfig(streamIp, streamPort, function (err, sdpRtpOfferString) { if (err) { return callback(error); } rtpEndpoint.processOffer(sdpRtpOfferString, function (error) { if (error) { return callback(error); } console.log('start process on: rtp://' + streamIp + ':' + streamPort); console.log('recv sdp answer:', sdpAnswer);
bindFFmpeg(streamIp, streamPort, sdpRtpOfferString, ws) return callback(null, sdpAnswer); }); }); })

加入session 和 ffmpeg:

全局
var session_index = 0;

用户连接端口生成:

                        //对于RTP,偶数端口被用来传输数据,奇数端口用来传输RTCP包
                        var streamPort = 55000 + (session_index * 2);
                        var audioPort = 49170 + (session_index * 2);
                        session_index++;    //change to next port

 

webRtcEndpoint.setMaxVideoRecvBandwidth(200)

 

node-media-server加入rtmp,flv

const NodeMediaServer = require('node-media-server').NodeMediaServer
const rtmp_server_config = {
    rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30
    },
    http: {
        port: 8000,
        allow_origin: '*'
    }
};

var nms = new NodeMediaServer(rtmp_server_config);
nms.run();

 整体流程图:

 浏览器console log

View Code

nodejs server.js日志输出

View Code

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章