Thursday, March 7, 2013

Web Cam streaming from Raspberry Pi to Android using streamer



I tried to stream video from webcam connected with Raspberry Pi to Android phone.


Hardware

webcam : PS3 Eye
Android Phone : Motorola RAZR XT912

Set up Raspberry Pi

Install gstreamer

Update your Raspbian before install gstreamer packages.
sudo apt-get upgrade
sudo apt-get update
Install gstreamer packages.
sudo apt-get install gstreamer-tools gstreamer0.10-plugins-bad gstreamer0.10-plugins-good v4l-utils

Create gstreamer start script

I like tcsh shell.
sudo apt-get install tcsh
Create the script gst-server.sh to start gstreamer streaming server. 
#!/bin/tcsh

set myip=192.168.0.103
set port=5000
set width=320
set height=240

gst-launch\
  v4l2src !\
  ffmpegcolorspace !\
  video/x-raw-yuv,width=${width},height=${height},framerate=\(fraction\)30/1 !\
  jpegenc !\
  tcpserversink host=${myip} port=${port} sync=false
 Add an executable permission to the script.
chmod u+x gst-server.sh

Connect a webcam

I used PS3 Eye as a webcam. You can use other webcams.

Start gstreamer

Run the script.
./gst-server.sh

Set up Android phone

I created an Android application simplemjpegview_gst based on SimpleMjpegStreamer. You can download the source codes.

You can download apk file here.
https://docs.google.com/file/d/0B1V8jj4Adbrpdzl4VGVjcW1GT28/edit?usp=sharing



                 

