MyHandler.h中RTSP流的連接

  MyHandler向外部提供了一個接口connect來完成RTSP流的連接。
  下面貼出安卓N版本MyHandler::connect原文:
  

    void connect() { 
        //下面是註冊兩個handler,mConn(sp<ARTSPConnection>)和mRTPConn(sp<ARTPConnection>)
        //註冊後mConn這個handler與looper()得到的looper綁定在一起
        //mRTPConn這個handler與mNetLooper這個looper綁定在一起
        looper()->registerHandler(mConn);
        (1 ? mNetLooper : looper())->registerHandler(mRTPConn);

        //新建立一個通知消息notify作爲參數傳遞給observeBinaryData函數
        //注意:notify消息創建傳遞的handler是this,也就是MyHandler,那麼對消息名爲'biny'的消息notify的處理在MyHandler::onMessageReceived的case 'biny'處理分支裏
        //mConn->observeBinaryData(notify)函數的功能在下文介紹
        sp<AMessage> notify = new AMessage('biny', this);
        mConn->observeBinaryData(notify);

        //新建一個應答消息,消息名爲'conn',消息的處理者爲this,也即MyHandler
        //相應的對消息名爲'conn'的消息reply處理就會在MyHandler::onMessageReceived的case 'conn'分支裏
        //mOriginalSessionURL.c_str()爲會話的url,也就是需要連接的RTSP源的地址
        //將參數mOriginalSessionURL.c_str()和reply傳遞給mConn(sp<ARTSPConnection>)的connect函數來完成RTSP流的連接
        //mConn(sp<ARTSPConnection>)的connect函數後續的文章向大家介紹
        sp<AMessage> reply = new AMessage('conn', this);
        mConn->connect(mOriginalSessionURL.c_str(), reply);
    }

  下面貼出安卓N版本mConn->observeBinaryData(notify)的調用流程:
  

==>
mConn->observeBinaryData(notify)

==>
void ARTSPConnection::observeBinaryData(const sp<AMessage> &reply) {
    //新建消息msg,消息名爲kWhatObserveBinaryData,消息的處理者爲this。也即ARTSPConnection
    //則對消息名爲kWhatObserveBinaryData的消息msg的處理就是在ARTSPConnection::onMessageReceived的case kWhatObserveBinaryData處理分支
    //將傳遞來的參數即消息reply的引用設置到"reply"字段中
    sp<AMessage> msg = new AMessage(kWhatObserveBinaryData, this);
    msg->setMessage("reply", reply);
    msg->post();
} 

==>
void ARTSPConnection::onMessageReceived(const sp<AMessage> &msg) {
    switch (msg->what()) {
        case kWhatConnect:
            onConnect(msg);
            break;

        case kWhatDisconnect:
            onDisconnect(msg);
            break;

        case kWhatCompleteConnection:
            onCompleteConnection(msg);
            break;

        case kWhatSendRequest:
            onSendRequest(msg);
            break;

        case kWhatReceiveResponse:
            onReceiveResponse();
            break;

        case kWhatObserveBinaryData:
        {
            //對消息名爲kWhatObserveBinaryData的處理
            //從該消息的"reply"字段得到reply消息的引用
            CHECK(msg->findMessage("reply", &mObserveBinaryMessage));
            break;
        }

        default:
            TRESPASS();
            break;
    }
}

  小結:MyHandler函數提供的connect函數接口對RTSP流的連接是通過MyHandler應有的ARTSPConnection對象的引用mConn提供的connect函數來完成對RTSP流的連接的。
  在連接前設置了ARTSPConnection對象mConn的成員對象mObserveBinaryMessage,即完成這個消息引用的設置,消息名爲’biny’,消息處理者爲MyHandler,對這個消息的處理將會在MyHandler::onMessageReceived的case ‘biny’處理分支裏。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章