android_GenericMediaPlayer.cpp 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Glenn Kasten's avatar
Glenn Kasten committed
17
//#define USE_LOG SLAndroidLogLevel_Verbose
18 19

#include "sles_allinclusive.h"
20 21
#include "android_GenericMediaPlayer.h"

22 23 24
#include <media/IMediaPlayerService.h>
#include <surfaceflinger/ISurfaceComposer.h>
#include <surfaceflinger/SurfaceComposerClient.h>
25
#include <media/stagefright/foundation/ADebug.h>
26

27 28 29 30
// default delay in Us used when reposting an event when the player is not ready to accept
// the command yet. This is for instance used when seeking on a MediaPlayer that's still preparing
#define DEFAULT_COMMAND_DELAY_FOR_REPOST_US (100*1000) // 100ms

31 32
// table of prefixes for known distant protocols; these are immediately dispatched to mediaserver
static const char* const kDistantProtocolPrefix[] = { "http://", "https://", "rtsp://"};
33 34
#define NB_DISTANT_PROTOCOLS (sizeof(kDistantProtocolPrefix)/sizeof(kDistantProtocolPrefix[0]))

35 36 37 38 39 40 41 42 43 44 45 46 47
// is the specified URI a known distant protocol?
bool isDistantProtocol(const char *uri)
{
    for (unsigned int i = 0; i < NB_DISTANT_PROTOCOLS; i++) {
        if (!strncasecmp(uri, kDistantProtocolPrefix[i], strlen(kDistantProtocolPrefix[i]))) {
            return true;
        }
    }
    return false;
}

