spydroid-ipcamera源码分析(一):Session
Session类
Session对象主要用于存储某个会话对象所需的属性以及配置信息,并且使用同步或者异步的方法对Session内部的流媒体对象进行操作和控制。
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:在主线程执行方法时用到的handler,其中所有的callback回调方法都是在主线程中回调的。
sHandler:在子线程执行方法用到的handler,大部分对Stream的操作都是在子线程中执行。
mAudioStream和mVideoStream:音频流媒体和视频流媒体对象,Session对其进行操作。
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();
}
在静态块中创建一个子线程,作为各个异步方法的执行线程。
/**
* 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) {}
}
构造函数中sSignal.await();是等待上面的子线程Looper创建完毕后,再往下执行主线程。
/** Deletes all existing tracks & release associated resources. */
public void release() {
removeAudioTrack();
removeVideoTrack();
sHandler.getLooper().quit();
}
在最后release时,释放音频流和视频流,并且需要将sHandler的looper释放掉,这个方法是在主线程中执行。
对于Stream进行操作的一系列方法,由于篇幅原因不在这里展开列举,其实大多数的操作动作都是由sHandler.post发出消息,在子线程中执行的;而动作执行完成的回调都是由mMainHandler.post发出消息,在主线程中执行回调。
SessionBuilder类
SessionBuilder类就是用来构建Session对象。SessionBuilder采用了单例模式,需要使用getInstance()方法来获取到实例对象。
// 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;
构建Session时默认的配置参数,可以通过set方法设置参数值。
/**
* 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模式来构建Session,build()方法创建Session对象并对其附上已设置完成的参数值,mAudioEncoder和mVideoEncoder用于设置音频和视频的编码格式。
这篇文章我们大致了解了Session内部机制(两个handler分别负责同步操作和异步操作)和构建方式(SessionBuilder的build()方法),下一步我们将逐步分析Stream类和MediaStream类。