现在的位置: 首页 > 综合 > 正文

参照openRTSP写的一个RTSP client 加了一些注解

2018年04月02日 ⁄ 综合 ⁄ 共 6425字 ⁄ 字号 评论关闭

From: http://blog.csdn.net/zblue78/article/details/5968941

  1. #include "liveMedia.hh"  
  2. #include "BasicUsageEnvironment.hh"  
  3. #include "GroupsockHelper.hh"  
  4. UsageEnvironment* env;  
  5. portNumBits tunnelOverHTTPPortNum = 0;  
  6. const char * url="rtsp://127.0.0.1:1935/vod/Extremists.m4v";  
  7. #if defined(__WIN32__) || defined(_WIN32)  
  8. #define snprintf _snprintf  
  9. #endif  
  10. int main(int argc,const char ** argv)  
  11. {  
  12.     //创建BasicTaskScheduler对象  
  13.     TaskScheduler* scheduler = BasicTaskScheduler::createNew();  
  14.     //创建BisicUsageEnvironment对象  
  15.     env = BasicUsageEnvironment::createNew(*scheduler);  
  16.     //创建RTSPClient对象  
  17.     RTSPClient * rtspClient= RTSPClient::createNew(*env);  
  18.     //由RTSPClient对象向服务器发送OPTION消息并接受回应  
  19.     char* optionsResponse=rtspClient->sendOptionsCmd(url);  
  20.     delete [] optionsResponse;  
  21.     //产生SDPDescription字符串(由RTSPClient对象向服务器发送DESCRIBE消息并接受回应,根据回应的信息产生SDPDescription字符串,其中包括视音频数据的协议和解码器类型)  
  22.     char* sdpDescription =rtspClient->describeURL(url);  
  23.     //创建MediaSession对象(根据SDPDescription在MediaSession中创建和初始化MediaSubSession子会话对象)  
  24.     MediaSession* session = MediaSession::createNew(*env, sdpDescription);  
  25.     delete[] sdpDescription;  
  26. /* 
  27. while循环中配置所有子会话对象 
  28. */  
  29.     MediaSubsessionIterator iter(*session);  
  30.     MediaSubsession *subsession;  
  31.     while ((subsession = iter.next()) != NULL) {  
  32.         // Creates a "RTPSource" for this subsession. (Has no effect if it's  
  33.         // already been created.)  Returns True iff this succeeds.  
  34.         if (!subsession->initiate()) {  
  35.             *env << "Unable to create receiver for /"" << subsession->mediumName()  
  36.                 << "/" << subsession->codecName()  
  37.                 << "/" subsession: " << env->getResultMsg() << "/n";  
  38.         } else {  
  39.             *env << "Created receiver for /"" << subsession->mediumName()  
  40.                 << "/" << subsession->codecName()  
  41.                 << "/" subsession (client ports " << subsession->clientPortNum()  
  42.                 << "-" << subsession->clientPortNum()+1 << ")/n";  
  43.             if (subsession->rtpSource() != NULL) {  
  44.                 // Because we're saving the incoming data, rather than playing  
  45.                 // it in real time, allow an especially large time threshold  
  46.                 // (1 second) for reordering misordered incoming packets:  
  47.                 unsigned const thresh = 1000000; // 1 second  
  48.                 subsession->rtpSource()->setPacketReorderingThresholdTime(thresh);  
  49.                 // Set the RTP source's OS socket buffer size as appropriate - either if we were explicitly asked (using -B),  
  50.                 // or if the desired FileSink buffer size happens to be larger than the current OS socket buffer size.  
  51.                 // (The latter case is a heuristic, on the assumption that if the user asked for a large FileSink buffer size,  
  52.                 // then the input data rate may be large enough to justify increasing the OS socket buffer size also.)  
  53.                 int socketNum = subsession->rtpSource()->RTPgs()->socketNum();  
  54.                 unsigned curBufferSize = getReceiveBufferSize(*env, socketNum);  
  55.                 unsigned newBufferSize = setReceiveBufferTo(*env, socketNum, 100000);  
  56.   
  57.             }  
  58.         }  
  59.     }  
  60.     //由RTSPClient对象向服务器发送SETUP消息并接受回应  
  61.     iter.reset();  
  62.     while ((subsession = iter.next()) != NULL) {  
  63.         if (subsession->clientPortNum() == 0) continue// port # was not set  
  64.         if (!rtspClient->setupMediaSubsession(*subsession)) {  
  65.             *env << "Failed to setup /"" << subsession->mediumName()  
  66.                 << "/" << subsession->codecName()  
  67.                 << "/" subsession: " << env->getResultMsg() << "/n";  
  68.         } else {  
  69.             *env << "Setup /"" << subsession->mediumName()  
  70.                 << "/" << subsession->codecName()  
  71.                 << "/" subsession (client ports " << subsession->clientPortNum()  
  72.                 << "-" << subsession->clientPortNum()+1 << ")/n";  
  73.         }  
  74.         if (subsession->rtpSource() != NULL) {  
  75.             // Because we're saving the incoming data, rather than playing  
  76.             // it in real time, allow an especially large time threshold  
  77.             // (1 second) for reordering misordered incoming packets:  
  78.             unsigned const thresh = 1000000; // 1 second  
  79.             subsession->rtpSource()->setPacketReorderingThresholdTime(thresh);  
  80.         }  
  81.     }  
  82.     iter.reset();  
  83.     while ((subsession = iter.next()) != NULL) {  
  84.         if (subsession->readSource() == NULL) continue// was not initiated  
  85.         char outFileName[1000];  
  86.         static unsigned streamCounter = 0;  
  87.         snprintf(outFileName, sizeof outFileName, "%s-%s-%d",  
  88.             subsession->mediumName(),  
  89.             subsession->codecName(), ++streamCounter);  
  90.         FileSink* fileSink;  
  91.         if (strcmp(subsession->mediumName(), "audio") == 0 &&  
  92.             (strcmp(subsession->codecName(), "AMR") == 0 ||  
  93.             strcmp(subsession->codecName(), "AMR-WB") == 0)) {  
  94.                 // For AMR audio streams, we use a special sink that inserts AMR frame hdrs:  
  95.                 fileSink = AMRAudioFileSink::createNew(*env, outFileName);  
  96.         } else if (strcmp(subsession->mediumName(), "video") == 0 &&  
  97.             (strcmp(subsession->codecName(), "H264") == 0)) {  
  98.                 // For H.264 video stream, we use a special sink that insert start_codes:  
  99.                 unsigned int num=0;  
  100.                 SPropRecord * sps=parseSPropParameterSets(subsession->fmtp_spropparametersets(),num);  
  101.                 fileSink = H264VideoFileSink::createNew(*env, outFileName,100000);  
  102.                 struct timeval tv={0,0};  
  103.                 unsigned char start_code[4] = {0x00, 0x00, 0x00, 0x01};  
  104.                 fileSink->addData(start_code, 4, tv);  
  105.                 fileSink->addData(sps[0].sPropBytes,sps[0].sPropLength,tv);  
  106.                 fileSink->addData(start_code, 4, tv);  
  107.                 fileSink->addData(sps[1].sPropBytes,sps[1].sPropLength,tv);  
  108.                 delete[] sps;  
  109.         } else {  
  110.             // Normal case:  
  111.             fileSink = FileSink::createNew(*env, outFileName);  
  112.         }  
  113.         subsession->sink = fileSink;  
  114.         subsession->sink->startPlaying(*(subsession->readSource()),NULL,NULL);  
  115.     }  
  116.     rtspClient->playMediaSession(*session, 0.0f, 0.0f, (float)1.0);  
  117.     env->taskScheduler().doEventLoop(); // does not return  
  118.     return 0; // only to prevent compiler warning  

抱歉!评论已关闭.