GeofP
Posts: 28
Joined: Fri Jan 31, 2014 9:59 am
Location: NE Hampshire UK

mjpg_streamer problem with tmpfs

Thu Mar 13, 2014 10:42 am

When I use mjpg_streamer with raspistill, storing the jpg on the SD card, I can stream the jpg images into a Google Chrome web browser on a Windows PC and to midori on the raspberry desktop (although midori only displays 1 image at a time, not a stream).

If I try the same but using tmpfs i.e. a jpg stored in ram, the jpg images will not stream. However, if I open up the jpg file location using Windows Explorer (I have samba server installed on the Pi), the stream will start in the browser on the PC, (but not in midori on the raspberry desktop). The stream stops if I close Windows Explorer or view a different file location.

The problem is the same using ramfs and /dev/shm (a RAM disk set up by the kernal).

So, using the SD card memory, the following streamed OK onto Google Chrome tab on the PC with http://192.168.x.x:8080/stream_simple.html (x.x is address of the Pi):

Code: Select all

sudo mkdir -m 1777 /data
raspistill -w 1920 -h 1080 -q 100 -o  /data/pic.jpg -tl 1000 -t 1000000 -th 0:0:0 &
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /data -n pic.jpg" -o "output_http.so -w /usr/local/www"&
Using tmpfs RAM, the following does not stream (unless the memory location is opened using Windows Explorer):

Code: Select all

sudo mkdir -m 1777 /data/ram
sudo mount -t tmpfs -o size=10m tmpfs /data/ram
raspistill -w 1920 -h 1080 -q 100 -o  /data/ram/pic.jpg -tl 1000 -t 1000000 -th 0:0:0 &
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /data/ram -n pic.jpg" -o "output_http.so -w /usr/local/www"&
I am running 3.10.33+ #654, with mjpg_streamer installed earlier using http://blog.miguelgrinberg.com/post/how ... spberry-pi.

User avatar
DougieLawson
Posts: 40466
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: mjpg_streamer problem with tmpfs

Sat Mar 15, 2014 11:14 am

sudo apt-get install iceweasel

Ditch midori it's c**p.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
jojopi
Posts: 3402
Joined: Tue Oct 11, 2011 8:38 pm

Re: mjpg_streamer problem with tmpfs

Sat Mar 15, 2014 1:15 pm

This is a bug in mjpg-streamer's input_file plugin. It uses the inotify mechanism to wake up a thread when there is a change in the directory it is watching. The kernel will return as many inotify events as are available and will fit into the buffer, but input_file only expects one event per read.

raspistill writes each image to pic.jpg~ and then renames it to pic.jpg when complete. When writing to the SD card it is slow enough for the close event and the rename event to be delivered separately. But tmpfs is "too fast" and the inotify events often arrive together, and mjpg-streamer misses the renames and ends up ignoring almost all frames.

Assuming that we only need compatibility with raspistill, and that there is nothing else writing into the same directory, the simplest fix is to remove IN_CLOSE_WRITE from the inotify mask in mjpg-streamer/plugins/input_file/input_file.c, so that we receive only the rename events:

Code: Select all

    rc = wd = inotify_add_watch(fd, folder, IN_MOVED_TO | IN_ONLYDIR);
Then:

Code: Select all

cd mjpg-streamer
make clean input_file.so
sudo cp input_file.so /usr/local/lib/
DougieLawson wrote:sudo apt-get install iceweasel
Seriously?

User avatar
DougieLawson
Posts: 40466
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: mjpg_streamer problem with tmpfs

Sat Mar 15, 2014 7:02 pm

jojopi wrote:
DougieLawson wrote:sudo apt-get install iceweasel
Seriously?
No, because all web browsers suck like a Dyson on the Raspberry Pi. Midori just sucks harder.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

GeofP
Posts: 28
Joined: Fri Jan 31, 2014 9:59 am
Location: NE Hampshire UK

Re: mjpg_streamer problem with tmpfs

Sun Mar 16, 2014 9:43 am

Sorry I mentioned midori - it was not an important pary of the question.

Thank you jojopi for the very informative answer to the mjpg_streamer problem. It is good to have both the explanation and the method to correct the problem.
I edited the following line in input_file.c, removing 'IN_CLOSE_WRITE | '
rc = wd = inotify_add_watch(fd, folder, IN_CLOSE_WRITE | IN_MOVED_TO | IN_ONLYDIR);
then followed your make instructions.

mjpg_streamer now streams OK from a tmpfs location to the web browser.

Excellent result!

User avatar
ds18s20
Posts: 83
Joined: Fri Dec 23, 2011 5:44 am
Location: San Francisco

Re: mjpg_streamer problem with tmpfs

Mon Mar 17, 2014 2:53 pm

So here is the uncomfortable question: when can we expect /dev/video0 for this cam?

User avatar
DougieLawson
Posts: 40466
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: mjpg_streamer problem with tmpfs

Mon Mar 17, 2014 2:59 pm

ds18s20 wrote:So here is the uncomfortable question: when can we expect /dev/video0 for this cam?
My picam appears as /dev/video0.

sudo rpi-update then add
bcm2835-v4l2
to /etc/modules before doing the reboot.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Return to “Camera board”