android_GenericMediaPlayer.cpp revision ad1ab1d13a9b043202b9d5cdc1d8c4ef66cbbca8
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 mVideoSurfaceTexture(0), 62 mPlayer(0), 63 mPlayerClient(0) 64{ 65 SL_LOGI("GenericMediaPlayer::GenericMediaPlayer()"); 66 67 mServiceManager = defaultServiceManager(); 68 mBinder = mServiceManager->getService(String16("media.player")); 69 mMediaPlayerService = interface_cast<IMediaPlayerService>(mBinder); 70 71 CHECK(mMediaPlayerService.get() != NULL); 72 73 mPlayerClient = new MediaPlayerNotificationClient(); 74} 75 76GenericMediaPlayer::~GenericMediaPlayer() { 77 SL_LOGI("GenericMediaPlayer::~GenericMediaPlayer()"); 78 79} 80 81//-------------------------------------------------- 82void GenericMediaPlayer::setVideoSurface(const sp<Surface> &surface) { 83 mVideoSurface = surface; 84} 85 86void GenericMediaPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) { 87 mVideoSurfaceTexture = surfaceTexture; 88} 89 90//-------------------------------------------------- 91// Event handlers 92void GenericMediaPlayer::onPrepare() { 93 SL_LOGI("GenericMediaPlayer::onPrepare()"); 94 if (!(mStateFlags & kFlagPrepared) && (mPlayer != 0)) { 95 if (mHasVideo) { 96 if (mVideoSurface != 0) { 97 mPlayer->setVideoSurface(mVideoSurface); 98 } else if (mVideoSurfaceTexture != 0) { 99 mPlayer->setVideoSurfaceTexture(mVideoSurfaceTexture); 100 } 101 } 102 mPlayer->setAudioStreamType(mPlaybackParams.streamType); 103 mPlayer->prepareAsync(); 104 mPlayerClient->blockUntilPlayerPrepared(); 105 GenericPlayer::onPrepare(); 106 } 107 SL_LOGI("GenericMediaPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags); 108} 109 110void GenericMediaPlayer::onPlay() { 111 SL_LOGI("GenericMediaPlayer::onPlay()"); 112 if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) { 113 SL_LOGI("starting player"); 114 mPlayer->start(); 115 mStateFlags |= kFlagPlaying; 116 } else { 117 SL_LOGV("NOT starting player mStateFlags=0x%x", mStateFlags); 118 } 119} 120 121void GenericMediaPlayer::onPause() { 122 SL_LOGI("GenericMediaPlayer::onPause()"); 123 if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) { 124 mPlayer->pause(); 125 mStateFlags &= ~kFlagPlaying; 126 } 127 128} 129 130} // namespace android 131