【Darwin学习笔记】之RTSP连接监听初始化

网上有很多大侠已经对Darwin做了详细的剖析,在下仅本着积累经验的目的,将Darwin的学习过程记录下来,同时与网友们交流学习,今天先来分析下Darwin如果根据配置文件监听RTSP连接的流程。

1. Darwin系统初始化,Bool16 QTSServer::Initialize(.....)

2. QTSServer::CreateListeners(...)根据本地xml配置文件中的地址和端口进行监听的建立,主要有两个配置项:

<PREF NAME="bind_ip_addr" >0</PREF>

<LIST-PREF NAME="rtsp_port" TYPE="UInt16" >

3. void QTSServer::StartTasks() 开始监听上一步建立起来的Socket 列表

Bool16 QTSServer::CreateListeners(Bool16 startListeningNow, QTSServerPrefs* inPrefs, UInt16 inPortOverride)
{
    struct PortTracking
    {
        PortTracking() : fPort(0), fIPAddr(0), fNeedsCreating(true) {}

        UInt16 fPort;  //端口号
        UInt32 fIPAddr;//地址
        Bool16 fNeedsCreating;//是否需要创建RTSPListenerSocket,默认创建
    };

    PortTracking* thePortTrackers = NULL;
    UInt32 theTotalPortTrackers = 0;

    // Get the IP addresses from the pref 获取配置文件中用于监听RTSP连接的IP地址和端口号
    UInt32 theNumAddrs = 0;
    UInt32* theIPAddrs = this->GetRTSPIPAddrs(inPrefs, &theNumAddrs);
    UInt32 index = 0;

	//inPortOverride该参数为命令行参数传入,默认为0. -p XXX: Specify the default RTSP listening port of the server
    if ( inPortOverride != 0)
    {
        theTotalPortTrackers = theNumAddrs; // one port tracking struct for each IP addr
        thePortTrackers = NEW PortTracking[theTotalPortTrackers];
        for (index = 0; index < theNumAddrs; index++)
        {
            thePortTrackers[index].fPort = inPortOverride;
            thePortTrackers[index].fIPAddr = theIPAddrs[index];
        }
    }
    else
    {
        UInt32 theNumPorts = 0;
        UInt16* thePorts = GetRTSPPorts(inPrefs, &theNumPorts);  //根据xml配置文件中的<LIST-PREF NAME="rtsp_port" TYPE="UInt16" >获取端口号
        theTotalPortTrackers = theNumAddrs * theNumPorts;
        thePortTrackers = NEW PortTracking[theTotalPortTrackers];//一共需要监听的数量为:端口数 * IP地址数

        UInt32 currentIndex  = 0;

        for (index = 0; index < theNumAddrs; index++)
        {
            for (UInt32 portIndex = 0; portIndex < theNumPorts; portIndex++)
            {
                currentIndex = (theNumPorts * index) + portIndex;

                thePortTrackers[currentIndex].fPort = thePorts[portIndex];
                thePortTrackers[currentIndex].fIPAddr = theIPAddrs[index];
            }
        }

                delete [] thePorts;
    }

        delete [] theIPAddrs;
    //
    // Now figure out which of these ports we are *already* listening on.
    // If we already are listening on that port, just move the pointer to the
    // listener over to the new array
    TCPListenerSocket** newListenerArray = NEW TCPListenerSocket*[theTotalPortTrackers];
    UInt32 curPortIndex = 0;

    for (UInt32 count = 0; count < theTotalPortTrackers; count++)
    {
        for (UInt32 count2 = 0; count2 < fNumListeners; count2++)
        {
            if ((fListeners[count2]->GetLocalPort() == thePortTrackers[count].fPort) &&
                (fListeners[count2]->GetLocalAddr() == thePortTrackers[count].fIPAddr))
            {
				//如果fListeners监听列表中已经对该ip+port进行了监听,那么就不再监听;
                thePortTrackers[count].fNeedsCreating = false;
                newListenerArray[curPortIndex++] = fListeners[count2];
                Assert(curPortIndex <= theTotalPortTrackers);
                break;
            }
        }
    }

    //
    // 创建为需要监听的IP+PORT创建RTSPListenerSocket
    for (UInt32 count3 = 0; count3 < theTotalPortTrackers; count3++)
    {
        if (thePortTrackers[count3].fNeedsCreating) //不需要监听的已经在上一步把该字段置为false了.
        {
			//创建RTSP监听任务;
            newListenerArray[curPortIndex] = NEW RTSPListenerSocket();
            QTSS_Error err = newListenerArray[curPortIndex]->Initialize(thePortTrackers[count3].fIPAddr, thePortTrackers[count3].fPort);

            char thePortStr[20];
            qtss_sprintf(thePortStr, "%hu", thePortTrackers[count3].fPort);

            //
            // If there was an error creating this listener, destroy it and log an error
            if ((startListeningNow) && (err != QTSS_NoErr))
                delete newListenerArray[curPortIndex];

            if (err == EADDRINUSE)
			{
                QTSSModuleUtils::LogError(qtssWarningVerbosity, qtssListenPortInUse, 0, thePortStr);
			}
            else if (err == EACCES)
			{
				QTSSModuleUtils::LogError(qtssWarningVerbosity, qtssListenPortAccessDenied, 0, thePortStr);
			}
            else if (err != QTSS_NoErr)
			{
                QTSSModuleUtils::LogError(qtssWarningVerbosity, qtssListenPortError, 0, thePortStr);
			}
            else
            {
				//将第一个可用端口赋值给fOKPort,保存,后续通过GetOKPort()对外提供RTSP监听端口即为此端口;
				if(fOKPort == 0)
				{
					fOKPort = thePortTrackers[count3].fPort;
				}

                //
                // This listener was successfully created.如果需要监听,则立即开始监听该socket上面的连接.
                if (startListeningNow)
                    newListenerArray[curPortIndex]->RequestEvent(EV_RE);
                curPortIndex++;
            }
        }
    }

    //
    //将fListeners监听列表中未在配置文件监听范围内的socket杀掉~
    for (UInt32 count4 = 0; count4 < fNumListeners; count4++)
    {
        Bool16 deleteThisOne = true;

        for (UInt32 count5 = 0; count5 < curPortIndex; count5++)
        {
            if (newListenerArray[count5] == fListeners[count4])
                deleteThisOne = false;
        }

        if (deleteThisOne)
            fListeners[count4]->Signal(Task::kKillEvent);
    }

    //
    // Finally, make our server attributes and fListener privy to the new...最后将监听列表赋给fListeners,监听数量赋给fNumListeners
    fListeners = newListenerArray;
    fNumListeners = curPortIndex;
    UInt32 portIndex = 0;
    //将监听的端口号列表保存起来,key值为qtssSvrRTSPPorts
    for (UInt32 count6 = 0; count6 < fNumListeners; count6++)
    {
        if  (fListeners[count6]->GetLocalAddr() != INADDR_LOOPBACK)
        {
            UInt16 thePort = fListeners[count6]->GetLocalPort();
            (void)this->SetValue(qtssSvrRTSPPorts, portIndex, &thePort, sizeof(thePort), QTSSDictionary::kDontObeyReadOnly);
            portIndex++;
        }
    }
	//设置端口列表的个数;
    this->SetNumValues(qtssSvrRTSPPorts, portIndex);

    delete [] thePortTrackers;
    return (fNumListeners > 0);
}

从配置文件中获取RTSP监听地址列表,即<PREF NAME="bind_ip_addr" >0</PREF>配置项,系统默认是为0,该配置项在多网卡的服务器上可能需要用到,一般我们调试学习的时候可以不考虑。但是还是介绍下,也比较简单。

UInt32* QTSServer::GetRTSPIPAddrs(QTSServerPrefs* inPrefs, UInt32* outNumAddrsPtr)
{
    UInt32 numAddrs = inPrefs->GetNumValues(qtssPrefsRTSPIPAddr);//获取配置文件中rtsp监听地址的数量;
    UInt32* theIPAddrArray = NULL;

    if (numAddrs == 0)//如果配置的监听地址数量为0,则设置为INADDR_ANY
    {
        *outNumAddrsPtr = 1;
        theIPAddrArray = NEW UInt32[1];
        theIPAddrArray[0] = INADDR_ANY;//INADDR_ANY就是指定地址为0.0.0.0的地址,这个地址事实上表示不确定地址,或“所有地址”、“任意地址”。
    }
    else
    {
        theIPAddrArray = NEW UInt32[numAddrs + 1];
        UInt32 arrIndex = 0;

        for (UInt32 theIndex = 0; theIndex < numAddrs; theIndex++)
        {
            // Get the ip addr out of the prefs dictionary
            QTSS_Error theErr = QTSS_NoErr;

            char* theIPAddrStr = NULL;
            theErr = inPrefs->GetValueAsString(qtssPrefsRTSPIPAddr, theIndex, &theIPAddrStr);
            if (theErr != QTSS_NoErr)
            {
                delete [] theIPAddrStr;
                break;
            }

            UInt32 theIPAddr = 0;
            if (theIPAddrStr != NULL)
            {
				//IP地址从字符串转换成整型,并从网络字节顺序转换为主机字节顺序
                theIPAddr = SocketUtils::ConvertStringToAddr(theIPAddrStr);
                delete [] theIPAddrStr;

                if (theIPAddr != 0)
                    theIPAddrArray[arrIndex++] = theIPAddr;
            }
        }

        if ((numAddrs == 1) && (arrIndex == 0))  //如果配置文件的IP地址数量为1,但是解析不到,则同样按照INADDR_ANY方式监听
            theIPAddrArray[arrIndex++] = INADDR_ANY;
        else
            theIPAddrArray[arrIndex++] = INADDR_LOOPBACK; //INADDR_LOOPBACK, 也就是绑定地址LOOPBAC, 往往是127.0.0.1, 只能收到127.0.0.1上面的连接请求

        *outNumAddrsPtr = arrIndex;
    }

    return theIPAddrArray;
}

