Broadcasting video with Android - without writing to local files

本文介绍了一种使用Android MediaRecorder类实现视频直播的方法,无需将数据写入文件系统,通过网络直接传输到服务器。该方案利用了ParcelFileDescriptor从Socket获取文件描述符的功能,解决了不支持的流媒体协议播放及避免因设备存储空间限制导致的直播长度受限问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

One of the weaker points of the Android platform is the Media API. When compared to the J2ME API, one important feature is missing: the ability to record to a stream and to playback from a stream.

Why is this important? There are a number of use cases.

For recording:

  • post-processing audio / video data before writing out to the file system
  • broadcasting audio / video without writing out the data first into the file system, which also limits the broadcast to the available free space on the device.

For playback:

  • pre-processing the audio / video data before playing
  • streaming using protocols that are not supported by the built-in media player

In this blog entry we will show a method to broadcast video (and audio) from an Android phone to a network server, without writing to the file system.

There is one promising method in the MediaRecorder class setOutputFile(FileDescriptor) .

We know that in Linux also network sockets have file descriptors. But how could we access the file descriptor of a regular java.net.Socket ?

Luckily, ParcelFileDescriptor comes to the rescue, where we can use the fromSocket(Socket) static method to create a ParcelFileDescriptor instance from a Socket object. From this instance, we may now grab the badly needed FileDescriptor.

It all boils down to these few lines (in pseudocode):

String hostname = "your.host.name";
int port = 1234;

Socket socket = new Socket(InetAddress.getByName(hostname), port);

ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);

MediaRecorder recorder = new MediaRecorder();

// Additional MediaRecorder setup (output format ... etc.) omitted

recorder.setOutputFile(pfd.getFileDescriptor());

recorder.prepare();

recorder.start();

Using this concept we created a small proof of concept application (together with an even simpler server), which is able to broadcast videos not limited in length by the available space on the SD Card.

There are a few gotchas, if you want to try this out yourself:

  • The MediaRecorder records either in 3GPP or in MP4 format. This file format consists of atoms , where each atom starts with its size. There are different kinds of atoms in a file, mdat atoms store the actual raw frames of the encoded video and audio. In the Cupcake version Android starts writing out an mdat atom with the encoded frames, but it has to leave the size of the atom empty for obvious reasons. When writing to a seekable file descriptor, it can simply fill in the blanks after the recording, but of course socket file descriptors are not seekable. So the received stream will have to be fixed up after the recording is finished, or the raw video / audio frames have to be processed by the server.
  • For some reason, the MediaRecorder also leaves the header of the file blank, which also has to be handled on the server.
  • High latency connections will cause the video to be choppy. Obviously some buffering is necessary. One method is to use a local mini server on the phone which receives the stream, buffers it, and sends to the remote server as fast as the network allows it. However, if using native code is an option, we can simply create a pipe to receive the data from the MediaRecorder. We will show this method in a future blog entry.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值