Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video streaming from Android device to LAMP Server

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)

like image 328
Felice Ostuni Avatar asked Apr 09 '12 10:04

Felice Ostuni


1 Answers

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.

like image 83
hohner Avatar answered Oct 26 '22 20:10

hohner