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

137,027

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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!

Share:
137,027
androidu
Author by

androidu

Updated on October 05, 2020

Comments

  • androidu
    androidu over 3 years

    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
    androidu over 11 years
    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.
  • ldam
    ldam over 11 years
    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.
  • androidu
    androidu over 11 years
    Yeah, this was going to be my last resort. Thanks for the feedback!
  • stealthcopter
    stealthcopter almost 11 years
    Surely a jpeg stream would be more efficient than a png stream?
  • androidu
    androidu over 10 years
    Yeah, I got no other choice I guess.
  • B770
    B770 over 10 years
    Hello Alex, have you any experiance with sending a local stored audio file from an android phone via RTP?
  • Arveen
    Arveen about 10 years
    I was just suggesting using an image, you can use any format you wish
  • user1914692
    user1914692 almost 10 years
    I am looking into Android VLC. Would you tell me how to use it to stream a video?
  • Jasper
    Jasper over 8 years
    @krossreg> can you please paste sample code for what you suggest?
  • Umar Asghar
    Umar Asghar over 8 years
    hi can you please send me the complete project of it?
  • User6006
    User6006 about 8 years
    can you please post your complete project?
  • User6006
    User6006 about 8 years
    which one is server socket and client socket
  • Alex I
    Alex I about 8 years
    @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.
  • User6006
    User6006 about 8 years
    But it through IllegalStateexception when i prepare media player
  • shakram02
    shakram02 about 7 years
    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
  • Alex I
    Alex I about 7 years
    @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
    Alex I about 7 years
    @AhmedHamdy If you were to save the file to disk, it's a MPEG2 transport stream file (en.wikipedia.org/wiki/MPEG_transport_stream). It would play in ffmpeg or avplay if you gave it a .ts extension.
  • shakram02
    shakram02 about 7 years
    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 )