• MediaPlayer_Analyze-5-NuPlayer


    MediaPlayer Analyze - NuPlayer

    一 初始化

    framework层的MediaPlayer初始化未调用NuPlayer用关的函数。

    二 设置Source setDataSource

    frameworks\av\media\libmediaplayerservice\nuplayer\NuPlayerDriver.cpp

    status_t NuPlayerDriver::setDataSource(
            const sp<IMediaHTTPService> &httpService,
            const char *url,
            const KeyedVector<String8, String8> *headers) {
        ......
        // 此处的mPlayer是指NuPlayer实例
        mPlayer->setDataSourceAsync(httpService, url, headers);
        ......
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    frameworks\av\media\libmediaplayerservice\nuplayer\NuPlayer.cpp

    void NuPlayer::setDataSourceAsync(
            const sp<IMediaHTTPService> &httpService,
            const char *url,
            const KeyedVector<String8, String8> *headers) {
    
        sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
        size_t len = strlen(url);
    
        sp<AMessage> notify = new AMessage(kWhatSourceNotify, this);
    
        sp<Source> source;
        if (...) {
            ...... // 在线视频流的检测
        } else {
            ALOGV("setDataSourceAsync GenericSource %s", url);
    
            sp<GenericSource> genericSource = new GenericSource(notify, mUIDValid, mUID, mMediaClock);
    
            status_t err = genericSource->setDataSource(httpService, url, headers);
    
            if (err == OK) {
                source = genericSource;
            } else {
                ALOGE("Failed to set data source!");
            }
    
            // regardless of success/failure
            mDataSourceType = DATA_SOURCE_TYPE_GENERIC_URL;
        }
        msg->setObject("source", source);
        msg->post();
    }
    
    void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
        switch (msg->what()) {
            case kWhatSetDataSource:
            {
                ALOGV("kWhatSetDataSource");
    
                CHECK(mSource == NULL);
    
                status_t err = OK;
                sp<RefBase> obj;
                CHECK(msg->findObject("source", &obj));
                if (obj != NULL) {
                    Mutex::Autolock autoLock(mSourceLock);
                    mSource = static_cast<Source *>(obj.get());
                } else {
                    err = UNKNOWN_ERROR;
                }
    
                CHECK(mDriver != NULL);
                sp<NuPlayerDriver> driver = mDriver.promote();
                if (driver != NULL) {
                    // 向NuPlayerDriver返回SetDataSourc的结果
                    driver->notifySetDataSourceCompleted(err);
                }
                break;
            }
            ......
        }
    }
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    三 准备 prepare

    frameworks\av\media\libmediaplayerservice\nuplayer\NuPlayer.cpp

    void NuPlayer::prepareAsync() {
        (new AMessage(kWhatPrepare, this))->post();
    }
    
    void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
        switch (msg->what()) {
            ......
            case kWhatPrepare:
            {
                mSource->prepareAsync();
                break;
            }
            ......
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    frameworks\av\media\libmediaplayerservice\nuplayer\GenericSource.cpp

    void NuPlayer::GenericSource::prepareAsync() {
        Mutex::Autolock _l(mLock);
        ALOGV("prepareAsync: (looper: %d)", (mLooper != NULL));
    
        if (mLooper == NULL) {
            mLooper = new ALooper;
            mLooper->setName("generic");
            mLooper->start();
    
            mLooper->registerHandler(this);
        }
    
        sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this);
        msg->post();
    }
    
    void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
        Mutex::Autolock _l(mLock);
        switch (msg->what()) {
          case kWhatPrepareAsync:
          {
              onPrepareAsync();
              break;
          }
          ......
        }
    }
    
    void NuPlayer::GenericSource::onPrepareAsync() {
        mDisconnectLock.lock();
        ALOGV("onPrepareAsync: mDataSource: %d", (mDataSource != NULL));
    
        // delayed data source creation
        if (mDataSource == NULL) {
            // set to false first, if the extractor
            // comes back as secure, set it to true then.
            mIsSecure = false;
    
            if (!mUri.empty()) {
                const char* uri = mUri.c_str();
                String8 contentType;
    
                if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) {
                    sp<DataSource> httpSource;
                    mDisconnectLock.unlock();
                    httpSource = PlayerServiceDataSourceFactory::getInstance()
                            ->CreateMediaHTTP(mHTTPService);
                    if (httpSource == NULL) {
                        ALOGE("Failed to create http source!");
                        notifyPreparedAndCleanup(UNKNOWN_ERROR);
                        return;
                    }
                    mDisconnectLock.lock();
    
                    if (!mDisconnected) {
                        mHttpSource = httpSource;
                    }
                }
    
                mLock.unlock();
                mDisconnectLock.unlock();
                // This might take long time if connection has some issue.
                sp<DataSource> dataSource = PlayerServiceDataSourceFactory::getInstance()
                        ->CreateFromURI(mHTTPService, uri, &mUriHeaders, &contentType,
                                static_cast<HTTPBase *>(mHttpSource.get()));
                mDisconnectLock.lock();
                mLock.lock();
                if (!mDisconnected) {
                    mDataSource = dataSource;
                }
            } else {
                if (property_get_bool("media.stagefright.extractremote", true) &&
                        !PlayerServiceFileSource::requiresDrm(
                                mFd.get(), mOffset, mLength, nullptr /* mime */)) {
                    sp<IBinder> binder =
                            defaultServiceManager()->getService(String16("media.extractor"));
                    if (binder != nullptr) {
                        ALOGD("FileSource remote");
                        sp<IMediaExtractorService> mediaExService(
                                interface_cast<IMediaExtractorService>(binder));
                        sp<IDataSource> source;
                        mediaExService->makeIDataSource(base::unique_fd(dup(mFd.get())), mOffset, mLength, &source);
                        ALOGV("IDataSource(FileSource): %p %d %lld %lld",
                                source.get(), mFd.get(), (long long)mOffset, (long long)mLength);
                        if (source.get() != nullptr) {
                            mDataSource = CreateDataSourceFromIDataSource(source);
                        } else {
                            ALOGW("extractor service cannot make data source");
                        }
                    } else {
                        ALOGW("extractor service not running");
                    }
                }
                if (mDataSource == nullptr) {
                    ALOGD("FileSource local");
                    mDataSource = new PlayerServiceFileSource(dup(mFd.get()), mOffset, mLength);
                }
            }
    
            if (mDataSource == NULL) {
                ALOGE("Failed to create data source!");
                mDisconnectLock.unlock();
                notifyPreparedAndCleanup(UNKNOWN_ERROR);
                return;
            }
        }
    
        if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
            mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
        }
    
        mDisconnectLock.unlock();
    
        // For cached streaming cases, we need to wait for enough
        // buffering before reporting prepared.
        mIsStreaming = (mCachedSource != NULL);
    
        // init extractor from data source
        status_t err = initFromDataSource();
    
        if (err != OK) {
            ALOGE("Failed to init from data source!");
            notifyPreparedAndCleanup(err);
            return;
        }
    
        if (mVideoTrack.mSource != NULL) {
            sp<MetaData> meta = getFormatMeta_l(false /* audio */);
            sp<AMessage> msg = new AMessage;
            err = convertMetaDataToMessage(meta, &msg);
            if(err != OK) {
                notifyPreparedAndCleanup(err);
                return;
            }
            notifyVideoSizeChanged(msg);
        }
    
        notifyFlagsChanged(
                // FLAG_SECURE will be known if/when prepareDrm is called by the app
                // FLAG_PROTECTED will be known if/when prepareDrm is called by the app
                FLAG_CAN_PAUSE |
                FLAG_CAN_SEEK_BACKWARD |
                FLAG_CAN_SEEK_FORWARD |
                FLAG_CAN_SEEK);
    
        finishPrepareAsync();
    
        ALOGV("onPrepareAsync: Done");
    }
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149

    四 开始/恢复播放 start/resume

    NuPlayer::onStart中调用postScanSources()postScanSources()中发送kWhatScanSources消息,在消息处理中分别初始化mVideoDecodermAudioDecoder

    frameworks\av\media\libmediaplayerservice\nuplayer\NuPlayer.cpp
    
    void NuPlayer::onStart(int64_t startPositionUs, MediaPlayerSeekMode mode) {
        ......
        postScanSources();
    }
    
    void NuPlayer::postScanSources() {
        ......
        sp<AMessage> msg = new AMessage(kWhatScanSources, this);
        msg->setInt32("generation", mScanSourcesGeneration);
        msg->post();
    
        mScanSourcesPending = true;
    }
    
    void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
        switch (msg->what()) {
            ......
            case kWhatScanSources:
            {
                ......
                // initialize video before audio because successful initialization of
                // video may change deep buffer mode of audio.
                if (mSurface != NULL) {
                    if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
                        rescan = true;
                    }
                }
    
                // Don't try to re-open audio sink if there's an existing decoder.
                if (mAudioSink != NULL && mAudioDecoder == NULL) {
                    if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
                        rescan = true;
                    }
                }
    
                ......
                break;
            }
            ......
    }
    
    status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
        // The audio decoder could be cleared by tear down. If still in shut down
        // process, no need to create a new audio decoder.
        if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
            return OK;
        }
        ......
    
        Mutex::Autolock autoLock(mDecoderLock);
    
        if (audio) {
            sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
            ++mAudioDecoderGeneration;
            notify->setInt32("generation", mAudioDecoderGeneration);
    
            if (checkAudioModeChange) {
                determineAudioModeChange(format);
            }
            if (mOffloadAudio) {
                mSource->setOffloadAudio(true /* offload */);
    
                const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
                format->setInt32("has-video", hasVideo);
                *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
                ALOGV("instantiateDecoder audio DecoderPassThrough  hasVideo: %d", hasVideo);
            } else {
                mSource->setOffloadAudio(false /* offload */);
                // new NuPlayer::Decoder
                *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer);
                ALOGV("instantiateDecoder audio Decoder");
            }
            mAudioDecoderError = false;
        } else {
            sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
            ++mVideoDecoderGeneration;
            notify->setInt32("generation", mVideoDecoderGeneration);
            // new NuPlayer::Decoder
            *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder);
            mVideoDecoderError = false;
    
            // enable FRC if high-quality AV sync is requested, even if not
            // directly queuing to display, as this will even improve textureview
            // playback.
            {
                if (property_get_bool("persist.sys.media.avsync", false)) {
                    format->setInt32("auto-frc", 1);
                }
            }
        }
        (*decoder)->init(); // 调用 NuPlayer::DecoderBase::init
        ......
        /* 调用NuPlayer::DecoderBase::configure,其内部发送一个kWhatConfigure消息,在消息处理中调用NuPlayer::Decoder::onConfigure */
        (*decoder)->configure(format); // 调用NuPlayer::Decoder::onConfigure
        ......
    }
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    NuPlayer::Decoder::onConfigure(const sp &format)中,创建了native层的MediaCodec的实例对象。

    frameworks\av\media\libmediaplayerservice\nuplayer\NuPlayerDecoder.cpp
    
    
    void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
        ......
        mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid, mUid);
        int32_t secure = 0;
        if (format->findInt32("secure", &secure) && secure != 0) {
            if (mCodec != NULL) {
                mCodec->getName(&mComponentName);
                mComponentName.append(".secure");
                mCodec->release();
                ALOGI("[%s] creating", mComponentName.c_str());
                mCodec = MediaCodec::CreateByComponentName(mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid, mUid);
            }
        }
        ......
        err = mCodec->configure(format, mSurface, crypto, 0 /* flags */);
        ......
        err = mCodec->start();
        ......
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    五 暂停 pause

    TODO

    六 停止 stop

    TODO

    七 重置 reset

    TODO

    八 释放 release

    TODO

  • 相关阅读:
    竞争与冒险 毛刺
    PCL点云滤波处理D435i深度图用于octomap
    优维产品最佳实践:主机合规性检查
    9月13日扒面经
    Python解析MDX词典数据并保存到Excel
    黑*头条_第5章_文章发布&粉丝管理成形记
    【C】语言文件操作(一)
    linux安装部署redis&配置远程连接
    【ArcGIS pro】-使用arcpy一次保存多个布局
    【标定】张正友:A Flexible New Technique for Camera Calibration
  • 原文地址:https://blog.csdn.net/tujidi1csd/article/details/126867895