android_GenericMediaPlayer.cpp revision 70c49ae2867094072a4365423417ea452bf82231
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//-------------------------------------------------- 92// Event handlers 93void GenericMediaPlayer::onPrepare() { 94 SL_LOGI("GenericMediaPlayer::onPrepare()"); 95 if (!(mStateFlags & kFlagPrepared) && (mPlayer != 0)) { 96 if (mHasVideo) { 97 if (mVideoSurface != 0) { 98 mPlayer->setVideoSurface(mVideoSurface); 99 } else if (mVideoSurfaceTexture != 0) { 100 mPlayer->setVideoSurfaceTexture(mVideoSurfaceTexture); 101 } 102 } 103 mPlayer->setAudioStreamType(mPlaybackParams.streamType); 104 mPlayer->prepareAsync(); 105 mPlayerClient->blockUntilPlayerPrepared(); 106 GenericPlayer::onPrepare(); 107 } 108 SL_LOGI("GenericMediaPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags); 109} 110 111void GenericMediaPlayer::onPlay() { 112 SL_LOGI("GenericMediaPlayer::onPlay()"); 113 if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) { 114 SL_LOGI("starting player"); 115 mPlayer->start(); 116 mStateFlags |= kFlagPlaying; 117 } else { 118 SL_LOGV("NOT starting player mStateFlags=0x%x", mStateFlags); 119 } 120} 121 122void GenericMediaPlayer::onPause() { 123 SL_LOGI("GenericMediaPlayer::onPause()"); 124 if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) { 125 mPlayer->pause(); 126 mStateFlags &= ~kFlagPlaying; 127 } 128 129} 130 131} // namespace android 132