android_GenericMediaPlayer.cpp revision 68d56b8ebaf60184a3aef988e3d2b09ed8b88c05
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define USE_LOG SLAndroidLogLevel_Verbose
18
19#include "sles_allinclusive.h"
20#include <media/IMediaPlayerService.h>
21#include <surfaceflinger/ISurfaceComposer.h>
22#include <surfaceflinger/SurfaceComposerClient.h>
23
24namespace android {
25
26//--------------------------------------------------------------------------------------------------
27MediaPlayerNotificationClient::MediaPlayerNotificationClient() :
28    mPlayerPrepared(false)
29{
30
31}
32
33MediaPlayerNotificationClient::~MediaPlayerNotificationClient() {
34
35}
36
37//--------------------------------------------------
38// IMediaPlayerClient implementation
39void MediaPlayerNotificationClient::notify(int msg, int ext1, int ext2) {
40    SL_LOGI("MediaPlayerNotificationClient::notify(msg=%d, ext1=%d, ext2=%d)", msg, ext1, ext2);
41
42    if (msg == MEDIA_PREPARED) {
43        mPlayerPrepared = true;
44        mPlayerPreparedCondition.signal();
45    }
46}
47
48//--------------------------------------------------
49void MediaPlayerNotificationClient::blockUntilPlayerPrepared() {
50    Mutex::Autolock _l(mLock);
51    while (!mPlayerPrepared) {
52        mPlayerPreparedCondition.wait(mLock);
53    }
54}
55
56//--------------------------------------------------------------------------------------------------
57GenericMediaPlayer::GenericMediaPlayer(const AudioPlayback_Parameters* params, bool hasVideo) :
58    GenericPlayer(params),
59    mHasVideo(hasVideo),
60    mVideoSurface(0),
61    mPlayer(0),
62    mPlayerClient(0)
63{
64    SL_LOGI("GenericMediaPlayer::GenericMediaPlayer()");
65
66    mServiceManager = defaultServiceManager();
67    mBinder = mServiceManager->getService(String16("media.player"));
68    mMediaPlayerService = interface_cast<IMediaPlayerService>(mBinder);
69
70    CHECK(mMediaPlayerService.get() != NULL);
71
72    mPlayerClient = new MediaPlayerNotificationClient();
73}
74
75GenericMediaPlayer::~GenericMediaPlayer() {
76    SL_LOGI("GenericMediaPlayer::~GenericMediaPlayer()");
77
78}
79
80//--------------------------------------------------
81void GenericMediaPlayer::setVideoSurface(void* surface) {
82    mVideoSurface = static_cast<Surface *>((ANativeWindow*)surface);
83}
84
85//--------------------------------------------------
86// Event handlers
87void GenericMediaPlayer::onPrepare() {
88    SL_LOGI("GenericMediaPlayer::onPrepare()");
89    if (!(mStateFlags & kFlagPrepared) && (mPlayer != 0)) {
90        if (mHasVideo && (mVideoSurface != 0)) {
91            mPlayer->setVideoSurface(mVideoSurface);
92        }
93        mPlayer->setAudioStreamType(mPlaybackParams.streamType);
94        mPlayer->prepareAsync();
95        mPlayerClient->blockUntilPlayerPrepared();
96        GenericPlayer::onPrepare();
97    }
98    SL_LOGI("GenericMediaPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
99}
100
101void GenericMediaPlayer::onPlay() {
102    SL_LOGI("GenericMediaPlayer::onPlay()");
103    if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) {
104        SL_LOGI("starting player");
105        mPlayer->start();
106        mStateFlags |= kFlagPlaying;
107    } else {
108        SL_LOGV("NOT starting player mStateFlags=0x%x", mStateFlags);
109    }
110}
111
112void GenericMediaPlayer::onPause() {
113    SL_LOGI("GenericMediaPlayer::onPause()");
114    if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) {
115        mPlayer->pause();
116        mStateFlags &= ~kFlagPlaying;
117    }
118
119}
120
121} // namespace android
122