namespace android {

48
//--------------------------------------------------------------------------------------------------
49 50
MediaPlayerNotificationClient::MediaPlayerNotificationClient(GenericMediaPlayer* gmp) :
    mGenericMediaPlayer(gmp),
51
    mPlayerPrepared(PREPARE_NOT_STARTED)
52
{
Glenn Kasten's avatar
Glenn Kasten committed
53
    SL_LOGV("MediaPlayerNotificationClient::MediaPlayerNotificationClient()");
54 55 56
}

MediaPlayerNotificationClient::~MediaPlayerNotificationClient() {
Glenn Kasten's avatar
Glenn Kasten committed
57 58
    SL_LOGV("MediaPlayerNotificationClient::~MediaPlayerNotificationClient()");
}
59

Glenn Kasten's avatar
Glenn Kasten committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// Map a MEDIA_* enum to a string
static const char *media_to_string(int msg)
{
    switch (msg) {
#define _(x) case MEDIA_##x: return "MEDIA_" #x;
      _(PREPARED)
      _(SET_VIDEO_SIZE)
      _(SEEK_COMPLETE)
      _(PLAYBACK_COMPLETE)
      _(BUFFERING_UPDATE)
      _(ERROR)
      _(NOP)
      _(TIMED_TEXT)
      _(INFO)
#undef _
    default:
        return NULL;
    }
78 79 80 81
}

//--------------------------------------------------
// IMediaPlayerClient implementation
82
void MediaPlayerNotificationClient::notify(int msg, int ext1, int ext2, const Parcel *obj) {
Glenn Kasten's avatar
Glenn Kasten committed
83 84
    SL_LOGV("MediaPlayerNotificationClient::notify(msg=%s (%d), ext1=%d, ext2=%d)",
            media_to_string(msg), msg, ext1, ext2);
85

86 87 88 89 90 91
    sp<GenericMediaPlayer> genericMediaPlayer(mGenericMediaPlayer.promote());
    if (genericMediaPlayer == NULL) {
        SL_LOGW("MediaPlayerNotificationClient::notify after GenericMediaPlayer destroyed");
        return;
    }

92 93
    switch (msg) {
      case MEDIA_PREPARED:
94 95
        {
        Mutex::Autolock _l(mLock);
96 97 98 99 100 101
        if (PREPARE_IN_PROGRESS == mPlayerPrepared) {
            mPlayerPrepared = PREPARE_COMPLETED_SUCCESSFULLY;
            mPlayerPreparedCondition.signal();
        } else {
            SL_LOGE("Unexpected MEDIA_PREPARED");
        }
102
        }
103 104 105
        break;

      case MEDIA_SET_VIDEO_SIZE:
106 107
        // only send video size updates if the player was flagged as having video, to avoid
        // sending video size updates of (0,0)
108 109 110 111 112 113
        // We're running on a different thread than genericMediaPlayer's ALooper thread,
        // so it would normally be racy to access fields within genericMediaPlayer.
        // But in this case mHasVideo is const, so it is safe to access.
        // Or alternatively, we could notify unconditionally and let it decide whether to handle.
        if (genericMediaPlayer->mHasVideo) {
            genericMediaPlayer->notify(PLAYEREVENT_VIDEO_SIZE_UPDATE,
114 115 116 117 118
                    (int32_t)ext1, (int32_t)ext2, true /*async*/);
        }
        break;

      case MEDIA_SEEK_COMPLETE:
119
        genericMediaPlayer->seekComplete();
120 121 122
        break;

      case MEDIA_PLAYBACK_COMPLETE:
123
        genericMediaPlayer->notify(PLAYEREVENT_ENDOFSTREAM, 1, true /*async*/);
124 125 126 127 128
        break;

      case MEDIA_BUFFERING_UPDATE:
        // values received from Android framework for buffer fill level use percent,
        //   while SL/XA use permille, so does GenericPlayer
129
        genericMediaPlayer->bufferingUpdate(ext1 * 10 /*fillLevelPerMille*/);
130 131
        break;

Glenn Kasten's avatar
Glenn Kasten committed
132
      case MEDIA_ERROR:
133 134
        {
        Mutex::Autolock _l(mLock);
135 136 137 138
        if (PREPARE_IN_PROGRESS == mPlayerPrepared) {
            mPlayerPrepared = PREPARE_COMPLETED_UNSUCCESSFULLY;
            mPlayerPreparedCondition.signal();
        } else {
139 140
            // inform client of errors after preparation
            genericMediaPlayer->notify(PLAYEREVENT_ERRORAFTERPREPARE, ext1, true /*async*/);
141
        }
142
        }
143 144
        break;

Glenn Kasten's avatar
Glenn Kasten committed
145 146 147 148 149
      case MEDIA_NOP:
      case MEDIA_TIMED_TEXT:
      case MEDIA_INFO:
        break;

150
      default: { }
151
    }
Glenn Kasten's avatar
Glenn Kasten committed
152

153 154 155
}

//--------------------------------------------------
156 157 158 159 160 161 162 163 164
void MediaPlayerNotificationClient::beforePrepare()
{
    Mutex::Autolock _l(mLock);
    assert(mPlayerPrepared == PREPARE_NOT_STARTED);
    mPlayerPrepared = PREPARE_IN_PROGRESS;
}

//--------------------------------------------------
bool MediaPlayerNotificationClient::blockUntilPlayerPrepared() {
165
    Mutex::Autolock _l(mLock);
166 167
    assert(mPlayerPrepared != PREPARE_NOT_STARTED);
    while (mPlayerPrepared == PREPARE_IN_PROGRESS) {
168 169
        mPlayerPreparedCondition.wait(mLock);
    }
170 171 172
    assert(mPlayerPrepared == PREPARE_COMPLETED_SUCCESSFULLY ||
            mPlayerPrepared == PREPARE_COMPLETED_UNSUCCESSFULLY);
    return mPlayerPrepared == PREPARE_COMPLETED_SUCCESSFULLY;
173 174 175 176 177 178
}

//--------------------------------------------------------------------------------------------------
GenericMediaPlayer::GenericMediaPlayer(const AudioPlayback_Parameters* params, bool hasVideo) :
    GenericPlayer(params),
    mHasVideo(hasVideo),
179
    mSeekTimeMsec(0),
180
    mVideoSurface(0),
181
    mVideoSurfaceTexture(0),
182
    mPlayer(0),
183
    mPlayerClient(new MediaPlayerNotificationClient(this))
184
{
185
    SL_LOGD("GenericMediaPlayer::GenericMediaPlayer()");
186 187 188 189

}

GenericMediaPlayer::~GenericMediaPlayer() {
190
    SL_LOGD("GenericMediaPlayer::~GenericMediaPlayer()");
191 192 193
}

void GenericMediaPlayer::preDestroy() {
194 195 196 197 198 199
    // FIXME can't access mPlayer from outside the looper (no mutex!) so using mPreparedPlayer
    sp<IMediaPlayer> player;
    getPreparedPlayer(player);
    if (player != NULL) {
        player->stop();
        // causes CHECK failure in Nuplayer, but commented out in the subclass preDestroy
200 201
        // randomly causes a NPE in StagefrightPlayer, heap corruption, or app hang
        //player->setDataSource(NULL);
202 203 204 205 206 207 208 209 210
        player->setVideoSurface(NULL);
        player->disconnect();
        // release all references to the IMediaPlayer
        // FIXME illegal if not on looper
        //mPlayer.clear();
        {
            Mutex::Autolock _l(mPreparedPlayerLock);
            mPreparedPlayer.clear();
        }
211
    }
212 213
    GenericPlayer::preDestroy();
}
214

215 216 217 218 219
//--------------------------------------------------
// overridden from GenericPlayer
// pre-condition:
//   msec != NULL
// post-condition
220
//   *msec ==
221 222 223 224
//                  ANDROID_UNKNOWN_TIME if position is unknown at time of query,
//               or the current MediaPlayer position
void GenericMediaPlayer::getPositionMsec(int* msec) {
    SL_LOGD("GenericMediaPlayer::getPositionMsec()");
225
    sp<IMediaPlayer> player;
226
    getPreparedPlayer(player);
227 228 229
    // To avoid deadlock, directly call the MediaPlayer object
    if (player == 0 || player->getCurrentPosition(msec) != NO_ERROR) {
        *msec = ANDROID_UNKNOWN_TIME;
230
    }
231 232 233
}

//--------------------------------------------------
234
void GenericMediaPlayer::setVideoSurface(const sp<Surface> &surface) {
Glenn Kasten's avatar
Glenn Kasten committed
235
    SL_LOGV("GenericMediaPlayer::setVideoSurface()");
236 237 238 239 240 241 242
    // FIXME bug - race condition, should do in looper
    if (mVideoSurface.get() == surface.get()) {
        return;
    }
    if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) {
        mPlayer->setVideoSurface(surface);
    }
243
    mVideoSurface = surface;
244
    mVideoSurfaceTexture = NULL;
245 246 247
}

void GenericMediaPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
Glenn Kasten's avatar
Glenn Kasten committed
248
    SL_LOGV("GenericMediaPlayer::setVideoSurfaceTexture()");
249 250 251 252 253 254 255
    // FIXME bug - race condition, should do in looper
    if (mVideoSurfaceTexture.get() == surfaceTexture.get()) {
        return;
    }
    if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)) {
        mPlayer->setVideoSurfaceTexture(surfaceTexture);
    }
256
    mVideoSurfaceTexture = surfaceTexture;
257
    mVideoSurface = NULL;
258 259
}

260

261 262
//--------------------------------------------------
// Event handlers
263

264
// blocks until mPlayer is prepared
265
void GenericMediaPlayer::onPrepare() {
266
    SL_LOGD("GenericMediaPlayer::onPrepare()");
267 268
    // Attempt to prepare at most once, and only if there is a MediaPlayer
    if (!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully)) && (mPlayer != 0)) {
269 270 271 272 273 274
        if (mHasVideo) {
            if (mVideoSurface != 0) {
                mPlayer->setVideoSurface(mVideoSurface);
            } else if (mVideoSurfaceTexture != 0) {
                mPlayer->setVideoSurfaceTexture(mVideoSurfaceTexture);
            }
275 276
        }
        mPlayer->setAudioStreamType(mPlaybackParams.streamType);
277
        mPlayerClient->beforePrepare();
278
        mPlayer->prepareAsync();
279 280 281 282 283 284
        if (mPlayerClient->blockUntilPlayerPrepared()) {
            mStateFlags |= kFlagPrepared;
            afterMediaPlayerPreparedSuccessfully();
        } else {
            mStateFlags |= kFlagPreparedUnsuccessfully;
        }
285
    }
