android_GenericMediaPlayer.cpp revision 241b9c06493479dc632a8851097c193b724a2b41
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define USE_LOG SLAndroidLogLevel_Verbose
18
19#include "sles_allinclusive.h"
20#include "android_GenericMediaPlayer.h"
21
22#include <media/IMediaPlayerService.h>
23#include <surfaceflinger/ISurfaceComposer.h>
24#include <surfaceflinger/SurfaceComposerClient.h>
25#include <media/stagefright/foundation/ADebug.h>
26
27// default delay in Us used when reposting an event when the player is not ready to accept
28// the command yet. This is for instance used when seeking on a MediaPlayer that's still preparing
29#define DEFAULT_COMMAND_DELAY_FOR_REPOST_US (100*1000) // 100ms
30
31// table of prefixes for known distant protocols; these are immediately dispatched to mediaserver
32static const char* const kDistantProtocolPrefix[] = { "http://", "https://", "rtsp://"};
33#define NB_DISTANT_PROTOCOLS (sizeof(kDistantProtocolPrefix)/sizeof(kDistantProtocolPrefix[0]))
34
35// is the specified URI a known distant protocol?
36bool isDistantProtocol(const char *uri)
37{
38    for (unsigned int i = 0; i < NB_DISTANT_PROTOCOLS; i++) {
39        if (!strncasecmp(uri, kDistantProtocolPrefix[i], strlen(kDistantProtocolPrefix[i]))) {
40            return true;
41        }
42    }
43    return false;
44}
45
46namespace android {
47
48//--------------------------------------------------------------------------------------------------
49MediaPlayerNotificationClient::MediaPlayerNotificationClient(GenericMediaPlayer* gmp) :
50    mGenericMediaPlayer(gmp),
51    mPlayerPrepared(PREPARE_NOT_STARTED)
52{
53    SL_LOGV("MediaPlayerNotificationClient::MediaPlayerNotificationClient()");
54}
55
56MediaPlayerNotificationClient::~MediaPlayerNotificationClient() {
57    SL_LOGV("MediaPlayerNotificationClient::~MediaPlayerNotificationClient()");
58}
59
60// Map a MEDIA_* enum to a string
61static const char *media_to_string(int msg)
62{
63    switch (msg) {
64#define _(x) case MEDIA_##x: return "MEDIA_" #x;
65      _(PREPARED)
66      _(SET_VIDEO_SIZE)
67      _(SEEK_COMPLETE)
68      _(PLAYBACK_COMPLETE)
69      _(BUFFERING_UPDATE)
70      _(ERROR)
71      _(NOP)
72      _(TIMED_TEXT)
73      _(INFO)
74#undef _
75    default:
76        return NULL;
77    }
78}
79
80//--------------------------------------------------
81// IMediaPlayerClient implementation
82void MediaPlayerNotificationClient::notify(int msg, int ext1, int ext2, const Parcel *obj) {
83    SL_LOGV("MediaPlayerNotificationClient::notify(msg=%s (%d), ext1=%d, ext2=%d)",
84            media_to_string(msg), msg, ext1, ext2);
85
86    sp<GenericMediaPlayer> genericMediaPlayer(mGenericMediaPlayer.promote());
87    if (genericMediaPlayer == NULL) {
88        SL_LOGW("MediaPlayerNotificationClient::notify after GenericMediaPlayer destroyed");
89        return;
90    }
91
92    switch (msg) {
93      case MEDIA_PREPARED:
94        {
95        Mutex::Autolock _l(mLock);
96        if (PREPARE_IN_PROGRESS == mPlayerPrepared) {
97            mPlayerPrepared = PREPARE_COMPLETED_SUCCESSFULLY;
98            mPlayerPreparedCondition.signal();
99        } else {
100            SL_LOGE("Unexpected MEDIA_PREPARED");
101        }
102        }
103        break;
104
105      case MEDIA_SET_VIDEO_SIZE:
106        // only send video size updates if the player was flagged as having video, to avoid
107        // sending video size updates of (0,0)
108        // We're running on a different thread than genericMediaPlayer's ALooper thread,
109        // so it would normally be racy to access fields within genericMediaPlayer.
110        // But in this case mHasVideo is const, so it is safe to access.
111        // Or alternatively, we could notify unconditionally and let it decide whether to handle.
112        if (genericMediaPlayer->mHasVideo) {
113            genericMediaPlayer->notify(PLAYEREVENT_VIDEO_SIZE_UPDATE,
114                    (int32_t)ext1, (int32_t)ext2, true /*async*/);
115        }
116        break;
117
118      case MEDIA_SEEK_COMPLETE:
119        genericMediaPlayer->seekComplete();
120        break;
121
122      case MEDIA_PLAYBACK_COMPLETE:
123        genericMediaPlayer->notify(PLAYEREVENT_ENDOFSTREAM, 1, true /*async*/);
124        break;
125
126      case MEDIA_BUFFERING_UPDATE:
127        // values received from Android framework for buffer fill level use percent,
128        //   while SL/XA use permille, so does GenericPlayer
129        genericMediaPlayer->bufferingUpdate(ext1 * 10 /*fillLevelPerMille*/);
130        break;
131
132      case MEDIA_ERROR:
133        {
134        Mutex::Autolock _l(mLock);
135        if (PREPARE_IN_PROGRESS == mPlayerPrepared) {
136            mPlayerPrepared = PREPARE_COMPLETED_UNSUCCESSFULLY;
137            mPlayerPreparedCondition.signal();
138        } else {
139            // inform client of errors after preparation
140            genericMediaPlayer->notify(PLAYEREVENT_ERRORAFTERPREPARE, ext1, true /*async*/);
141        }
142        }
143        break;
144
145      case MEDIA_NOP:
146      case MEDIA_TIMED_TEXT:
147      case MEDIA_INFO:
148        break;
149
150      default: { }
151    }
152
153}
154
155//--------------------------------------------------
156void MediaPlayerNotificationClient::beforePrepare()
157{
158    Mutex::Autolock _l(mLock);
159    assert(mPlayerPrepared == PREPARE_NOT_STARTED);
160    mPlayerPrepared = PREPARE_IN_PROGRESS;
161}
162
163//--------------------------------------------------
164bool MediaPlayerNotificationClient::blockUntilPlayerPrepared() {
165    Mutex::Autolock _l(mLock);
166    assert(mPlayerPrepared != PREPARE_NOT_STARTED);
167    while (mPlayerPrepared == PREPARE_IN_PROGRESS) {
168        mPlayerPreparedCondition.wait(mLock);
169    }
170    assert(mPlayerPrepared == PREPARE_COMPLETED_SUCCESSFULLY ||
171            mPlayerPrepared == PREPARE_COMPLETED_UNSUCCESSFULLY);
172    return mPlayerPrepared == PREPARE_COMPLETED_SUCCESSFULLY;
173}
174
175//--------------------------------------------------------------------------------------------------
176GenericMediaPlayer::GenericMediaPlayer(const AudioPlayback_Parameters* params, bool hasVideo) :
177    GenericPlayer(params),
178    mHasVideo(hasVideo),
179    mSeekTimeMsec(0),
180    mVideoSurfaceTexture(0),
181    mPlayer(0),
182    mPlayerClient(new MediaPlayerNotificationClient(this))
183{
184    SL_LOGD("GenericMediaPlayer::GenericMediaPlayer()");
185
186}
187
188GenericMediaPlayer::~GenericMediaPlayer() {
189    SL_LOGD("GenericMediaPlayer::~GenericMediaPlayer()");
190}
191
192void GenericMediaPlayer::preDestroy() {
193    // FIXME can't access mPlayer from outside the looper (no mutex!) so using mPreparedPlayer
194    sp<IMediaPlayer> player;
195    getPreparedPlayer(player);
196    if (player != NULL) {
197        player->stop();
198        // causes CHECK failure in Nuplayer, but commented out in the subclass preDestroy
199        // randomly causes a NPE in StagefrightPlayer, heap corruption, or app hang
200        //player->setDataSource(NULL);
201        player->setVideoSurfaceTexture(NULL);
202        player->disconnect();
203        // release all references to the IMediaPlayer
204        // FIXME illegal if not on looper
205        //mPlayer.clear();
206        {
207            Mutex::Autolock _l(mPreparedPlayerLock);
208            mPreparedPlayer.clear();
209        }
210    }
211    GenericPlayer::preDestroy();
212}
213
214//--------------------------------------------------
215// overridden from GenericPlayer
216// pre-condition:
217//   msec != NULL
218// post-condition
219//   *msec ==
220//                  ANDROID_UNKNOWN_TIME if position is unknown at time of query,
221//               or the current MediaPlayer position
222void GenericMediaPlayer::getPositionMsec(int* msec) {
223    SL_LOGD("GenericMediaPlayer::getPositionMsec()");
224    sp<IMediaPlayer> player;
225    getPreparedPlayer(player);
226    // To avoid deadlock, directly call the MediaPlayer object
227    if (player == 0 || player->getCurrentPosition(msec) != NO_ERROR) {
228        *msec = ANDROID_UNKNOWN_TIME;
229    }
230}
231
232//--------------------------------------------------
233void GenericMediaPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
234    SL_LOGV("GenericMediaPlayer::setVideoSurfaceTexture()");
235    // FIXME bug - race condition, should do in looper
236    if (mVideoSurfaceTexture.get() == surfaceTexture.get()) {
237        return;
238    }
239    if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) {
240        mPlayer->setVideoSurfaceTexture(surfaceTexture);
241    }
242    mVideoSurfaceTexture = surfaceTexture;
243}
244
245
246//--------------------------------------------------
247// Event handlers
248
249// blocks until mPlayer is prepared
250void GenericMediaPlayer::onPrepare() {
251    SL_LOGD("GenericMediaPlayer::onPrepare()");
252    // Attempt to prepare at most once, and only if there is a MediaPlayer
253    if (!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully)) && (mPlayer != 0)) {
254        if (mHasVideo) {
255            if (mVideoSurfaceTexture != 0) {
256                mPlayer->setVideoSurfaceTexture(mVideoSurfaceTexture);
257            }
258        }
259        mPlayer->setAudioStreamType(mPlaybackParams.streamType);
260        mPlayerClient->beforePrepare();
261        mPlayer->prepareAsync();
262        if (mPlayerClient->blockUntilPlayerPrepared()) {
263            mStateFlags |= kFlagPrepared;
264            afterMediaPlayerPreparedSuccessfully();
265        } else {
266            mStateFlags |= kFlagPreparedUnsuccessfully;
267        }
268    }
269    GenericPlayer::onPrepare();
270    SL_LOGD("GenericMediaPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
271}
272
273
274void GenericMediaPlayer::onPlay() {
275    SL_LOGD("GenericMediaPlayer::onPlay()");
276    if (((mStateFlags & (kFlagPrepared | kFlagPlaying)) == kFlagPrepared) && (mPlayer != 0)) {
277        mPlayer->start();
278    }
279    GenericPlayer::onPlay();
280}
281
282
283void GenericMediaPlayer::onPause() {
284    SL_LOGD("GenericMediaPlayer::onPause()");
285    if (!(~mStateFlags & (kFlagPrepared | kFlagPlaying)) && (mPlayer != 0)) {
286        mPlayer->pause();
287    }
288    GenericPlayer::onPause();
289}
290
291
292void GenericMediaPlayer::onSeekComplete() {
293    SL_LOGV("GenericMediaPlayer::onSeekComplete()");
294    // did we initiate the seek?
295    if (!(mStateFlags & kFlagSeeking)) {
296        // no, are we looping?
297        if (mStateFlags & kFlagLooping) {
298            // yes, per OpenSL ES 1.0.1 and 1.1 do NOT report it to client
299            // notify(PLAYEREVENT_ENDOFSTREAM, 1, true /*async*/);
300        // no, well that's surprising, but it's probably just a benign race condition
301        } else {
302            SL_LOGW("Unexpected seek complete event ignored");
303        }
304    }
305    GenericPlayer::onSeekComplete();
306}
307
308
309/**
310 * pre-condition: WHATPARAM_SEEK_SEEKTIME_MS parameter value >= 0
311 */
312void GenericMediaPlayer::onSeek(const sp<AMessage> &msg) {
313    SL_LOGV("GenericMediaPlayer::onSeek");
314    int64_t timeMsec = ANDROID_UNKNOWN_TIME;
315    if (!msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec)) {
316        // invalid command, drop it
317        return;
318    }
319    if ((mStateFlags & kFlagSeeking) && (timeMsec == mSeekTimeMsec) &&
320            (timeMsec != ANDROID_UNKNOWN_TIME)) {
321        // already seeking to the same non-unknown time, cancel this command
322        return;
323    } else if (mStateFlags & kFlagPreparedUnsuccessfully) {
324        // discard seeks after unsuccessful prepare
325    } else if (!(mStateFlags & kFlagPrepared)) {
326        // we are not ready to accept a seek command at this time, retry later
327        msg->post(DEFAULT_COMMAND_DELAY_FOR_REPOST_US);
328    } else {
329        if (mPlayer != 0) {
330            mStateFlags |= kFlagSeeking;
331            mSeekTimeMsec = (int32_t)timeMsec;
332            // seek to unknown time is used by StreamPlayer after discontinuity
333            if (timeMsec == ANDROID_UNKNOWN_TIME) {
334                // FIXME simulate a MEDIA_SEEK_COMPLETE event in 250 ms;
335                // this is a terrible hack to make up for mediaserver not sending one
336                (new AMessage(kWhatSeekComplete, id()))->post(250000);
337            } else if (OK != mPlayer->seekTo(timeMsec)) {
338                mStateFlags &= ~kFlagSeeking;
339                mSeekTimeMsec = ANDROID_UNKNOWN_TIME;
340            }
341        }
342    }
343}
344
345
346void GenericMediaPlayer::onLoop(const sp<AMessage> &msg) {
347    SL_LOGV("GenericMediaPlayer::onLoop");
348    int32_t loop = 0;
349    if (msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop)) {
350        if (loop) {
351            mStateFlags |= kFlagLooping;
352        } else {
353            mStateFlags &= ~kFlagLooping;
354        }
355        // if we have a MediaPlayer then tell it now, otherwise we'll tell it after it's created
356        if (mPlayer != 0) {
357            (void) mPlayer->setLooping(loop);
358        }
359    }
360}
361
362
363void GenericMediaPlayer::onVolumeUpdate() {
364    SL_LOGD("GenericMediaPlayer::onVolumeUpdate()");
365    // use settings lock to read the volume settings
366    Mutex::Autolock _l(mSettingsLock);
367    if (mPlayer != 0) {
368        mPlayer->setVolume(mAndroidAudioLevels.mFinalVolume[0],
369                mAndroidAudioLevels.mFinalVolume[1]);
370    }
371}
372
373
374void GenericMediaPlayer::onAttachAuxEffect(const sp<AMessage> &msg) {
375    SL_LOGD("GenericMediaPlayer::onAttachAuxEffect()");
376    int32_t effectId = 0;
377    if (msg->findInt32(WHATPARAM_ATTACHAUXEFFECT, &effectId)) {
378        if (mPlayer != 0) {
379            status_t status;
380            status = mPlayer->attachAuxEffect(effectId);
381            // attachAuxEffect returns a status but we have no way to report it back to app
382            (void) status;
383        }
384    }
385}
386
387
388void GenericMediaPlayer::onSetAuxEffectSendLevel(const sp<AMessage> &msg) {
389    SL_LOGD("GenericMediaPlayer::onSetAuxEffectSendLevel()");
390    float level = 0.0f;
391    if (msg->findFloat(WHATPARAM_SETAUXEFFECTSENDLEVEL, &level)) {
392        if (mPlayer != 0) {
393            status_t status;
394            status = mPlayer->setAuxEffectSendLevel(level);
395            // setAuxEffectSendLevel returns a status but we have no way to report it back to app
396            (void) status;
397        }
398    }
399}
400
401
402void GenericMediaPlayer::onBufferingUpdate(const sp<AMessage> &msg) {
403    int32_t fillLevel = 0;
404    if (msg->findInt32(WHATPARAM_BUFFERING_UPDATE, &fillLevel)) {
405        SL_LOGD("GenericMediaPlayer::onBufferingUpdate(fillLevel=%d)", fillLevel);
406
407        Mutex::Autolock _l(mSettingsLock);
408        mCacheFill = fillLevel;
409        // handle cache fill update
410        if (mCacheFill - mLastNotifiedCacheFill >= mCacheFillNotifThreshold) {
411            notifyCacheFill();
412        }
413        // handle prefetch status update
414        //   compute how much time ahead of position is buffered
415        int durationMsec, positionMsec = -1;
416        if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)
417                && (OK == mPlayer->getDuration(&durationMsec))
418                        && (OK == mPlayer->getCurrentPosition(&positionMsec))) {
419            if ((-1 != durationMsec) && (-1 != positionMsec)) {
420                // evaluate prefetch status based on buffer time thresholds
421                int64_t bufferedDurationMsec = (durationMsec * fillLevel / 100) - positionMsec;
422                CacheStatus_t newCacheStatus = mCacheStatus;
423                if (bufferedDurationMsec > DURATION_CACHED_HIGH_MS) {
424                    newCacheStatus = kStatusHigh;
425                } else if (bufferedDurationMsec > DURATION_CACHED_MED_MS) {
426                    newCacheStatus = kStatusEnough;
427                } else if (bufferedDurationMsec > DURATION_CACHED_LOW_MS) {
428                    newCacheStatus = kStatusIntermediate;
429                } else if (bufferedDurationMsec == 0) {
430                    newCacheStatus = kStatusEmpty;
431                } else {
432                    newCacheStatus = kStatusLow;
433                }
434
435                if (newCacheStatus != mCacheStatus) {
436                    mCacheStatus = newCacheStatus;
437                    notifyStatus();
438                }
439            }
440        }
441    } else {
442        SL_LOGV("GenericMediaPlayer::onBufferingUpdate(fillLevel=unknown)");
443    }
444}
445
446
447//--------------------------------------------------
448/**
449 * called from GenericMediaPlayer::onPrepare after the MediaPlayer mPlayer is prepared successfully
450 * pre-conditions:
451 *  mPlayer != 0
452 *  mPlayer is prepared successfully
453 */
454void GenericMediaPlayer::afterMediaPlayerPreparedSuccessfully() {
455    SL_LOGV("GenericMediaPlayer::afterMediaPlayerPrepared()");
456    assert(mPlayer != 0);
457    assert(mStateFlags & kFlagPrepared);
458    // Mark this player as prepared successfully, so safe to directly call getCurrentPosition
459    {
460        Mutex::Autolock _l(mPreparedPlayerLock);
461        assert(mPreparedPlayer == 0);
462        mPreparedPlayer = mPlayer;
463    }
464    // retrieve channel count
465    int32_t channelCount;
466    Parcel *reply = new Parcel();
467    status_t status = mPlayer->getParameter(KEY_PARAMETER_AUDIO_CHANNEL_COUNT, reply);
468    if (status == NO_ERROR) {
469        channelCount = reply->readInt32();
470    } else {
471        // FIXME MPEG-2 TS doesn't yet implement this key, so default to stereo
472        channelCount = 2;
473    }
474    if (UNKNOWN_NUMCHANNELS != channelCount) {
475        // now that we know the channel count, re-calculate the volumes
476        notify(PLAYEREVENT_CHANNEL_COUNT, channelCount, true /*async*/);
477    } else {
478        LOGW("channel count is still unknown after prepare");
479    }
480    delete reply;
481    // retrieve duration
482    {
483        int msec = 0;
484        if (OK == mPlayer->getDuration(&msec)) {
485            Mutex::Autolock _l(mSettingsLock);
486            mDurationMsec = msec;
487        }
488    }
489    // now that we have a MediaPlayer, set the looping flag
490    if (mStateFlags & kFlagLooping) {
491        (void) mPlayer->setLooping(1);
492    }
493    // when the MediaPlayer mPlayer is prepared, there is "sufficient data" in the playback buffers
494    // if the data source was local, and the buffers are considered full so we need to notify that
495    bool isLocalSource = true;
496    if (kDataLocatorUri == mDataLocatorType) {
497        isLocalSource = !isDistantProtocol(mDataLocator.uriRef);
498    }
499    if (isLocalSource) {
500        SL_LOGD("media player prepared on local source");
501        {
502            Mutex::Autolock _l(mSettingsLock);
503            mCacheStatus = kStatusHigh;
504            mCacheFill = 1000;
505            notifyStatus();
506            notifyCacheFill();
507        }
508    } else {
509        SL_LOGD("media player prepared on non-local source");
510    }
511}
512
513
514//--------------------------------------------------
515// If player is prepared successfully, set output parameter to that reference, otherwise NULL
516void GenericMediaPlayer::getPreparedPlayer(sp<IMediaPlayer> &preparedPlayer)
517{
518    Mutex::Autolock _l(mPreparedPlayerLock);
519    preparedPlayer = mPreparedPlayer;
520}
521
522} // namespace android
523