LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-03-2006, 06:37 PM   #1
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Rep: Reputation: 30
Accessing v4l devices from PHP


How's it done?

I've written a simple javascript webcam application, which uses a very basic PHP backend for framegrabbing. My script looks like this:

PHP Code:
$tempfile "webcam-frametemp.jpeg";
$cmd "streamer -c /dev/video0 -b 24 -o $tempfile";
$shell_out shell_exec($cmd);
$frame imagecreatefromjpeg$tempfile );
header('Content-type: image/jpeg');
imagejpeg($frame); 
But for some reason this doesn't work - the shell command fails and I get absolutely *no* information thrown back at me as to what I did wrong. I've tried using absolute file paths, and I've made sure that the apache user has access rights to /dev/v4l/video0. The command works as my normal user, and I even temporarily modified /etc/passwd just enough for me to su to apache, and the command works from the shell there, too.

My httpd.conf file says this:
Code:
User apache
Group apache
So I know I've added the permissions to the proper user.

I can't think of anything else, and so I turn to you wonderfully knowledgable people - what can I do?

Last edited by Napalm Llama; 02-03-2006 at 06:38 PM.
 
Old 02-04-2006, 09:49 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What are you safe-mode settings in you php.ini file?

shell_exec() is disabled in safe mode

Does the streamer command have any switch to display more output, like connection errors etc?
 
Old 02-04-2006, 10:46 AM   #3
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
Thanks for your reply.

I'm fairly sure I'm not running in safe mode - another project of mine uses shell_exec() it and it works fine.

And sure enough, php.ini says this:
Code:
; Safe Mode
;
safe_mode = Off
This is the normal (Bash shell) output of the command:
Code:
apache@splig ~ $ streamer -c /dev/video0 -b 24 -o webcam-frametemp.jpeg
files / video: JPEG (JFIF) / audio: none
So when run normally, it saves the JPEG and I do get a message. It's just that for some reason when PHP runs it, it doesn't and I don't...
 
Old 02-04-2006, 11:50 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What do you get when you echo $shell_out?
Does the webcam-frametemp.jpeg file exist
a) before
b) after
What happens when you run the command as user apache?
 
Old 02-04-2006, 12:32 PM   #5
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
Absolute Path Needed ... Possibly

Using su to test things still doesn't work exactly as you would expect. Sometimes your environment get's passed to your su ... if you don't call su correctly. I suggest that it may still be a permission problem. The web server needs to create the image in a directory that it has write access to.

So ... as a test:

Code:
$tempfile = "/tmp/webcam-frametemp.jpeg";
The web server can almost always write to the temp directory. If it works, then you'll need to change the permission of the directory that you really want it to sit in and call it with a full path. Example:

Code:
chown apache:apache /var/www/html/webcam
chmod 775 /var/www/html/webcam

$tempfile = "/var/www/html/webcam/webcam-frametemp.jpeg";

-- Tony

Last edited by tonyfreeman; 02-04-2006 at 12:35 PM.
 
Old 02-05-2006, 09:07 AM   #6
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
OK, I get absolutely *nothing* when I echo $shell_out - I added the following line underneath the shell_exec() call:
PHP Code:
echo "<pre>start:$shell_out:end</pre>"
And it resulted in this:
Code:
start::end
If I edit /etc/passwd so I can "su - apache" then running the command works fine, I get the output detailed in my pevious post, and the file appears/changes. When I run the PHP script, I don't get any output and no file operation occurs.

I've already tried full paths - that is /usr/bin/streamer and /srv/www/htdocs/webcam-frametemp.jpeg. The destination file is definately owned by apache:apache, and chmodded to 664 (it doesn't need to be executable) - and I just changed /srv/www/htdocs to 775 (from 755) but it didn't make any difference.

The only thing that I can think of is if PHP isn't actually running the command for some reason - and then refusing to tell me why

I can post my php.ini if you think that'd be useful...

Last edited by Napalm Llama; 02-05-2006 at 09:08 AM.
 
Old 02-05-2006, 10:56 AM   #7
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
Use system command