286
    GenericPlayer::onPrepare();
287
    SL_LOGD("GenericMediaPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
288 289
}

290

291
void GenericMediaPlayer::onPlay() {
292
    SL_LOGD("GenericMediaPlayer::onPlay()");
293
    if (((mStateFlags & (kFlagPrepared | kFlagPlaying)) == kFlagPrepared) && (mPlayer != 0)) {
294 295
        mPlayer->start();
    }
296
    GenericPlayer::onPlay();
297 298
}

299

300
void GenericMediaPlayer::onPause() {
301
    SL_LOGD("GenericMediaPlayer::onPause()");
302
    if (!(~mStateFlags & (kFlagPrepared | kFlagPlaying)) && (mPlayer != 0)) {
303 304
        mPlayer->pause();
    }
305
    GenericPlayer::onPause();
306 307
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

void GenericMediaPlayer::onSeekComplete() {
    SL_LOGV("GenericMediaPlayer::onSeekComplete()");
    // did we initiate the seek?
    if (!(mStateFlags & kFlagSeeking)) {
        // no, are we looping?
        if (mStateFlags & kFlagLooping) {
            // yes, per OpenSL ES 1.0.1 and 1.1 do NOT report it to client
            // notify(PLAYEREVENT_ENDOFSTREAM, 1, true /*async*/);
        // no, well that's surprising, but it's probably just a benign race condition
        } else {
            SL_LOGW("Unexpected seek complete event ignored");
        }
    }
    GenericPlayer::onSeekComplete();
}


326 327 328
/**
 * pre-condition: WHATPARAM_SEEK_SEEKTIME_MS parameter value >= 0
 */
329 330
void GenericMediaPlayer::onSeek(const sp<AMessage> &msg) {
    SL_LOGV("GenericMediaPlayer::onSeek");
331 332 333 334 335
    int64_t timeMsec = ANDROID_UNKNOWN_TIME;
    if (!msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec)) {
        // invalid command, drop it
        return;
    }
336 337 338
    if ((mStateFlags & kFlagSeeking) && (timeMsec == mSeekTimeMsec) &&
            (timeMsec != ANDROID_UNKNOWN_TIME)) {
        // already seeking to the same non-unknown time, cancel this command
339
        return;
340 341
    } else if (mStateFlags & kFlagPreparedUnsuccessfully) {
        // discard seeks after unsuccessful prepare
342
    } else if (!(mStateFlags & kFlagPrepared)) {
343 344 345
        // we are not ready to accept a seek command at this time, retry later
        msg->post(DEFAULT_COMMAND_DELAY_FOR_REPOST_US);
    } else {
Glenn Kasten's avatar
Glenn Kasten committed
346
        if (mPlayer != 0) {
347 348
            mStateFlags |= kFlagSeeking;
            mSeekTimeMsec = (int32_t)timeMsec;
349 350 351 352 353 354
            // seek to unknown time is used by StreamPlayer after discontinuity
            if (timeMsec == ANDROID_UNKNOWN_TIME) {
                // FIXME simulate a MEDIA_SEEK_COMPLETE event in 250 ms;
                // this is a terrible hack to make up for mediaserver not sending one
                (new AMessage(kWhatSeekComplete, id()))->post(250000);
            } else if (OK != mPlayer->seekTo(timeMsec)) {
355 356
                mStateFlags &= ~kFlagSeeking;
                mSeekTimeMsec = ANDROID_UNKNOWN_TIME;
357 358 359 360 361 362 363 364 365 366
            }
        }
    }
}


