http streaming缓冲buffer更新进度的处理流程

对于http streaming,更新buffer进度的处理在AwesomePlayer::onBufferingUpdate()函数中,首先判断mCachedSource != NULL,即是http://流媒体的情况下,通过getBitrate(&bitrate)函数取得bitrate,然后计算出buffer的进度,通过notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage)发消息,在MediaPlayer.cpp类对应MediaPlayer::notify函数接收到MEDIA_BUFFERING_UPDATE消息,然后在应用层通过注册MediaPlayer的setOnBufferingUpdateListener监听,接收到MEDIA_BUFFERING_UPDATE消息,即可以获得buffer更新的进度值percent。

1.AwesomePlayer::onBufferingUpdate()函数

计算buffer更新的percent

            if (getBitrate(&bitrate)) {
                LOGV("onBufferingUpdate: bitrate: %lld", bitrate);
                size_t lowWaterThreshold = (size_t) ((bitrate * kLowWaterMarkUs
                        * (1.0f + kThresholdPaddingFactor)) / 8000000ll);
                size_t highWaterThreshold = (size_t) ((bitrate
                        * kHighWaterMarkUs * (1.0f + kThresholdPaddingFactor))
                        / 8000000ll);
                mCachedSource->setThresholds(lowWaterThreshold,
                        highWaterThreshold);
                size_t cachedSize = mCachedSource->cachedSize();
                int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate;

                int percentage = 100.0 * (double)cachedDurationUs / mDurationUs;
                if (percentage > 100) {
                    percentage = 100;
                }

                notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage);
            } else {

其中8000000ll为1byte以us为单位的bit数,单位long long类型,即1byte = 1byte * 8bits/byte * 1000000us = 8000000ll

 2.AwesomePlayer::getBitrate函数

bool AwesomePlayer::getBitrate(int64_t *bitrate) {
    off64_t size;
    if (mDurationUs >= 0 && mCachedSource != NULL
            && mCachedSource->getSize(&size) == OK) {
        *bitrate = size * 8000000ll / mDurationUs;  // in bits/sec
        return true;
    }

    if (mBitrate >= 0) {
        *bitrate = mBitrate;
        return true;
    }

    *bitrate = 0;

    return false;
}

3. NuCachedSource2::getSize

调用其成员变量mSource的getSize函数取得当前更新的size大小,mSouce即为NuCachedSource2构造函数传递的参数,此参数为AwesomePlayer::finishSetDataSource_l()函数中创建mCachedSource对象时传递的类型为NuHTTPDataSource的参数mConnectingDataSource。

所以,mSource->getSize调用的是NuHTTPDataSource的getSize函数。

status_t NuCachedSource2::getSize(off64_t *size) {
    return mSource->getSize(size);
}

4.NuHTTPDataSource::getSize函数

此函数取得size值为mContentLength的值,取得mContentLength的值的处理在NuHTTPDataSource::connect函数,从http header中读取的,即从"Content-Length"或者"Content-Range"头域指定的值中计算出的。

NuCachedSource2和NuHTTPDataSource类以及getSize函数的关系图如下:

 

 5.mediaplayer.cpp的MediaPlayer::notify函数

void MediaPlayer::notify(int msg, int ext1, int ext2)
{
...
    case MEDIA_BUFFERING_UPDATE:
        LOGV("buffering %d", ext1);
        break;
...
    sp<MediaPlayerListener> listener = mListener;
    if (locked) mLock.unlock();

    // this prevents re-entrant calls into client code
    if ((listener != 0) && send) {
        Mutex::Autolock _l(mNotifyLock);
        LOGV("callback application");
        listener->notify(msg, ext1, ext2); // 发送消息给MediaPlayer.java,然后应用层就接收到
        LOGV("back from callback");
    }


6.MediaPlayer.java的setOnBufferingUpdateListener函数

在onBufferingUpdate函数中的参数percent即为buffer更新进度值0-100.

    /**
     * Interface definition of a callback to be invoked indicating buffering
     * status of a media resource being streamed over the network.
     */
    public interface OnBufferingUpdateListener
    {
        /**
         * Called to update status in buffering a media stream.
         *
         * @param mp      the MediaPlayer the update pertains to
         * @param percent the percentage (0-100) of the buffer
         *                that has been filled thus far
         */
        void onBufferingUpdate(MediaPlayer mp, int percent);
    }

    /**
     * Register a callback to be invoked when the status of a network
     * stream's buffer has changed.
     *
     * @param listener the callback that will be run.
     */
    public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener)
    {
        mOnBufferingUpdateListener = listener;
    }
发布了270 篇原创文章 · 获赞 274 · 访问量 377万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览