基于DSS的先侦听后推送式流媒体转发

http://blog.csdn.net/xiejiashu/article/details/8298583

   前面文章中说到的,DSS转发可以划分为先拉后推和先侦听后推送两种模式,今天我们解析的是DSS进行的先侦听后推送的流程,具体流程可以大致描述为:源端或者中继端(我们称之为推送端)先通过主动的连接,告知推送端信息(ID,IP等等),服务器维护与源端的会话Session,建立一定的保活与超时机制,并通过此路Session相互交换控制或者上送信息,其中就包含流媒体推送的命令。可按照具体的需求,服务器可通过命令发送的方式,开启源端的推送流程(通过已经建立的Session自定义开启交互的命令),也可以是源端主动开始推送流程(DSS中描述为Broadcast广播),具体的RTSP推送流程大致为:Announce、Setup、Play、RTP(DSS为RTP over TCP)。这种模式的转发通常用于类似于3G视频监控这种难以穿透的网络类型的数据的转发。

      那么我们就不具体介绍关于DSS对会话的维护以及各自自定义的RTSP头字段的操作等等,主要就步骤:Announce->Setup->Play->RTP数据接收与转发进行详细的分析。在DSS中,处理推送报文的模块为QTSSReflectorModule,其中维护了一个静态的转发列表sSessionMap,用于存储各个转发会话的信息。下面就对具体的报文解析和数据处理进行分析。

      Announce:RTSP Announce命令为源端向服务器端主动发起的上报本地媒体sdp信息的命令,处理函数为QTSSReflectorModule模块的DoAnnounce()函数,这里就只对该函数的重点部分进行解析,不全部一一描述了。首先判断server配置中的enable_broadcast_announce字段是否为true,开启了广播推送转发,在通过获取inParams->inRTSPRequest(在RTSPSession::Run调用前复制的当前请求的rtspRequest对象)的字典中的qtssRTSPReqLocalPath键值作为标识转发的唯一区别(例如:.\Movies/test.sdp,必须以sdp结尾,可以修改sSDPSuffix进行配置),这里的值既是一个标识,又是一个路径,用于存储获取到的sdp数据,后面此标识作为存储于sSessionMap中对象的键值。函数中通过对头字段的解析,获取到Content-Length:字段值,进而去读取具体的spd值,再存储到qtssRTSPReqLocalPath路径中,返回200 OK。

      Setup:这里的只解析DoSetup中isPush为true(表示为推送的Session)这条路路径,具体isPush值由Setup请求中的mode值有关,mode="receive" || mode="record"表示isPush为true,

[cpp]  view plain copy
  1. {             
  2.             theSession = DoSessionSetup(inParams, qtssRTSPReqFilePathTrunc,isPush,&foundSession); //根据前面Announce中存储于qtssRTSPReqLocalPath的路径读取sdp信息,创建转发会话ReflectorSession,或者直接引用已经存在的Session  
  3.             if (theSession == NULL)  
  4.                 return QTSS_RequestFailed;    
  5.             // This is an incoming data session. Set the Reflector Session in the ClientSession  
  6.             theErr = QTSS_SetValue(inParams->inClientSession, sClientBroadcastSessionAttr, 0, &theSession, sizeof(theSession));//ReflectorSession附属于RTPSession中的sClientBroadcastSessionAttr字典  
  7.             Assert(theErr == QTSS_NoErr);  
  8.             //qtss_printf("QTSSReflectorModule.cpp:SETsession    sClientBroadcastSessionAttr=%lu theSession=%lu err=%ld \n",(UInt32)sClientBroadcastSessionAttr, (UInt32) theSession,theErr);  
  9.             (void) QTSS_SetValue(inParams->inClientSession, qtssCliSesTimeoutMsec, 0, &sBroadcasterSessionTimeoutMilliSecs, sizeof(sBroadcasterSessionTimeoutMilliSecs));  
  10.        }  

这里需要注意的是,当我们前面已经有一路相同qtssRTSPReqLocalPath路径的ReflectorSession存在的时候,将不进行再创建,直接Resolve原有的ReflectorSession,所以会出现一种情况,当开始的推送与后面进行的推送音视频sdp不一致的时候,就会出现错误,所以, ReflectorSession的引用与释放需要注意!

