目前,我有一个工作原型应用程序,可将一部手机的相机输出流式传输到另一部手机的屏幕。
然而,流与实际视频之间的延迟范围从1秒到最多5秒。对于我的特定用例,我需要延迟不到1秒。
我发现比特率/分辨率不会影响延迟。
我正在使用libstreaming library通过wifi-direct传输h264视频。
它似乎只支持h263或h264的编码......我发现h263不能像h264那样一致。
以下是捕获视频和流到RTSP服务器的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
// Sets the port of the RTSP server to 8988
Editor editor = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext()).edit();
editor.putString(RtspServer.KEY_PORT, String.valueOf(8988));
editor.commit();
// Get bitrate
int bitrate = Integer.valueOf(getIntent().getStringExtra(BITRATE));
if (bitrate < 100000)
bitrate = 100000;
// Get resolution
String resolution = getIntent().getStringExtra(RESOLUTION);
int resX = 176;
int resY = 144;
if (resolution.equals("352x288")) {
resX = 352;
resY = 288;
} else if (resolution.equals("528x432")) {
resX = 528;
resY = 432;
} else if (resolution.equals("704x576")) {
resX = 704;
resY = 576;
} else {
resX = 176;
resY = 144;
}
Toast.makeText(this, "Resolution: " + resX + "x" + resY + ", Bitrate: "
+ bitrate, Toast.LENGTH_LONG).show();
// Configures the SessionBuilder
SessionBuilder.getInstance().setSurfaceView(mSurfaceView)
.setPreviewOrientation(0).setContext(this)
.setVideoQuality(new VideoQuality(resX, resY, 20, bitrate))
.setAudioEncoder(SessionBuilder.AUDIO_NONE)
.setVideoEncoder(SessionBuilder.VIDEO_H264);
// Starts the RTSP server
getApplicationContext().startService(
new Intent(getApplicationContext(), RtspServer.class));
}
这是观看流媒体视频的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
mVideoIP = getIntent().getStringExtra(SERVER_IP);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
if (savedInstanceState == null) {
}
}
...
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setDataSource("rtsp://" + mVideoIP
+ ":8988");
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setScreenOnWhilePlaying(true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}