113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/*
213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project
313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * you may not use this file except in compliance with the License.
613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * You may obtain a copy of the License at
713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
1013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
1113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
1213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * See the License for the specific language governing permissions and
1413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * limitations under the License.
1513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi */
1613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//#define USE_LOG SLAndroidLogLevel_Verbose
1813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#include "sles_allinclusive.h"
2013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
214ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/foundation/ADebug.h>
224ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <sys/stat.h>
234ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
2413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
2513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
2713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviGenericPlayer::GenericPlayer(const AudioPlayback_Parameters* params) :
2813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDataLocatorType(kDataLocatorNone),
2913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mNotifyClient(NULL),
3013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mNotifyUser(NULL),
3113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags(0),
3237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        mPlaybackParams(*params),
334ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        mDurationMsec(ANDROID_UNKNOWN_TIME),
3491145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi        mPlaybackRatePermille(1000),
354ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        mCacheStatus(kStatusEmpty),
364ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        mCacheFill(0),
374ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        mLastNotifiedCacheFill(0),
385933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mCacheFillNotifThreshold(100),
395933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mEventFlags(0),
405933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mMarkerPositionMs(ANDROID_UNKNOWN_TIME),
415933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mPositionUpdatePeriodMs(1000), // per spec
425933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mOneShotGeneration(0),
435933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mDeliveredNewPosMs(ANDROID_UNKNOWN_TIME),
445933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        mObservedPositionMs(ANDROID_UNKNOWN_TIME)
4513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
46e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::GenericPlayer()");
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mLooper = new android::ALooper();
4913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // Post-construction accesses need to be protected by mSettingsLock
5137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    mAndroidAudioLevels.mFinalVolume[0] = 1.0f;
5237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    mAndroidAudioLevels.mFinalVolume[1] = 1.0f;
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviGenericPlayer::~GenericPlayer() {
57e31a69fcbe27894a597176d05a08e04c06092021Glenn Kasten    SL_LOGV("GenericPlayer::~GenericPlayer()");
5813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
59833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten    resetDataLocator();
6013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
6113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::init(const notif_cbf_t cbf, void* notifUser) {
64e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::init()");
6513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
67e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        android::Mutex::Autolock autoLock(mNotifyClientLock);
68e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mNotifyClient = cbf;
69e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mNotifyUser = notifUser;
70e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    }
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mLooper->registerHandler(this);
733ac5dcc05fe321e4f01918aef2e3e54e22c9a5c1Glenn Kasten    mLooper->start(false /*runOnCallingThread*/, false /*canCallJava*/, PRIORITY_DEFAULT);
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
77e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivivoid GenericPlayer::preDestroy() {
78e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    SL_LOGD("GenericPlayer::preDestroy()");
79e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
80e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        android::Mutex::Autolock autoLock(mNotifyClientLock);
81e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mNotifyClient = NULL;
82e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mNotifyUser = NULL;
83e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    }
84e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
85e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    mLooper->stop();
86e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    mLooper->unregisterHandler(id());
87e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi}
88e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
89e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::setDataSource(const char *uri) {
91b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::setDataSource(uri=%s)", uri);
9213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    resetDataLocator();
9313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataLocator.uriRef = uri;
9513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataLocatorType = kDataLocatorUri;
9713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
9813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
100833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kastenvoid GenericPlayer::setDataSource(int fd, int64_t offset, int64_t length, bool closeAfterUse) {
101833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten    SL_LOGV("GenericPlayer::setDataSource(fd=%d, offset=%lld, length=%lld, closeAfterUse=%s)", fd,
102833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            offset, length, closeAfterUse ? "true" : "false");
10313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    resetDataLocator();
10413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataLocator.fdi.fd = fd;
10613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    struct stat sb;
10813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int ret = fstat(fd, &sb);
10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (ret != 0) {
11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("GenericPlayer::setDataSource: fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
11113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
11213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
11313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (offset >= sb.st_size) {
11513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("SfPlayer::setDataSource: invalid offset");
11613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
11713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
11813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataLocator.fdi.offset = offset;
11913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
12013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (PLAYER_FD_FIND_FILE_SIZE == length) {
12113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDataLocator.fdi.length = sb.st_size;
12213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else if (offset + length > sb.st_size) {
12313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDataLocator.fdi.length = sb.st_size - offset;
12413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
12513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDataLocator.fdi.length = length;
12613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
12713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
128833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten    mDataLocator.fdi.mCloseAfterUse = closeAfterUse;
129833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten
13013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataLocatorType = kDataLocatorFd;
13113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
13213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::prepare() {
135e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::prepare()");
13685edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten    // do not attempt prepare more than once
13785edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten    if (!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully))) {
13885edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten        sp<AMessage> msg = new AMessage(kWhatPrepare, id());
13985edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten        msg->post();
14085edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten    }
14113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
14213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::play() {
145e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::play()");
14613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatPlay, id());
14713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
14813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
14913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
15013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
15113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::pause() {
152e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::pause()");
15313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatPause, id());
15413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
15513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
15613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
15713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
15813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::stop() {
159e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::stop()");
16013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    (new AMessage(kWhatPause, id()))->post();
16113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // after a stop, playback should resume from the start.
16313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    seek(0);
16413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
16513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::seek(int64_t timeMsec) {
16813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("GenericPlayer::seek %lld", timeMsec);
169a9f22e6f5f53e90daa779e38b22f88e4faa35c95Glenn Kasten    if (timeMsec < 0 && timeMsec != ANDROID_UNKNOWN_TIME) {
1707ef5526a7bd12eccfa777cc8bc167794634f405aJean-Michel Trivi        SL_LOGE("GenericPlayer::seek error, can't seek to negative time %lldms", timeMsec);
1717ef5526a7bd12eccfa777cc8bc167794634f405aJean-Michel Trivi        return;
1727ef5526a7bd12eccfa777cc8bc167794634f405aJean-Michel Trivi    }
17313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatSeek, id());
17413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->setInt64(WHATPARAM_SEEK_SEEKTIME_MS, timeMsec);
17513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
17613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
17713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::loop(bool loop) {
180b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::loop %s", loop ? "true" : "false");
18113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatLoop, id());
18213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->setInt32(WHATPARAM_LOOP_LOOPING, (int32_t)loop);
18313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
18413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
18513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1874ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivivoid GenericPlayer::setBufferingUpdateThreshold(int16_t thresholdPercent) {
188b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::setBufferingUpdateThreshold %d", thresholdPercent);
1894ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatBuffUpdateThres, id());
1904ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    msg->setInt32(WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT, (int32_t)thresholdPercent);
1914ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    msg->post();
1924ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi}
1934ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
1944ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
19513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
19670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivivoid GenericPlayer::getDurationMsec(int* msec) {
1971fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    Mutex::Autolock _l(mSettingsLock);
1984ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    *msec = mDurationMsec;
1994ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi}
2004ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
20137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi//--------------------------------------------------
202fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kastenvoid GenericPlayer::setVolume(float leftVol, float rightVol)
203fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten{
20437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    {
20537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        Mutex::Autolock _l(mSettingsLock);
20637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        mAndroidAudioLevels.mFinalVolume[0] = leftVol;
20737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        mAndroidAudioLevels.mFinalVolume[1] = rightVol;
20837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    }
20937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    // send a message for the volume to be updated by the object which implements the volume
21037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    (new AMessage(kWhatVolumeUpdate, id()))->post();
21137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi}
21237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
21370c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
21470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi//--------------------------------------------------
2153610785fa93586ce84a27a27530feb77b8035229Glenn Kastenvoid GenericPlayer::attachAuxEffect(int32_t effectId)
2163610785fa93586ce84a27a27530feb77b8035229Glenn Kasten{
2173610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    SL_LOGV("GenericPlayer::attachAuxEffect(id=%d)", effectId);
2183610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    sp<AMessage> msg = new AMessage(kWhatAttachAuxEffect, id());
2193610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    msg->setInt32(WHATPARAM_ATTACHAUXEFFECT, effectId);
2203610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    msg->post();
2213610785fa93586ce84a27a27530feb77b8035229Glenn Kasten}
2223610785fa93586ce84a27a27530feb77b8035229Glenn Kasten
2233610785fa93586ce84a27a27530feb77b8035229Glenn Kasten
2243610785fa93586ce84a27a27530feb77b8035229Glenn Kasten//--------------------------------------------------
2253610785fa93586ce84a27a27530feb77b8035229Glenn Kastenvoid GenericPlayer::setAuxEffectSendLevel(float level)
2263610785fa93586ce84a27a27530feb77b8035229Glenn Kasten{
2273610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    SL_LOGV("GenericPlayer::setAuxEffectSendLevel(level=%g)", level);
2283610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    sp<AMessage> msg = new AMessage(kWhatSetAuxEffectSendLevel, id());
2293610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    msg->setFloat(WHATPARAM_SETAUXEFFECTSENDLEVEL, level);
2303610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    msg->post();
2313610785fa93586ce84a27a27530feb77b8035229Glenn Kasten}
2323610785fa93586ce84a27a27530feb77b8035229Glenn Kasten
2333610785fa93586ce84a27a27530feb77b8035229Glenn Kasten
2343610785fa93586ce84a27a27530feb77b8035229Glenn Kasten//--------------------------------------------------
23591145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivivoid GenericPlayer::setPlaybackRate(int32_t ratePermille) {
23691145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi    SL_LOGV("GenericPlayer::setPlaybackRate(ratePermille=%d)", ratePermille);
23791145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi    {
23891145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi        Mutex::Autolock _l(mSettingsLock);
23991145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi        mPlaybackRatePermille = (int16_t)ratePermille;
24091145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi    }
24191145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi}
24291145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi
24391145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi//--------------------------------------------------
2445933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten// Call after changing any of the IPlay settings related to SL_PLAYEVENT_*
2455933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kastenvoid GenericPlayer::setPlayEvents(int32_t eventFlags, int32_t markerPositionMs,
2465933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        int32_t positionUpdatePeriodMs)
2475933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten{
2485933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    // Normalize ms that are within the valid unsigned range, but not in the int32_t range
2495933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    if (markerPositionMs < 0) {
2505933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        markerPositionMs = ANDROID_UNKNOWN_TIME;
2515933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    }
2525933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    if (positionUpdatePeriodMs < 0) {
2535933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        positionUpdatePeriodMs = ANDROID_UNKNOWN_TIME;
2545933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    }
2555933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    // markers are delivered accurately, but new position updates are limited to every 100 ms
2565933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    if (positionUpdatePeriodMs < 100) {
2575933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        positionUpdatePeriodMs = 100;
2585933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    }
2595933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    sp<AMessage> msg = new AMessage(kWhatSetPlayEvents, id());
2605933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    msg->setInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, eventFlags);
2615933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    msg->setInt32(WHATPARAM_SETPLAYEVENTS_MARKER, markerPositionMs);
2625933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    msg->setInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, positionUpdatePeriodMs);
2635933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    msg->post();
2645933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten}
2655933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten
2665933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten
2675933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten//--------------------------------------------------
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/*
26913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * post-condition: mDataLocatorType == kDataLocatorNone
27013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi */
27213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::resetDataLocator() {
273b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::resetDataLocator()");
274833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten    if (mDataLocatorType == kDataLocatorFd && mDataLocator.fdi.mCloseAfterUse) {
275833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        (void) ::close(mDataLocator.fdi.fd);
276833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // would be redundant, as we're about to invalidate the union mDataLocator
277833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        //mDataLocator.fdi.fd = -1;
278833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        //mDataLocator.fdi.mCloseAfterUse = false;
279833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten    }
28013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataLocatorType = kDataLocatorNone;
28113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
28213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
28313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
28413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::notify(const char* event, int data, bool async) {
285b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::notify(event=%s, data=%d, async=%s)", event, data,
286b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            async ? "true" : "false");
28713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatNotif, id());
28813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->setInt32(event, (int32_t)data);
28913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (async) {
29013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        msg->post();
29113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
29270e776a54bc945a6a01717a784e66476bc5d450fGlenn Kasten        onNotify(msg);
29313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
29413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
29513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
29613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
29737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivivoid GenericPlayer::notify(const char* event, int data1, int data2, bool async) {
298b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::notify(event=%s, data1=%d, data2=%d, async=%s)", event, data1, data2,
299b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            async ? "true" : "false");
30037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatNotif, id());
30137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    msg->setRect(event, 0, 0, (int32_t)data1, (int32_t)data2);
30237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    if (async) {
30337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        msg->post();
30437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    } else {
30570e776a54bc945a6a01717a784e66476bc5d450fGlenn Kasten        onNotify(msg);
30637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    }
30737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi}
30837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
30937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
31113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// AHandler implementation
31213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::onMessageReceived(const sp<AMessage> &msg) {
313b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::onMessageReceived()");
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (msg->what()) {
31513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPrepare:
316fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten            SL_LOGV("kWhatPrepare");
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPrepare();
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
31913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatNotif:
321fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten            SL_LOGV("kWhatNotif");
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onNotify(msg);
32313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
32413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPlay:
326b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatPlay");
32713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPlay();
32813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
32913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPause:
331b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatPause");
33213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPause();
33313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
33413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatSeek:
336b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatSeek");
33713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onSeek(msg);
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
33913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatLoop:
341b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatLoop");
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onLoop(msg);
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        case kWhatVolumeUpdate:
346b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatVolumeUpdate");
34737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi            onVolumeUpdate();
34837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi            break;
34937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
3504ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        case kWhatSeekComplete:
351b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatSeekComplete");
3524ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            onSeekComplete();
3534ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            break;
3544ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
3554ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        case kWhatBufferingUpdate:
356b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatBufferingUpdate");
3574ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            onBufferingUpdate(msg);
3584ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            break;
3594ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
3604ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        case kWhatBuffUpdateThres:
361b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten            SL_LOGV("kWhatBuffUpdateThres");
3624ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            onSetBufferingUpdateThreshold(msg);
3634ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            break;
3644ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
3653610785fa93586ce84a27a27530feb77b8035229Glenn Kasten        case kWhatAttachAuxEffect:
3663610785fa93586ce84a27a27530feb77b8035229Glenn Kasten            SL_LOGV("kWhatAttachAuxEffect");
3673610785fa93586ce84a27a27530feb77b8035229Glenn Kasten            onAttachAuxEffect(msg);
3683610785fa93586ce84a27a27530feb77b8035229Glenn Kasten            break;
3693610785fa93586ce84a27a27530feb77b8035229Glenn Kasten
3703610785fa93586ce84a27a27530feb77b8035229Glenn Kasten        case kWhatSetAuxEffectSendLevel:
3713610785fa93586ce84a27a27530feb77b8035229Glenn Kasten            SL_LOGV("kWhatSetAuxEffectSendLevel");
3723610785fa93586ce84a27a27530feb77b8035229Glenn Kasten            onSetAuxEffectSendLevel(msg);
3733610785fa93586ce84a27a27530feb77b8035229Glenn Kasten            break;
3743610785fa93586ce84a27a27530feb77b8035229Glenn Kasten
3755933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        case kWhatSetPlayEvents:
3765933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            SL_LOGV("kWhatSetPlayEvents");
3775933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            onSetPlayEvents(msg);
3785933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            break;
3795933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten
3805933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        case kWhatOneShot:
3815933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            SL_LOGV("kWhatOneShot");
3825933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            onOneShot(msg);
3835933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            break;
3845933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        default:
3865933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten            SL_LOGE("GenericPlayer::onMessageReceived unknown message %d", msg->what());
38713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            TRESPASS();
38813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
38913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
39013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
39313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
3944ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi//  it is strictly verboten to call those methods outside of the event loop
3954ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::onPrepare() {
397fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten    SL_LOGV("GenericPlayer::onPrepare()");
39849935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    // Subclass is responsible for indicating whether prepare was successful or unsuccessful
39949935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    // by updating mStateFlags accordingly.  It must set exactly one of these two flags.
40049935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    assert(!(mStateFlags & kFlagPrepared) != !(mStateFlags & kFlagPreparedUnsuccessfully));
40149935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    notify(PLAYEREVENT_PREPARED, mStateFlags & kFlagPrepared ? PLAYER_SUCCESS : PLAYER_FAILURE,
402d6be802c5267a3c8599c11af2c38ead666f74a72Glenn Kasten            true /*async*/);
403e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::onNotify(const sp<AMessage> &msg) {
408b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    SL_LOGV("GenericPlayer::onNotify()");
409e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    notif_cbf_t notifClient;
410e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    void*       notifUser;
411e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
412e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        android::Mutex::Autolock autoLock(mNotifyClientLock);
413e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL == mNotifyClient) {
414e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
415e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        } else {
416e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifClient = mNotifyClient;
417e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifUser   = mNotifyUser;
418e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
41913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    int32_t val1, val2;
4224ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val1)) {
4234ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val1);
424e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifClient(kEventPrefetchStatusChange, val1, 0, notifUser);
425e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // There is exactly one notification per message, hence "else if" instead of "if"
4264ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    } else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val1)) {
4274ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val1);
428e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifClient(kEventPrefetchFillLevelUpdate, val1, 0, notifUser);
4294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    } else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val1)) {
4304ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val1);
431e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifClient(kEventEndOfStream, val1, 0, notifUser);
4324ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    } else if (msg->findInt32(PLAYEREVENT_PREPARED, &val1)) {
43337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREPARED, val1);
434e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifClient(kEventPrepared, val1, 0, notifUser);
435fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten    } else if (msg->findInt32(PLAYEREVENT_CHANNEL_COUNT, &val1)) {
436fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_CHANNEL_COUNT, val1);
437fa2bd93c3a9852a1f879663eeff598d13cf8fa81Glenn Kasten        notifClient(kEventChannelCount, val1, 0, notifUser);
43837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    } else if (msg->findRect(PLAYEREVENT_VIDEO_SIZE_UPDATE, &val1, &val2, &val1, &val2)) {
4394ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi        SL_LOGV("GenericPlayer notifying %s = %d, %d", PLAYEREVENT_VIDEO_SIZE_UPDATE, val1, val2);
440e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifClient(kEventHasVideoSize, val1, val2, notifUser);
4415933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    } else if (msg->findInt32(PLAYEREVENT_PLAY, &val1)) {
4425933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PLAY, val1);
4435933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        notifClient(kEventPlay, val1, 0, notifUser);
4445e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten    } else if (msg->findInt32(PLAYEREVENT_ERRORAFTERPREPARE, &val1)) {
4455e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_ERRORAFTERPREPARE, val1);
4465e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        notifClient(kEventErrorAfterPrepare, val1, 0, notifUser);
447b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten    } else {
448b2549c73290f1955f3a7731bf98446a45f295dfaGlenn Kasten        SL_LOGV("GenericPlayer notifying unknown");
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid GenericPlayer::onPlay() {
454e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("GenericPlayer::onPlay()");
4555933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten    if ((mStateFlags & (kFlagPrepared | kFlagPlaying)) == kFlagPrepared) {
456e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi        SL_LOGD("starting player");
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagPlaying;
4585933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        updateOneShot();
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
461