RTSP连接服务器

来源:互联网 发布:淘宝新店的扶持期 编辑:程序博客网 时间:2024/05/29 07:50
RTSP连接服务器是否成功,以及是否从服务器接收到实际播放的数据,主要是判断两个linux select函数的执行结果。
1.连接服务器的处理过程
[cpp] view plaincopy
  1. ARTSPConnection::onConnect(const sp<AMessage> &msg)  
  2.   
  3.     int err = ::connect(  
  4.             mSocket, (const struct sockaddr *)&remote, sizeof(remote));  
  5.     LOGE("%s L%d err = %d", __FUNCTION__, __LINE__, err);  
  6.       
  7.     if (err < 0) {  
  8.         if (errno == EINPROGRESS) { // 正在连接中  
  9.             sp<AMessage> msg = new AMessage(kWhatCompleteConnection, id());  
  10.             msg->setMessage("reply", reply);  
  11.             msg->setInt32("connection-id", mConnectionID);  
  12.             msg->post();  
调用---------->
[cpp] view plaincopy
  1. void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) {  
  2. case kWhatCompleteConnection:  
  3.             onCompleteConnection(msg);  
调用----------> 
[cpp] view plaincopy
  1. ARTSPConnection::onCompleteConnection  
  2.   
  3.     int res = select(mSocket + 1, NULL, &ws, NULL, &tv);  
  4.     CHECK_GE(res, 0);  
  5.       
  6.     if (res == 0) {  
  7.         // Timed out. Not yet connected.  
  8.         LOGE("%s L%d -ECONNABORTED", __FUNCTION__, __LINE__);  
  9.   
  10.   
  11.         msg->post(); // 循环执行此函数检查是否connected  
  12.         return;  
  13.     }  

2.是否接收到服务器发送的播放数据的处理

2.1 在ARTSPConnection::onCompleteConnection成功连接后,发送检查接收数据的event

[cpp] view plaincopy
  1. void ARTSPConnection::postReceiveReponseEvent() {  
  2.     if (mReceiveResponseEventPending) {  
  3.         return;  
  4.     }  
  5.   
  6.   
  7.     sp<AMessage> msg = new AMessage(kWhatReceiveResponse, id());  
  8.     msg->post();  
  9.   
  10.   
  11.     mReceiveResponseEventPending = true;  
  12. }  
调用----------> 2.2 void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) {
        case kWhatReceiveResponse:
            onReceiveResponse();
            break;
继续调用---------->
2.3
[cpp] view plaincopy
  1. void ARTSPConnection::onReceiveResponse() {  
  2.    int res = select(mSocket + 1, &rs, NULL, NULL, &tv);  
  3.      
  4. //   LOGE("%s L%d select res = %d", __FUNCTION__, __LINE__, res);  
  5.    CHECK_GE(res, 0);  
  6.   
  7.    if (res == 1) {  
  8.        MakeSocketBlocking(mSocket, true);  
  9.          
  10.        bool success = receiveRTSPReponse(); // 2.3.1  
  11.   
  12.        MakeSocketBlocking(mSocket, false);  
  13.        LOGE("%s L%d select ==== success = %d", __FUNCTION__, __LINE__, success);  
  14.   
  15.        if (!success) {  
  16.            // Something horrible, irreparable has happened.  
  17.            flushPendingRequests();  
  18.            LOGE("%s L%d return ", __FUNCTION__, __LINE__);  
  19.            return;  
  20.        }  
  21.    }  
  22.   
  23.    postReceiveReponseEvent(); // 循环执行此函数  

继续调用……

2.3.1 bool ARTSPConnection::receiveRTSPReponse() {

--->
  2.3.1.1 bool ARTSPConnection::receiveLine(AString *line) {
  --->
    2.3.1.1.1 status_t ARTSPConnection::receive(void *data, size_t size) { // 每次读一个字符
    --->
      2.3.1.1.1.1 ssize_t n = recv(mSocket, (uint8_t *)data + offset, size - offset, 0); // 从TCP连接的另一端接收数据
--->  
  2.3.1.2 sp<ABuffer> ARTSPConnection::receiveBinaryData()
  --->
    2.3.1.2.1 status_t ARTSPConnection::receive(void *data, size_t size) { // 每次读3个字符
    --->
      recv()
    2.3.1.2.2 status_t ARTSPConnection::receive(void *data, size_t size) { // 每次读buffer->size()个字符
    --->
      recv()      
--->
  2.3.1.3 new ARTSPResponse
--->
  2.3.1.4 receiveLine()
--->
  2.3.1.5 recv() // while (numBytesRead < contentLength)
--->
  2.3.1.6 函数返回
  return isRequest
        ? handleServerRequest(response)
        : notifyResponseListener(response);
原创粉丝点击