简体   繁体   中英

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)

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. T his is something to look at concerning possible concerns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM