Darwin Streaming Server Core code Analysis

Source: Internet
Author: User

Basic Concepts

First, the code I was targeting was a version of Darwin streaming Server 6.0.3 without any changes.

Darwin Streaming server in design mode, the reactor concurrent server design pattern is used, and a certain understanding of reactor will help to understand the Darwin streaming Server Core code.

Reactor mode is a typical event triggering mode, when an event occurs, the completion of the corresponding Task,task is accomplished by invoking the corresponding handle, and the call to handle is done by a finite number of thread.

A task class is defined in Darwin streaming server. The task class has two typical methods, one is signal, and the other is run. Call signal to add a task to the Taskthread task queue and wait for it to complete, and run is the handle to complete a task. Based on the task class, three types of tasks are defined, namely Idletask,timeouttask, and normal task.

In Darwin streaming server, there are three types of threads except the main thread, namely Taskthread,eventthread and Idletaskthread:

    1. Taskthread,taskthread completes the processing of the corresponding task by running the task type object's Run method. The typical task types are rtspsession and rtpsession. The number of Taskthread is configurable, by default the number of Taskthread is the same as the number of processors. Waits for a task to be called and run by Taskthread to be placed in a queue or heap.
    2. Eventthread,eventthread is responsible for listening for socket interface events, in Darwin Streaming server, there are two types of events that are being listened to, namely the arrival of an RTSP connection request and the arrival of the RTSP request. For the events of the RTSP connection request, Eventthread establishes a rtspsession and initiates a listener for the corresponding socket. For RTSP-requested events, Eventthread adds the corresponding rtspsession type of task to the Taskthread queue, waiting for the RTSP request to be processed.
    3. Idletaskthread,idletaskthread manages the queue of Idletask type objects, triggering idletask scheduling based on pre-set timers. Tcplistenersocket is a derived class of idletask, and when the number of concurrent connections reaches the maximum value set, The rtsplistenersocket derived from Tcplistenersocket is added to the Idletaskthread managed Idletask queue to temporarily stop listening to the RTSP port until the set timer is triggered.

Core Architecture

Is the Darwin Streaming Server Core architecture. There are three types of features in this, namely, threads, task queues, or heaps, events that are being listened to. The text in the diagram is copied from the source code, making it easier for the reader to match the source code with the lookup.





The figure shows three threads, namely Taskthread::entry,eventthread::entry and Idletaskthread::entry. These three types of threads are described in the previous article.

In addition to the three threads, there are five additional rectangular blocks in the figure. There are two threads associated with the Taskthread::entry line, namely the Taskthread::ftaskqueue queue and the Taskthread::fheap heap, and the task that is scheduled to be completed by calling signal is placed in the queue or heap. There is a idletaskthread::idleheap heap associated with the Idletaskthread::entry line threads. Associated with Eventthread::entry is the Eventcontext::feventreq, which is the port that is being listened to. Another is the Timeouttaskthread::fqueue queue, which is actually associated with Taskthread::entry through Timeouttask.

The connection line to the thread in the figure indicates that the task is taken out of the queue or heap, and for the eventthread::entry thread, it is the occurrence of the listening event. The connection line to the port being listened to indicates that the port is joined to listen, to the queue of the task or to the heap's connector, indicating that the task is added to the queue or heap. The text of the connection line gives the corresponding function call, which can be searched directly in the source code.

Eventthread

When the system starts, call Qtsserver::starttasks () to add the RTSP service port to the listening queue. At this point, the client's RTSP connection request begins to be received.

Calling the Select_waitevent function in Eventthread::entry waits for the event to occur, and when an event occurs, the event is handled appropriately by calling the Processevent method. Note that processevent is a virtual function, with a total of two implementations. The processevent method is implemented in the Eventcontext class, and the Processevent method is also implemented in the Eventcontext derived class Tcplistenersocket.

For the request to establish an RTSP connection, call Tcplistenersocket::P The Rocessevent method to process, this method calls Rtsplistenersocket Getsessiontask method to establish a rtspsession, and the corresponding socket is added to the listening queue, waiting for RTSP request. It is then necessary to call This->requestevent (ev_re) to join the request to establish the RTSP connection to the listening queue.

For RTSP request events on an RTSP connection, the call is Eventcontext::P the Rocessevent method to add the corresponding rtspsession type of task to the taskthread through the signal of the task: Waiting for taskthread processing in Ftaskqueue.

Taskthread and Task

Taskthread::entry calls the Taskthread::waitfortask () method to get the next task to be processed. Taskthread::waitfortask () First obtains a task from taskthread::fheap, if there is no task in taskthread::fheap that satisfies the condition, from Taskthread:: Get a task in Ftaskqueue.

