Live-stream video from one android phone to another over WiFi

I have searched the internet for days now on how to implement a video streaming feature from an android phone to another android phone over a WiFi connection but I can't seem to find anything useful. I looked on android developers for sample code, stackoverflow, google, android blogs but nothing. All I can find are some sort of phone-to-desktop or desktop-to-phone solutions for streaming, but nothing that I can borrow in my implementation.

I need to control a robot using an arduino ADK, so I am using 2 phones, one which will be mounted on the robot and another which will receive the video stream from the robot. I am mentioning this because I am trying to achieve the smallest delay between the broadcast time and the viewing time.

I am writing 2 apps, one master app to control the robot(from the handheld phone) which will control the slave app and receive the stream, and the second slave app which will run on the robot-strapped phone, controlling the motors/actuators/streaming to master app. I can not use third party apps unfortunately. I need to integrate the video stream code into my 2 apps.

What options are there for achieving this? Also is it very hard to do because I never worked with videostreaming, tough I am doing pretty good in both Java and Android development. How should I encode/decode the stream, how do I initiate the connection, will I need to work with UDP instead of TCP/IP ? I really don't know where to start, with no sample code anywhere. I am pretty sure this can be achieved. I just can't find anything useful to get me started in the right direction.

I stumbled across spydroid but it is using VLC on a desktop so its no good for me.


EDIT: Check out Cagney Moreau's blog. He goes into details about implementing this.

138565 次浏览

您可以使用 IP 网络摄像头,也可以使用 DLNA。例如,三星设备带有一个名为 AllShare 的应用程序,可以在网络上共享和访问启用 DLNA 的设备。我认为 IP 网络摄像头是你最好的选择。你应该能够打开它创建的流使用 MX 视频播放器或类似的东西。

如果你的应用程序不需要录制和播放功能,使用现成的流媒体应用程序和播放器是一个合理的选择。

If you do need them to be in your app, however, you will have to look into MediaRecorder API (for the server/camera app) and MediaPlayer (for client/player app).

服务器的快速示例代码:

// this is your network socket
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// this is the unofficially supported MPEG2TS format, suitable for streaming (Android 3.0+)
mMediaRecorder.setOutputFormat(8);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile(pfd.getFileDescriptor());
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();

On the player side it is a bit tricky, you could try this:

// this is your network socket, connected to the server
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(pfd.getFileDescriptor());
mMediaPlayer.prepare();
mMediaPlayer.start();

不幸的是,媒体播放器往往不喜欢这样,所以你有两个选择: (a)保存数据从套接字到文件和(在你有一点数据)播放媒体播放器从文件,或者(b)作出一个微小的 HTTP 代理,运行在本地,可以接受媒体播放器的 GET 请求,回复 HTTP 头,然后复制数据从远程服务器到它。对于(a) ,您将创建一个文件路径或文件 URL 的媒体播放器,对于(b) ,给它一个指向您的代理的 http URL。

参见:

使用插座 fd 将实时视频从手机传送到手机

MediaPlayer 在 MP3播放开始时断断续续

我曾经做过类似的东西,但是发送视频并实时播放是一件非常复杂的事情。 我建议你只用巴布亚新几内亚的。在我的实现中,我所做的是使用主机摄像头捕获 PNG,然后通过网络发送给客户端,客户端将在收到图像后立即显示图像,并请求主机发送下一张图像。因为你在 wifi 上,通信速度足够快,每秒可以获得8-10张图片(大约只有蓝牙)。因此,这将看起来像一个连续的视频,但更少的努力。 对于通信,您可以使用 UDP 套接字(更快、更简单)或 DLNA (不确定如何工作)。

你可以检查一下 机器人 VLC它可以流媒体和播放视频,如果你想收集更多的信息,你可以检查一下他们的 GIT来分析他们做了什么。祝你好运!