使用Live555类库实现的网络直播系统
来源:互联网 发布:找淘宝推广团队 编辑:程序博客网 时间:2024/06/06 02:31
Live555主要有四个类库:
libUsageEnvironment.lib;libliveMedia.lib;libgroupsock.lib;libBasicUsageEnvironment.lib
将这四个类库以及相关的头文件导入VC++2010之后,可以轻松实现网络直播系统。
在这里直接贴上完整代码,粘贴到VC里面就可以运行。
注:程序运行后,使用播放器软件(VLC Media Player,FFplay等),打开URL:rtp://239.255.42.42:1234,即可收看直播的视频。
-
-
-
-
-
- #include "stdafx.h"
-
- #include "liveMedia.hh"
- #include "BasicUsageEnvironment.hh"
- #include "GroupsockHelper.hh"
-
-
-
- #ifdef USE_SSM
- Boolean const isSSM = True;
- #else
- Boolean const isSSM = False;
- #endif
-
-
-
- #define TRANSPORT_PACKET_SIZE 188
- #define TRANSPORT_PACKETS_PER_NETWORK_PACKET 7
-
-
- UsageEnvironment* env;
- char const* inputFileName = "test.ts";
- FramedSource* videoSource;
- RTPSink* videoSink;
-
- void play();
-
- int main(int argc, char** argv) {
-
- TaskScheduler* scheduler = BasicTaskScheduler::createNew();
- env = BasicUsageEnvironment::createNew(*scheduler);
-
-
- char const* destinationAddressStr
- #ifdef USE_SSM
- = "232.255.42.42";
- #else
- = "239.255.42.42";
-
- #endif
- const unsigned short rtpPortNum = 1234;
- const unsigned short rtcpPortNum = rtpPortNum+1;
- const unsigned char ttl = 7;
-
-
- struct in_addr destinationAddress;
- destinationAddress.s_addr = our_inet_addr(destinationAddressStr);
- const Port rtpPort(rtpPortNum);
- const Port rtcpPort(rtcpPortNum);
-
- Groupsock rtpGroupsock(*env, destinationAddress, rtpPort, ttl);
- Groupsock rtcpGroupsock(*env, destinationAddress, rtcpPort, ttl);
- #ifdef USE_SSM
- rtpGroupsock.multicastSendOnly();
- rtcpGroupsock.multicastSendOnly();
- #endif
-
-
-
- videoSink =
- SimpleRTPSink::createNew(*env, &rtpGroupsock, 33, 90000, "video", "mp2t",
- 1, True, False );
-
-
- const unsigned estimatedSessionBandwidth = 5000;
- const unsigned maxCNAMElen = 100;
- unsigned char CNAME[maxCNAMElen+1];
- gethostname((char*)CNAME, maxCNAMElen);
- CNAME[maxCNAMElen] = '\0';
- #ifdef IMPLEMENT_RTSP_SERVER
- RTCPInstance* rtcp =
- #endif
- RTCPInstance::createNew(*env, &rtcpGroupsock,
- estimatedSessionBandwidth, CNAME,
- videoSink, NULL , isSSM);
-
-
- #ifdef IMPLEMENT_RTSP_SERVER
- RTSPServer* rtspServer = RTSPServer::createNew(*env);
-
- if (rtspServer == NULL) {
- *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
- exit(1);
- }
- ServerMediaSession* sms
- = ServerMediaSession::createNew(*env, "testStream", inputFileName,
- "Session streamed by \"testMPEG2TransportStreamer\"",
- isSSM);
- sms->addSubsession(PassiveServerMediaSubsession::createNew(*videoSink, rtcp));
- rtspServer->addServerMediaSession(sms);
-
- char* url = rtspServer->rtspURL(sms);
- *env << "Play this stream using the URL \"" << url << "\"\n";
- delete[] url;
- #endif
-
-
- *env << "开始发送流媒体...\n";
- play();
-
- env->taskScheduler().doEventLoop();
-
- return 0;
-
- }
-
- void afterPlaying(void* ) {
- *env << "...从文件中读取完毕\n";
-
- Medium::close(videoSource);
-
-
- play();
- }
-
- void play() {
- unsigned const inputDataChunkSize
- = TRANSPORT_PACKETS_PER_NETWORK_PACKET*TRANSPORT_PACKET_SIZE;
-
-
-
- ByteStreamFileSource* fileSource
- = ByteStreamFileSource::createNew(*env, inputFileName, inputDataChunkSize);
- if (fileSource == NULL) {
- *env << "无法打开文件 \"" << inputFileName
- << "\" 作为 file source\n";
- exit(1);
- }
-
-
- videoSource = MPEG2TransportStreamFramer::createNew(*env, fileSource);
-
-
- *env << "Beginning to read from file...\n";
- videoSink->startPlaying(*videoSource, afterPlaying, videoSink);
- }
原文链接:http://blog.csdn.net/leixiaohua1020/article/details/11696449
3 0