1eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi/*
2eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project
3eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi *
4eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
5eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * you may not use this file except in compliance with the License.
6eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * You may obtain a copy of the License at
7eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi *
8eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
9eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi *
10eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
12eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * See the License for the specific language governing permissions and
14eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi * limitations under the License.
15eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi */
16eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
1701e4a8ff63523bba5c8f919a72e0adb66daf4b98Mathias Agopian#include <system/window.h>
1801e4a8ff63523bba5c8f919a72e0adb66daf4b98Mathias Agopian#include <utils/StrongPointer.h>
1901e4a8ff63523bba5c8f919a72e0adb66daf4b98Mathias Agopian#include <gui/Surface.h>
2001e4a8ff63523bba5c8f919a72e0adb66daf4b98Mathias Agopian
21eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi#include "sles_allinclusive.h"
22eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi#include "android_prompts.h"
232b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi// LocAVPlayer and StreamPlayer derive from GenericMediaPlayer,
242b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi//    so no need to #include "android_GenericMediaPlayer.h"
252b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi#include "android_LocAVPlayer.h"
262b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi#include "android_StreamPlayer.h"
27eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
28581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi//-----------------------------------------------------------------------------
2937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivistatic void player_handleMediaPlayerEventNotifications(int event, int data1, int data2, void* user)
30581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi{
31c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
32c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    // FIXME This code is derived from similar code in sfplayer_handlePrefetchEvent.  The two
33c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    // versions are quite similar, but still different enough that they need to be separate.
34c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    // At some point they should be re-factored and merged if feasible.
35c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    // As with other OpenMAX AL implementation code, this copy mostly uses SL_ symbols
36c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    // rather than XA_ unless the difference is significant.
37c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
38581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    if (NULL == user) {
39581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        return;
40581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    }
41581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
42581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    CMediaPlayer* mp = (CMediaPlayer*) user;
439d8a98601357c0669dca4de63e43196c0a70553dGlenn Kasten    if (!android::CallbackProtector::enterCbIfOk(mp->mCallbackProtector)) {
449d8a98601357c0669dca4de63e43196c0a70553dGlenn Kasten        // it is not safe to enter the callback (the media player is about to go away)
459d8a98601357c0669dca4de63e43196c0a70553dGlenn Kasten        return;
469d8a98601357c0669dca4de63e43196c0a70553dGlenn Kasten    }
47c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    union {
48c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        char c[sizeof(int)];
49c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        int i;
50c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    } u;
51c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    u.i = event;
52c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten    SL_LOGV("player_handleMediaPlayerEventNotifications(event='%c%c%c%c' (%d), data1=%d, data2=%d, "
53c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            "user=%p) from AVPlayer", u.c[3], u.c[2], u.c[1], u.c[0], event, data1, data2, user);
54581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    switch(event) {
55581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
5637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi      case android::GenericPlayer::kEventPrepared: {
577f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        SL_LOGV("Received GenericPlayer::kEventPrepared for CMediaPlayer %p", mp);
58c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
59c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        // assume no callback
60c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        slPrefetchCallback callback = NULL;
617f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        void* callbackPContext;
627f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        XAuint32 events;
63c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
64c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        object_lock_exclusive(&mp->mObject);
657f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten
667f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        // mark object as prepared; same state is used for successful or unsuccessful prepare
677f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        assert(mp->mAndroidObjState == ANDROID_PREPARING);
68c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        mp->mAndroidObjState = ANDROID_READY;
69c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
707f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        if (PLAYER_SUCCESS == data1) {
717f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            // Most of successful prepare completion for mp->mAVPlayer
727f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            // is handled by GenericPlayer and its subclasses.
737f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten        } else {
747f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            // AVPlayer prepare() failed prefetching, there is no event in XAPrefetchStatus to
757f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            //  indicate a prefetch error, so we signal it by sending simultaneously two events:
767f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            //  - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0
777f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            //  - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW
787f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            SL_LOGE(ERROR_PLAYER_PREFETCH_d, data1);
797f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            if (IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
807f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                mp->mPrefetchStatus.mLevel = 0;
817f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
827f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                if (!(~mp->mPrefetchStatus.mCallbackEventsMask &
837f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                        (SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE))) {
847f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                    callback = mp->mPrefetchStatus.mCallback;
857f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                    callbackPContext = mp->mPrefetchStatus.mContext;
867f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                    events = SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE;
877f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten                }
88c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            }
89581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        }
907f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten
91c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        object_unlock_exclusive(&mp->mObject);
92c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
93c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        // callback with no lock held
94c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (NULL != callback) {
957f250a17c145382b866d5d4d7ef23d65fada6236Glenn Kasten            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext, events);
96581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        }
97c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
9837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        break;
9937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi      }
10037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
10137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi      case android::GenericPlayer::kEventHasVideoSize: {
10237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p",
10337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                data1, data2, mp);
10437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
10537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        object_lock_exclusive(&mp->mObject);
10637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
10737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        // remove an existing video info entry (here we only have one video stream)
10837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        for(size_t i=0 ; i < mp->mStreamInfo.mStreamInfoTable.size() ; i++) {
10937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi            if (XA_DOMAINTYPE_VIDEO == mp->mStreamInfo.mStreamInfoTable.itemAt(i).domain) {
11037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                mp->mStreamInfo.mStreamInfoTable.removeAt(i);
11137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                break;
11237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi            }
11337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        }
11437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        // update the stream information with a new video info entry
11537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        StreamInfo streamInfo;
11637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        streamInfo.domain = XA_DOMAINTYPE_VIDEO;
11737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        streamInfo.videoInfo.codecId = 0;// unknown, we don't have that info FIXME
11837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        streamInfo.videoInfo.width = (XAuint32)data1;
11937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        streamInfo.videoInfo.height = (XAuint32)data2;
12037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        streamInfo.videoInfo.bitRate = 0;// unknown, we don't have that info FIXME
121cb441acdda6f8e81d44fcdaadd4ff7ab3d3e367bGlenn Kasten        streamInfo.videoInfo.frameRate = 0;
12237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        streamInfo.videoInfo.duration = XA_TIME_UNKNOWN;
12337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        StreamInfo &contInfo = mp->mStreamInfo.mStreamInfoTable.editItemAt(0);
12437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        contInfo.containerInfo.numStreams = 1;
12537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        ssize_t index = mp->mStreamInfo.mStreamInfoTable.add(streamInfo);
12637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
127e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten        // callback is unconditional; there is no bitmask of enabled events
12837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback;
12937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        void* callbackPContext = mp->mStreamInfo.mContext;
13037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
13137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        object_unlock_exclusive(&mp->mObject);
13237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
13385133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi        // enqueue notification (outside of lock) that the stream information has been updated
13437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        if ((NULL != callback) && (index >= 0)) {
135dd177e2d3923d4653eaa4226f07b89a999907970Glenn Kasten#ifndef USE_ASYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE_CALLBACK
13637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi            (*callback)(&mp->mStreamInfo.mItf, XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
13737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                    1 /*streamIndex, only one stream supported here, 0 is reserved*/,
13837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                    NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
13937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                    callbackPContext /*pContext*/);
14085133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi#else
14185133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi            SLresult res = EnqueueAsyncCallback_piipp(mp, callback,
14285133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    /*p1*/ &mp->mStreamInfo.mItf,
14385133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    /*i1*/ XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
14485133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    /*i2*/ 1 /*streamIndex, only one stream supported here, 0 is reserved*/,
14585133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    /*p2*/ NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
14685133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    /*p3*/ callbackPContext /*pContext*/);
147a6c69c7e1665b38da8d6784e65210acbe501b92cSteve Block            ALOGW_IF(SL_RESULT_SUCCESS != res,
148dd177e2d3923d4653eaa4226f07b89a999907970Glenn Kasten                        "Callback %p(%p, XA_STREAMCBEVENT_PROPERTYCHANGE, 1, NULL, %p) dropped",
149dd177e2d3923d4653eaa4226f07b89a999907970Glenn Kasten                        callback, &mp->mStreamInfo.mItf, callbackPContext);
15085133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi#endif
151581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        }
152581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        break;
15337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi      }
154581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
15592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      case android::GenericPlayer::kEventEndOfStream: {
15692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp);
15792b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
15892b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        object_lock_exclusive(&mp->mObject);
15992b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        // should be xaPlayCallback but we're sharing the itf between SL and AL
16092b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        slPlayCallback playCallback = NULL;
16192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        void * playContext = NULL;
16292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        // XAPlayItf callback or no callback?
16392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        if (mp->mPlay.mEventFlags & XA_PLAYEVENT_HEADATEND) {
16492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi            playCallback = mp->mPlay.mCallback;
16592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi            playContext = mp->mPlay.mContext;
16692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        }
167e37c62f0691f9a137afae60f9d6dbd1a65d36aedGlenn Kasten        mp->mPlay.mState = XA_PLAYSTATE_PAUSED;
16892b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        object_unlock_exclusive(&mp->mObject);
16992b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
17085133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi        // enqueue callback with no lock held
17192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        if (NULL != playCallback) {
172dd177e2d3923d4653eaa4226f07b89a999907970Glenn Kasten#ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK
17392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi            (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND);
17485133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi#else
17585133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi            SLresult res = EnqueueAsyncCallback_ppi(mp, playCallback, &mp->mPlay.mItf, playContext,
17685133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    XA_PLAYEVENT_HEADATEND);
177a6c69c7e1665b38da8d6784e65210acbe501b92cSteve Block            ALOGW_IF(SL_RESULT_SUCCESS != res,
178c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten                    "Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback,
17985133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi                    &mp->mPlay.mItf, playContext);
18085133c817f6f387cd7d072988a8818f18bb53702Jean-Michel Trivi#endif
18192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        }
182581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        break;
18392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      }
18492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
18599b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      case android::GenericPlayer::kEventChannelCount: {
18699b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        SL_LOGV("kEventChannelCount channels = %d", data1);
18799b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        object_lock_exclusive(&mp->mObject);
18899b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        if (UNKNOWN_NUMCHANNELS == mp->mNumChannels && UNKNOWN_NUMCHANNELS != data1) {
18999b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten            mp->mNumChannels = data1;
19099b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten            android_Player_volumeUpdate(mp);
19199b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        }
19299b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        object_unlock_exclusive(&mp->mObject);
19399b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      }
19499b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      break;
19599b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten
19699b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      case android::GenericPlayer::kEventPrefetchFillLevelUpdate: {
19799b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        SL_LOGV("kEventPrefetchFillLevelUpdate");
198c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (!IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
199c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            break;
200c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
201c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        slPrefetchCallback callback = NULL;
202c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        void* callbackPContext = NULL;
203c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
204c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        // SLPrefetchStatusItf callback or no callback?
205c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        interface_lock_exclusive(&mp->mPrefetchStatus);
206c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (mp->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
207c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            callback = mp->mPrefetchStatus.mCallback;
208c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            callbackPContext = mp->mPrefetchStatus.mContext;
209c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
210c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        mp->mPrefetchStatus.mLevel = (SLpermille)data1;
211c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        interface_unlock_exclusive(&mp->mPrefetchStatus);
212c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
213c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        // callback with no lock held
214c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (NULL != callback) {
215c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext,
216c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten                    SL_PREFETCHEVENT_FILLLEVELCHANGE);
217c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
21899b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      }
21999b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      break;
22099b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten
22199b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      case android::GenericPlayer::kEventPrefetchStatusChange: {
22299b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        SL_LOGV("kEventPrefetchStatusChange");
223c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (!IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
224c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            break;
225c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
226c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        slPrefetchCallback callback = NULL;
227c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        void* callbackPContext = NULL;
228c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
229c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        // SLPrefetchStatusItf callback or no callback?
230c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        object_lock_exclusive(&mp->mObject);
231c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (mp->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) {
232c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            callback = mp->mPrefetchStatus.mCallback;
233c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            callbackPContext = mp->mPrefetchStatus.mContext;
234c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
235c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (data1 >= android::kStatusIntermediate) {
236c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
237c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        } else if (data1 < android::kStatusIntermediate) {
238c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
239c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
240c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        object_unlock_exclusive(&mp->mObject);
241c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten
242c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        // callback with no lock held
243c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        if (NULL != callback) {
244c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE);
245c623c89c0a32c5fc77c998f1742d58e7be69e8c1Glenn Kasten        }
24699b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      }
24799b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten      break;
24899b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten
249bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten      case android::GenericPlayer::kEventPlay: {
250bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        SL_LOGV("kEventPlay");
251bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten
252bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        interface_lock_shared(&mp->mPlay);
253bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        slPlayCallback callback = mp->mPlay.mCallback;
254bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        void* callbackPContext = mp->mPlay.mContext;
255bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        interface_unlock_shared(&mp->mPlay);
256bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten
257bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        if (NULL != callback) {
258bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten            (*callback)(&mp->mPlay.mItf, callbackPContext, (SLuint32) data1); // SL_PLAYEVENT_HEAD*
259bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        }
260bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten      }
261bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten      break;
26299b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten
2635e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten      case android::GenericPlayer::kEventErrorAfterPrepare: {
2645e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        SL_LOGV("kEventErrorAfterPrepare");
2655e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten
2665e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        // assume no callback
2675e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        slPrefetchCallback callback = NULL;
2685e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        void* callbackPContext = NULL;
2695e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten
2705e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        object_lock_exclusive(&mp->mObject);
2715e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        if (IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
2725e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten            mp->mPrefetchStatus.mLevel = 0;
2735e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten            mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
2745e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten            if (!(~mp->mPrefetchStatus.mCallbackEventsMask &
2755e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten                    (SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE))) {
2765e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten                callback = mp->mPrefetchStatus.mCallback;
2775e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten                callbackPContext = mp->mPrefetchStatus.mContext;
2785e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten            }
2795e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        }
2805e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        object_unlock_exclusive(&mp->mObject);
2815e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten
2825e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        // FIXME there's interesting information in data1, but no API to convey it to client
2835e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        SL_LOGE("Error after prepare: %d", data1);
2845e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten
2855e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        // callback with no lock held
2865e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        if (NULL != callback) {
2875e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext,
2885e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten                    SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
2895e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten        }
2905e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten
2915e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten      }
2925e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten      break;
2935e4d65e369f28746767aba11b618dee314bb8197Glenn Kasten
29492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      default: {
29592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1);
29692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      }
297581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    }
2989d8a98601357c0669dca4de63e43196c0a70553dGlenn Kasten
2999d8a98601357c0669dca4de63e43196c0a70553dGlenn Kasten    mp->mCallbackProtector->exitCb();
300581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi}
301581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
302eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
303eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi//-----------------------------------------------------------------------------
304d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel TriviXAresult android_Player_checkSourceSink(CMediaPlayer *mp) {
305d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
306d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
307d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
308d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    const SLDataSource *pSrc    = &mp->mDataSource.u.mSource;
309d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
310d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
311d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    // format check:
312d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator;
313d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    const SLuint32 sourceFormatType  = *(SLuint32 *)pSrc->pFormat;
314d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
315d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat;
316d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
317d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    // Source check
318d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    switch(sourceLocatorType) {
319d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
320d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: {
321d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        switch (sourceFormatType) {
322d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        case XA_DATAFORMAT_MIME: {
323d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi            SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat;
324d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi            if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) {
325d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
326d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        "that is not fed MPEG-2 TS data");
327d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                return SL_RESULT_CONTENT_UNSUPPORTED;
328d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi            }
329d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        } break;
330d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        default:
331d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi            SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
332d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    "without SL_DATAFORMAT_MIME format");
333d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi            return XA_RESULT_CONTENT_UNSUPPORTED;
334d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        }
335d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    } break;
336d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
337d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    case XA_DATALOCATOR_URI: // intended fall-through
338d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    case XA_DATALOCATOR_ANDROIDFD:
339d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        break;
340d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
341d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    default:
342d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        SL_LOGE("Cannot create media player with data locator type 0x%x",
343d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                (unsigned) sourceLocatorType);
344d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        return SL_RESULT_PARAMETER_INVALID;
345d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    }// switch (locatorType)
346d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
347d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    // Audio sink check: only playback is supported here
348d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    switch(audioSinkLocatorType) {
349d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
350d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    case XA_DATALOCATOR_OUTPUTMIX:
351d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        break;
352d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
353d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    default:
354d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x",
355d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                (unsigned) audioSinkLocatorType);
356d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi        return XA_RESULT_PARAMETER_INVALID;
357d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    }// switch (locaaudioSinkLocatorTypeorType)
358d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
359d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi    return result;
360d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi}
361d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
362d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
363d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi//-----------------------------------------------------------------------------
364eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel TriviXAresult android_Player_create(CMediaPlayer *mp) {
365eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
366eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
367eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
368eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    // FIXME verify data source
369eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
370eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    // FIXME verify audio data sink
371eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
372eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    // FIXME verify image data sink
373eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink;
374eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
37597876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi    XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator;
376eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    switch(sourceLocator) {
37797876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi    // FIXME support Android simple buffer queue as well
378f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    case XA_DATALOCATOR_ANDROIDBUFFERQUEUE:
379b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi        mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE;
380eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        break;
381f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    case XA_DATALOCATOR_URI: // intended fall-through
38297876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi    case SL_DATALOCATOR_ANDROIDFD:
383b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi        mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
38497876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        break;
385f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    case XA_DATALOCATOR_ADDRESS: // intended fall-through
386eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    default:
38772042d4448cee63528c619537321ba73944c6382Glenn Kasten        mp->mAndroidObjType = INVALID_TYPE;
388a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten        SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
389eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        result = XA_RESULT_PARAMETER_INVALID;
390eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        break;
391eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    }
392eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
39364621eac543d714d4d3f7cb9c24205f2ddc59201Glenn Kasten    // FIXME duplicates an initialization also done by higher level
394f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    mp->mAndroidObjState = ANDROID_UNINITIALIZED;
395f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
396d0081ece36dcb6ecdadeb9fc0ee912cffb7effc7Eric Laurent    mp->mSessionId = android::AudioSystem::newAudioUniqueId();
397eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
39872042d4448cee63528c619537321ba73944c6382Glenn Kasten    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
39972042d4448cee63528c619537321ba73944c6382Glenn Kasten    // android::AudioSystem::acquireAudioSessionId(mp->mSessionId);
40072042d4448cee63528c619537321ba73944c6382Glenn Kasten
401485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    mp->mCallbackProtector = new android::CallbackProtector();
402485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten
403eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    return result;
404eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi}
405eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
406eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
407eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi//-----------------------------------------------------------------------------
408eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
409eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel TriviXAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
410ecc4fe22e076c4e5c891d823b01db1a683ba6690Glenn Kasten    SL_LOGV("android_Player_realize_l(%p)", mp);
411eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
412eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
413eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
414eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
415eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
416eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    AudioPlayback_Parameters ap_params;
417f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    ap_params.sessionId = mp->mSessionId;
418f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    ap_params.streamType = mp->mStreamType;
419eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
420f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi    switch(mp->mAndroidObjType) {
421b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
4227133228a478e16458b659946f2180ecddd13fda7Glenn Kasten        mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/,
4237133228a478e16458b659946f2180ecddd13fda7Glenn Kasten                &mp->mAndroidBufferQueue, mp->mCallbackProtector);
424581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi        mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
425eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        }
426eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        break;
427b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi    case AUDIOVIDEOPLAYER_FROM_URIFD: {
42868d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi        mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/);
42997876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
43097876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        switch (mp->mDataSource.mLocator.mLocatorType) {
43197876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        case XA_DATALOCATOR_URI:
43297876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
43397876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi                    (const char*)mp->mDataSource.mLocator.mURI.URI);
43497876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            break;
43597876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        case XA_DATALOCATOR_ANDROIDFD: {
43697876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset;
43797876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
43897876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi                    (int)mp->mDataSource.mLocator.mFD.fd,
43997876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi                    offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
44097876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi                            (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
44197876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi                    (int64_t)mp->mDataSource.mLocator.mFD.length);
44297876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            }
44397876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            break;
44497876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        default:
445a8179ea15c4ff78db589d742b135649f0eda7ef2Glenn Kasten            SL_LOGE("Invalid or unsupported data locator type %u for data source",
44697876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi                    mp->mDataSource.mLocator.mLocatorType);
44797876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi            result = XA_RESULT_PARAMETER_INVALID;
44897876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        }
44997876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        }
45097876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi        break;
451321f2cffd7dd560bf2e5c898be6953e19bed8496Jean-Michel Trivi    case INVALID_TYPE: // intended fall-through
452eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    default:
453f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi        SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
454eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        result = XA_RESULT_PARAMETER_INVALID;
455eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi        break;
456eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    }
457eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
45835ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    if (XA_RESULT_SUCCESS == result) {
45935ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten
46035ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        // if there is a video sink
46135ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        if (XA_DATALOCATOR_NATIVEDISPLAY ==
46235ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten                mp->mImageVideoSink.mLocator.mLocatorType) {
46335ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten            ANativeWindow *nativeWindow = (ANativeWindow *)
46435ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten                    mp->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
46535ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten            // we already verified earlier that hWindow is non-NULL
46635ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten            assert(nativeWindow != NULL);
46735ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten            result = android_Player_setNativeWindow(mp, nativeWindow);
46835ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        }
46935ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten
47035ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    }
47135ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten
472eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    return result;
473eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi}
474eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
475485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten// Called with a lock on MediaPlayer, and blocks until safe to destroy
476485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn KastenXAresult android_Player_preDestroy(CMediaPlayer *mp) {
477485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    SL_LOGV("android_Player_preDestroy(%p)", mp);
478485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten
479b66dfcb9e7b944c45927314ef2282d6cc95cfa0aGlenn Kasten    // Not yet clear why this order is important, but it reduces detected deadlocks
480485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    object_unlock_exclusive(&mp->mObject);
481485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    if (mp->mCallbackProtector != 0) {
482485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten        mp->mCallbackProtector->requestCbExitAndWait();
483485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    }
484485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    object_lock_exclusive(&mp->mObject);
485485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten
486b66dfcb9e7b944c45927314ef2282d6cc95cfa0aGlenn Kasten    if (mp->mAVPlayer != 0) {
487b66dfcb9e7b944c45927314ef2282d6cc95cfa0aGlenn Kasten        mp->mAVPlayer->preDestroy();
488b66dfcb9e7b944c45927314ef2282d6cc95cfa0aGlenn Kasten    }
489b66dfcb9e7b944c45927314ef2282d6cc95cfa0aGlenn Kasten    SL_LOGV("android_Player_preDestroy(%p) after mAVPlayer->preDestroy()", mp);
490b66dfcb9e7b944c45927314ef2282d6cc95cfa0aGlenn Kasten
491485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    return XA_RESULT_SUCCESS;
492485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten}
493485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten
49497876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi//-----------------------------------------------------------------------------
49597876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel TriviXAresult android_Player_destroy(CMediaPlayer *mp) {
496ecc4fe22e076c4e5c891d823b01db1a683ba6690Glenn Kasten    SL_LOGV("android_Player_destroy(%p)", mp);
49797876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi
498485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    mp->mAVPlayer.clear();
49972042d4448cee63528c619537321ba73944c6382Glenn Kasten
50072042d4448cee63528c619537321ba73944c6382Glenn Kasten    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
50172042d4448cee63528c619537321ba73944c6382Glenn Kasten    // android::AudioSystem::releaseAudioSessionId(mp->mSessionId);
50272042d4448cee63528c619537321ba73944c6382Glenn Kasten
503485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    mp->mCallbackProtector.clear();
50497876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi
505485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    // explicit destructor
506485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    mp->mAVPlayer.~sp();
507485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    mp->mCallbackProtector.~sp();
508485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten
509485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten    return XA_RESULT_SUCCESS;
51097876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi}
511eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
512ad1ab1d13a9b043202b9d5cdc1d8c4ef66cbbca8Glenn Kasten
513bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kastenvoid android_Player_usePlayEventMask(CMediaPlayer *mp) {
514bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten    if (mp->mAVPlayer != 0) {
515bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        IPlay *pPlayItf = &mp->mPlay;
516bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten        mp->mAVPlayer->setPlayEvents((int32_t) pPlayItf->mEventFlags,
517bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten                (int32_t) pPlayItf->mMarkerPosition, (int32_t) pPlayItf->mPositionUpdatePeriod);
518bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten    }
519bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten}
520bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten
521bcfe680db1e392f3bb29382c2e15e89c3af783edGlenn Kasten
52270c49ae2867094072a4365423417ea452bf82231Jean-Michel TriviXAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
52370c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi    CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
52470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
52570c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi    switch (avp->mAndroidObjType) {
52670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
527b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi    case AUDIOVIDEOPLAYER_FROM_URIFD: {
5283d332ff421e7179c36fb652771cc8ded53383729Glenn Kasten        int dur = ANDROID_UNKNOWN_TIME;
52970c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi        if (avp->mAVPlayer != 0) {
53070c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi            avp->mAVPlayer->getDurationMsec(&dur);
53170c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi        }
53235a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        if (dur == ANDROID_UNKNOWN_TIME) {
53335a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi            *pDurMsec = XA_TIME_UNKNOWN;
53470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi        } else {
53570c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi            *pDurMsec = (XAmillisecond)dur;
53670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi        }
53770c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi    } break;
53870c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
5393d332ff421e7179c36fb652771cc8ded53383729Glenn Kasten    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
54070c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi    default:
5413d332ff421e7179c36fb652771cc8ded53383729Glenn Kasten        *pDurMsec = XA_TIME_UNKNOWN;
54235a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        break;
54335a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    }
54435a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi
5453d332ff421e7179c36fb652771cc8ded53383729Glenn Kasten    return XA_RESULT_SUCCESS;
54635a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi}
54735a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi
54835a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi
54935a5a30fdad179ccf38d8d756590411326159a89Jean-Michel TriviXAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) {
55035a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    SL_LOGD("android_Player_getPosition()");
55135a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
55235a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
55335a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi
55435a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    switch (avp->mAndroidObjType) {
55535a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi
55635a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
55735a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    case AUDIOVIDEOPLAYER_FROM_URIFD: {
558136965c92c625d52a6cbad42f82a2091d7769c9cGlenn Kasten        int pos = ANDROID_UNKNOWN_TIME;
55935a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        if (avp->mAVPlayer != 0) {
56035a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi            avp->mAVPlayer->getPositionMsec(&pos);
56135a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        }
56235a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        if (pos == ANDROID_UNKNOWN_TIME) {
563136965c92c625d52a6cbad42f82a2091d7769c9cGlenn Kasten            *pPosMsec = 0;
56435a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        } else {
56535a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi            *pPosMsec = (XAmillisecond)pos;
56635a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        }
56735a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    } break;
56835a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi
56935a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi    default:
57035a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        // we shouldn't be here
57135a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi        assert(false);
57270c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi        break;
57370c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi    }
57470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
57570c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi    return result;
57670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi}
57770c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
57837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
57937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi//-----------------------------------------------------------------------------
58037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi/**
58199b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten * pre-condition: mp != NULL
58237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi */
58399b927751677abfb60a388d65dfeed1fed1db12cGlenn Kastenvoid android_Player_volumeUpdate(CMediaPlayer* mp)
58437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi{
58599b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten    android::GenericPlayer* avp = mp->mAVPlayer.get();
58699b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten    if (avp != NULL) {
58799b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        float volumes[2];
58899b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        // MediaPlayer does not currently support EffectSend or MuteSolo
58999b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        android_player_volumeUpdate(volumes, &mp->mVolume, mp->mNumChannels, 1.0f, NULL);
59099b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        float leftVol = volumes[0], rightVol = volumes[1];
59199b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten        avp->setVolume(leftVol, rightVol);
59299b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten    }
59337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi}
59437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
5950e47a0657162bbff48886ea2f5c68d9edb607768Jean-Michel Trivi//-----------------------------------------------------------------------------
5960e47a0657162bbff48886ea2f5c68d9edb607768Jean-Michel Trivi/**
59736b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi * pre-condition: gp != 0
598f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi */
59936b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel TriviXAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
60036b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi        SLuint32 playState,
601b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi        AndroidObjectState* pObjState)
602eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi{
603eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
604b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi    AndroidObjectState objState = *pObjState;
605eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
606eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    switch (playState) {
607eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi     case SL_PLAYSTATE_STOPPED: {
608f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
60936b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi         gp->stop();
61097876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi         }
61197876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi         break;
612eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi     case SL_PLAYSTATE_PAUSED: {
613f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
614eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         switch(objState) {
615e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi         case ANDROID_UNINITIALIZED:
616581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             *pObjState = ANDROID_PREPARING;
61736b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi             gp->prepare();
618581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             break;
619e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi         case ANDROID_PREPARING:
620eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi             break;
621e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi         case ANDROID_READY:
62236b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi             gp->pause();
623581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             break;
624eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         default:
625f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi             SL_LOGE("Android object in invalid state");
626eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi             break;
627eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         }
62897876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi         }
62997876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi         break;
630eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi     case SL_PLAYSTATE_PLAYING: {
631f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
632eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         switch(objState) {
633e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi         case ANDROID_UNINITIALIZED:
634581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             *pObjState = ANDROID_PREPARING;
63536b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi             gp->prepare();
636581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             // intended fall through
637e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi         case ANDROID_PREPARING:
638581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             // intended fall through
639e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi         case ANDROID_READY:
64036b700a829b7a02b873b4cd0cdb0a95342b20a31Jean-Michel Trivi             gp->play();
641581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi             break;
642eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         default:
643f271eea20f9fff6c101213b34652399f457bcd50Jean-Michel Trivi             SL_LOGE("Android object in invalid state");
644eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi             break;
645eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         }
64697876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi         }
64797876858aa17c7f24c6a1d60be09a57bc1824ba3Jean-Michel Trivi         break;
648eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi     default:
649eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         // checked by caller, should not happen
650eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi         break;
651eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi     }
652eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
653eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    return result;
654eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi}
655eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
656eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
65792b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi/**
65892b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi * pre-condition: mp != NULL
65992b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi */
66092b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel TriviXAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) {
66192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
66292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    switch (mp->mAndroidObjType) {
66392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      case AUDIOVIDEOPLAYER_FROM_URIFD:
66492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        if (mp->mAVPlayer !=0) {
66592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi            mp->mAVPlayer->seek(posMsec);
66692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        }
66792b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        break;
66892b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
66992b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      default: {
670f5ff1a75f55677163bd9a8bd804e8f5c33ef592cGlenn Kasten          result = XA_RESULT_FEATURE_UNSUPPORTED;
67192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      }
67292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    }
67392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    return result;
67492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi}
67592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
67692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
67792b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi/**
67892b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi * pre-condition: mp != NULL
67992b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi */
68092b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel TriviXAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) {
68192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    XAresult result = XA_RESULT_SUCCESS;
68292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    switch (mp->mAndroidObjType) {
68392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      case AUDIOVIDEOPLAYER_FROM_URIFD:
68492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        if (mp->mAVPlayer !=0) {
68592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi            mp->mAVPlayer->loop(loopEnable);
68692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        }
68792b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi        break;
68892b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
68992b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      default: {
690f5ff1a75f55677163bd9a8bd804e8f5c33ef592cGlenn Kasten          result = XA_RESULT_FEATURE_UNSUPPORTED;
69192b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi      }
69292b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    }
69392b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi    return result;
69492b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi}
69592b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
69692b245bf8828db9e469febebbe8774c00570b5b9Jean-Michel Trivi
697eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi//-----------------------------------------------------------------------------
698e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivivoid android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) {
699b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
700b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi            && (mp->mAVPlayer != 0)) {
701e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
702e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi        splr->appClear_l();
703e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi    }
704e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi}
705e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi
706e7bfcdc183454ec959ff51342f0973cabba219b2Jean-Michel Trivi
70770c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivivoid android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) {
708b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
709b05ea38e5131001884aa226f90fd50cf594a23f3Jean-Michel Trivi            && (mp->mAVPlayer != 0)) {
71070c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
711bc0e642e6c1a51b3ae3a02d490d94b03e718e6b5Jean-Michel Trivi        splr->queueRefilled();
712eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi    }
713eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi}
714eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
715eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi
71635ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten/*
71735ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten *  pre-conditions:
71835ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten *      mp != NULL
71935ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten *      mp->mAVPlayer != 0 (player is realized)
72035ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten *      nativeWindow can be NULL, but if NULL it is treated as an error
72135ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten */
72235ac702ee1ad91e5c8748c12450222d50b366a52Glenn KastenSLresult android_Player_setNativeWindow(CMediaPlayer *mp, ANativeWindow *nativeWindow)
72335ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten{
72435ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    assert(mp != NULL);
72535ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    assert(mp->mAVPlayer != 0);
72635ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    if (nativeWindow == NULL) {
72735ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        SL_LOGE("ANativeWindow is NULL");
72835ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
72935ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    }
73035ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    SLresult result;
73135ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    int err;
73235ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    int value;
73335ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    // this could crash if app passes in a bad parameter, but that's OK
73435ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    err = (*nativeWindow->query)(nativeWindow, NATIVE_WINDOW_CONCRETE_TYPE, &value);
73535ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    if (0 != err) {
73635ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        SL_LOGE("Query NATIVE_WINDOW_CONCRETE_TYPE on ANativeWindow * %p failed; "
73735ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten                "errno %d", nativeWindow, err);
73835ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
73935ac702ee1ad91e5c8748c12450222d50b366a52Glenn Kasten    } else {
740