本文主要是介绍用live555将内网摄像机视频推送到外网服务器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近很多人问,如何将内网的摄像机流媒体数据发布到公网,如果用公网与局域网间的端口映射方式太过麻烦,一个摄像机要做一组映射,而且不是每一个局域网都是有固定ip地址,即使外网主机配置好了每一个摄像机的映射地址,也有可能会因为宽带公网ip地址变动而导致配置无效。
再换一个应用场景,当我们的所有IP摄像机都部署在一个没有有线网络的环境里面,所有的流媒体数据都要通过3G/4G网络发布出去。那么就必须有这么一个服务单元,能够通过先拉后推的方式,将内网的流媒体数据,推送并发布到外网的流媒体服务器上去:
在实现先拉后推式转发之前,我们先熟悉下live555的运转模式,live555主要运转的是一个source与sink的循环,sink想要数据,就调用source的getNextFrame,source获取到数据后,再调用afterGettingFrame回调,返回给sink数据,sink处理完后,再调用source的getNextFrame,如此循环。那么我们这里要实现从摄像机获取数据,那么我们的source就是一个RTPSource,我们又需要将数据以RTP的方式发送给流媒体服务器,那么我们的sink就是一个RTPSink,我们需要打通的就是一个RTPSource到一个RTPSink的过程。
ok,live555已经帮我们实现了大部分的功能,我们只需要将已有的部分组合起来就行了,这里我们主要用到的就是live555的ProxyServerMediaSession类和DarwinInjector类,我们用ProxyServerMediaSession从摄像机获取流媒体,再用DarwinInjector推送到Darwin Streaming Server,主要实现流程在下面代码注释中:
-
-
-
-
-
-
-
-
-
-
- #include "liveMedia.hh"
- #include "BasicUsageEnvironment.hh"
- #include "RTSPCommon.hh"
-
- char* server = "www.easydss.com";
- int port = 8554;
- char* streamName = "live.sdp";
- char* src = "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp";//源端URL
-
- UsageEnvironment* env = NULL;
- TaskScheduler* scheduler = NULL;
- char eventLoopWatchVariable = 0;
-
- DarwinInjector* injector = NULL;
- FramedSource* vSource = NULL;
- FramedSource* aSource = NULL;
-
- RTPSink* vSink = NULL;
- RTPSink* aSink = NULL;
-
- Groupsock* rtpGroupsockVideo = NULL;
- Groupsock* rtpGroupsockAudio = NULL;
-
- ProxyServerMediaSession* sms = NULL;
-
-
- bool RedirectStream(char const* ip, unsigned port);
-
-
- void afterPlaying(void* clientData);
-
-
- void sleep(void* clientSession)
- {
- char* var = (char*)clientSession;
- *var = ~0;
- }
-
-
- int main(int argc, char** argv)
- {
-
- scheduler = BasicTaskScheduler::createNew();
- env = BasicUsageEnvironment::createNew(*scheduler);
-
-
- sms = ProxyServerMediaSession::createNew(*env, NULL, src);
-
-
- while(sms->numSubsessions() <= 0 )
- {
- char fWatchVariable = 0;
- env->taskScheduler().scheduleDelayedTask(2*1000000,(TaskFunc*)sleep,&fWatchVariable);
- env->taskScheduler().doEventLoop(&fWatchVariable);
- }
-
-
- RedirectStream(server, port);
-
- env->taskScheduler().doEventLoop(&eventLoopWatchVariable);
-
- return 0;
- }
-
-
-
- bool RedirectStream(char const* ip, unsigned port)
- {
-
- if( sms == NULL) return false;
-
-
- if( sms->numSubsessions() <= 0 )
- {
- *env << "sms numSubsessions() == 0\n";
- return false;
- }
-
-
- injector = DarwinInjector::createNew(*env);
-
- struct in_addr dummyDestAddress;
- dummyDestAddress.s_addr = 0;
- rtpGroupsockVideo = new Groupsock(*env, dummyDestAddress, 0, 0);
-
- struct in_addr dummyDestAddressAudio;
- dummyDestAddressAudio.s_addr = 0;
- rtpGroupsockAudio = new Groupsock(*env, dummyDestAddressAudio, 0, 0);
-
- ServerMediaSubsession* subsession = NULL;
- ServerMediaSubsessionIterator iter(*sms);
- while ((subsession = iter.next()) != NULL)
- {
- ProxyServerMediaSubsession* proxySubsession = (ProxyServerMediaSubsession*)subsession;
-
- unsigned streamBitrate;
- FramedSource* source = proxySubsession->createNewStreamSource(1, streamBitrate);
-
- if (strcmp(proxySubsession->mediumName(), "video") == 0)
- {
-
- vSource = source;
- unsigned char rtpPayloadType = proxySubsession->rtpPayloadFormat();
-
- vSink = proxySubsession->createNewRTPSink(rtpGroupsockVideo,rtpPayloadType,source);
-
- injector->addStream(vSink,NULL);
- }
- else
- {
-
- aSource = source;
- unsigned char rtpPayloadType = proxySubsession->rtpPayloadFormat();
-
- aSink = proxySubsession->createNewRTPSink(rtpGroupsockVideo,rtpPayloadType,source);
-
- injector->addStream(aSink,NULL);
- }
- }
-
-
- if (!injector->setDestination(ip, streamName, "live555", "LIVE555", port))
- {
- *env << "injector->setDestination() failed: " << env->getResultMsg() << "\n";
- return false;
- }
-
-
- if((vSink != NULL) && (vSource != NULL))
- vSink->startPlaying(*vSource,afterPlaying,vSink);
-
-
- if((aSink != NULL) && (aSource != NULL))
- aSink->startPlaying(*aSource,afterPlaying,aSink);
-
- *env << "\nBeginning to get camera video...\n";
- return true;
- }
-
-
-
- void afterPlaying(void* clientData)
- {
- if( clientData == NULL ) return;
-
- if(vSink != NULL)
- vSink->stopPlaying();
-
- if(aSink != NULL)
- aSink->stopPlaying();
-
- if(injector != NULL)
- {
- Medium::close(*env, injector->name());
- injector == NULL;
- }
-
- ServerMediaSubsession* subsession = NULL;
- ServerMediaSubsessionIterator iter(*sms);
- while ((subsession = iter.next()) != NULL)
- {
- ProxyServerMediaSubsession* proxySubsession = (ProxyServerMediaSubsession*)subsession;
- if (strcmp(proxySubsession->mediumName(), "video") == 0)
- proxySubsession->closeStreamSource(vSource);
-
- else
- proxySubsession->closeStreamSource(aSource);
- }
-
- if(vSink != NULL)
- Medium::close(vSink);<pre name="code" class="html">
if(aSink != NULL)Medium::close(aSink);if(vSource != NULL)Medium::close(vSource);if(aSource != NULL)Medium::close(aSource);delete rtpGroupsockVideo;rtpGroupsockVideo = NULL;delete rtpGroupsockAudio;rtpGroupsockAudio = NULL;}
程序还有许多要完善的地方,只是一个简单的实现。
源码下载:
http://pan.baidu.com/s/1sj6Ue4l
非常感谢感谢6楼 Boris_Cao_2015 5天前 的回复,是这样的!
- “按着这个代码不同时支持音视频,要修改LIVE555里面DarwinInjector源码, stream channel id记得加1,因为RTCP instance不存在,所以RTP流的stream channel id必须自动加1, 否则跟RTCP的stream channel id重合,这就是原因。跟楼主和大家分享。嘻嘻!”
------------------------------------------------------------
这篇关于用live555将内网摄像机视频推送到外网服务器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!