获取RTSP监听端口列表的方法比较简单,如下:

UInt16* QTSServer::GetRTSPPorts(QTSServerPrefs* inPrefs, UInt32* outNumPortsPtr)
{
    *outNumPortsPtr = inPrefs->GetNumValues(qtssPrefsRTSPPorts); //获取qtssPrefsRTSPPorts对应的数组长度;

    if (*outNumPortsPtr == 0)
        return NULL;

    UInt16* thePortArray = NEW UInt16[*outNumPortsPtr];
    //获取监听端口号列表;
    for (UInt32 theIndex = 0; theIndex < *outNumPortsPtr; theIndex++)
    {
        // Get the ip addr out of the prefs dictionary
        UInt32 theLen = sizeof(UInt16);
        QTSS_Error theErr = QTSS_NoErr;
        theErr = inPrefs->GetValue(qtssPrefsRTSPPorts, theIndex, &thePortArray[theIndex], &theLen);
        Assert(theErr == QTSS_NoErr);
    }

    return thePortArray;
}

如此根据配置文件的IP、port,即已将对应的Socket建立完成,且startListeningNow默认是false,需要在Runserver中调用sServer->StartTasks();方能开始监听

void QTSServer::StartTasks()
{
    fRTCPTask = new RTCPTask();
    fStatsTask = new RTPStatsUpdaterTask();

    //
    // Start listening
    for (UInt32 x = 0; x < fNumListeners; x++)
        fListeners[x]->RequestEvent(EV_RE);
}

时间: 2024-07-31 09:07:26

【Darwin学习笔记】之RTSP连接监听初始化的相关文章

JAVA学习笔记(四十七)- 事件监听

定义ActionListener接口的实现类实现事件监听 import java.awt.Button; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; /* * 事件监听 */ public class Te

jQuery-mobile 学习笔记之三(事件监听)

续上 触摸事件 - 当用户触摸屏幕时触发(敲击和滑动) 滚动事件 - 当上下滚动时触发 方向事件 - 当设备垂直或水平旋转时触发 页面事件 - 当页面被显示.隐藏.创建.加载以及/或卸载时触发 一.初始化事件 1. ready 事件 页面加载完成 $(document).ready(function(){ //your code here... }); 2. 页面加载完成事件二 pageinit $(document).on('pageinit','#pageone',function(){ /

【学习笔记】DirectX应用程序如何初始化

一.使用Windows API创建一个窗口 二.创建一个Swap Chain(交换链) 这一步需要用到一个名为DXGI_SWAP_CHAIN_DESC的结构体,其详细定义如下: typedef struct DXGI_SWAP_CHAIN_DESC { DXGI_MODE_DESC BufferDesc; DXGI_SAMPLE_DESC SampleDesc; DXGI_USAGE BufferUsage; UINT BufferCount; HWND OutputWindow; BOOL W

【Darwin学习笔记】之EventThread

[转载请注明出处]:http://blog.csdn.net/longlong530 EventThread负责侦听Darwin系统的Socket事件,包括以下两类, 对于两类事件是分别如何处理的,我们从头开始分析. a)   建立新的RTSP连接请求事件 b)   已有RTSP连接上的RTSP请求消息事件 一.  EventThread 的创建 RunServer.cpp 中StartServer函数初始化调用了 Socket::Initialize(),在该函数内即创建了EventThrea

【Darwin学习笔记】之RTSPSession

今天详细的看了下RTSPSession的会话处理代码,该模块主要是Run方法内对会话的状态机进行管理,实现对RTSP会话的处理,我以点播Movie文件夹下的视频文件为例,深入的学习了(Describe.Setup.Play的点播流程),并按照状态机的处理顺序对他们进行一一解析,如下: [转载请注明出处]:http://blog.csdn.net/longlong530 1. 状态机 kReadingFirstRequest 通过fInputStream.ReadRequest()调用Sessio

Python学习笔记--Python字符串连接方法总结

声明: 这些总结的学习笔记,一部分是自己在工作学习中总结,一部分是收集网络中的知识点总结而成的,但不到原文链接.如果有侵权,请知会,多谢. python中有很多字符串连接方式,总结一下: 1)最原始的字符串连接方式:str1 + str2 这个估计是Python中最常用的方式,直接用 “+” 来连接两个字符串: 'Jim' + 'Green' = 'JimGreen' 2)python 新字符串连接语法:str1, str2 第二种比较特殊,如果两个字符串用“逗号”隔开,那么这两个字符串将被连接

ZooKeeper个人笔记之节点的监听

create public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException 1.不支持递归创建节点,比如你要创建/master/a,如果master不存在,你就不能创建a( KeeperException.NoNode). 2.不可以再ephemeral类型的节点下创建子节点(KeeperExcep

迷你MVVM框架 avalonjs 学习教程15、属性监听与模块通信

avalon的ViewModel对象从其内部EventManager里继承了三个方法,$watch.$unwatch.$fire三个方法,它们就是我们本节的主题. 词如其名,非常直白,一看就知道做什么.我们先从$watch方法说起,它能监听当前的VM第一层的监控属性 与 计算属性,如果某属性是一个对象,想监控其子孙属性,就需要定位到此对象上使用$watch回调了.$watch回调会默认传入先后两个属性值. <!DOCTYPE html> <html> <head> &l

iframe学习(三)之监听窗口

前言 经常会遇到这样一种情况. 在iframe里嵌入另外一个页面时.如果iframe载入的页面响应较快,或许我们感觉不到页面载入的不同步,但试想,如果一个需要内嵌到iframe里的页面的响应很慢,这里会出现一种什么现象呢?这时将会出现所有页面已经载入完成,但 iframe元素处,将会出现空白,直到内嵌页面完成载入时,该空白处才会显示新载入的页面.可想而知,一个页面如果长时间的空白,对于浏览者来说将意味着什么.如果在内嵌页面未载入完成时,给出一种加载提示信息.如:“页面加载 中”之类的,我想这对浏