Low-latency playback: disable network caching
# vlc --network-caching 0 source
MediaRecorder recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
recorder.setOutputFile(outputFile);
recorder.setPreviewDisplay(holder.getSurface());
recorder.prepare();
recorder.start();
// ...
recorder.stop();
Video only accessible after stop()
returns
Use a LocalSocket
instead of a file
LocalServerSocket server = new LocalServerSocket("some name");
receiver = new LocalSocket();
receiver.connect(server.getLocalSocketAddress());
receiver.setReceiveBufferSize(BUFFER_SIZE);
LocalServerSocket sender = server.accept();
sender.setSendBufferSize(BUFFER_SIZE);
// ...
// Give the MediaRecorder our fake file
recorder.setOutputFile(sender.getFileDescriptor());
Video is accessible as soon as the MediaRecorder writes it
It's a video file, not a stream
From Wikipedia
Specifically, an MPEG-4 file
Skip over the meta-data to the video box
byte[] mdat = { 'm', 'd', 'a', 't' };
byte[] buffer = new byte[mdat.length];
do
{
fillBuffer(buffer);
} while (!Arrays.equals(buffer, mdat));
Reliable channel of communication
Fire and forget, no guarantees, less overhead
Only care about latest frame: dropped packets are OK
Latency too high
Altering bit-rate, quality, packet size, ... had no effect
Latency is from the media encoding
Preview surface is real-time ... stream that?
A stream of JPEGs, each one representing a frame
Camera.PreviewCallback previewCallback = new Camera.PreviewCallback()
{
public void onPreviewFrame(byte[] data, Camera camera)
{
// Create JPEG
YuvImage image = new YuvImage(data, format, width, height,
null /* strides */);
image.compressToJpeg(crop, quality, outputStream);
// Send it over the network ...
}
};
camera.setPreviewCallback(previewCallback);
Use a Camera.PreviewCallback
to get the raw camera image
Unreliable playback on VLC
Transcoded to OGG for browser playback. Download the original.Using wireshark, pycapy, and javacv, created a local version for faster development.
Webcam icon by ~kyo-tux. The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.
Quickly created a local MJPEG over HTTP streamer
Use A/B buffering instead of dropping packets
The project is on Github. Try it out thedemo. Slides created using reveal.js.