在android系統(tǒng)中凡桥,MediaPlayer提供播放音視頻的功能技羔,本文打算先簡要分析一下MediaPlayer框架。
如圖一所示泌类,java framework提供了MediaPlayer類供上層應(yīng)用使用癞谒,java層的MediaPlayer對(duì)象對(duì)應(yīng)一個(gè)native層的MediaPlayer對(duì)象,同時(shí)對(duì)應(yīng)一個(gè)mediaserver進(jìn)程中的Client對(duì)象刃榨,native層的MediaPlayer對(duì)象通過IMediaPlayer binder接口調(diào)用到mediaserver進(jìn)程中的Client對(duì)象弹砚。MediaPlayer的核心功能都是在mediaserver進(jìn)程中完成的,在應(yīng)用程序這一端枢希,只是提供接口桌吃,邏輯比較簡單,就不細(xì)說了苞轿,IMediaPlayer接口定義如下茅诱,主要就是提供設(shè)置數(shù)據(jù)源逗物、設(shè)置輸出Surface和啟停等控制接口。
class IMediaPlayer: public IInterface
{
public:
DECLARE_META_INTERFACE(MediaPlayer);
virtual void disconnect() = 0;
virtual status_t setDataSource(const char *url,
const KeyedVector<String8, String8>* headers) = 0;
virtual status_t setDataSource(int fd, int64_t offset, int64_t length) = 0;
virtual status_t setDataSource(const sp<IStreamSource>& source) = 0;
virtual status_t setVideoSurfaceTexture(
const sp<IGraphicBufferProducer>& bufferProducer) = 0;
virtual status_t prepareAsync() = 0;
virtual status_t start() = 0;
virtual status_t stop() = 0;
virtual status_t pause() = 0;
virtual status_t isPlaying(bool* state) = 0;
virtual status_t seekTo(int msec) = 0;
virtual status_t getCurrentPosition(int* msec) = 0;
virtual status_t getDuration(int* msec) = 0;
virtual status_t reset() = 0;
virtual status_t setAudioStreamType(audio_stream_type_t type) = 0;
virtual status_t setLooping(int loop) = 0;
virtual status_t setVolume(float leftVolume, float rightVolume) = 0;
virtual status_t setAuxEffectSendLevel(float level) = 0;
virtual status_t attachAuxEffect(int effectId) = 0;
virtual status_t setParameter(int key, const Parcel& request) = 0;
virtual status_t getParameter(int key, Parcel* reply) = 0;
virtual status_t setRetransmitEndpoint(const struct sockaddr_in* endpoint) = 0;
virtual status_t getRetransmitEndpoint(struct sockaddr_in* endpoint) = 0;
virtual status_t setNextPlayer(const sp<IMediaPlayer>& next) = 0;
// Invoke a generic method on the player by using opaque parcels
// for the request and reply.
// @param request Parcel that must start with the media player
// interface token.
// @param[out] reply Parcel to hold the reply data. Cannot be null.
// @return OK if the invocation was made successfully.
virtual status_t invoke(const Parcel& request, Parcel *reply) = 0;
// Set a new metadata filter.
// @param filter A set of allow and drop rules serialized in a Parcel.
// @return OK if the invocation was made successfully.
virtual status_t setMetadataFilter(const Parcel& filter) = 0;
// Retrieve a set of metadata.
// @param update_only Include only the metadata that have changed
// since the last invocation of getMetadata.
// The set is built using the unfiltered
// notifications the native player sent to the
// MediaPlayerService during that period of
// time. If false, all the metadatas are considered.
// @param apply_filter If true, once the metadata set has been built based
// on the value update_only, the current filter is
// applied.
// @param[out] metadata On exit contains a set (possibly empty) of metadata.
// Valid only if the call returned OK.
// @return OK if the invocation was made successfully.
virtual status_t getMetadata(bool update_only,
bool apply_filter,
Parcel *metadata) = 0;
};
在mediaserver進(jìn)程這一端瑟俭,Client對(duì)象會(huì)創(chuàng)建一個(gè)實(shí)現(xiàn)了MediaPlayerInterface接口的播放器對(duì)象翎卓,這個(gè)對(duì)象在不同的android版本上有所不同,比如
2.2版本定義如下播放器類型
enum player_type {
PV_PLAYER = 1,
SONIVOX_PLAYER = 2,
VORBIS_PLAYER = 3,
STAGEFRIGHT_PLAYER = 4,
TEST_PLAYER = 5,
APE_PLAYER = 6,
FLAC_PLAYER = 7
};
具體使用那種播放器類型摆寄,是由數(shù)據(jù)源類型決定的失暴,映射關(guān)系如下所示
extmap FILE_EXTS [] = {
{".mid", SONIVOX_PLAYER},
{".midi", SONIVOX_PLAYER},
{".smf", SONIVOX_PLAYER},
{".xmf", SONIVOX_PLAYER},
{".imy", SONIVOX_PLAYER},
{".rtttl", SONIVOX_PLAYER},
{".rtx", SONIVOX_PLAYER},
{".ota", SONIVOX_PLAYER},
{".ogg", VORBIS_PLAYER},
{".oga", VORBIS_PLAYER},
{".ape", APE_PLAYER},
{".flac", FLAC_PLAYER},
};
每種類型對(duì)應(yīng)的實(shí)現(xiàn)如下所示:
switch (playerType) {
#ifndef NO_OPENCORE
case PV_PLAYER:
LOGV(" create PVPlayer");
p = new PVPlayer();
break;
#endif
case SONIVOX_PLAYER:
LOGV(" create MidiFile");
p = new MidiFile();
break;
case VORBIS_PLAYER:
LOGV(" create VorbisPlayer");
p = new VorbisPlayer();
break;
case APE_PLAYER:
LOGV(" create ApePlayer");
p = new ApePlayer();
break;
case FLAC_PLAYER:
LOGV(" create FlacPlayer");
p = new FlacPlayer();
break;
#if BUILD_WITH_FULL_STAGEFRIGHT
case STAGEFRIGHT_PLAYER:
LOGV(" create StagefrightPlayer");
p = new StagefrightPlayer;
break;
#endif
case TEST_PLAYER:
LOGV("Create Test Player stub");
p = new TestPlayerStub();
break;
}
4.4版本定義如下播放器類型:
enum player_type {
PV_PLAYER = 1,
SONIVOX_PLAYER = 2,
STAGEFRIGHT_PLAYER = 3,
NU_PLAYER = 4,
TEST_PLAYER = 5
}
具體使用那種播放器類型,也是由數(shù)據(jù)源類型決定的微饥,不過不是采用簡單的映射關(guān)系逗扒,而是采用一種數(shù)據(jù)源匹配評(píng)分機(jī)制,如下所示:
#define GET_PLAYER_TYPE_IMPL(a...) \
Mutex::Autolock lock_(&sLock); \
\
player_type ret = STAGEFRIGHT_PLAYER; \
float bestScore = 0.0; \
\
for (size_t i = 0; i < sFactoryMap.size(); ++i) { \
\
IFactory* v = sFactoryMap.valueAt(i); \
float thisScore; \
CHECK(v != NULL); \
thisScore = v->scoreFactory(a, bestScore); \
if (thisScore > bestScore) { \
ret = sFactoryMap.keyAt(i); \
bestScore = thisScore; \
} \
} \
\
if (0.0 == bestScore) { \
ret = getDefaultPlayerType(); \
} \
\
return ret;
每種類型提供了一個(gè)MediaPlayerFactory對(duì)象欠橘,實(shí)現(xiàn)了scoreFactory方法和createPlayer方法矩肩,每種類型對(duì)應(yīng)的MediaPlayerFactory如下所示:
registerFactory_l(new StagefrightPlayerFactory(), STAGEFRIGHT_PLAYER);
registerFactory_l(new NuPlayerFactory(), NU_PLAYER);
registerFactory_l(new SonivoxPlayerFactory(), SONIVOX_PLAYER);
registerFactory_l(new TestPlayerFactory(), TEST_PLAYER);
以StagefrightPlayerFactory為例,createPlayer創(chuàng)建的是一個(gè)StagefrightPlayer對(duì)象肃续。
virtual sp<MediaPlayerBase> createPlayer() {
ALOGV(" create StagefrightPlayer");
return new StagefrightPlayer();
}
6.0版本定義如下播放器類型蛮拔,雖然保留了數(shù)據(jù)源匹配評(píng)分機(jī)制,但是基本上都是使用NU_PLAYER了痹升。
enum player_type {
NU_PLAYER = 4,
// Test players are available only in the 'test' and 'eng' builds.
// The shared library with the test player is passed passed as an
// argument to the 'test:' url in the setDataSource call.
TEST_PLAYER = 5,
DASH_PLAYER = 6,
};
有些芯片廠商也可能提供自己的實(shí)現(xiàn)建炫,比如我之前接觸過的海思平臺(tái)的方案,就自己搞了一個(gè)HiMediaPlayerManage疼蛾。
如圖一所示肛跌,實(shí)現(xiàn)了MediaPlayerInterface接口的播放器對(duì)象其實(shí)只是個(gè)簡單的代理對(duì)象,核心功能都是委托給具體的播放器對(duì)象來實(shí)現(xiàn)察郁,比如StagefrightPlayer就是委托AwesomePlayer來處理衍慎,NuPlayerDriver就是委托NuPlayer來處理。因此要分析實(shí)際的播放器流程皮钠,主要就是分析AwesomePlayer和NuPlayer業(yè)務(wù)流程稳捆。主要類的關(guān)系如下所示,可以看出麦轰,就是一個(gè)比較典型的代理模式乔夯。
扯了這么多,總結(jié)起來就是MediaPlayer核心功能是由mediaserver進(jìn)程中具體的播放器引擎完成的款侵,而播放器引擎在不同的android版本上有很大的變化末荐,從早期的OpenCore到StagefrightPlayer再到最新的NuPlayer,中間也長時(shí)間存在共存的情況新锈,比如播放本地文件采用StagefrightPlayer甲脏,而播放流媒體則采用NuPlayer,不過最后都統(tǒng)一為NuPlayer了。
好了今天先寫到這了块请,比較簡單娜氏,沒啥實(shí)質(zhì)性內(nèi)容,后續(xù)再抽時(shí)間寫一下AwesomePlayer和NuPlayer相關(guān)內(nèi)容墩新。