void GenericMediaPlayer::onLoop(const sp<AMessage> &msg) {
    SL_LOGV("GenericMediaPlayer::onLoop");
    int32_t loop = 0;
    if (msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop)) {
367 368 369 370 371 372 373 374
        if (loop) {
            mStateFlags |= kFlagLooping;
        } else {
            mStateFlags &= ~kFlagLooping;
        }
        // if we have a MediaPlayer then tell it now, otherwise we'll tell it after it's created
        if (mPlayer != 0) {
            (void) mPlayer->setLooping(loop);
375 376 377 378 379
        }
    }
}


380
void GenericMediaPlayer::onVolumeUpdate() {
381
    SL_LOGD("GenericMediaPlayer::onVolumeUpdate()");
382 383
    // use settings lock to read the volume settings
    Mutex::Autolock _l(mSettingsLock);
384
    if (mPlayer != 0) {
385 386
        mPlayer->setVolume(mAndroidAudioLevels.mFinalVolume[0],
                mAndroidAudioLevels.mFinalVolume[1]);
387
    }
388 389 390
}


391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
void GenericMediaPlayer::onAttachAuxEffect(const sp<AMessage> &msg) {
    SL_LOGD("GenericMediaPlayer::onAttachAuxEffect()");
    int32_t effectId = 0;
    if (msg->findInt32(WHATPARAM_ATTACHAUXEFFECT, &effectId)) {
        if (mPlayer != 0) {
            status_t status;
            status = mPlayer->attachAuxEffect(effectId);
            // attachAuxEffect returns a status but we have no way to report it back to app
            (void) status;
        }
    }
}


void GenericMediaPlayer::onSetAuxEffectSendLevel(const sp<AMessage> &msg) {
    SL_LOGD("GenericMediaPlayer::onSetAuxEffectSendLevel()");
    float level = 0.0f;
    if (msg->findFloat(WHATPARAM_SETAUXEFFECTSENDLEVEL, &level)) {
        if (mPlayer != 0) {
            status_t status;
            status = mPlayer->setAuxEffectSendLevel(level);
            // setAuxEffectSendLevel returns a status but we have no way to report it back to app
            (void) status;
        }
    }
}


419 420 421 422
void GenericMediaPlayer::onBufferingUpdate(const sp<AMessage> &msg) {
    int32_t fillLevel = 0;
    if (msg->findInt32(WHATPARAM_BUFFERING_UPDATE, &fillLevel)) {
        SL_LOGD("GenericMediaPlayer::onBufferingUpdate(fillLevel=%d)", fillLevel);
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
        Mutex::Autolock _l(mSettingsLock);
        mCacheFill = fillLevel;
        // handle cache fill update
        if (mCacheFill - mLastNotifiedCacheFill >= mCacheFillNotifThreshold) {
            notifyCacheFill();
        }
        // handle prefetch status update
        //   compute how much time ahead of position is buffered
        int durationMsec, positionMsec = -1;
        if ((mStateFlags & kFlagPrepared) && (mPlayer != 0)
                && (OK == mPlayer->getDuration(&durationMsec))
                        && (OK == mPlayer->getCurrentPosition(&positionMsec))) {
            if ((-1 != durationMsec) && (-1 != positionMsec)) {
                // evaluate prefetch status based on buffer time thresholds
                int64_t bufferedDurationMsec = (durationMsec * fillLevel / 100) - positionMsec;
                CacheStatus_t newCacheStatus = mCacheStatus;
                if (bufferedDurationMsec > DURATION_CACHED_HIGH_MS) {
                    newCacheStatus = kStatusHigh;
                } else if (bufferedDurationMsec > DURATION_CACHED_MED_MS) {
                    newCacheStatus = kStatusEnough;
                } else if (bufferedDurationMsec > DURATION_CACHED_LOW_MS) {
                    newCacheStatus = kStatusIntermediate;
                } else if (bufferedDurationMsec == 0) {
                    newCacheStatus = kStatusEmpty;
                } else {
                    newCacheStatus = kStatusLow;
                }

                if (newCacheStatus != mCacheStatus) {
                    mCacheStatus = newCacheStatus;
                    notifyStatus();
                }
            }
        }
Glenn Kasten's avatar
Glenn Kasten committed
458 459
    } else {
        SL_LOGV("GenericMediaPlayer::onBufferingUpdate(fillLevel=unknown)");
460 461 462 463 464 465
    }
}


