Video streaming from Android device to LAMP Server - php

starting from this point: http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system
I'm trying to create an application to save a video stream from mobile camera to a remote server.
(I found several examples in google code for android part: ipcamera-for-android, spydroid-ipcamera, etc..)
I read some answers here and around the network, but can not find the solution about how to "read" and save the stream of data on the server side.
My knowledge of java is poor, so I'd rather be able to create server-side script in PHP (using server sockets, or other stuffs). Someone can help on this part?
UPDATE
using my little knowledge of tools such as mplayer / ffmpeg mencorer I'm able to save the video stream ...for example using ipcamera-for-android and its nanohttp server using on server side:
ffmpeg-i "http://{ip of android phone}:8080/live.flv" /my/server/path/stream.flv
However, can only be used in LAN,
I need that mobile connect server and not viceversa.
UPDATE 2
Some progress.. using this script on server side
#!/usr/bin/php5
<?php
$handle = fopen("stream.3gp","w");
$socket = stream_socket_server("tcp://192.168.0.102:9000", $errno, $errstr);
if ($socket)
{
echo "start listening\n";
while ( $conn = stream_socket_accept($socket, 180))
{
echo "phone connected\n";
while ($chunk = stream_socket_recvfrom($conn, 1500))
{
fwrite($handle,$chunk);
}
}
}
fclose($handle);
fclose($socket);
?>
however, 3gp file is not yet playable..
UPDATE 3
#!/usr/bin/php5
<?php
$socket = stream_socket_server("tcp://192.168.0.102:9000", $errno, $errstr);
$file = "saved.3gp";
$threegp_header = "\x00\x00\x00\x18\x66\x74\x79\x70\x33\x67\x70\x34\x00\x00\x03\x00\x33\x67\x70\x34\x33\x67\x70\x36";
$four_bytes = "\x00\x00\x00\x00";
if (!$socket) {
echo "$errstr ($errno)\n";
} else {
echo "server start listening\n";
while ( $conn = #stream_socket_accept($socket, 180))
{
echo "phone connected\n";
$handle = fopen($file,"w");
//mediaRecorder gives invalid stream header, so I replace it discarding first 32 byte, replacing with 28 good byte (standard 3gp header plus 4 empty bytes)
$discard = stream_get_contents($conn, 32);
fwrite($handle, $threegp_header);
fwrite($handle, $four_bytes);
//then confinue to write stream on file until phone stop streaming
while(!feof($conn))
{
fwrite($handle, stream_get_contents($conn, 1500));
}
echo "phone disconnected\n";
fclose($handle);
//then i had to update 3gp header (bytes 25 to 28) with the offset where moov atom starts
$handle = fopen($file,"c");
$output = shell_exec('grep -aobE "moov" '.$file);
$moov_pos = preg_replace('/moov:(\d+)/i', '\\1', $output);
$moov_pos_ex = strtoupper(str_pad(dechex($moov_pos - 24), 8, "0", STR_PAD_LEFT));
fwrite($handle, $threegp_header);
$tmp = '';
foreach(str_split($moov_pos_ex,2) as $hex)
{
$tmp .= pack('C*', hexdec($hex));
}
fwrite($handle, $tmp);
fclose($handle);
}
echo "phone disconnected\n";
}
#fclose($handle);
fclose($socket);
?>
after some experiments, this time vlc/mplayer seems that can play it.. still some problems with audio (but i think i've something wrong on android side)

You're probably going to want to use PHP's server socket functionality.
Here's a handy tutorial which goes through what you need to do in order to implement data streaming.

Depending on the incoming stream (protocol, etc.) you've ended up or want to end up using:
I'm not sure what you are willing to use/install on the LAMP, or what you'd prefer, but I know VLC can easily capture an incoming stream.
http://wiki.videolan.org/Documentation:Streaming_HowTo/Receive_and_Save_a_Stream
Of course, the Command Line only version of VLC is probably what you want. I've never done it, not sure how that works, I would hope it doesn't install a metric ton of extra packages. This is something to look at concerning possible concerns.

Related

Sending a hex command to Device using Php and COMPort (USB to RS232)

HI I have posted a lot of question regarding this but it is different from this one. It's like a continuation or rather other part question.
I have a card vending device (CVD), Now my laptop has no serial port so I used the USB to connect to the device.
My cable is, USB to RS232. Now I have downloaded or rather given to me by a friend an exe which send a command to device on a click of a button. But note that this is an exe only. No Source code.
I'm now creating my own program to send the data command to device on a button click as well.
Here's some facts:
1. Im using Php
2. I'm need to send a hex command of 42
3. is the command for cardout
Here's what I've got so far.
exec("mode COM15 BAUD=9600 PARITY=none data=8 stop=1 xon=off");
$fp = #fopen('COM2', "w+");
if (!$fp) {
echo "Not open";
} else {
sleep(3);
echo "Open";
$str = "01";
$str = pack("H*",$str);
fwrite($fp, $str);
fputs($fp, $str);
$str2 = "2A";
$str2 = pack("H*",$str2);
fwrite($fp, $str2);
fputs($fp,$str2);
$buff = fread($fp, 10);
echo ">> ".$buff." <<";
//fclose($fp);
}
But it is just returning Open>> <<
Try with a
print_r($buff);
It will show you the type of variable and all data it contains and maybe continue working on how to show that variable.

PHP - Trying to make a chatbot for my game server

Alright, so first of all, i'm still kind of a newbie in PHP.
I'm trying to make a chatbot that responds to the keyword "shut up" from people that tell that to the bot in my game server, but I can't seem to find a way how...
Here's the bot's original code...
<?php
$choosechar = "#43CC#1#35#ItsJustABot#%";
$fh = fopen('badtimetim.txt','r');
$word_array = array(fgets($fh));
$word = rand(0,58);
$lines = file("badtimetim.txt");
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
// echo($line);
// Connect to the AO Server
if (!($fp = fsockopen("127.0.0.1", "27017", $errno, $errstr, 15))) {
die("Failed to connect. Doesn't seem like the server is up anyway?");
}
// Set timeout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout.");
// Fetch first line of response and echo it
echo fgets($fp);
// Say line and repeat
fwrite($fp, $choosechar);
fwrite($fp, "#4D90#chat#(a)dolannormal#Dolan#dolannormal#".$lines[array_rand($lines)]."#jud#1#1#0#0#0#0#35#0#1#%");
sleep(120);
// Stuff
echo fgets($fp);
}
fclose($fh);
What i'm exactly trying to achieve here is when the bot detects this (asterisks should be wildcards or something):
#4D90#chat#*#*#*#shut up#*#*#*#*#*#*#*#*#*#*#%
I want the bot to send this data to the server in response using fwrite:
#4D90#chat#(a)dolanangry#Dolan#dolanangry#no#jud#1#1#0#0#0#0#35#0#1#%
How do I do this? Any help is appreciated, thanks.
EDIT: Forgot to mention, i'm using a .bat file to run PHP and the PHP code and not a website.
EDIT2: Made question more specific
$input_string_with_shut_up=$_POST['chat_msg']
$output=str_replace('%shut up%','#4D90#chat#(a)dolanangry#Dolan#dolanangry#no#jud#1#1#0#0#0#0#35#0#1#%',$input_string_with_shut_up)
echo $output;
This might do

Using UDP fsockopen to get info from file on gameserver

I got a file on a gameserver called "current_map.tmp".
This file contains a number depending on the current map.
What I need is to read that number.
This is what I got so far:
<?php
$server_ip = '213.239.207.85';
$server_port = 27960;
$server_timeout = 2;
$server_addr = "udp://" . $server_ip;
$fp = fsockopen($server_addr, $server_port, $errno, $errstr, $server_timeout);
socket_set_timeout ($fp, $server_timeout);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
$File = "current_map.tmp";
$filesize = filesize($File);
$handle = fopen($File, "r");
$map_id = fread($handle, $filesize);
fclose($handle);
}
fclose($fp);
?>
$fp returns "Resource id #2".
So that works.
Then there is nothing.
1) How do I know wich folder I connected to with $fp?
2) How can I read the content of this file?
$fp returns "Resource id #2". So that works.
No; this doesn't actually mean anything! Since UDP sockets are connectionless, there is no such thing as a UDP "connection"; calling fsockopen() only initializes sockets to prepare to send packets.
In any case, sending and receiving UDP packets does not allow you to access files on a remote server, unless that server has implemented a protocol to allow you to do so, and you are making use of that protocol. It certainly will not allow you to use fopen() to access remote files — this code is essentially just nonsense.

