goprowifihack icon indicating copy to clipboard operation
goprowifihack copied to clipboard

Android: live streaming from GoPro

Open chri1389 opened this issue 7 years ago • 80 comments

Problem:

I need to develop an Android App that display live streaming from GoPro Hero 4..

Can You help me, please?

I have used a Python script that work very well (it using ffplay), but I do not know how to do with Android.

GoPro Hero4.

Thank you very much! Best Regards!

chri1389 avatar Sep 11 '17 20:09 chri1389

FFmpeg-Android from Writing Minds. Learn to use it and adapt GoProStream to udp://127.0.0.1:1000 (see gopro-py-api). Then just play it using your favorite video library (I use Video view)

KonradIT avatar Sep 11 '17 22:09 KonradIT

@KonradIT thank you very much! I have used this library: http://writingminds.github.io/ffmpeg-android-java/

This is my code: String command = "ffplay -loglevel panic -fflags nobuffer -f:v mpegts -probesize 8192 udp://:8554"; String[] cmd = command.split(" "); ... ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler(){ etc...

I have used this command:

ffplay -loglevel panic -fflags nobuffer -f:v mpegts -probesize 8192 udp://:8554

because it works well from a linux terminal.

But Android call the onFailure(String message) method.

I think that the command is not correct in Android.

Can you help me, please?

Thank you very much!!! Best Regards!

chri1389 avatar Sep 12 '17 16:09 chri1389

No. You need to stream udp://:8554 to localhost. See: https://github.com/KonradIT/gopro-py-api/blob/master/goprocam/GoProCamera.py#L784

KonradIT avatar Sep 12 '17 17:09 KonradIT

@KonradIT Then I should run the Python script on a pc that open a stream udp on its ip (through this command) subprocess.Popen("ffmpeg -f mpegts -i udp://" + self.ip_addr + ":8554 -b 800k -r 30 -f mpegts " + addr, shell=True)

and then the Android library must read this stream from the pc? is it correct?

I am sorry, but I'm confused. @KonradIT thank you!

chri1389 avatar Sep 12 '17 17:09 chri1389

ffmpeg -f mpegts -i udp://" + self.ip_addr + ":8554 -b 800k -r 30 -f mpegts

KonradIT avatar Sep 12 '17 20:09 KonradIT

This weekend I'll release a quick PoC (if i even have time), school is killing me already.

KonradIT avatar Sep 13 '17 16:09 KonradIT

@KonradIT

Now I can reiceve frames from the GoPro. It call always the onProgress(String message) method..

"String message" parameter has that values:

frame= 262 fps= 67 q=29.0 size= 46kB time=00:00:09.00 bitrate= 41.8kbits/s dup=199 drop=0 speed=2.31x frame= 276 fps= 62 q=29.0 size= 60kB time=00:00:09.47 bitrate= 51.9kbits/s dup=200 drop=0 speed=2.12x frame= 296 fps= 59 q=26.0 size= 72kB time=00:00:10.11 bitrate= 58.0kbits/s dup=200 drop=0 speed= 2x etc....

Is it correct? How can I display them, or read the frame content?

@KonradIT

chri1389 avatar Sep 18 '17 10:09 chri1389

Yes you're receiving data from the GoPro, now stream it to a localhost url (maybe read the docs on ffmpeg to understand how it works) and play the URL back using VideoView/ExoPlayer, etc..

KonradIT avatar Sep 18 '17 17:09 KonradIT

@KonradIT thank you very much!! You are very kind!

I have used this command to restream in Android: -fflags nobuffer -i udp://:8554 -f rtp -an rtp://10.5.5.102:1000/gopro

10.5.5.102 is the Ip of a PC where I can see live stream using VLC (through this--> udp://@:1000/gopro).

If I stream in localhost url, VideoView write:

Can't play this video

Do you know what is the right ffmpeg command? What protocol should I use (rtp, udp, rtmp....)? @KonradIT

(When I finish, I will publish a tutorial to get live streaming from GoPro on Android)

chri1389 avatar Sep 19 '17 13:09 chri1389

@chri1389 Looking forward to the tutorial! :smiley:

garydelarosausername avatar Sep 19 '17 15:09 garydelarosausername

@garydelarosausername @KonradIT certainly!! As soon as I can get it to work! ;-)

chri1389 avatar Sep 19 '17 15:09 chri1389

Ok, let's settle this down.

First, @chri1389 you must understand the mere basics of internet and computer networks, for this I recommend reading Computer Networks - a top down approach (available as PDF).

Android development experience will also help, if you want to leverage your Android developing skills take a look at EDX Android course or the coursera Android course.

Last but not least it is critically important to learn about the tools you're using. @chri1389 you need to look at the man page for ffmpeg (by running man ffmpeg on your PC) or by looking at the FFmpeg Wiki: https://ffmpeg.org/documentation.html

Maybe this weekend I can get a PoC rolling, but I doubt it since I'm crushed under a ton of work from school. Please learn so you don't have to depend on me.

KonradIT avatar Sep 19 '17 18:09 KonradIT

@chri1389 seems like you commented something but I'm not able to see it. You got the preview to work? I tried using ExoPlayer, mPlayer and videoView but could not get it to display. Seems like it needs to be udp > rtmp/m3u8.

KonradIT avatar Oct 06 '17 15:10 KonradIT

@KonradIT now I can display live stream from GoPro Hero4.

You can use the vlc library (that can display udp stream). With the ffmpeg library, use this command: String[] cmd = {"-f", "mpegts", "-i", "udp://:8554", "-f", "mpegts","udp://127.0.0.1:8555/gopro?pkt_size=64"}; and with vlc library, display the stream "udp://@:8555/gopro" and then you can see the live stream.

If you use only vlc library, opening the url "udp://:8554" (without ffmpeg) you can display the live stream, but the quality is very bad...

Now, there is only a problem: there is a delay of 3 seconds...

Furthermore, you must use a thread that do a request to this link: http://10.5.5.9/gp/gpControl/execute?p1=gpStream&a1=proto_v2&c1=restart about every seven seconds (I have used the Volley library)...

If you have any question about it, you can ask :)