Taskthread::entry calls the Task::run method to complete the return value type of the corresponding Task,task::run method is SInt64, also known as the signed long long int type. Taskthread::entry is handled differently depending on the return value of the Task::run method. For a return value less than 0, delete the task, and for a return value greater than 0, the return value represents the amount of time to wait for the next processing of the task, Taskthread::entry calls Fheap.insert (&theTask-> Ftimerheapelem) Insert the task into the heap and set the wait time. For a return value equal to 0, taskthread::entry no longer cares about the task.

Timeouttask

From the code, Timeouttaskthread is a derived class of idletask, and after analysis, it is found that there is no relationship from Timeouttaskthread to Idletask, it is entirely possible to derive from a task and validate the idea after modifying the code. So Timeouttaskthread is an ordinary task,timeouttaskthread that monitors a set of timeout tasks through its run method, such as the RTSP protocol or the RTP protocol timeout.

Timeouttaskthread is added to the Taskthread queue when the system is started, which is accomplished by calling Timeouttask::initialize () in the StartServer function. The return value of the Timeouttaskthread::run function is Intervalmilli = kintervalseconds * 1000, which is a positive number, The Timeouttaskthread task is then added to the taskthread::fheap to be periodically called.

The Timeouttaskthread::run method finds a task that has timed out, and the signal method dispatches the task,event to Task::ktimeoutevent. This set of tasks to be managed should have a refreshtimeout mechanism.

processing of one-on-demand requests

To better understand the architecture of Darwin's streaming server, we initiated on-demand from the client, triggering the creation of the server's RTSP connection event, to see what the DSS workflow was like.

For the RTSP protocol, darwing streaming server listens on port 554, and when a connection request arrives, a socket is returned via the accept call, and the corresponding subsequent RTSP request is routed through the socket. We divided the RTSP related events into two categories, one is the RTSP connection request, and the other is the RTSP request. First look at the process of RTSP connection request:

    1. After the RTSP connection request arrives, it is captured by the Select_waitevent function, and the code is 232 lines in the EventContext.cpp eventthread::entry.
    2. Find the eventthread::freftable and get the corresponding eventcontext. The derived class rtsplistenersocket of the Eventcontext type is obtained. The corresponding code is 249 to 253 lines in EventContext.cpp.
    3. Call processevent to handle the event. The corresponding code is 257 lines in EventContext.cpp. Note that because the corresponding Eventcontext class is actually rtsplistenersocket, the call should be tcplistenersocket::P rocessevent.
    4. In the TCPListenerSocket.cpp 106 line Tcplistenersocket::P rocessevent method, call accept to get the socket, call the Getsessiontask method in 160 rows, The corresponding is rtsplistenersocket::getsessiontask, defined in QTSServer.cpp.
    5. In the Rtsplistenersocket::getsessiontask method, a new rtspsession is created for the 1077 line of QTSServer.cpp, which is called by the rtspsession.
    6. Back to Tcplistenersocket in the TCPListenerSocket.cpp file::P rocessevent method, note 189 lines, add the newly established RTSP connection to the listening queue, waiting for the arrival of the RTSP request.

The process steps for RTSP requests are as follows, note that the first step is the same:

    1. After the RTSP connection request arrives, it is captured by the Select_waitevent function, and the code is 232 lines in the EventContext.cpp eventthread::entry.
    2. Find the eventthread::freftable and get the corresponding eventcontext. The Eventcontext class is obtained. The corresponding code is 249 to 253 lines in EventContext.cpp.
    3. Call processevent to handle the event. The corresponding code is 257 lines in EventContext.cpp. Notice that the eventcontext is called at this time::P rocessevent.
    4. Eventcontext::P The Rocessevent method is implemented in EventContext.h, in 127 rows. Ftask->signal (task::kreadevent) is called in line 138, and Ftask is the Rtspsession class. The queue that joins Rtspsession to Taskthread waits for Rtspsession::run () to be called.
    5. Follow-up is the rtspsession::run () specific processing of RTSP requests.

More Articles from me:
    • the AIO under Linux (2011-10-27 14:33:22)
    • Write Darwin's module to support the new streaming media file format (2011-02-15 21:35:30)
    • Libev Getting Started (2011-01-09 15:21:10)
    • implementation of the RTSP protocol in Darwin (2010-12-31 21:24:44)
    • implementation of the file Access section of QTSS invocation in Darwin (draft) (2010-12-28 23:47:32)
    • dictionary data types used in the Darwin Video Server (draft) (2010-12-19 15:11:04)
    • High Performance IO design mode and its application (2010-12-18 21:40:30)
From:http://blog.sina.com.cn/s/blog_6a4c492f0100witu.html

Darwin Streaming Server Core code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.