参考:http://blog.csdn.net/niu_gao/article/details/6988044
大家应该已理解了GroupSocket这个类。理论上讲那些需要操作udp socket 的类应保存GroupSocket的实例。但事实并不是这样,可以看一下RTPSink,RTPSource,RTCPInstance等,它们都没有保存GroupSocket型的变量。那它们通过哪个类进行socket操作呢?是RTPInterface!!
这些类接收的GroupSocket指针最后都传给了 RTPInterface 。为什么用RTPInterface而不直接用GroupSocket呢?这里面有个故事...扯远了。
要解答这个问题,让我们先提出问题吧。
首先请问,Live555即支持rtp over udp,又支持rtp over tcp。那么在rtp over tcp情况下,用 GroupSocket 怎么实现呢?GroupSocket可是仅仅代表UDP啊!
那么RTPInterface既然用于网络读写,它就应该既支持tcp收发,也支持udp收发。而且它还要像GroupSocket那样支持一对多。因为服务端是一对多个客户端哦。我们看一下RTPInterface的成员:
Groupsock* fGS;
tcpStreamRecord* fTCPStreams; // optional, for RTP-over-TCP streaming/receiving
嘿嘿,这两个紧靠着,说明它们关系不一般啊(难道他们有一腿?)。fGS--代表了一个udp socket和它对应的多个目的端,fTCPStreams--代表了多个TCP socket,当然这些socket都是从一个socket accept()出来的客户端socket(tcpStreamRecord是一个链表哦)。
看到这个架式,我想大家都要得出结论了:RTPInterface还真是男女通吃啊!不论你客户端与我建立的是tcp连接,还是udp连接,我RTPInterface一律能接收你们的数据,并向你们发出数据!
证据一:向所有客户端发出数据:
- Boolean RTPInterface::sendPacket(unsigned char* packet, unsigned packetSize)
- {
- Boolean success = True; // we'll return False instead if any of the sends fail
- // Normal case: Send as a UDP packet:
- if (!fGS->output(envir(), fGS->ttl(), packet, packetSize))
- success = False;
- // Also, send over each of our TCP sockets:
- for (tcpStreamRecord* streams = fTCPStreams; streams != NULL;
- streams = streams->fNext) {
- if (!sendRTPOverTCP(packet, packetSize, streams->fStreamSocketNum,
- streams->fStreamChannelId)) {
- success = False;
- }
- }
- return success;
- }
证据二:从所有客户端读取数据:
我现在找不到直接的证据,所以我就憶想一下吧:当udp端口或tcp端口收到数据时,分析后,是哪个客户端的数据就发给对应这个客户端的RTPSink或RTCPInstance。
好像已经把最开始的问题解答完了。下面让我们来分析一下RTPInterface吧。
- void RTPInterface::setStreamSocket(int sockNum, unsigned char streamChannelId)
- {
- fGS->removeAllDestinations();
- addStreamSocket(sockNum, streamChannelId);
- }
- void RTPInterface::addStreamSocket(int sockNum, unsigned char streamChannelId)
- {
- if (sockNum < 0)
- return;
- for (tcpStreamRecord* streams = fTCPStreams; streams != NULL;
- streams = streams->fNext) {
- if (streams->fStreamSocketNum == sockNum
- && streams->fStreamChannelId == streamChannelId) {
- return; // we already have it
- }
- }
- fTCPStreams = new tcpStreamRecord(sockNum, streamChannelId, fTCPStreams);
- }
想像一下,服务端要从这个tcp socket读写数据,必须把一个handler加入TaskScheduler中,这个handler在可读数据时进行读,在可写数据时进行写。在读数据时,对读出的数据进行分析,取得数据包的长度,以及其channel id,跟据channel id找到相应的处handler和对象,交给它们去处理自己的数据。
试想两个建立在tcp上的rtp session,这个两个tcp socket既担负着rtsp通讯,又担负着rtp通讯。如果这两个rtp session共用一个stream,那么最终负责这两个session通信的就只有一个RTPInterface,那么这个RTPInterface中的fTCPStreams这个链表中就会有两项,分别对应这两个session。tcpStreamRecord主要用于socket number与channel id的对应。这些tcpStreamRecord是通过addStreamSocket()添加的。处理数据的handler是通过startNetworkReading()添加的,看一下下:
- void RTPInterface::startNetworkReading(TaskScheduler::BackgroundHandlerProc* handlerProc)
- {
- // Normal case: Arrange to read UDP packets:
- envir().taskScheduler().turnOnBackgroundReadHandling(fGS->socketNum(),handlerProc,
- fOwner);
- // Also, receive RTP over TCP, on each of our TCP connections:
- fReadHandlerProc = handlerProc;
- for (tcpStreamRecord* streams = fTCPStreams; streams != NULL;
- streams = streams->fNext) {
- // Get a socket descriptor for "streams->fStreamSocketNum":
- SocketDescriptor* socketDescriptor = lookupSocketDescriptor(envir(),
- streams->fStreamSocketNum);
- // Tell it about our subChannel:
- socketDescriptor->registerRTPInterface(streams->fStreamChannelId, this);
- }
- }
上述主要讲的是一个RTPInterface对应多个客户端tcp socket的情形。现在又发现一个问题:SocketDescriptor为什么需要对应多个RTPInterface呢?上面已经讲了,是为了多路复用,因为这个socket即负担rtsp通信又负担rtp通信还负担RTCP通信。SocketDescriptor记录多路复用数据(也就是RTPInterface与channel id)用了一个Hash table:HashTable* fSubChannelHashTable。SocketDescriptor读数据使用函数:static void tcpReadHandler(SocketDescriptor*, int mask)。证据如下:
- void SocketDescriptor::registerRTPInterface(
- unsigned char streamChannelId,
- RTPInterface* rtpInterface)
- {
- Boolean isFirstRegistration = fSubChannelHashTable->IsEmpty();
- fSubChannelHashTable->Add((char const*) (long) streamChannelId,
- rtpInterface);
- if (isFirstRegistration) {
- // Arrange to handle reads on this TCP socket:
- TaskScheduler::BackgroundHandlerProc* handler =
- (TaskScheduler::BackgroundHandlerProc*) &tcpReadHandler;
- fEnv.taskScheduler().turnOnBackgroundReadHandling(fOurSocketNum,
- handler, this);
- }
- }
- void SocketDescriptor::tcpReadHandler1(int mask)
- {
- // We expect the following data over the TCP channel:
- // optional RTSP command or response bytes (before the first '$' character)
- // a '$' character
- // a 1-byte channel id
- // a 2-byte packet size (in network byte order)
- // the packet data.
- // However, because the socket is being read asynchronously, this data might arrive in pieces.
- u_int8_t c;
- struct sockaddr_in fromAddress;
- if (fTCPReadingState != AWAITING_PACKET_DATA) {
- int result = readSocket(fEnv, fOurSocketNum, &c, 1, fromAddress);
- if (result != 1) { // error reading TCP socket, or no more data available
- if (result < 0) { // error
- fEnv.taskScheduler().turnOffBackgroundReadHandling(
- fOurSocketNum); // stops further calls to us
- }
- return;
- }
- }
- switch (fTCPReadingState) {
- case AWAITING_DOLLAR: {
- if (c == '$') {
- fTCPReadingState = AWAITING_STREAM_CHANNEL_ID;
- } else {
- // This character is part of a RTSP request or command, which is handled separately:
- if (fServerRequestAlternativeByteHandler != NULL) {
- (*fServerRequestAlternativeByteHandler)(
- fServerRequestAlternativeByteHandlerClientData, c);
- }
- }
- break;
- }
- case AWAITING_STREAM_CHANNEL_ID: {
- // The byte that we read is the stream channel id.
- if (lookupRTPInterface(c) != NULL) { // sanity check
- fStreamChannelId = c;
- fTCPReadingState = AWAITING_SIZE1;
- } else {
- // This wasn't a stream channel id that we expected. We're (somehow) in a strange state. Try to recover:
- fTCPReadingState = AWAITING_DOLLAR;
- }
- break;
- }
- case AWAITING_SIZE1: {
- // The byte that we read is the first (high) byte of the 16-bit RTP or RTCP packet 'size'.
- fSizeByte1 = c;
- fTCPReadingState = AWAITING_SIZE2;
- break;
- }
- case AWAITING_SIZE2: {
- // The byte that we read is the second (low) byte of the 16-bit RTP or RTCP packet 'size'.
- unsigned short size = (fSizeByte1 << 8) | c;
- // Record the information about the packet data that will be read next:
- RTPInterface* rtpInterface = lookupRTPInterface(fStreamChannelId);
- if (rtpInterface != NULL) {
- rtpInterface->fNextTCPReadSize = size;
- rtpInterface->fNextTCPReadStreamSocketNum = fOurSocketNum;
- rtpInterface->fNextTCPReadStreamChannelId = fStreamChannelId;
- }
- fTCPReadingState = AWAITING_PACKET_DATA;
- break;
- }
- case AWAITING_PACKET_DATA: {
- // Call the appropriate read handler to get the packet data from the TCP stream:
- RTPInterface* rtpInterface = lookupRTPInterface(fStreamChannelId);
- if (rtpInterface != NULL) {
- if (rtpInterface->fNextTCPReadSize == 0) {
- // We've already read all the data for this packet.
- fTCPReadingState = AWAITING_DOLLAR;
- break;
- }
- if (rtpInterface->fReadHandlerProc != NULL) {
- rtpInterface->fReadHandlerProc(rtpInterface->fOwner, mask);
- }
- }
- return;
- }
- }
- }
- case AWAITING_DOLLAR: {
- if (c == $) {
- fTCPReadingState = AWAITING_STREAM_CHANNEL_ID;
- } else {
- // This character is part of a RTSP request or command, which is handled separately:
- if (fServerRequestAlternativeByteHandler != NULL) {
- (*fServerRequestAlternativeByteHandler)(
- fServerRequestAlternativeByteHandlerClientData, c);
- }
- }
- break;
- }
还有,socketDescriptor们并不属于任何RTPInterface,而是单独保存在一个Hash table中,这样多个RTPInterface都可以注册到一个socketDescriptor中,以实现多路复用。
总结一下通过RTPInterface,live555不仅实现了rtp over udp,还实现了rtp over tcp,而且还实现了同时即有rtp over tcp,又有rtp over udp!
最后,channel id是从哪里来的呢?是在RTSP请求中指定的。在哪个请求中呢?自己找去吧。