Reading data from fsockopen using fgets/fread hangs

Here is the code that I am using:
if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
echo "Could not connect to host";
$server_response = fread($fp, 256);
echo $server_response;
fwrite($fp, "C01 CAPABILITY"."\r\n");
while (!feof($fp)) {
echo fgets($fp, 256);
}
I get the first response:
OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15
but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc. but could not get it to work. I need to read all the data that the server sends and then proceed with other commands (I would be retrieving emails using imap).
Thanks
Your script is hanging in the while loop at the end. This is because you have used !feof() as the condition for the loop, and the server is not closing the connection. This means the feof() will always return false and the loop will continue forever.
This will not be problem when your write a full implementation, as you will be looking for response codes and can break out of the loop accordingly, for example:
<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) {
die("Could not connect to host");
}
// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");
// Fetch first line of response and echo it
echo fgets($fp);
// Send data to server
echo "Writing data...";
fwrite($fp, "C01 CAPABILITY\r\n");
echo " Done\r\n";
// Keep fetching lines until response code is correct
while ($line = fgets($fp)) {
echo $line;
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == 'C01') {
break;
}
}
echo "I've finished!";
Your script should be working. In fact, it is working.
See the results below on my pc when I ran your code:
* OK Gimap ready for requests from xx.xx.xx.xx l5if4585958ebb.20
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
C01 OK Thats all she wrote! l5if4585958ebb.20
Since gmail doesn't disconnect you. No end of file occurs. And the page loading simply times out.
In other words: Your script will just keep waiting and waiting until gmail does disconnect, which unfortunately happens after your page load has already timed out.

