Android Media Framework(2): Video Playback流程

重点研究libstagefright
AwesomePlayer.cpp

MediaExtractor.cpp

OpenMAX
在这里插入图片描述
在这里插入图片描述
在Java播放视频,我们一般会这样写:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(PATH_TO_FILE); ...... (1)
mp.prepare(); ........................ (2)(3)
mp.start(); .......................... (4)

在Stagefright中有对应的处理:
** AwesomePlayer.cpp**中

指定文件路径给mUri

status_t AwesomePlayer::setDataSource(const char* uri, ...)
{  
     return setDataSource_l(uri, ...);
}
status_t AwesomePlayer::setDataSource_l(const char* uri, ...)
{ 
    mUri = uri;
}

prepare()启动mQueue作为event handler

status_t AwesomePlayer::prepare(){  
    return prepare_l();
}
status_t AwesomePlayer::prepare_l()
{  
    prepareAsync_l();  
    while (mFlags & PREPARING) 
   {     
       mPreparedCondition.wait(mLock);  
   }
}
status_t AwesomePlayer::prepareAsync_l()
{  
    mQueue.start();  mFlags |= PREPARING;  
    mAsyncPrepareEvent = new AwesomeEvent(this, &AwesomePlayer::onPrepareAsyncEvent);  
    mQueue.postEvent(mAsyncPrepareEvent);
}

onPrepareAsyncEvent被触发

void AwesomePlayer::onPrepareAsyncEvent()
{  
    finishSetDataSource_l();  
    initVideoDecoder(); ...... 
    initAudioDecoder();
}
status_t AwesomePlayer::finishSetDataSource_l()
{  
    dataSource = DataSource::CreateFromURI(mUri.string(), ...);  
    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 
    return setDataSource_l(extractor); 
}

由数据源DataSource生成MediaExtractor(在MediaExtractor.cpp中)

解析mUri所指定的文件,并根据其header来选择对应的extractor

sp MediaExtractor::Create(const sp &source, …)
{

MediaExtractor *ret = NULL;
if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)
|| !strcasecmp(mime, “audio/mp4”)) {
ret = new MPEG4Extractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {
ret = new MP3Extractor(source, meta);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)
|| !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
ret = new AMRExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) {
ret = new FLACExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) {
ret = new WAVExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)) {
ret = new OggExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)) {
ret = new MatroskaExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
ret = new MPEG2TSExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WVM)) {
// Return now. WVExtractor should not have the DrmFlag set in the block below.
return new WVMExtractor(source);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC_ADTS)) {
ret = new AACExtractor(source, meta);
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2PS)) {
ret = new MPEG2PSExtractor(source);
}
}

使用extractor对文件做A/V的分离(mVideoTrack/mAudioTrack)

status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor)
{
     for (size_t i = 0; i < extractor->countTracks(); ++i)
    {
        sp<MetaData> meta = extractor->getTrackMetaData(i);
        CHECK(meta->findCString(kKeyMIMEType, &mime));
        if (!haveVideo && !strncasecmp(mime, "video/", 6))
        {
            setVideoSource(extractor->getTrack(i));
            haveVideo = true;
        }
        else if (!haveAudio && !strncasecmp(mime, "audio/", 6))
        {
            setAudioSource(extractor->getTrack(i));
            haveAudio = true;
        }
    }
}
void AwesomePlayer::setVideoSource(sp<MediaSource> source)
{  
    mVideoTrack = source;
}

根据mVideoTrack中的编码类型來选择video decoder (mVideoSource)

status_t AwesomePlayer::initVideoDecoder()
{  
    mVideoSource = OMXCodec::Create(mClient.interface(), 
                                                    mVideoTrack->getFormat(), false,  mVideoTrack);}

将mVideoEvent放入mQueue中,开始解码播放,并交由mVideoRenderer來画出

status_t AwesomePlayer::play()
{  
    return play_l();
}
status_t AwesomePlayer::play_l()
{  
    postVideoEvent_l();
}
void AwesomePlayer::postVideoEvent_l(int64_t delayUs)
{  
    mQueue.postEventWithDelay(mVideoEvent, delayUs);
}
void AwesomePlayer::onVideoEvent()
{  
    mVideoSource->read(&mVideoBuffer, &options);  
    [CheckTimestamp]  mVideoRenderer->render(mVideoBuffer);  postVideoEvent_l();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值