117

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.

androidu
  • 4,277
  • 6
  • 33
  • 50
  • 1
    Can you share your project, I'm doing similar thing, but streaming video between android & desktop (WebClient) ... I thought about html5 + websockets but I'm facing many problems ... – Buksy Apr 24 '13 at 12:34
  • Of course, just send me your email adress and I will reply to you. You have my email adress on my profile. – androidu Apr 25 '13 at 15:40
  • 1
    I am developing such solution, that is 80% same as what you are trying to do. You need to configure a VOIP server and then create clients for that VOIP that will transfer your voice and video. I am also looking for it. if any one knows please let me know. – AZ_ May 17 '13 at 07:26
  • 1
    email is given on my profile – AZ_ May 17 '13 at 07:26
  • @MarciCăşvan, Did you find solution to your problem? I am also trying to integrate video chat in my application. If possible, can you please send me your code? – Kameswari Nov 19 '13 at 10:24
  • @Kameswari No, I gave up the idea. But you can achieve it by sending actual images over a UDP connection. – androidu Nov 19 '13 at 11:54
  • i should implement live video from one device to another device.i saved video file in sd card and i transfered to another device through socket connection and i received that file in another device.saved in sd card and played successfully but i should implement live video.so what i should do? plz help me – Aravi Dec 09 '13 at 09:00
  • @Aravi like I said above, open a UDP connection between 2 devices and start sending low resolution images (640x480 or lower, depending of your bandwidth) to the other device where you can display them via OpenGL or native ImageView :) – androidu Jan 12 '14 at 18:50
  • @MarcelCăşvan- I would like to do same kind of small app. Could you guide me? – Ramki Anba Feb 24 '15 at 05:24
  • @RamkiAnba Hi Ramki, I gave up on this project, it did not come to fruition. I don't know if I can help you more that Google. – androidu Feb 27 '15 at 14:57
  • 1
    Check out my project. Everything in java and explained in detail http://cagneymoreau.com/stream-video-android/ – cagney Oct 04 '18 at 17:11
  • @cagney thanks. I linked you in the edit. – androidu Oct 04 '18 at 20:38
  • 1
    thanks I'm also working on a robot. Fun stuff – cagney Oct 04 '18 at 20:53

4 Answers4

55

If you do not need the recording and playback functionality in your app, using off-the-shelf streaming app and player is a reasonable choice.

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).

Quick sample code for the server:

// 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();

Unfortunately mediaplayer tends to not like this, so you have a couple of options: either (a) save data from socket to file and (after you have a bit of data) play with mediaplayer from file, or (b) make a tiny http proxy that runs locally and can accept mediaplayer's GET request, reply with HTTP headers, and then copy data from the remote server to it. For (a) you would create the mediaplayer with a file path or file url, for (b) give it a http url pointing to your proxy.

See also:

Stream live video from phone to phone using socket fd

MediaPlayer stutters at start of mp3 playback

Community
  • 1
  • 1
Alex I
  • 18,105
  • 7
  • 72
  • 135
  • 2
    Hello Alex, have you any experiance with sending a local stored audio file from an android phone via RTP? – B770 Aug 19 '13 at 09:57
  • hi can you please send me the complete project of it? – Umar Asghar Jan 18 '16 at 08:18
  • 1
    can you please post your complete project? – Yazhini Murugaiya Mar 18 '16 at 07:15
  • which one is server socket and client socket – Yazhini Murugaiya Mar 18 '16 at 07:21
  • @UserAndroid: I don't have a complete project, but this should be enough to get you started. When you get it running, please put it on github and let us know. – Alex I Mar 18 '16 at 09:27
  • But it through IllegalStateexception when i prepare media player – Yazhini Murugaiya Mar 18 '16 at 10:14
  • Hello, this post seems a bit old, is there any newer / better way to do this ? what's the format of the output file in this case ( assuming I want to save it on disc ) ? thanks – shakram02 Feb 07 '17 at 11:43
  • 1
    @AhmedHamdy Indeed: in API level 23 (Android 6.0) you can use `setDataSource(MediaDataSource)` instead of `FileDescriptor`. Implementing `MediaDataSource` would allow you to do your own network reading and buffering, without writing to a temp file. – Alex I Feb 15 '17 at 10:15
  • @AhmedHamdy If you were to save the file to disk, it's a MPEG2 transport stream file (https://en.wikipedia.org/wiki/MPEG_transport_stream). It would play in ffmpeg or avplay if you gave it a .ts extension. – Alex I Feb 15 '17 at 10:16
  • Thank you so much Alex for the valuable info :) , any ideas what should I use with Android 4.1 ? ( I tried direct streaming but that failed ) – shakram02 Feb 15 '17 at 16:10
9

I did work on something like this once, but sending a video and playing it in real time is a really complex thing. I suggest you work with PNG's only. In my implementation What i did was capture PNGs using the host camera and then sending them over the network to the client, Which will display the image as soon as received and request the next image from the host. Since you are on wifi that communication will be fast enough to get around 8-10 images per-second(approximation only, i worked on Bluetooth). So this will look like a continuous video but with much less effort. For communication you may use UDP sockets(Faster and less complex) or DLNA (Not sure how that works).

Arveen
  • 858
  • 6
  • 18
8

You can use IP Webcam, or perhaps use DLNA. For example Samsung devices come with an app called AllShare which can share and access DLNA enabled devices on the network. I think IP Webcam is your best bet, though. You should be able to open the stream it creates using MX Video player or something like that.

ldam
  • 3,971
  • 5
  • 38
  • 67
  • 2
    I am writing 2 apps, one master app to control the robot(from the handheld phone) and to view what the robot sees, and the second slave app which will run on the robot-strapped phone, controlling the motors/actuators/streaming. I can not use third party apps unfortunately. I need to integrate the video stream code into my 2 apps. – androidu Jan 18 '13 at 15:07
  • Perhaps you can do some research on the DLNA spec and take a stab at making an app using it for your own or maybe email IP Webcam's developer and ask for some help. – ldam Jan 18 '13 at 18:00
  • Yeah, I got no other choice I guess. – androidu Aug 13 '13 at 10:20
5

You can check the android VLC it can stream and play video, if you want to indagate more, you can check their GIT to analyze what their do. Good luck!