RTSP串連伺服器是否成功,以及是否從伺服器接收到實際播放的資料,主要是判斷兩個linux select函數的執行結果。
1.串連伺服器的處理過程
ARTSPConnection::onConnect(const sp<AMessage> &msg) int err = ::connect( mSocket, (const struct sockaddr *)&remote, sizeof(remote)); LOGE("%s L%d err = %d", __FUNCTION__, __LINE__, err); if (err < 0) { if (errno == EINPROGRESS) { // 正在串連中 sp<AMessage> msg = new AMessage(kWhatCompleteConnection, id()); msg->setMessage("reply", reply); msg->setInt32("connection-id", mConnectionID); msg->post();
調用---------->
void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) {case kWhatCompleteConnection: onCompleteConnection(msg);
調用---------->
ARTSPConnection::onCompleteConnection int res = select(mSocket + 1, NULL, &ws, NULL, &tv); CHECK_GE(res, 0); if (res == 0) { // Timed out. Not yet connected. LOGE("%s L%d -ECONNABORTED", __FUNCTION__, __LINE__); msg->post(); // 迴圈執行此函數檢查是否connected return; }
2.是否接收到伺服器發送的播放資料的處理
2.1 在ARTSPConnection::onCompleteConnection成功串連後,發送檢查接收資料的event
void ARTSPConnection::postReceiveReponseEvent() { if (mReceiveResponseEventPending) { return; } sp<AMessage> msg = new AMessage(kWhatReceiveResponse, id()); msg->post(); mReceiveResponseEventPending = true;}
調用---------->
2.2 void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) {
case kWhatReceiveResponse:
onReceiveResponse();
break;
繼續調用---------->
2.3
void ARTSPConnection::onReceiveResponse() { int res = select(mSocket + 1, &rs, NULL, NULL, &tv); // LOGE("%s L%d select res = %d", __FUNCTION__, __LINE__, res); CHECK_GE(res, 0); if (res == 1) { MakeSocketBlocking(mSocket, true); bool success = receiveRTSPReponse(); // 2.3.1 MakeSocketBlocking(mSocket, false); LOGE("%s L%d select ==== success = %d", __FUNCTION__, __LINE__, success); if (!success) { // Something horrible, irreparable has happened. flushPendingRequests(); LOGE("%s L%d return ", __FUNCTION__, __LINE__); return; } } 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);
附:
SDP: Session Description Protocol
[RFC4566]
http://www.ietf.org/rfc/rfc4566.txt