REF//http://www.mcuol.com/Tech/265/34522.htm


Android 系統整體架構:
我們先看一下多媒體框架在整個Android系統所處的位置

120705_001

從框架圖可以看出Media Framework處於Libraries這一層,這層的Library不是用Java實現,一般是C/C++實現,它們通過Java的JNI方式調用。

多媒體架構:

基於第三方PacketVideo 公司的OpenCORE platform來實現

支持所有通用的音頻,視頻,靜態圖象格式

CODEC(編解碼器)使用OpenMAX 1L interface 接口進行擴展,可以方便得支持hardware / software codec plug-ins

支持的格式包括:MPEG4、H.264、MP3、AAC、AMR、JPG、PNG、GIF等。

Open Core多媒體框架有一套通用可擴展的接口針對第三方的多媒體遍解碼器,輸入,輸出設備等等 。

多媒體文件的播放,下載,包括3GPP, MPEG-4,AAC and MP3 containers

流媒體文件的下載,實時播放,包括:3GPP, HTTP and RTSP/RTP

動態視頻和靜態圖象的編碼,解碼,例如:MPEG-4, H.263 and AVC (H.264), JPEG

語音編碼格式: AMR-NB and AMR-WB

音樂編碼格式: MP3, AAC, AAC+

視頻和圖象格式: 3GPP, MPEG-4 and JPEG

視頻會議:基於H324-M standard


圖中用黃線圈出的是Media Framework

120705_002

Open Core介紹:
Open Core是Android 多媒體框架的核心,所有Android平台的音視頻採集,播放的操作都是通過它來實現。它也被稱為PV(Packet Video), Packet Video是一家專門提供多媒體解決方案的公司。

通過Open Core程序員可以方便快速的開發出想要的多媒體應用程序,例如:音視頻的採集,回放,視頻會議,實時的流媒體播放等等應用。


Open Core 框架

120705_003

代碼結構:

Open Core的代碼在Android代碼的External/Opencore目錄中。這個目錄是OpenCore的根目錄,其中包含的子目錄如下所示:

android:這裡面是一個上層的庫,它實現了一個為Android使用的音視頻採集,播放的接口,和DRM數字版權管理的接口實現。
baselibs:包含數據結構和線程安全等內容的底層庫
codecs_v2:音視頻的編解碼器,基於OpenMAX實現
engines:核心部分,多媒體引擎的實現
extern_libs_v2:包含了khronos的OpenMAX的頭文件
fileformats:文件格式的解析(parser)工具
nodes:提供一些PVMF的NODE,主要是編解碼和文件解析方面的。
oscl:操作系統兼容庫
pvmi: 輸入輸出控制的抽象接口
protocols:主要是與網絡相關的RTSP、RTP、HTTP等協議的相關內容
pvcommon:pvcommon庫文件的Android.mk文件,沒有源文件。
pvplayer:pvplayer庫文件的Android.mk文件,沒有源文件。
pvauthor:pvauthor庫文件的Android.mk文件,沒有源文件。
tools_v2:編譯工具以及一些可註冊的模組。
Open Core 上層代碼結構

在實際開發中我們並不會過多的研究Open Core的實現,Android提供了上層的Media API給開發人員使用,MediaPlayer和MediaRecorder

Android Media APIs

The Android platform is capable of playing both audio and video media. It is also capable of playing media contained in the resources for an application, or a standalone file in the filesystem, or even streaming media over a data connection. Playback is achieved through the android.media.MediaPlayer class.

The Android platform can also record audio. Video recording capabilities are coming in the future. This is achieved through the android.media.MediaRecorder class.

Media Player

提供的基本接口如下:

Public Methods

static MediaPlayer create(Context context, Uri uri)

Convenience method to create a MediaPlayer for a given Uri.

int getCurrentPosition()

Gets the current playback position.

int getDuration()

Gets the duration of the file.

int getVideoHeight()

Returns the height of the video.

int getVideoWidth()

Returns the width of the video.

boolean isPlaying()

Checks whether the MediaPlayer is playing.

void pause()

Pauses playback.

void prepare()

Prepares the player for playback, synchronously.

void prepareAsync()

Prepares the player for playback, asynchronously.

void release()

Releases resources associated with this MediaPlayer object.

void reset()

Resets the MediaPlayer to its uninitialized state.

void seekTo(int msec)

Seeks to specified time position.

void setAudioStreamType(int streamtype)

Sets the audio stream type for this MediaPlayer.

void setDataSource(String path)

Sets the data source (file-path or http/rtsp URL) to use.

void setDisplay(SurfaceHolder sh)

Sets the SurfaceHolder to use for displaying the video portion of the media.

void setVolume(float leftVolume, float rightVolume)

Sets the volume on this player.

void start()

Starts or resumes playback.

void stop()

Stops playback after playback has been stopped or paused.


我們可以看出MediaPlayer類提供了一個多媒體播放器的基本操作,播放,暫停,停止,設置音量等等。

簡單的例子:

Playing a File

MediaPlayer mp = new MediaPlayer();

mp.setDataSource(PATH_TO_FILE);

mp.prepare();

mp.start();

Playing a Raw Resource

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);

mp.start();



Media Recorder

提供的基本接口如下:

Public Method:

void prepare()

Prepares the recorder to begin capturing and encoding data.

void release()

Releases resources associated with this MediaRecorder object.

void reset()

Restarts the MediaRecorder to its idle state.

void setAudioEncoder(int audio_encoder)

Sets the audio encoder to be used for recording.

void setAudioSource(int audio_source)

Sets the audio source to be used for recording.

void setOutputFile(String path)

Sets the path of the output file to be produced.

void setOutputFormat(int output_format)

Sets the format of the output file produced during recording.

void setPreviewDisplay(Surface sv)

Sets a Surface to show a preview of recorded media (video).

void start()

Begins capturing and encoding data to the file specified with setOutputFile().

void stop()

Stops recording.



簡單的例子:

Example:

MediaRecorder recorder = new MediaRecorder();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

recorder.setOutputFile(PATH_NAME);

recorder.prepare();

recorder.start(); // Recording is now started ... recorder.stop();

recorder.reset(); // You can reuse the object by going back to setAudioSource() step

recorder.release(); // Now the object cannot be reused


整體的結構如下圖所示:

120705_004

MediaPlayer JNI 代碼位置 /frameworks/base/media/jni

MediaPlayer (Native) 代碼位置 /frameworks/base/media/libmedia

MediaPlayerService (Server) 代碼位置 /frameworks/base/media/libmediaplayerservice

MediaPlayerService Host Process 代碼位置 /frameworks/base/media/mediaserver/main_mediaserver.cpp

PVPlayer 代碼位置 /external/opencore/android/

實際調用過程如下圖所示:

120705_005
arrow
arrow
    全站熱搜

    BB 發表在 痞客邦 留言(0) 人氣()