70 comments:

  1. Hello, nice dev,
    It's working fine
    But I can not retrived it also on VLC? How can I do it?

    J

    ReplyDelete
    Replies
    1. Thank you for your comment!
      I enter below URL into text box "Open Source > Network > URL".
      tcp://(yourip):(port#)/
      (ex. tcp://192.168.0.115:5000/ )

      Plus, I changed the launch script like this:

      #!/bin/tcsh

      set myip=192.168.0.115
      set port=5000
      set width=320
      set height=240

      gst-launch\
      v4l2src !\
      ffmpegcolorspace !\
      video/x-raw-yuv,width=${width},height=${height},framerate=\(fraction\)30/1 !\
      queue !\
      videorate !\
      video/x-raw-yuv,framerate=10/1 !\
      jpegenc !\
      multipartmux !\
      tcpserversink host=${myip} port=${port} sync=false

      I can see the video from VLC. I hope this information helps you. Thanks!

      Delete
    2. thanks for your quick reply, it is working well. I will test no if the paspberry can stream during the night
      Regards

      Delete
    3. Ok I try to streamed, it is working for 10 to 15 minutes and after the processed is killed by itself. Do you have any idea about that?
      Regards

      Delete
    4. Yeah, I experienced that kind of things but I have no idea about it at the moment. If you got a clue, please tell me. Thanks!

      Delete
    5. hello jeromi i am doing my final year project which is video transmission from raspberry pi to smart phone using wi-fi module. but after i put all the raspberry pi and i powered it but when i move to
      sudo apt-get update
      sudo apt-get upgrade
      it is not working please tell me what is the problem or attach on my email how do i work on it to see the video transmission from USB webcamera that inserted from raspberry pi board to my smart phone i need your quick replied please dear tnx
      please email me on richsole@yahoo.com.au

      Delete
    6. Hi Woretaw,

      Have you established internet connection? To execute the command, you need internet connection.

      Delete
  2. Hey COISME, Awesome job on this, I was wondering is there any way you can view this stream via HTTP web browser (Firefox)? Thanks

    ReplyDelete
  3. Hi Randy, thank you for your comment.
    I tried streaming via HTTP using gstreamer. It seems possible but it is complicated. That's why I'm using TCP. If you want to use HTTP, you can use other streaming package. For example, mjpeg-streamer.
    http://www.talktech.info/2013/01/21/pieye-raspberry-pi-streaming-webcam/

    ReplyDelete
  4. Thanks for share this. May I know how to stop use port 5000. What is service name ?

    ReplyDelete
    Replies
    1. Do you want to stop running the script? I use ctrl+c or kill command.

      Find the script process by ps command. Then run the below command:
      kill -HUP (process num)

      Delete
  5. I have an error:

    Setting pipeline to PAUSED ...
    Pipeline is live and does not need PREROLL ...
    Setting pipeline to PLAYING ...
    New clock: GstSystemClock
    WARNING: from element / GstPipeline: pipeline0/GstV4l2Src: v4l2src0: Got unexpected frame size of 145036 instead of 153600.

    And nothing is visible.

    My PS3 Eye webcam is not.

    ReplyDelete
    Replies
    1. Hi, I think these are not error message. I saw these messages when run the script.
      Is your Android phone in the same network with Rpi?

      Delete
  6. thanks for the tutorial , but i still confused how to connect RPi to my android device. After i turned on wifi on my phone, it doesn't show the SSID. Or maybe it just activated and connect with the IP ?is that right? thanks for your help.

    ReplyDelete
    Replies
    1. Hi, I'm sorry but the system diagram was not accurate. I connected RPi and Android Phone to a Wi-Fi router.

      Delete
    2. I corrected the above figure. Thanks.

      Delete
  7. Hi, Im working on something similar to this for my senior design project. I want to mount a camera to an RC car and control the car via android while also streaming the video to android. So in your video the pi and the camera are not connected at all to the android phone correct? The wire connected to the android is just to charge the phone? Awesome project by the way

    Thanks ^_^

    ReplyDelete
    Replies
    1. Hi, thank you for your comment. Yes, there is no connection with android phone. The connection in the video is only for charging purpose. We also built an RC car system. Please see this post:
      http://sanjosetech.blogspot.com/2013/05/android-rc-car-with-live-video-camera.html

      Delete
  8. Hi Sir,I tried followed you but I have a problem.
    WARNING: erroneous pipeline: could not link ffmpegcsp0 to jpegenc0
    I don't unstand this error.Can you help me pls.

    ReplyDelete
  9. Hi Sir, i am trying to use Raspberry Pi Camera Module not a usb webcam. But i cannot define the raspicam to v4l2src. I am just a beginner at this staff. Please, help me.
    Thank you.

    ReplyDelete
  10. Hi,
    you mentioned you tried http streaming, I was wondering if you picked that up or if you know any way to stream h264 (like raspicams output) via http to multiple clients ? So not with mjpegstreamer :D

    ReplyDelete
    Replies
    1. Sorry, I have no information about that. :-(

      Delete
  11. Hi sir,
    Tried with .apk as provided on android tablet I am able to view the stream from beagleboard xm. However, if I compile the source code on ADT and tried to run on Android Tablet only black screen is visible. Not able to set port and ip address as nothing comes on tablet. Help me out.
    Thanks and Regards,
    Siddarth

    ReplyDelete
    Replies
    1. I wonder if you can't see the setting or video streaming. Which can't you see?

      Delete
  12. Hi, I was wondering if you came across any serious latency issues during development. My server and android app work fine but the video rarely gets above 4 fps even when the network isn't busy.

    ReplyDelete
  13. hello coisme ,

    I tried running the project in my pi everything runs fine in the pi but i get only black screen in the android app. i used the ip 192.168.0.118. used the same .apk file as u have provided above. does the wifi router need to have a static ip by itself coz the ip i get when i write "ip route" in my pi is different frm what i see on d phone . my wifi adaptter is working fine as i am able to browse net through it wirelessly. plz help

    ReplyDelete
    Replies
    1. also do u need to install any driver of the web cam?

      Delete
    2. Hi Sundaresh, thank you for your comment.
      Were your Android Phone and Raspberry Pi in the same network? I don't think the router need to be allocated static IP. In my experience, such things happened when my Android Phone was connected to mobile network, not my local network (Wi-Fi).
      In addition, I didn't have to install any driver for my webcam.

      Delete
    3. on running the gst-server i get a message like this..
      /GstPipeling:pipeline0/GstFFmpegCsp:ffmpegcsp0.gstPad.src: caps =video/x-raw-yuv , format=(fourcc)YUY2, width=(int)320 , interlaces=(boolean)false ,poxel aspect-ratio=(fraction)1/1, framerate=(fraction)30/1
      is this normal??

      also my motion daemon would not start, disabled via /etc/default/motion

      Delete
  14. error occurs in line chmod u+x gst-server.sh
    it says"chmod: changing permissions of 'gst-server.sh' : Operation not permitted"
    plz help
    thnx

    ReplyDelete
  15. Nikhil Amatya, try "sudo chmod u+x gst-server.sh";

    COISME, Now when i try to run your app on a phone it ask for port ect. i pt it in black screen for 2 mins then back to the menu why is this happening?

    I am using ad hoc.
    i am running the command :
    raspivid -t 999999 -h 100 -w 100 -fps 25 -b 2000000 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=192.168.42.1 port=5000
    instead of your file.
    android version: 2.3.3
    i am using the pi camera if you didn't notice by the command.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  16. thnx for the reply steve bobins but i got pipeline error when i run the ./gst-server.sh
    plz hlp

    ReplyDelete
    Replies
    1. Hi Nikhil, please copy & paste your error message.

      Delete
  17. hi COISME ,
    i was able to resolve my problem.. the problem was the set myip command was not quite working on the gst-server i was able to find d ip to which my pi was connected to d modem and gave d same in d application and voila.. it worked.. i was wondering if d same project can be done without the router but rather pi as an access point. . ??

    ReplyDelete
  18. Hello, i'm trying to stream on my galaxy nexus but the application is incredible slow. The video is not faster than 5 fps, and with hardware acceleration is quite the same.

    ReplyDelete
  19. COISME, can you tell me how i can stream to android using your android programme with the raspberry pi camera instead of the eye?

    ReplyDelete
    Replies
    1. Yeah I'm also trying to perform this without any success...

      Delete
  20. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hello COISME,
      I'm trying to stream video using your method. But actually when I launch my script, I get an error message like :
      "ERROR: pipeline could not be constructed: Unrecoverable syntax error while parsing pipeline"
      Can you help me please?
      Thank you in advance

      Delete
  21. I have a errr as below.

    pi@raspberrypi ~ $ ./gst-server.sh
    Setting pipeline to PAUSED ...
    ERROR: Pipeline doesn't want to pause.
    ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Cannot identify device '/dev/video0'.
    Additional debug info:
    v4l2_calls.c(493): gst_v4l2_open (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
    system error: No such file or directory
    Setting pipeline to NULL ...
    Freeing pipeline ...

    I tried .sh command but Pi don't like me as above.
    please help me, do you have another idea?

    ReplyDelete
    Replies
    1. My Pi have a Raspberry Pi original camera, don't like with you.

      and,

      myIP is Pi's IP, is right?

      Delete
    2. I installed UV4L driver on Rpi.
      and then
      ./gst-server.sh

      pi@raspberrypi ~ $ ./gst-server.sh
      Setting pipeline to PAUSED ...
      ERROR: Pipeline doesn't want to pause.
      ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Could not negotiate format
      Additional debug info:
      gstbasesrc.c(2830): gst_base_src_start (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
      Check your filtered caps, if any
      Setting pipeline to NULL ...
      Freeing pipeline ...

      Error is like above.
      what problem? please.

      Delete
    3. It's played video on Rpi for 2 seconds, and then disappear video and display Error like above.
      I used RaspberryPi orignal Camera, So I guess Gstmeamer can not negotiation video format.
      What should i do?

      Please comment me, Regards.

      Delete
    4. Hi.. sun bae yim..
      Even i am facing the same problem.. if u get any solution please share.. Thank in advance.. :)

      Delete
  22. hello coisme i got error while playing in emulator android. how to fix it ( sorry for bad english)
    03-27 22:41:46.348: W/dalvikvm(1373): No implementation found for native Lcom/pengendali/robot/kamera/MjpegInputStream;.pixeltobmp:([BILandroid/graphics/Bitmap;)V
    03-27 22:41:46.348: W/dalvikvm(1373): threadid=12: thread exiting with uncaught exception (group=0xb4a77b90)
    03-27 22:41:46.358: E/AndroidRuntime(1373): FATAL EXCEPTION: Thread-97
    03-27 22:41:46.358: E/AndroidRuntime(1373): Process: com.pengendali.robot.kamera, PID: 1373
    03-27 22:41:46.358: E/AndroidRuntime(1373): java.lang.UnsatisfiedLinkError: Native method not found: com.pengendali.robot.kamera.MjpegInputStream.pixeltobmp:([BILandroid/graphics/Bitmap;)V
    03-27 22:41:46.358: E/AndroidRuntime(1373): at com.pengendali.robot.kamera.MjpegInputStream.pixeltobmp(Native Method)
    03-27 22:41:46.358: E/AndroidRuntime(1373): at com.pengendali.robot.kamera.MjpegInputStream.readMjpegFrame(MjpegInputStream.java:258)
    03-27 22:41:46.358: E/AndroidRuntime(1373): at com.pengendali.robot.kamera.MjpegView$MjpegViewThread.run(MjpegView.java:131)

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. Hello, i have a team project streaming with PI and PI camera and am very impressive to this posting. what we want to do now is to make a android app which can play the video our PI is streaming. So we followed your way on this posting and downloaded "SimpleMjpegStreamer" and your application, but we can't play our streaming video on "SimpleMjpegStreamer". I guess the streaming type(or protocol) you used on you app is different or you changed a lot so that the app can play based on mjpeg. Could you provide your source code or some information about how to solve our problem. we aren't good at android programming, so please explain easily...

    ReplyDelete
  25. can i use this code with modul camera raspberry pi sir ???
    thanks before

    ReplyDelete
  26. Oh thats a very nice blog which give a suitable info about It Support San Jose and it is very useful info for a novice in it fields thanks to share such a great info with us and want to continue with your blogs.

    ReplyDelete
  27. Hello, i need your project as part of my senior project. Thanks for all you have done, but when I try to execute gst-server.sh script i get this error. Please, if you could help... Thanks ;)

    WARNING: erroneous pipeline: link without sink element
    video/x-raw-yuv,width=320,height=240,framerate=(fraction)30/1: Command not found.

    ReplyDelete
  28. Error Like this.. how to solve.. please help

    Setting pipeline to PAUSED ...
    ERROR: Pipeline doesn't want to pause.
    ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Could not negotiate format
    Additional debug info:
    gstbasesrc.c(2830): gst_base_src_start (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
    Check your filtered caps, if any
    Setting pipeline to NULL ...
    Freeing pipeline ...

    ReplyDelete
  29. i gor this following error after using " sudo ./gst-server.sh" Can anyone help how to overcome this.. Thank You in advance..
    Setting pipeline to PAUSED ...
    ERROR: Pipeline doesn't want to pause.
    ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Could not negotiate format
    Additional debug info:
    gstbasesrc.c(2830): gst_base_src_start (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
    Check your filtered caps, if any
    Setting pipeline to NULL ...
    Freeing pipeline ...

    ReplyDelete
  30. I got the following error after using last command " sudo ./gst-server.sh".. can anyone help how to overcome.. Thank u in advance

    Setting pipeline to PAUSED ...
    ERROR: Pipeline doesn't want to pause.
    ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Could not negotiate format
    Additional debug info:
    gstbasesrc.c(2830): gst_base_src_start (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
    Check your filtered caps, if any
    Setting pipeline to NULL ...
    Freeing pipeline ...

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. My test using your script to VLC worked great after inserting the new code you replied with on April 6, 2013.
    One question out of curiosity:

    Why are these additional lines (below) needed for it to work in VLC? What do the lines do?

    queue !\
    videorate !\
    video/x-raw-yuv,framerate=10/1 !\
    jpegenc !\
    multipartmux !\

    Thanks!

    ReplyDelete
  33. My test using your script to VLC worked great after inserting the new code you replied with on April 6, 2013.
    Thanks!
    One question out of curiosity:

    Why are these additional lines (below) needed for it to work in VLC? What do the lines do?

    queue !\
    videorate !\
    video/x-raw-yuv,framerate=10/1 !\
    jpegenc !\
    multipartmux !\

    Thanks!

    ReplyDelete
  34. Thanks for good sample. I just start to study Rasberry Pi.

    ReplyDelete
  35. This comment has been removed by the author.

    ReplyDelete
  36. This comment has been removed by the author.

    ReplyDelete
  37. This works very well with an Xbox camera on my telescope, viewing on a Samsung Galaxy S5.

    Do you know of an Android app that will allow me to record the stream?

    ReplyDelete
  38. Is it possible to share your android application source code. I want to learn how to develop a similar application.

    ReplyDelete
    Replies
    1. Please see the page below:
      https://bitbucket.org/coisme/simplemjpegview_gst/src

      Delete
  39. Videocon and stay in touch with your Contacts, Friends, and Loved ones.Seamless Roaming across India. National Roaming is Pre-Activated on Videocon Prepaid Connection with Zero Monthly Rentals. Tariff while Roaming anywhere in India:

    ReplyDelete
  40. Thank you for sharing very useful information.

    Videocon and stay in touch with your Contacts, Friends, and Loved ones.Seamless Roaming across India. National Roaming is Pre-Activated on Videocon Prepaid Connection with Zero Monthly Rentals. Tariff while Roaming anywhere in India:

    ReplyDelete
  41. Good work! I did my own on Banana Pi M1, too. But, I would like to customize the apk, for I also have the other requirements like control DC servo for different view angles.

    Can I know the apk you wrote? Or, where I can get all related information. Thank you!

    ReplyDelete
    Replies
    1. Hi, there's a link to the apk source code in the article. It might be a little bit difficult to find. I'll copy it here for your convenience.

      https://bitbucket.org/coisme/simplemjpegview_gst/wiki/Home

      Delete
  42. Hi jose
    Thanks for the tutorial, but for me there is one problem .
    My problem is this.

    pi@raspberrypi ~$ ./gst-server.sh
    Setting pipeline to PAUSED ...
    ERROR: Pibeline doesn't want to pause.
    ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Cannot identify device '/dev/video0'.
    Additional debug info:
    v4l2_calls.c(493): gst_v412_open (): /GstPipeline:pipeline0/GstV4l2Src:v412src0:
    system error: No such file ofr directory
    Setting pipeline to NULL ...
    Freeing pipeline ...

    ReplyDelete
  43. Hello Have you figured out a way to watch the stream on HTTP/internet browser? Is it also possible to stream this stream with an android app such as tinycam monitor?

    ReplyDelete