OK ... it doesn't appear that you are using the information in $shell_out ... so how about trying the system() php call. To keep the system call from printing stuff to the screen you'll have to change the command to redirect stdout to /dev/null as well as stderr.

Code:
$cmd = "streamer -c /dev/video0 -b 24 -o $tempfile 2>/dev/null";
$shell_out = system($cmd, $retval);

if ($retval) {
    print "Successful system call!";
} else {
    print "Unsuccessful system call ... crap!";
}

-- Tony
 
Old 02-05-2006, 11:18 AM   #8
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
OK, I've stuck that in like this:
PHP Code:
$tempfile "/srv/www/htdocs/webcam-frametemp.jpeg";
$cmd "streamer -c /dev/video0 -b 24 -o $tempfile 2>/dev/null";
$shell_out system($cmd$retval);

if (
$retval) {
    print 
"Successful system call!";
} else {
    print 
"Unsuccessful system call ... crap!";
}
echo 
"<br>$retval";
echo 
"<br>$cmd";
echo 
"<pre>start:$shell_out:end</pre>"
$tempfile isn't modified, but I get this:
Code:
Successful system call!
139
streamer -c /dev/video0 -b 24 -o /srv/www/htdocs/webcam-frametemp.jpeg 2>/dev/null

start::end
If I remove the 2>/dev/null part, the exit code (I'm guessing that's what it is) becomes an 11... and we all know what that means
 
Old 02-05-2006, 04:55 PM   #9
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
Closer Still

OK ... I think we're getting close.

How about providing the full path to 'streamer' in the command:

Code:
$cmd = "/usr/local/bin/streamer -c /dev/video0 -b 24 -o $tempfile
-- Tony
 
Old 02-05-2006, 05:49 PM   #10
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
No such luck:
Code:
Successful system call!
139
/usr/bin/streamer -c /dev/video0 -b 24 -o /srv/www/htdocs/webcam-frametemp.jpeg 2>/dev/null

start::end
(no typo - streamer is definately at /usr/bin on my system)
 
Old 02-05-2006, 06:38 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What do you get when you try different commands?

Such as:
pwd
ifconfig
/sbin/ifconfig
streamer --version
streamer --help

I know it's not going to solve the problem but it may shake something else up.
 
Old 02-06-2006, 10:51 AM   #12
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
OK, the 2 streamer commands you said still don't work, but I got these:

Code:
/srv/www/htdocs Unsuccessful system call ... crap!
0
pwd

start:/srv/www/htdocs:end
Code:
eth0 Link encap:Ethernet HWaddr 00:04:61:78:0F:82 inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::204:61ff:fe78:f82/64 Scope:Link UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1127354 errors:0 dropped:0 overruns:0 frame:0 TX packets:1145762 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:667975229 (637.0 Mb) TX bytes:224896918 (214.4 Mb) Interrupt:5 Base address:0x6000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:36691 errors:0 dropped:0 overruns:0 frame:0 TX packets:36691 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2787249 (2.6 Mb) TX bytes:2787249 (2.6 Mb) Unsuccessful system call ... crap!
0
ifconfig

start::end
Looks like there's something that PHP/Apache doesn't like about streamer...
 
Old 02-06-2006, 11:05 AM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I have never used it but you may want to look at the passthru() function, it may help.
 
Old 02-06-2006, 02:28 PM   #14
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
Well I tried substituting passthru() instead of system(), but it didn't seem to make any difference - unless I've done it wrong and it needs different syntax? I had a look at the PHP function list and it seemed the same.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[php] accessing command line dtra Linux - Software 1 04-26-2005 08:19 PM
Accessing usb devices 1madstork Slackware 2 10-28-2004 03:04 PM
Accessing http header in PHP artois_val Programming 1 08-07-2004 11:20 AM
rights to v4l devices on devfs acid_kewpie Linux - General 5 07-23-2003 03:08 AM
PHP 4.3 accessing Interbase 7 on Linux PaulC24 Linux - Software 0 04-20-2003 06:15 PM


All times are GMT -5. The time now is 06:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration