Skip to content

Commit 2fa0ac2

Browse files
author
Jamie Gennis
committed
Stagefright: ANW::connect in MediaPlayerService
This change moves the ANativeWindow connect and disconnect logic from MediaPlayer to MediaPlayerService::Client. Bug: 5502654 Change-Id: Ifc43b98b01ad8f35d62d7ece43110724ec7fda3d
1 parent ed12460 commit 2fa0ac2

File tree

4 files changed

+65
-100
lines changed

4 files changed

+65
-100
lines changed

include/media/mediaplayer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ class MediaPlayer : public BnMediaPlayerClient,
209209
status_t prepareAsync_l();
210210
status_t getDuration_l(int *msec);
211211
status_t attachNewPlayer(const sp<IMediaPlayer>& player);
212-
void disconnectNativeWindow();
213212
status_t reset_l();
214213

215214
sp<IMediaPlayer> mPlayer;
@@ -233,8 +232,6 @@ class MediaPlayer : public BnMediaPlayerClient,
233232
int mVideoHeight;
234233
int mAudioSessionId;
235234
float mSendLevel;
236-
sp<ANativeWindow> mConnectedWindow;
237-
sp<IBinder> mConnectedWindowBinder;
238235
};
239236

240237
}; // namespace android

media/libmedia/mediaplayer.cpp

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ void MediaPlayer::disconnect()
8686
if (p != 0) {
8787
p->disconnect();
8888
}
89-
90-
disconnectNativeWindow();
9189
}
9290

9391
// always call with lock held
@@ -221,63 +219,12 @@ status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *m
221219
return mPlayer->getMetadata(update_only, apply_filter, metadata);
222220
}
223221

224-
void MediaPlayer::disconnectNativeWindow() {
225-
if (mConnectedWindow != NULL) {
226-
status_t err = native_window_api_disconnect(mConnectedWindow.get(),
227-
NATIVE_WINDOW_API_MEDIA);
228-
229-
if (err != OK) {
230-
LOGW("native_window_api_disconnect returned an error: %s (%d)",
231-
strerror(-err), err);
232-
}
233-
}
234-
mConnectedWindow.clear();
235-
}
236-
237222
status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface)
238223
{
239224
LOGV("setVideoSurface");
240225
Mutex::Autolock _l(mLock);
241226
if (mPlayer == 0) return NO_INIT;
242-
243-
sp<IBinder> binder(surface == NULL ? NULL : surface->asBinder());
244-
if (mConnectedWindowBinder == binder) {
245-
return OK;
246-
}
247-
248-
if (surface != NULL) {
249-
status_t err = native_window_api_connect(surface.get(),
250-
NATIVE_WINDOW_API_MEDIA);
251-
252-
if (err != OK) {
253-
LOGE("setVideoSurface failed: %d", err);
254-
// Note that we must do the reset before disconnecting from the ANW.
255-
// Otherwise queue/dequeue calls could be made on the disconnected
256-
// ANW, which may result in errors.
257-
reset_l();
258-
259-
disconnectNativeWindow();
260-
261-
return err;
262-
}
263-
}
264-
265-
// Note that we must set the player's new surface before disconnecting the
266-
// old one. Otherwise queue/dequeue calls could be made on the disconnected
267-
// ANW, which may result in errors.
268-
status_t err = mPlayer->setVideoSurface(surface);
269-
270-
disconnectNativeWindow();
271-
272-
mConnectedWindow = surface;
273-
274-
if (err == OK) {
275-
mConnectedWindowBinder = binder;
276-
} else {
277-
disconnectNativeWindow();
278-
}
279-
280-
return err;
227+
return mPlayer->setVideoSurface(surface);
281228
}
282229

283230
status_t MediaPlayer::setVideoSurfaceTexture(
@@ -286,48 +233,7 @@ status_t MediaPlayer::setVideoSurfaceTexture(
286233
LOGV("setVideoSurfaceTexture");
287234
Mutex::Autolock _l(mLock);
288235
if (mPlayer == 0) return NO_INIT;
289-
290-
sp<IBinder> binder(surfaceTexture == NULL ? NULL :
291-
surfaceTexture->asBinder());
292-
if (mConnectedWindowBinder == binder) {
293-
return OK;
294-
}
295-
296-
sp<ANativeWindow> anw;
297-
if (surfaceTexture != NULL) {
298-
anw = new SurfaceTextureClient(surfaceTexture);
299-
status_t err = native_window_api_connect(anw.get(),
300-
NATIVE_WINDOW_API_MEDIA);
301-
302-
if (err != OK) {
303-
LOGE("setVideoSurfaceTexture failed: %d", err);
304-
// Note that we must do the reset before disconnecting from the ANW.
305-
// Otherwise queue/dequeue calls could be made on the disconnected
306-
// ANW, which may result in errors.
307-
reset_l();
308-
309-
disconnectNativeWindow();
310-
311-
return err;
312-
}
313-
}
314-
315-
// Note that we must set the player's new SurfaceTexture before
316-
// disconnecting the old one. Otherwise queue/dequeue calls could be made
317-
// on the disconnected ANW, which may result in errors.
318-
status_t err = mPlayer->setVideoSurfaceTexture(surfaceTexture);
319-
320-
disconnectNativeWindow();
321-
322-
mConnectedWindow = anw;
323-
324-
if (err == OK) {
325-
mConnectedWindowBinder = binder;
326-
} else {
327-
disconnectNativeWindow();
328-
}
329-
330-
return err;
236+
return mPlayer->setVideoSurfaceTexture(surfaceTexture);
331237
}
332238

333239
// must call with lock held

media/libmediaplayerservice/MediaPlayerService.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <binder/IServiceManager.h>
4141
#include <binder/MemoryHeapBase.h>
4242
#include <binder/MemoryBase.h>
43+
#include <gui/SurfaceTextureClient.h>
4344
#include <utils/Errors.h> // for status_t
4445
#include <utils/String8.h>
4546
#include <utils/SystemClock.h>
@@ -528,6 +529,8 @@ void MediaPlayerService::Client::disconnect()
528529
p->reset();
529530
}
530531

532+
disconnectNativeWindow();
533+
531534
IPCThreadState::self()->flushCommands();
532535
}
533536

@@ -789,13 +792,67 @@ status_t MediaPlayerService::Client::setVideoSurface(const sp<Surface>& surface)
789792
return p->setVideoSurface(surface);
790793
}
791794

795+
void MediaPlayerService::Client::disconnectNativeWindow() {
796+
if (mConnectedWindow != NULL) {
797+
status_t err = native_window_api_disconnect(mConnectedWindow.get(),
798+
NATIVE_WINDOW_API_MEDIA);
799+
800+
if (err != OK) {
801+
LOGW("native_window_api_disconnect returned an error: %s (%d)",
802+
strerror(-err), err);
803+
}
804+
}
805+
mConnectedWindow.clear();
806+
}
807+
792808
status_t MediaPlayerService::Client::setVideoSurfaceTexture(
793809
const sp<ISurfaceTexture>& surfaceTexture)
794810
{
795811
LOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
796812
sp<MediaPlayerBase> p = getPlayer();
797813
if (p == 0) return UNKNOWN_ERROR;
798-
return p->setVideoSurfaceTexture(surfaceTexture);
814+
815+
sp<IBinder> binder(surfaceTexture == NULL ? NULL :
816+
surfaceTexture->asBinder());
817+
if (mConnectedWindowBinder == binder) {
818+
return OK;
819+
}
820+
821+
sp<ANativeWindow> anw;
822+
if (surfaceTexture != NULL) {
823+
anw = new SurfaceTextureClient(surfaceTexture);
824+
status_t err = native_window_api_connect(anw.get(),
825+
NATIVE_WINDOW_API_MEDIA);
826+
827+
if (err != OK) {
828+
LOGE("setVideoSurfaceTexture failed: %d", err);
829+
// Note that we must do the reset before disconnecting from the ANW.
830+
// Otherwise queue/dequeue calls could be made on the disconnected
831+
// ANW, which may result in errors.
832+
reset();
833+
834+
disconnectNativeWindow();
835+
836+
return err;
837+
}
838+
}
839+
840+
// Note that we must set the player's new SurfaceTexture before
841+
// disconnecting the old one. Otherwise queue/dequeue calls could be made
842+
// on the disconnected ANW, which may result in errors.
843+
status_t err = p->setVideoSurfaceTexture(surfaceTexture);
844+
845+
disconnectNativeWindow();
846+
847+
mConnectedWindow = anw;
848+
849+
if (err == OK) {
850+
mConnectedWindowBinder = binder;
851+
} else {
852+
disconnectNativeWindow();
853+
}
854+
855+
return err;
799856
}
800857

801858
status_t MediaPlayerService::Client::invoke(const Parcel& request,

media/libmediaplayerservice/MediaPlayerService.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ class MediaPlayerService : public BnMediaPlayerService
318318
// @param type Of the metadata to be recorded.
319319
void addNewMetadataUpdate(media::Metadata::Type type);
320320

321+
// Disconnect from the currently connected ANativeWindow.
322+
void disconnectNativeWindow();
323+
321324
mutable Mutex mLock;
322325
sp<MediaPlayerBase> mPlayer;
323326
sp<MediaPlayerService> mService;
@@ -329,6 +332,8 @@ class MediaPlayerService : public BnMediaPlayerService
329332
int32_t mConnId;
330333
int mAudioSessionId;
331334
uid_t mUID;
335+
sp<ANativeWindow> mConnectedWindow;
336+
sp<IBinder> mConnectedWindowBinder;
332337

333338
// Metadata filters.
334339
media::Metadata::Filter mMetadataAllow; // protected by mLock

0 commit comments

Comments
 (0)