//--------------------------------------------------
/**
466
 * called from GenericMediaPlayer::onPrepare after the MediaPlayer mPlayer is prepared successfully
467 468
 * pre-conditions:
 *  mPlayer != 0
469
 *  mPlayer is prepared successfully
470
 */
471 472
void GenericMediaPlayer::afterMediaPlayerPreparedSuccessfully() {
    SL_LOGV("GenericMediaPlayer::afterMediaPlayerPrepared()");
473
    assert(mPlayer != 0);
474
    assert(mStateFlags & kFlagPrepared);
475 476
    // Mark this player as prepared successfully, so safe to directly call getCurrentPosition
    {
477 478 479
        Mutex::Autolock _l(mPreparedPlayerLock);
        assert(mPreparedPlayer == 0);
        mPreparedPlayer = mPlayer;
480
    }
481
    // retrieve channel count
482
    int32_t channelCount;
483 484 485
    Parcel *reply = new Parcel();
    status_t status = mPlayer->getParameter(KEY_PARAMETER_AUDIO_CHANNEL_COUNT, reply);
    if (status == NO_ERROR) {
486
        channelCount = reply->readInt32();
Glenn Kasten's avatar
Glenn Kasten committed
487 488
    } else {
        // FIXME MPEG-2 TS doesn't yet implement this key, so default to stereo
489
        channelCount = 2;
Glenn Kasten's avatar
Glenn Kasten committed
490
    }
491
    if (UNKNOWN_NUMCHANNELS != channelCount) {
Glenn Kasten's avatar
Glenn Kasten committed
492
        // now that we know the channel count, re-calculate the volumes
493
        notify(PLAYEREVENT_CHANNEL_COUNT, channelCount, true /*async*/);
Glenn Kasten's avatar
Glenn Kasten committed
494 495
    } else {
        LOGW("channel count is still unknown after prepare");
496 497 498
    }
    delete reply;
    // retrieve duration
499 500 501 502 503 504 505
    {
        Mutex::Autolock _l(mSettingsLock);
        int msec = 0;
        if (OK == mPlayer->getDuration(&msec)) {
            mDurationMsec = msec;
        }
    }
506 507 508 509
    // now that we have a MediaPlayer, set the looping flag
    if (mStateFlags & kFlagLooping) {
        (void) mPlayer->setLooping(1);
    }
510 511 512 513
    // when the MediaPlayer mPlayer is prepared, there is "sufficient data" in the playback buffers
    // if the data source was local, and the buffers are considered full so we need to notify that
    bool isLocalSource = true;
    if (kDataLocatorUri == mDataLocatorType) {
514
        isLocalSource = !isDistantProtocol(mDataLocator.uriRef);
515 516 517 518 519 520 521 522 523 524 525 526 527
    }
    if (isLocalSource) {
        SL_LOGD("media player prepared on local source");
        {
            Mutex::Autolock _l(mSettingsLock);
            mCacheStatus = kStatusHigh;
            mCacheFill = 1000;
            notifyStatus();
            notifyCacheFill();
        }
    } else {
        SL_LOGD("media player prepared on non-local source");
    }
528 529
}

530 531 532

//--------------------------------------------------
// If player is prepared successfully, set output parameter to that reference, otherwise NULL
533
void GenericMediaPlayer::getPreparedPlayer(sp<IMediaPlayer> &preparedPlayer)
534
{
535 536
    Mutex::Autolock _l(mPreparedPlayerLock);
    preparedPlayer = mPreparedPlayer;
537 538
}

539
} // namespace android