MediaRecorder を使用してビデオ (H264/3gpp) のみを記録しようとしており、LocalSocket 記述子を MediaRecorder に渡しました。データは受信できますが、動画を再生できません。オーディオ(AMR)でも同じコードが機能します。
LocalSocket クラス
public class MediaStreamer extends MediaRecorder{
private static int id = 0;
private LocalServerSocket lss = null;
private LocalSocket receiver, sender = null;
public void prepare() throws IllegalStateException,IOException {
receiver = new LocalSocket();
try {
lss = new LocalServerSocket("librtp-"+id);
receiver.connect(new LocalSocketAddress("librtp-"+id));
receiver.setReceiveBufferSize(4096);
receiver.setSendBufferSize(4096);
sender = lss.accept();
sender.setReceiveBufferSize(4096);
sender.setSendBufferSize(4096);
id++;
} catch (IOException e1) {
throw new IOException("Can't create local socket !");
}
setOutputFile(sender.getFileDescriptor());
try {
super.prepare();
} catch (IllegalStateException e) {
closeSockets();
throw e;
} catch (IOException e) {
closeSockets();
throw e;
}
}
public InputStream getInputStream() {
InputStream out = null;
try {
out = receiver.getInputStream();
} catch (IOException e) {
}
return out;
}
public void stop() {
closeSockets();
super.stop();
}
private void closeSockets() {
if (lss!=null) {
try {
lss.close();
sender.close();
receiver.close();
}
catch (IOException e) {
}
lss = null; sender = null; receiver = null;
}
}
}
メディアレコーダー
video = new MediaStreamer();
video.reset();
video.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
video.setPreviewDisplay(holder.getSurface());
video.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
video.setVideoFrameRate(VideoConstants.frameRate);
video.setVideoEncodingBitRate(VideoConstants.bitRate*1000);
video.setVideoSize(VideoConstants.resolationX, VideoConstants.resolationY);
video.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
try {
video.prepare();
videoWriter = new Writer(Environment.getExternalStorageDirectory()+"/video.mp4",video.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
video.start();
videoWriter.startWriting();