转载
RTSP连接服务器是否成功,以及是否从服务器接收到实际播放的数据,主要是判断两个linux select函数的执行结果。
1.连接服务器的处理过程
[cpp]
view plain
copy
- 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();
[cpp]
view plain
copy
- void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) {
- case kWhatCompleteConnection:
- onCompleteConnection(msg);
[cpp]
view plain
copy
- 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
[cpp]
view plain
copy
- void ARTSPConnection::postReceiveReponseEvent() {
- if (mReceiveResponseEventPending) {
- return;
- }
- sp<AMessage> msg = new AMessage(kWhatReceiveResponse, id());
- msg->post();
- mReceiveResponseEventPending = true;
- }
case kWhatReceiveResponse:
onReceiveResponse();
break;
继续调用---------->
2.3
[cpp]
view plain
copy
- 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]