参照openRTSP写的一个RTSP client

 
  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   
  119. }  
### 回答1: 在 C++ 中,您可以使用开源库如 FFmpeg 来实现 RTSP 协议的操作。以下是一个通过 RTSP 协议获取视频分辨率的示例代码: ``` #include <iostream> #include <string> #include <stdio.h> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <algorithm> #include <cctype> #include <time.h> #include <errno.h> #include <unistd.h> #include <libavformat/avformat.h> using namespace std; int main(int argc, char *argv[]) { av_register_all(); AVFormatContext *pFormatCtx = avformat_alloc_context(); if (avformat_open_input(&pFormatCtx, "rtsp://192.168.1.100:8554/test", NULL, NULL) != 0) { cout << "Couldn't open input stream." << endl; return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { cout << "Couldn't find stream information." << endl; return -1; } for (int i = 0; i < pFormatCtx->nb_streams; i++) { AVStream *stream = pFormatCtx->streams[i]; if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { cout << "Video Resolution: " << stream->codecpar->width << "x" << stream->codecpar->height << endl; } } avformat_close_input(&pFormatCtx); return 0; } ``` 需要注意的是,您需要在计算机上安装 FFmpeg 库,并且在编译时需要链接相应的库。具体链接方式请参考 FFmpeg 的文档说明。 ### 回答2: 通过C++编一个通过RTSP协议获取视频分辨率的程序需以下几个步骤: 1. 导入相关库:首先需要导入C++库,以便在程序中进行网络通信。常用的库有libcurl、Boost.Asio等,这些库提供了实现RTSP协议的功能。 2. 建立网络连接:使用库中提供的函数建立与RTSP服务器的连接。这可以通过curl_easy_init()函数创建一个CURL对象,并使用curl_easy_setopt()函数设置连接选项,如设置服务器URL、用户名和密码、连接超时时间等。 3. 发送RTSP请求:使用curl_easy_perform()函数发送RTSP请求。根据RTSP协议规范,可以发送DESCRIBE请求获取媒体信息,或发送OPTIONS请求获取支持的方法等。这些请求会返回服务器的回应,其中包含了视频分辨率等相关信息。 4. 解析回应:接收到服务器的回应后,需要解析回应的内容,提取出视频分辨率等所需信息。可以使用字符串处理函数将回应内容进行解析,提取出分辨率信息。 5. 输出结果:将解析得到的视频分辨率信息输出到控制台或保存到文件中等。 6. 清理资源:使用curl_easy_cleanup()函数释放CURL对象,关闭与服务器的连接,释放内存资源。 C++代码示例: ```cpp #include <iostream> #include <curl/curl.h> int main() { CURL *curl; CURLcode res; // 初始化CURL对象 curl = curl_easy_init(); if (curl) { // 设置RTSP服务器URL curl_easy_setopt(curl, CURLOPT_URL, "rtsp://example.com/video"); // 发送DESCRIBE请求 curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE); // 接收回应 res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); return 1; } // 解析回应内容,提取分辨率信息 // 输出结果 // 清理资源 curl_easy_cleanup(curl); } return 0; } ``` 上述代码仅为示例,具体的实现需要根据实际情况进行调整和完善。 ### 回答3: 下面是一个用C++编的程序,可以通过RTSP协议获取视频分辨率: ```cpp #include <iostream> #include <liveMedia.hh> #include <BasicUsageEnvironment.hh> int main() { TaskScheduler* scheduler = BasicTaskScheduler::createNew(); UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler); char const* url = "rtsp://example.com/video"; // 换成你自己的RTSP视频地址 char const* userAgent = "RTSPClient"; int verbosityLevel = 0; // 设置日志详细程度 RTSPClient* rtspClient = openRTSP(*env, url, verbosityLevel, userAgent); if (rtspClient == NULL) { *env << "Failed to open RTSP URL: " << env->getResultMsg() << "\n"; return 1; } MediaSession* session = rtspClient->session(); int numSubsessions = session->subsessionCount(); for (int i = 0; i < numSubsessions; ++i) { MediaSubsession* subsession = session->subsession(i); if (subsession->readSource() != NULL) { char const* mediumName = subsession->mediumName(); char const* codecName = subsession->codecName(); int videoWidth = subsession->videoWidth(); int videoHeight = subsession->videoHeight(); std::cout << "媒体类型: " << mediumName << ", 编码方式: " << codecName << ", 分辨率: " << videoWidth << " x " << videoHeight << std::endl; } } rtspClient->closeMediaSession(); env->taskScheduler().doEventLoop(); Medium::close(rtspClient); env->reclaim(); delete scheduler; return 0; } ``` 在程序中,我们首先通过传入的RTSP视频地址创建一个`RTSPClient`对象。然后,我们获取到`MediaSession`对象,并遍历其中的`MediaSubsession`对象。对于每个具有有效的`readSource()`的子会话,我们可以得到媒体类型,编码方式和视频的宽度和高度。 请注意,你需要在程序中修改`url`的值为你实际的RTSP视频地址。另外,你需要链接`liveMedia`和`BasicUsageEnvironment`库以及相关的依赖项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值