现在的位置: 首页 > 综合 > 正文

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

2013年08月15日 ⁄ 综合 ⁄ 共 3880字 ⁄ 字号 评论关闭

对于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

  1. if (getBitrate(&bitrate)) {  
  2.     LOGV("onBufferingUpdate: bitrate: %lld", bitrate);  
  3.     size_t lowWaterThreshold = (size_t) ((bitrate * kLowWaterMarkUs  
  4.             * (1.0f + kThresholdPaddingFactor)) / 8000000ll);  
  5.     size_t highWaterThreshold = (size_t) ((bitrate  
  6.             * kHighWaterMarkUs * (1.0f + kThresholdPaddingFactor))  
  7.             / 8000000ll);  
  8.     mCachedSource->setThresholds(lowWaterThreshold,  
  9.             highWaterThreshold);  
  10.     size_t cachedSize = mCachedSource->cachedSize();  
  11.     int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate;  
  12.   
  13.     int percentage = 100.0 * (double)cachedDurationUs / mDurationUs;  
  14.     if (percentage > 100) {  
  15.         percentage = 100;  
  16.     }  
  17.   
  18.     notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage);  
  19. else {  

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

 2.AwesomePlayer::getBitrate函数

  1. bool AwesomePlayer::getBitrate(int64_t *bitrate) {  
  2.     off64_t size;  
  3.     if (mDurationUs >= 0 && mCachedSource != NULL  
  4.             && mCachedSource->getSize(&size) == OK) {  
  5.         *bitrate = size * 8000000ll / mDurationUs;  // in bits/sec  
  6.         return true;  
  7.     }  
  8.   
  9.     if (mBitrate >= 0) {  
  10.         *bitrate = mBitrate;  
  11.         return true;  
  12.     }  
  13.   
  14.     *bitrate = 0;  
  15.   
  16.     return false;  
  17. }  

3. NuCachedSource2::getSize

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

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

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

4.NuHTTPDataSource::getSize函数

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

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

 

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

  1. void MediaPlayer::notify(int msg, int ext1, int ext2)  
  2. {  
  3. ...  
  4.     case MEDIA_BUFFERING_UPDATE:  
  5.         LOGV("buffering %d", ext1);  
  6.         break;  
  7. ...  
  8.     sp<MediaPlayerListener> listener = mListener;  
  9.     if (locked) mLock.unlock();  
  10.   
  11.     // this prevents re-entrant calls into client code  
  12.     if ((listener != 0) && send) {  
  13.         Mutex::Autolock _l(mNotifyLock);  
  14.         LOGV("callback application");  
  15.         listener->notify(msg, ext1, ext2); // 发送消息给MediaPlayer.java,然后应用层就接收到  
  16.         LOGV("back from callback");  
  17.     }  

6.MediaPlayer.java的setOnBufferingUpdateListener函数

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

  1. /** 
  2.  * Interface definition of a callback to be invoked indicating buffering 
  3.  * status of a media resource being streamed over the network. 
  4.  */  
  5. public interface OnBufferingUpdateListener  
  6. {  
  7.     /** 
  8.      * Called to update status in buffering a media stream. 
  9.      * 
  10.      * @param mp      the MediaPlayer the update pertains to 
  11.      * @param percent the percentage (0-100) of the buffer 
  12.      *                that has been filled thus far 
  13.      */  
  14.     void onBufferingUpdate(MediaPlayer mp, int percent);  
  15. }  
  16.   
  17. /** 
  18.  * Register a callback to be invoked when the status of a network 
  19.  * stream's buffer has changed. 
  20.  * 
  21.  * @param listener the callback that will be run. 
  22.  */  
  23. public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener)  
  24. {  
  25.     mOnBufferingUpdateListener = listener;  

抱歉!评论已关闭.