• MediaPlayer_Analyze-3-native


    MediaPlayer Analyze - Framework Native层

    一 初始化 new MediaPlayer

    1.1 jni接口

        sp<MediaPlayer> mp = new MediaPlayer(); // new C++ MediaPlayer()
    
    • 1

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    MediaPlayer::MediaPlayer()
    {
        ALOGV("constructor");
        mListener = NULL;
        mCookie = NULL;
        mStreamType = AUDIO_STREAM_MUSIC;
        mAudioAttributesParcel = NULL;
        mCurrentPosition = -1;
        mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
        mSeekPosition = -1;
        mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
        mCurrentState = MEDIA_PLAYER_IDLE;
        mPrepareSync = false;
        mPrepareStatus = NO_ERROR;
        mLoop = false;
        mLeftVolume = mRightVolume = 1.0;
        mVideoWidth = mVideoHeight = 0;
        mLockThreadId = 0;
        mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
        AudioSystem::acquireAudioSessionId(mAudioSessionId, (pid_t)-1, (uid_t)-1); // always in client.
        mSendLevel = 0;
        mRetransmitEndpointValid = false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    二 设置setDataSource

    2.1 jni接口

        // 调用native层接口
        status_t opStatus =
            mp->setDataSource(
                    httpService,
                    pathStr,
                    headersVector.size() > 0? &headersVector : NULL);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    status_t MediaPlayer::setDataSource(
            const sp<IMediaHTTPService> &httpService,
            const char *url, const KeyedVector<String8, String8> *headers)
    {
        ALOGV("setDataSource(%s)", url);
        status_t err = BAD_VALUE;
        if (url != NULL) {
            // 获取MediaPlayerService
            const sp<IMediaPlayerService> service(getMediaPlayerService());
            if (service != 0) {
                // 通过MediaPlayerService调用create
                sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
                if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
                    (NO_ERROR != player->setDataSource(httpService, url, headers))) {
                    player.clear();
                }
                // 关联新的IMediaPlayer对象player
                err = attachNewPlayer(player);
            }
        }
        return err;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    三 准备 prepare

    1.1 jni接口

        mp->setVideoSurfaceTexture(st);
    
        process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
    
    • 1
    • 2
    • 3

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    status_t MediaPlayer::setVideoSurfaceTexture(
            const sp<IGraphicBufferProducer>& bufferProducer)
    {
        ALOGV("setVideoSurfaceTexture");
        Mutex::Autolock _l(mLock);
        if (mPlayer == 0) return NO_INIT;
        return mPlayer->setVideoSurfaceTexture(bufferProducer);
    }
    
    // must call with lock held
    status_t MediaPlayer::prepareAsync_l()
    {
        if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
            if (mAudioAttributesParcel != NULL) {
                mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
            } else {
                mPlayer->setAudioStreamType(mStreamType);
            }
            mCurrentState = MEDIA_PLAYER_PREPARING;
            return mPlayer->prepareAsync();
        }
        ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
        return INVALID_OPERATION;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    四 开始/恢复播放 start/resume

    1.1 jni接口

        process_media_player_call( env, thiz, mp->start(), NULL, NULL );
    
    • 1

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    status_t MediaPlayer::start()
    {
        ALOGV("start");
    
        status_t ret = NO_ERROR;
        Mutex::Autolock _l(mLock);
    
        mLockThreadId = getThreadId();
    
        if (mCurrentState & MEDIA_PLAYER_STARTED) {
            ret = NO_ERROR;
        } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
                        MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
            mPlayer->setLooping(mLoop);
            mPlayer->setVolume(mLeftVolume, mRightVolume);
            mPlayer->setAuxEffectSendLevel(mSendLevel);
            mCurrentState = MEDIA_PLAYER_STARTED;
            ret = mPlayer->start();
            if (ret != NO_ERROR) {
                mCurrentState = MEDIA_PLAYER_STATE_ERROR;
            } else {
                if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
                    ALOGV("playback completed immediately following start()");
                }
            }
        } else {
            ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
            ret = INVALID_OPERATION;
        }
    
        mLockThreadId = 0;
    
        return ret;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    五 暂停播放 pause

    1.1 jni接口

        process_media_player_call( env, thiz, mp->pause(), NULL, NULL );
    
    • 1

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    status_t MediaPlayer::pause()
    {
        ALOGV("pause");
        Mutex::Autolock _l(mLock);
        if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
            return NO_ERROR;
        if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
            status_t ret = mPlayer->pause();
            if (ret != NO_ERROR) {
                mCurrentState = MEDIA_PLAYER_STATE_ERROR;
            } else {
                mCurrentState = MEDIA_PLAYER_PAUSED;
            }
            return ret;
        }
        ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
        return INVALID_OPERATION;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    六 停止播放 stop

    1.1 jni接口

        process_media_player_call( env, thiz, mp->stop(), NULL, NULL );
    
    • 1

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    status_t MediaPlayer::stop()
    {
        ALOGV("stop");
        Mutex::Autolock _l(mLock);
        if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
        if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
                        MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
            status_t ret = mPlayer->stop();
            if (ret != NO_ERROR) {
                mCurrentState = MEDIA_PLAYER_STATE_ERROR;
            } else {
                mCurrentState = MEDIA_PLAYER_STOPPED;
            }
            return ret;
        }
        ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
        return INVALID_OPERATION;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    七 重置 reset

    1.1 jni接口

        process_media_player_call( env, thiz, mp->reset(), NULL, NULL );
    
    • 1

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    status_t MediaPlayer::reset_l()
    {
        mLoop = false;
        if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
        mPrepareSync = false;
        if (mPlayer != 0) {
            status_t ret = mPlayer->reset();
            if (ret != NO_ERROR) {
                ALOGE("reset() failed with return code (%d)", ret);
                mCurrentState = MEDIA_PLAYER_STATE_ERROR;
            } else {
                mPlayer->disconnect();
                mCurrentState = MEDIA_PLAYER_IDLE;
            }
            // setDataSource has to be called again to create a
            // new mediaplayer.
            mPlayer = 0;
            return ret;
        }
        // 重置状态
        clear_l();
        return NO_ERROR;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    八 释放 release

    1.1 jni接口

        if (mp != NULL) {
            // this prevents native callbacks after the object is released
            mp->setListener(0);
            mp->disconnect();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.2 native接口

    frameworks\av\media\libmedia\mediaplayer.cpp

    void MediaPlayer::disconnect()
    {
        ALOGV("disconnect");
        sp<IMediaPlayer> p;
        {
            Mutex::Autolock _l(mLock);
            p = mPlayer;
            mPlayer.clear();
        }
    
        if (p != 0) {
            p->disconnect();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    数学建模十大算法01-蒙特卡洛算法(Monte Carlo)
    文本标注工具doccano 中上传dataset无法成功
    五十行代码教你写一个简单的内存池(二级指针的应用)
    代码随想录算法训练营第23期day12| 239. 滑动窗口最大值 、347. 前K个高频元素
    vue课后习题及答案
    【C语言】归并排序和计次排序
    ROS 2 Humble Hawksbill 之 f1tenth gym
    数据结构 - 栈和队列
    Connect-The-Dots_2
    分享一款低损耗 高效率高性能 低 VCE(sat) 晶体管 NSS60600MZ4T1G
  • 原文地址:https://blog.csdn.net/tujidi1csd/article/details/126867789