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; }
发布了0 篇原创文章 · 获赞 1 · 访问量 1万+
展开阅读全文

使用URL如何播放视频?

01-04

我使用媒体播放器,试图从youtube上下载视频和音频流。 使用 url如何实现呢? 我使用的例子是: http://www.youtube.com/watch?v=SgGhtjKWLOE&feature=feedrec 但是得到的 LogCat Error: 01-03 22:22:30.140: INFO/AwesomePlayer(85): setDataSource_l('http://www.youtube.com/watch?v=I3jv0IF9n6A') 01-03 22:22:30.140: INFO/NuHTTPDataSource(85): connect to www.youtube.com:80/watch?v=I3jv0IF9n6A @0 01-03 22:22:30.250: INFO/NuHTTPDataSource(85): connect to m.youtube.com:80/watch?desktop_uri=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DI3jv0IF9n6A&v=I3jv0IF9n6A&gl=US @0 09-04 22:22:30.410: INFO/NuHTTPDataSource(85): connect to m.youtube.com:80/#/watch?desktop_uri=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DI3jv0IF9n6A&v=I3jv0IF9n6A&gl=US @0 01-03 22:22:30.570: INFO/NuHTTPDataSource(85): Chunked transfer encoding applied. 01-03 22:22:30.570: WARN/NuHTTPDataSource(85): Server did not give us the content length! 01-03 22:22:31.000: INFO/NuCachedSource2(85): ERROR_END_OF_STREAM 01-03 22:22:31.630: ERROR/MediaPlayer(8404): error (1, -2147483648) 01-03 22:22:31.630: WARN/System.err(8404): java.io.IOException: Prepare failed.: status=0x1 01-03 22:22:31.630: WARN/System.err(8404): at android.media.MediaPlayer.prepare(Native Method) 01-03 22:22:31.630: WARN/System.err(8404): at com.fttech.example.youtube.example.AccessibleYouTube.watchVideo(AccessibleYouTube.java:139) 01-03 22:22:31.630: WARN/System.err(8404): at java.lang.reflect.Method.invokeNative(Native Method) 01-03 22:22:31.630: WARN/System.err(8404): at java.lang.reflect.Method.invoke(Method.java:491) 01-03 22:22:31.630: WARN/System.err(8404): at android.view.View$1.onClick(View.java:2678) 01-03 22:22:31.630: WARN/System.err(8404): at android.view.View.performClick(View.java:3110) 01-03 22:22:31.630: WARN/System.err(8404): at android.view.View$PerformClick.run(View.java:11928) 01-03 22:22:31.630: WARN/System.err(8404): at android.os.Handler.handleCallback(Handler.java:587) 01-03 22:22:31.630: WARN/System.err(8404): at android.os.Handler.dispatchMessage(Handler.java:92) 问答

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

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

分享到微信朋友圈

×

扫一扫,手机浏览