Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ef498052 authored by Jaekyun Seok's avatar Jaekyun Seok
Browse files

Handle failure of creating MediaPlayer properly

MediaPlayer::setDataSource() doesn't fail with a non-existing file. So
we should check the result of MediaPlayer::prepare() as well.

Note that MediaPlayer::disconnect() is called instead of deleting
MediaPlayer directly because the latter causes crash of CameraService.
MediaPlayer::disconnect() is called even in
CameraService::releaseSound().

Bug: 74376839
Test: tested an app using CameraService
Change-Id: Idf68837a55d14ad47dbbedff0256269bd90f9dfd
parent 1c57a0e6
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -1999,14 +1999,17 @@ status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* re
// A reference count is kept to determine when we will actually release the
// media players.

MediaPlayer* CameraService::newMediaPlayer(const char *file) {
    MediaPlayer* mp = new MediaPlayer();
    if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) {
    sp<MediaPlayer> mp = new MediaPlayer();
    status_t error;
    if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) {
        mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
        mp->prepare();
    } else {
        error = mp->prepare();
    }
    if (error != NO_ERROR) {
        ALOGE("Failed to load CameraService sounds: %s", file);
        delete mp;
        mp->disconnect();
        mp.clear();
        return nullptr;
    }
    return mp;
+1 −1
Original line number Diff line number Diff line
@@ -736,7 +736,7 @@ private:
    std::vector<std::string> mNormalDeviceIds;

    // sounds
    MediaPlayer*        newMediaPlayer(const char *file);
    sp<MediaPlayer>     newMediaPlayer(const char *file);

    Mutex               mSoundLock;
    sp<MediaPlayer>     mSoundPlayer[NUM_SOUNDS];