chri1389 avatar Oct 06 '17 19:10 chri1389

@chri1389 you mean LibVLC? Haven't tried it, will do tomorrow.

KonradIT avatar Oct 06 '17 19:10 KonradIT

@chri1389 https://github.com/KonradIT/gopro-udp-stream-android

Can't get it to play.

KonradIT avatar Oct 07 '17 07:10 KonradIT

Ok @KonradIT!

Try doing these steps:

  1. Create a new Android Project

  2. Learn this tutorial: http://www.truiton.com/2015/03/stream-rtmp-live-android/ and use the "Android VLC SDK" (it is very simple to use, follow the steps).

  3. Import and learn about the FFmpeg Android Library. I have used this command: String[] cmd = {"-f", "mpegts", "-i", "udp://:8554", "-f", "mpegts","udp://127.0.0.1:8555/gopro?pkt_size=64"};

  4. Set the url that vlc must open: mFilePath = "udp://@:8555/gopro";

  5. Very important: you must do a thread that do a http request to this link: http://10.5.5.9/gp/gpControl/execute?p1=gpStream&a1=proto_v2&c1=restart It must do this request, about every seven seconds. It is important that the first request is after that ffmpeg is started!

  6. you must add in the AndroidManifest, the internet permission: <uses-permission android:name="android.permission.INTERNET" />

@KonradIT Let me know if there are problems or it works properly!

chri1389 avatar Oct 07 '17 08:10 chri1389

@chri1389 thanks, now it works. There's delay, its not smooth and the image has artefacts and turns green, but at least it displays the stream.

KonradIT avatar Oct 07 '17 17:10 KonradIT

Perfect @KonradIT Can we work togheter to take away the delay? :)

chri1389 avatar Oct 07 '17 20:10 chri1389

@chri1389 sure, needs faster refresh rate so I suggest dump LibVLC and do UDP>x transcoding

KonradIT avatar Oct 07 '17 21:10 KonradIT

@konradit

What do you mean with "...needs faster refresh rate so I suggest dump LibVLC and do UDP>x transcoding" ?

Thank you very much!

chri1389 avatar Oct 07 '17 21:10 chri1389

there is the possibility to use WebRTC for a stream on android?

Frequel avatar Nov 21 '17 16:11 Frequel

@KonradIT I followed your source code from gopro-udp-stream-android but it is not working. can you help me to stream from gopro to android. thanks.

ShyamBarange avatar Feb 05 '18 09:02 ShyamBarange

