DSS Source Code Analyse (10) - RTPSession::Run

本文详细介绍了RTP会话管理中任务运行逻辑,包括如何处理客户端关闭请求、内部状态检查、同步与异步操作调度以及数据包发送流程。特别关注了会话清理角色、发送数据包的时间安排及重传机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SInt64 RTPSession::Run()
{
#if DEBUG
    Assert(fActivateCalled);
#endif
    EventFlags events = this->GetEvents();
    QTSS_RoleParams theParams;
    theParams.clientSessionClosingParams.inClientSession = this;    //every single role being invoked now has this
                                                    //as the first parameter
    
#if RTPSESSION_DEBUGGING
    qtss_printf("RTPSession %"_S32BITARG_": In Run. Events %"_S32BITARG_"\n",(SInt32)this, (SInt32)events);
#endif
    // Some callbacks look for this struct in the thread object
    OSThreadDataSetter theSetter(&fModuleState, NULL);


    //if we have been instructed to go away, then let's delete ourselves
    if ((events & Task::kKillEvent) || (events & Task::kTimeoutEvent) || (fModuleDoingAsyncStuff))
    {
        if (!fModuleDoingAsyncStuff)
    {
        if (events & Task::kTimeoutEvent)
            fClosingReason = qtssCliSesCloseTimeout;
            
        //deletion is a bit complicated. For one thing, it must happen from within
        //the Run function to ensure that we aren't getting events when we are deleting
        //ourselves. We also need to make sure that we aren't getting RTSP requests
        //(or, more accurately, that the stream object isn't being used by any other
        //threads). We do this by first removing the session from the session map.
        
#if RTPSESSION_DEBUGGING
        qtss_printf("RTPSession %"_S32BITARG_": about to be killed. Eventmask = %"_S32BITARG_"\n",(SInt32)this, (SInt32)events);
#endif
        // We cannot block waiting to UnRegister, because we have to
        // give the RTSPSessionTask a chance to release the RTPSession.
        OSRefTable* sessionTable = QTSServerInterface::GetServer()->GetRTPSessionMap();
        Assert(sessionTable != NULL);
        if (!sessionTable->TryUnRegister(&fRTPMapElem))
        {
            this->Signal(Task::kKillEvent);// So that we get back to this place in the code
            return kCantGetMutexIdleTime;
        }
        
            // The ClientSessionClosing role is allowed to do async stuff
            fModuleState.curTask = this;
            fModuleDoingAsyncStuff = true;  // So that we know to jump back to the
            fCurrentModule = 0;             // right place in the code
        
            // Set the reason parameter 
            theParams.clientSessionClosingParams.inReason = fClosingReason;
            
            // If RTCP packets are being generated internally for this stream, 
            // Send a BYE now.
            RTPStream** theStream = NULL;
            UInt32 theLen = 0;
            
            if (this->GetPlayFlags() & qtssPlayFlagsSendRTCP)
            {
                SInt64 byePacketTime = OS::Milliseconds();
                for (int x = 0; this->GetValuePtr(qtssCliSesStreamObjects, x, (void**)&theStream, &theLen) == QTSS_NoErr; x++)
                    if (theStream && *theStream != NULL)
                        (*theStream)->SendRTCPSR(byePacketTime, true);
            }
        }
        
        //at this point, we know no one is using this session, so invoke the
        //session cleanup role. We don't need to grab the session mutex before
        //invoking modules here, because the session is unregistered and
        //therefore there's no way another thread could get involved anyway


        UInt32 numModules = QTSServerInterface::GetNumModulesInRole(QTSSModule::kClientSessionClosingRole);
        {
            for (; fCurrentModule < numModules; fCurrentModule++)
            {  
                fModuleState.eventRequested = false;
                fModuleState.idleTime = 0;
                QTSSModule* theModule = QTSServerInterface::GetModule(QTSSModule::kClientSessionClosingRole, fCurrentModule);
                (void)theModule->CallDispatch(QTSS_ClientSessionClosing_Role, &theParams);


                // If this module has requested an event, return and wait for the event to transpire
                if (fModuleState.eventRequested)
                    return fModuleState.idleTime; // If the module has requested idle time...
            }
        }
        
        return -1;//doing this will cause the destructor to get called.
    }
    
    //if the stream is currently paused, just return without doing anything.
    //We'll get woken up again when a play is issued
    if ((fState == qtssPausedState) || (fModule == NULL))
        return 0;
        
    //Make sure to grab the session mutex here, to protect the module against
    //RTSP requests coming in while it's sending packets
    {
        OSMutexLocker locker(&fSessionMutex);


        //just make sure we haven't been scheduled before our scheduled play
        //time. If so, reschedule ourselves for the proper time. (if client
        //sends a play while we are already playing, this may occur)
        theParams.rtpSendPacketsParams.inCurrentTime = OS::Milliseconds();
        if (fNextSendPacketsTime > theParams.rtpSendPacketsParams.inCurrentTime)
        {
            RTPStream** retransStream = NULL;
            UInt32 retransStreamLen = 0;


            //
            // Send retransmits if we need to
            for (int streamIter = 0; this->GetValuePtr(qtssCliSesStreamObjects, streamIter, (void**)&retransStream, &retransStreamLen) == QTSS_NoErr; streamIter++)
                if (retransStream && *retransStream)
                    (*retransStream)->SendRetransmits();
            
            theParams.rtpSendPacketsParams.outNextPacketTime = fNextSendPacketsTime - theParams.rtpSendPacketsParams.inCurrentTime;
        }
        else
        {           
    #if RTPSESSION_DEBUGGING
            qtss_printf("RTPSession %"_S32BITARG_": about to call SendPackets\n",(SInt32)this);
    #endif
            if ((theParams.rtpSendPacketsParams.inCurrentTime - fLastBandwidthTrackerStatsUpdate) > 1000)
                this->GetBandwidthTracker()->UpdateStats();
                
            theParams.rtpSendPacketsParams.outNextPacketTime = 0;
            // Async event registration is definitely allowed from this role.
            fModuleState.eventRequested = false;
            Assert(fModule != NULL);
            (void)fModule->CallDispatch(QTSS_RTPSendPackets_Role, &theParams);
    #if RTPSESSION_DEBUGGING
            qtss_printf("RTPSession %"_S32BITARG_": back from sendPackets, nextPacketTime = %"_64BITARG_"d\n",(SInt32)this, theParams.rtpSendPacketsParams.outNextPacketTime);
    #endif
            //make sure not to get deleted accidently!
            if (theParams.rtpSendPacketsParams.outNextPacketTime < 0)
                theParams.rtpSendPacketsParams.outNextPacketTime = 0;
            fNextSendPacketsTime = theParams.rtpSendPacketsParams.inCurrentTime + theParams.rtpSendPacketsParams.outNextPacketTime;
        }
        
    }
    
    //
    // Make sure the duration between calls to Run() isn't greater than the
    // max retransmit delay interval.
    UInt32 theRetransDelayInMsec = QTSServerInterface::GetServer()->GetPrefs()->GetMaxRetransmitDelayInMsec();
    UInt32 theSendInterval = QTSServerInterface::GetServer()->GetPrefs()->GetSendIntervalInMsec();
    
    //
    // We want to avoid waking up to do retransmits, and then going back to sleep for like, 1 msec. So, 
    // only adjust the time to wake up if the next packet time is greater than the max retransmit delay +
    // the standard interval between wakeups.
    if (theParams.rtpSendPacketsParams.outNextPacketTime > (theRetransDelayInMsec + theSendInterval))
        theParams.rtpSendPacketsParams.outNextPacketTime = theRetransDelayInMsec;
    
    Assert(theParams.rtpSendPacketsParams.outNextPacketTime >= 0);//we'd better not get deleted accidently!
    return theParams.rtpSendPacketsParams.outNextPacketTime;
}
### 配置SSL/TLS加密套件以绑定到443端口 为了在服务器上配置 `bind *:443` 并启用 SSL/TLS 加密,特别是使用 AES 及其他指定的加密套件,通常涉及以下几个方面: #### 修改Web服务器配置文件 对于Nginx Web服务器而言,在其配置文件中(通常是 `/etc/nginx/nginx.conf` 或者站点特定的 `.conf` 文件),可以找到类似于如下所示的部分来设置监听443端口并应用SSL证书和私钥。 ```nginx server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; ssl_protocols TLSv1.2 TLSv1.3; # 启用更安全版本的TLS协议[^1] ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:!DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"; ssl_prefer_server_ciphers on; # 让服务器优先选择上述定义好的密码列表中的算法 } ``` 这段配置指定了要使用的具体加密套件,并通过 `ssl_ciphers` 指令设置了允许客户端连接时所采用的一系列强加密方法。这里包含了多种基于AES的不同模式以及其他高强度加密方式,确保通信的安全性和效率。 另外需要注意的是,当涉及到实际部署时,应当根据最新的安全性建议调整这些参数,因为随着时间推移某些曾经被认为是安全的做法可能会变得不再可靠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值