Session類
Session對(duì)象主要用于存儲(chǔ)某個(gè)會(huì)話對(duì)象所需的屬性以及配置信息埃叭,并且使用同步或者異步的方法對(duì)Session內(nèi)部的流媒體對(duì)象進(jìn)行操作和控制。
private String mOrigin;
private String mDestination;
private int mTimeToLive = 64;
private long mTimestamp;
private AudioStream mAudioStream = null;
private VideoStream mVideoStream = null;
private Callback mCallback;
private Handler mMainHandler;
private static CountDownLatch sSignal;
private static Handler sHandler;
mMainHandler:在主線程執(zhí)行方法時(shí)用到的handler赶诊,其中所有的callback回調(diào)方法都是在主線程中回調(diào)的。
sHandler:在子線程執(zhí)行方法用到的handler园骆,大部分對(duì)Stream的操作都是在子線程中執(zhí)行舔痪。
mAudioStream和mVideoStream:音頻流媒體和視頻流媒體對(duì)象,Session對(duì)其進(jìn)行操作锌唾。
static {
// Creates the Thread that will be used when asynchronous methods of a Session are called
sSignal = new CountDownLatch(1);
new HandlerThread("net.majorkernelpanic.streaming.Session"){
@Override
protected void onLooperPrepared() {
sHandler = new Handler();
sSignal.countDown();
}
}.start();
}
在靜態(tài)塊中創(chuàng)建一個(gè)子線程锄码,作為各個(gè)異步方法的執(zhí)行線程。
/**
* Creates a streaming session that can be customized by adding tracks.
*/
public Session() {
long uptime = System.currentTimeMillis();
mMainHandler = new Handler(Looper.getMainLooper());
mTimestamp = (uptime/1000)<<32 & (((uptime-((uptime/1000)*1000))>>32)/1000); // NTP timestamp
mOrigin = "127.0.0.1";
// Me make sure that we won't send Runnables to a non existing thread
try {
sSignal.await();
} catch (InterruptedException e) {}
}
構(gòu)造函數(shù)中sSignal.await();是等待上面的子線程Looper創(chuàng)建完畢后,再往下執(zhí)行主線程巍耗。
/** Deletes all existing tracks & release associated resources. */
public void release() {
removeAudioTrack();
removeVideoTrack();
sHandler.getLooper().quit();
}
在最后release時(shí)秋麸,釋放音頻流和視頻流,并且需要將sHandler的looper釋放掉炬太,這個(gè)方法是在主線程中執(zhí)行灸蟆。
對(duì)于Stream進(jìn)行操作的一系列方法,由于篇幅原因不在這里展開列舉亲族,其實(shí)大多數(shù)的操作動(dòng)作都是由sHandler.post發(fā)出消息炒考,在子線程中執(zhí)行的;而動(dòng)作執(zhí)行完成的回調(diào)都是由mMainHandler.post發(fā)出消息霎迫,在主線程中執(zhí)行回調(diào)斋枢。
SessionBuilder類
SessionBuilder類就是用來(lái)構(gòu)建Session對(duì)象。SessionBuilder采用了單例模式知给,需要使用getInstance()方法來(lái)獲取到實(shí)例對(duì)象瓤帚。
// Default configuration
private VideoQuality mVideoQuality = VideoQuality.DEFAULT_VIDEO_QUALITY;
private AudioQuality mAudioQuality = AudioQuality.DEFAULT_AUDIO_QUALITY;
private Context mContext;
private int mVideoEncoder = VIDEO_H263;
private int mAudioEncoder = AUDIO_AMRNB;
private int mCamera = CameraInfo.CAMERA_FACING_BACK;
private int mTimeToLive = 64;
private int mOrientation = 0;
private boolean mFlash = false;
private SurfaceView mSurfaceView = null;
private String mOrigin = null;
private String mDestination = null;
private Session.Callback mCallback = null;
構(gòu)建Session時(shí)默認(rèn)的配置參數(shù),可以通過(guò)set方法設(shè)置參數(shù)值涩赢。
/**
* Creates a new {@link Session}.
* @return The new Session
* @throws IOException
*/
public Session build() {
Session session;
session = new Session();
session.setOrigin(mOrigin);
session.setDestination(mDestination);
session.setTimeToLive(mTimeToLive);
session.setCallback(mCallback);
switch (mAudioEncoder) {
case AUDIO_AAC:
AACStream stream = new AACStream();
session.addAudioTrack(stream);
if (mContext!=null)
stream.setPreferences(PreferenceManager.getDefaultSharedPreferences(mContext));
break;
case AUDIO_AMRNB:
session.addAudioTrack(new AMRNBStream());
break;
}
switch (mVideoEncoder) {
case VIDEO_H263:
session.addVideoTrack(new H263Stream(mCamera));
break;
case VIDEO_H264:
H264Stream stream = new H264Stream(mCamera);
if (mContext!=null)
stream.setPreferences(PreferenceManager.getDefaultSharedPreferences(mContext));
session.addVideoTrack(stream);
break;
}
if (session.getVideoTrack()!=null) {
VideoStream video = session.getVideoTrack();
video.setFlashState(mFlash);
video.setVideoQuality(mVideoQuality);
video.setSurfaceView(mSurfaceView);
video.setPreviewOrientation(mOrientation);
video.setDestinationPorts(5006);
}
if (session.getAudioTrack()!=null) {
AudioStream audio = session.getAudioTrack();
audio.setAudioQuality(mAudioQuality);
audio.setDestinationPorts(5004);
}
return session;
}
SessionBuilder使用了build模式來(lái)構(gòu)建Session戈次,build()方法創(chuàng)建Session對(duì)象并對(duì)其附上已設(shè)置完成的參數(shù)值,mAudioEncoder和mVideoEncoder用于設(shè)置音頻和視頻的編碼格式筒扒。
這篇文章我們大致了解了Session內(nèi)部機(jī)制(兩個(gè)handler分別負(fù)責(zé)同步操作和異步操作)和構(gòu)建方式(SessionBuilder的build()方法)怯邪,下一步我們將逐步分析Stream類和MediaStream類。