@chri1389 tried all steps you explained , still no success.. :( can you help me please?

ShyamBarange avatar Feb 06 '18 08:02 ShyamBarange

@ShyamBarange sure!

You must follow this steps:

  • create a new Android Project
  • import and use VLC library. For this, for example you can use this tutorial (use it only to import and use VLC): http://www.truiton.com/2015/03/stream-rtmp-live-android/
  • you must import FFMPEG library http://writingminds.github.io/ffmpeg-android-java/
  • now, the FFMPEG command is String[] cmd = {"-f", "mpegts", "-i", "udp://:8554", "-f", "mpegts","udp://127.0.0.1:8555/gopro?pkt_size=64"};
  • the VLC url must be mFilePath = "udp://@:8555/gopro";
  • You must also do an http request to the GoPro, to command the Live Streaming. The request must be about every 7 seconds at this URL: http://10.5.5.9/gp/gpControl/execute?p1=gpStream&a1=proto_v2&c1=restart
  • It is very important that the first HTPP request is after that ffmpeg is started!
  • you must add the INTERNET permission in the AndroidManifest: <uses-permission android:name="android.permission.INTERNET" />
  • before run the application, assure that Android Phone is connected to the GoPro WiFi network!

Let me know if there are problems or it works properly!

chri1389 avatar Feb 06 '18 09:02 chri1389

@chri1389 Thanks for the quick response. I have imported the VLC library and FFMpeg library successfully and its working (since I can stream video from a URL in VideoView widget in android app). But when my phone is connected to the GoPro WiFi network, it is showing some error message that "unable to create media player". I might be doing some mistake in passing FFMpeg command. I am also doing http request using Volley library. still no success. can you help me in passing the FFmpeg command.

thanks

ShyamBarange avatar Feb 06 '18 11:02 ShyamBarange

Hello @ShyamBarange

Can you write the code?

chri1389 avatar Feb 07 '18 14:02 chri1389

@chri1389 I managed to get a stream following your tutorial, thanks. However, I have a lag in the video stream and it seems to vary:

03-04 16:41:08.585 30518-30669/com.gopro.stream D/VLC﹕ [b4463128/77cd] core video output: picture might be displayed late (missing 9 ms) 03-04 16:41:08.999 30518-30518/com.gopro.stream D/STREAM﹕ frame= 530 fps= 35 q=9.2 size= 1751kB time=00:00:20.14 bitrate= 711.9kbits/s speed=1.34x 03-04 16:41:09.086 30518-30663/com.gopro.stream W/VLC﹕ [aeb85728/77c7] core audio output: playback too late (67090): up-sampling 03-04 16:41:09.513 30518-30518/com.gopro.stream D/STREAM﹕ frame= 547 fps= 35 q=11.4 size= 1798kB time=00:00:20.72 bitrate= 710.8kbits/s speed=1.33x 03-04 16:41:10.025 30518-30518/com.gopro.stream D/STREAM﹕ frame= 562 fps= 35 q=11.4 size= 1839kB time=00:00:21.22 bitrate= 709.7kbits/s speed=1.32x 03-04 16:41:10.095 30518-30669/com.gopro.stream W/VLC﹕ [b4463128/77cd] core video output: picture is too late to be displayed (missing 42 ms) 03-04 16:41:10.095 30518-30669/com.gopro.stream D/VLC﹕ [b4463128/77cd] core video output: picture might be displayed late (missing 8 ms) 03-04 16:41:10.565 30518-30518/com.gopro.stream D/STREAM﹕ frame= 578 fps= 35 q=8.9 size= 1891kB time=00:00:21.77 bitrate= 711.2kbits/s speed=1.31x 03-04 16:41:11.075 30518-30518/com.gopro.stream D/STREAM﹕ frame= 594 fps= 35 q=12.2 size= 1934kB time=00:00:22.30 bitrate= 710.3kbits/s speed= 1.3x 03-04 16:41:11.595 30518-30518/com.gopro.stream D/STREAM﹕ frame= 609 fps= 35 q=12.1 size= 1977kB time=00:00:22.81 bitrate= 710.2kbits/s speed=1.29x 03-04 16:41:12.097 30518-30518/com.gopro.stream D/STREAM﹕ frame= 624 fps= 34 q=11.6 size= 2019kB time=00:00:23.29 bitrate= 710.0kbits/s speed=1.28x 03-04 16:41:12.601 30518-30518/com.gopro.stream D/STREAM﹕ frame= 639 fps= 34 q=11.5 size= 2069kB time=00:00:23.79 bitrate= 712.5kbits/s speed=1.28x 03-04 16:41:13.109 30518-30518/com.gopro.stream D/STREAM﹕ frame= 654 fps= 34 q=12.1 size= 2109kB time=00:00:24.29 bitrate= 711.1kbits/s speed=1.27x 03-04 16:41:13.629 30518-30518/com.gopro.stream D/STREAM﹕ frame= 670 fps= 34 q=12.7 size= 2152kB time=00:00:24.82 bitrate= 710.0kbits/s speed=1.26x 03-04 16:41:15.739 30518-30669/com.gopro.stream D/VLC﹕ [b4463128/77cd] core video output: picture might be displayed late (missing 5 ms)

@KonradIT @chri1389 did you manage to fix the lag?

If so can I please know how? :)

Messier87 avatar Mar 04 '18 16:03 Messier87

Hi,

I haven't managed to fix the Lag yet, I still have to try ExoPlayer and SurfaceView renderer.

KonradIT avatar Mar 04 '18 17:03 KonradIT

@KonradIT Thanks, Also is it a possibility to replace LibVLC with UDP>x? I couldn't find a PoC...If you can please point to a reference, I can try this path.

@ShyamBarange sent

Messier87 avatar Mar 04 '18 18:03 Messier87