OSDN Git Service

Various improvements to cleanly shutdown a wfd session.
authorAndreas Huber <andih@google.com>
Wed, 12 Sep 2012 23:25:14 +0000 (16:25 -0700)
committerThe Android Automerger <android-build@android.com>
Thu, 13 Sep 2012 22:47:40 +0000 (15:47 -0700)
Change-Id: I86f0a27d7e8eb96200153bab847a862f21a19d13

media/libstagefright/wifi-display/source/Converter.cpp
media/libstagefright/wifi-display/source/MediaPuller.cpp
media/libstagefright/wifi-display/source/MediaPuller.h
media/libstagefright/wifi-display/source/PlaybackSession.cpp
media/libstagefright/wifi-display/source/PlaybackSession.h
media/libstagefright/wifi-display/source/WifiDisplaySource.cpp

index 7c3b908..0e10b8d 100644 (file)
@@ -49,6 +49,10 @@ Converter::~Converter() {
         mEncoder->release();
         mEncoder.clear();
     }
+
+    AString mime;
+    CHECK(mInputFormat->findString("mime", &mime));
+    ALOGI("encoder (%s) shut down.", mime.c_str());
 }
 
 status_t Converter::initCheck() const {
index 786029a..35ae539 100644 (file)
@@ -33,7 +33,13 @@ MediaPuller::MediaPuller(
         const sp<MediaSource> &source, const sp<AMessage> &notify)
     : mSource(source),
       mNotify(notify),
-      mPullGeneration(0) {
+      mPullGeneration(0),
+      mIsAudio(false) {
+    sp<MetaData> meta = source->getFormat();
+    const char *mime;
+    CHECK(meta->findCString(kKeyMIMEType, &mime));
+
+    mIsAudio = !strncasecmp(mime, "audio/", 6);
 }
 
 MediaPuller::~MediaPuller() {
@@ -77,7 +83,14 @@ void MediaPuller::onMessageReceived(const sp<AMessage> &msg) {
                     schedulePull();
                 }
             } else {
+                sp<MetaData> meta = mSource->getFormat();
+                const char *tmp;
+                CHECK(meta->findCString(kKeyMIMEType, &tmp));
+                AString mime = tmp;
+
+                ALOGI("MediaPuller(%s) stopping.", mime.c_str());
                 err = mSource->stop();
+                ALOGI("MediaPuller(%s) stopped.", mime.c_str());
                 ++mPullGeneration;
             }
 
@@ -124,7 +137,15 @@ void MediaPuller::onMessageReceived(const sp<AMessage> &msg) {
                        mbuf->range_length());
 
                 accessUnit->meta()->setInt64("timeUs", timeUs);
-                accessUnit->meta()->setPointer("mediaBuffer", mbuf);
+
+                if (mIsAudio) {
+                    mbuf->release();
+                    mbuf = NULL;
+                } else {
+                    // video encoder will release MediaBuffer when done
+                    // with underlying data.
+                    accessUnit->meta()->setPointer("mediaBuffer", mbuf);
+                }
 
                 sp<AMessage> notify = mNotify->dup();
 
index 5297501..134e1c0 100644 (file)
@@ -49,6 +49,7 @@ private:
     sp<MediaSource> mSource;
     sp<AMessage> mNotify;
     int32_t mPullGeneration;
+    bool mIsAudio;
 
     status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
     void schedulePull();
index a6bb8c6..41beba4 100644 (file)
@@ -159,6 +159,8 @@ status_t WifiDisplaySource::PlaybackSession::Track::stop() {
         err = mMediaPuller->stop();
     }
 
+    mConverter.clear();
+
     mStarted = false;
 
     return err;
@@ -288,41 +290,6 @@ WifiDisplaySource::PlaybackSession::~PlaybackSession() {
         mLogFile = NULL;
     }
 #endif
-
-    mTracks.clear();
-
-    mPacketizer.clear();
-
-    if (mSerializer != NULL) {
-        mSerializer->stop();
-
-        looper()->unregisterHandler(mSerializer->id());
-        mSerializer.clear();
-    }
-
-    mTracks.clear();
-
-    if (mSerializerLooper != NULL) {
-        mSerializerLooper->stop();
-        mSerializerLooper.clear();
-    }
-
-    if (mLegacyMode) {
-        sp<IServiceManager> sm = defaultServiceManager();
-        sp<IBinder> binder = sm->getService(String16("SurfaceFlinger"));
-        sp<ISurfaceComposer> service = interface_cast<ISurfaceComposer>(binder);
-        CHECK(service != NULL);
-
-        service->connectDisplay(NULL);
-    }
-
-    if (mRTCPSessionID != 0) {
-        mNetSession->destroySession(mRTCPSessionID);
-    }
-
-    if (mRTPSessionID != 0) {
-        mNetSession->destroySession(mRTPSessionID);
-    }
 }
 
 int32_t WifiDisplaySource::PlaybackSession::getRTPPort() const {
@@ -369,6 +336,45 @@ status_t WifiDisplaySource::PlaybackSession::pause() {
     return OK;
 }
 
+status_t WifiDisplaySource::PlaybackSession::destroy() {
+    mTracks.clear();
+
+    mPacketizer.clear();
+
+    if (mSerializer != NULL) {
+        mSerializer->stop();
+
+        looper()->unregisterHandler(mSerializer->id());
+        mSerializer.clear();
+    }
+
+    mTracks.clear();
+
+    if (mSerializerLooper != NULL) {
+        mSerializerLooper->stop();
+        mSerializerLooper.clear();
+    }
+
+    if (mLegacyMode) {
+        sp<IServiceManager> sm = defaultServiceManager();
+        sp<IBinder> binder = sm->getService(String16("SurfaceFlinger"));
+        sp<ISurfaceComposer> service = interface_cast<ISurfaceComposer>(binder);
+        CHECK(service != NULL);
+
+        service->connectDisplay(NULL);
+    }
+
+    if (mRTCPSessionID != 0) {
+        mNetSession->destroySession(mRTCPSessionID);
+    }
+
+    if (mRTPSessionID != 0) {
+        mNetSession->destroySession(mRTPSessionID);
+    }
+
+    return OK;
+}
+
 void WifiDisplaySource::PlaybackSession::onMessageReceived(
         const sp<AMessage> &msg) {
     switch (msg->what()) {
index 528a039..cd0308e 100644 (file)
@@ -44,6 +44,8 @@ struct WifiDisplaySource::PlaybackSession : public AHandler {
             const char *clientIP, int32_t clientRtp, int32_t clientRtcp,
             bool useInterleavedTCP);
 
+    status_t destroy();
+
     int32_t getRTPPort() const;
 
     int64_t getLastLifesignUs() const;
index 8e8f04a..aeefcf3 100644 (file)
@@ -200,11 +200,14 @@ void WifiDisplaySource::onMessageReceived(const sp<AMessage> &msg) {
             CHECK(msg->senderAwaitsResponse(&replyID));
 
             for (size_t i = mPlaybackSessions.size(); i-- > 0;) {
-                const sp<PlaybackSession> &playbackSession =
+                sp<PlaybackSession> playbackSession =
                     mPlaybackSessions.valueAt(i);
 
-                looper()->unregisterHandler(playbackSession->id());
                 mPlaybackSessions.removeItemsAt(i);
+
+                playbackSession->destroy();
+                looper()->unregisterHandler(playbackSession->id());
+                playbackSession.clear();
             }
 
             if (mClient != NULL) {
@@ -454,9 +457,7 @@ status_t WifiDisplaySource::sendM16(int32_t sessionID) {
 
     const ClientInfo &info = mClientInfos.valueFor(sessionID);
     request.append(StringPrintf("Session: %d\r\n", info.mPlaybackSessionID));
-
-    request.append("Content-Length: 0\r\n");
-    request.append("\r\n");
+    request.append("\r\n");  // Empty body
 
     status_t err =
         mNetSession->sendRequest(sessionID, request.c_str(), request.size());
@@ -761,7 +762,7 @@ void WifiDisplaySource::onSetupRequest(
             return;
         }
 #if 1
-    // The LG dongle doesn't specify client_port=xxx apparently.
+    // The older LG dongles doesn't specify client_port=xxx apparently.
     } else if (transport == "RTP/AVP/UDP;unicast") {
         clientRtp = 19000;
         clientRtcp = clientRtp + 1;
@@ -966,19 +967,21 @@ void WifiDisplaySource::onSetParameterRequest(
         int32_t cseq,
         const sp<ParsedMessage> &data) {
     int32_t playbackSessionID;
-#if 0
-    // XXX the dongle does not include a "Session:" header in this request.
     sp<PlaybackSession> playbackSession =
         findPlaybackSession(data, &playbackSessionID);
 
+#if 1
+    // XXX the older dongles do not include a "Session:" header in this request.
+    if (playbackSession == NULL) {
+        CHECK_EQ(mPlaybackSessions.size(), 1u);
+        playbackSessionID = mPlaybackSessions.keyAt(0);
+        playbackSession = mPlaybackSessions.valueAt(0);
+    }
+#else
     if (playbackSession == NULL) {
         sendErrorResponse(sessionID, "454 Session Not Found", cseq);
         return;
     }
-#else
-    CHECK_EQ(mPlaybackSessions.size(), 1u);
-    playbackSessionID = mPlaybackSessions.keyAt(0);
-    sp<PlaybackSession> playbackSession = mPlaybackSessions.valueAt(0);
 #endif
 
     playbackSession->updateLiveness();