下面贴出安卓N版本ARTSPConnection::connect的调用流程:
==> void ARTSPConnection::connect(const char *url, const sp<AMessage> &reply) { //新建消息msg,消息名为kWhatConnect,消息的处理者为this也就是ARTSPConnection //那么,消息名为kWhatConnect消息的处理则会在ARTSPConnection::onMessageReceived 的case kWhatConnect处理分支中 //将传进来的两个参数url和reply消息引用分别添加到msg消息的"url"和"reply"字段中 sp<AMessage> msg = new AMessage(kWhatConnect, this); msg->setString("url", url); msg->setMessage("reply", reply); msg->post(); } ==> void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) { switch (msg->what()) { case kWhatConnect: //调用onConnect(msg)函数对该消息进行处理完成后续的连接 onConnect(msg); break; case kWhatDisconnect: onDisconnect(msg); break; case kWhatCompleteConnection: onCompleteConnection(msg); break; case kWhatSendRequest: onSendRequest(msg); break; case kWhatReceiveResponse: onReceiveResponse(); break; case kWhatObserveBinaryData: { CHECK(msg->findMessage("reply", &mObserveBinaryMessage)); break; } default: TRESPASS(); break; } } ==> void ARTSPConnection::onConnect(const sp<AMessage> &msg) { ++mConnectionID; //初次调用onConnect函数必须是DISCONNECTED状态,否则进行相应的异常处理 if (mState != DISCONNECTED) { if (mUIDValid) { HTTPBase::UnRegisterSocketUserTag(mSocket); HTTPBase::UnRegisterSocketUserMark(mSocket); } close(mSocket); mSocket = -1; flushPendingRequests(); } //将状态设置为CONNECTING状态,表示正在进行连接 mState = CONNECTING; //从消息里得到参数url和reply消息(注:此消息的消息名为"conn",消息的处理者为MyHandler) AString url; CHECK(msg->findString("url", &url)); sp<AMessage> reply; CHECK(msg->findMessage("reply", &reply)); //调用ParseURL函数从url.c_str()里解析出主机号host,路径path,端口号port //其中主机号host,路径path是AString类型的字符串,port是unsigned类型的整数 //具体是如何解析的后续文章会向大家介绍 AString host, path; unsigned port; if (!ParseURL(url.c_str(), &host, &port, &path, &mUser, &mPass) || (mUser.size() > 0 && mPass.size() == 0)) { // If we have a user name but no password we have to give up // right here, since we currently have no way of asking the user // for this information. ALOGE("Malformed rtsp url %s", uriDebugString(url).c_str()); reply->setInt32("result", ERROR_MALFORMED); reply->post(); mState = DISCONNECTED; return; } if (mUser.size() > 0) { ALOGV("user = '%s', pass = '%s'", mUser.c_str(), mPass.c_str()); } host //传递参数reply消息,主机号host,和端口号port来调用函数performConnect完成连接 performConnect(reply, host, port); }小结:ARTSPConnection::onConnect函数是对ARTSPConnection::connect发送的消息名为kWhatConnect的处理。具体处理的事情是获取到reply消息的引用(该消息的消息名为”conn”,消息的处理者为MyHandler),获取到参数url,从参数url里解析出主机号host,路径path,端口号port,然后调用函数performConnect(reply, host, port)来完成RTSP流的连接。