PHP Socket and proc_open

I am trying to create a inetd-like service for Windows in PHP for future use with my other application.
So all I can think of is to use Steam Server and proc_open to pipe the stream directly to the process (like inetd). Because on Windows there is no pcntl_fork(), and PHP doesn't support threading.
So far, here is my code. The inetdtest program is a simple program with single printf (written in C). But the problem is that when I connected to my server (via netcat), I got no response message.
<?php
define ('SERVICE_COMMAND', 'inetdtest');
define ('SERVICE_PORT', 35123);
function main() {
echo "Simple inetd starting...\n";
$socket = stream_socket_server('tcp://0.0.0.0:' . SERVICE_PORT, $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN);
if ($socket === false) {
echo "Can't bind to service port.\n";
echo "[$errno] $errstr";
die(1);
}
$processes = array();
while (true) {
$current = #stream_socket_accept($socket, 5, $host);
if ($current !== false) {
echo 'Incomming connection from client ' . $host . "\n";
echo "Lunching child process... ";
$io = array(
0 => $current,
1 => $current,
2 => array('file', 'stderr.log', 'a')
);
$proc = proc_open(SERVICE_COMMAND, $io, $pipes, NULL, NULL, array('bypass_shell'));
$status = proc_get_status($proc);
echo " DONE! PID : {$status['pid']}\n";
$processes[] = array($current, $proc);
}
foreach ($processes as $k=>$v) {
$status = proc_get_status($v[1]);
if (false === $status['running']) {
echo "Finalizing process {$status['pid']}... ";
fflush($v[0]);
fclose($v[0]);
proc_close($v[1]);
unset($processes[$k]);
echo "DONE!\n";
}
}
}
}
main();
The code justs works as it stands here (using cat as program and on linux), so the problem lies somewhere in the windows side of things.
For one thing, the option you are passing, to bypass the shell, should be given as
array('bypass_shell'=>true)
This may fix things already. The tricky part with these things, is that you're passing a socket fd to a process, which may or may not be expected to handle that properly. I don't know how these things are done in windows, but cutting cmd out of the equation can only help.
If it still doesn't work, you should create a loop which waits for data (either from network or child processes) and sends data from the network socket to the process pipe, and vice versa.

Categories