完成ReflectorSession的创建,下一步解析track ID,具体的解析方法可以根据自己的实际应用,有的按照track%d解析,有的按照trackID=%d解析,再根据trackId获取具体的track sdp信息,AddRTPStream创建对应于具体track的RTP流

[cpp]  view plain copy
  1. theStreamInfo->fSetupToReceive = true;//标识流转发的建立  
  2. // This is an incoming data session. Set the Reflector Session in the ClientSession  
  3. theErr = QTSS_SetValue(inParams->inClientSession, sClientBroadcastSessionAttr, 0, &theSession, sizeof(theSession));//设置转发会话的RTPSession字典的sClientBroadcastSessionAttr字段  
  4. Assert(theErr == QTSS_NoErr);  
  5.       
  6. if (theSession != NULL)  
  7.     theSession->AddBroadcasterClientSession(inParams);//设置ReflectorSession的fBroadcasterSession属性为inParams->inClientSession,呵呵比较乱噢,相当于相互引用  


      Play: 具体到DoPlay过程,isPush为true的路径就比较简单了,只是将推送的RTSPSession中的sRTSPBroadcastSessionAttr属性设置为前面DoSetup中获取到的ReflectorSession

[cpp]  view plain copy
  1. theLen = sizeof(inSession);  
  2. theErr = QTSS_GetValue(inParams->inClientSession, sClientBroadcastSessionAttr, 0, &inSession, &theLen);//DoSetup()中已经设置sClientBroadcastSessionAttr属性  
  3. if (theErr != QTSS_NoErr)  
  4.     return QTSS_RequestFailed;  
  5.       
  6. theErr = QTSS_SetValue(inParams->inClientSession, sKillClientsEnabledAttr, 0, &sTearDownClientsOnDisconnect, sizeof(sTearDownClientsOnDisconnect));  
  7. if (theErr != QTSS_NoErr)  
  8.     return QTSS_RequestFailed;  
  9.   
  10. Assert(inSession != NULL);  
  11.   
  12. theErr = QTSS_SetValue(inParams->inRTSPSession, sRTSPBroadcastSessionAttr, 0, &inSession, sizeof(inSession));//设置到inParams->inRTSPSession的sRTSPBroadcastSessionAttr属性  
  13. f (theErr != QTSS_NoErr)  
  14.     return QTSS_RequestFailed;  


      RTP数据处理:ProcessRTPData(),这里只处理RTP over TCP的数据,根据RTP数据中的channel值,调用特定的ReflectorStream进行处理和转发,具体函数为:ProcessRTPData(),先通过前面在DoSetup() & isPush为true时设置的sRTSPBroadcastSessionAttr属性,获取ReflectorSession

[cpp]  view plain copy
  1. ReflectorSession* theSession = NULL;  
  2.     UInt32 theLen = sizeof(theSession);  
  3.     QTSS_Error theErr = QTSS_GetValue(inParams->inRTSPSession, sRTSPBroadcastSessionAttr, 0, &theSession, &theLen);  
  4.     if (theSession == NULL || theErr != QTSS_NoErr)   
  5.         return QTSS_NoErr;  

再根据channelID获取具体的ReflectorStream并进行数据推送,给具体的ReflectorStream进行处理

[cpp]  view plain copy
  1. UInt32 inIndex = packetChannel / 2; // one stream per every 2 channels rtcp channel handled below  
  2. ReflectorStream* theStream = NULL;  
  3. if (inIndex < numStreams)   
  4. {   theStream = theSession->GetStreamByIndex(inIndex);//获取对应track的ReflectorStream  
  5.   
  6.     SourceInfo::StreamInfo* theStreamInfo =theStream->GetStreamInfo();    
  7.     UInt16 serverReceivePort =theStreamInfo->fPort;           
  8.   
  9.     Bool16 isRTCP =false;  
  10.     if (theStream != NULL)  
  11.     {   if (packetChannel & 1)  
  12.         {   serverReceivePort ++;  
  13.             isRTCP = true;  
  14.         }  
  15.         theStream->PushPacket(rtpPacket,packetDataLen, isRTCP);//推送数据给ReflectorStream并转发给分发列表  
  16.     }  
  17. }  

好了,今天就到这里,接下来的文章中,我们将讲述客户端如何接入到ReflectorSession中,以及theStream->PushPacket如何将rtpPacket推送至各个接入到的Output中~!

 

流媒体开发交流群:288214068
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值