从Android设备到LAMP服务器的视频流

Fel*_*uni 6 php sockets android video-streaming

从这一点开始:http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system 我正在尝试创建一个应用程序来保存从移动摄像头到远程服务器的视频流.(我在Android代码的google代码中找到了几个例子:ipcamera-for-android,spydroid-ipcamera等...)

我在这里和网络周围阅读了一些答案,但无法找到有关如何"读取"并在服务器端保存数据流的解决方案.

我对java的了解很差,所以我宁愿能够在PHP中创建服务器端脚本(使用服务器套接字或其他东西).有人可以帮忙吗?

UPDATE

使用我的小工具知识,如mplayer/ffmpeg mencorer,我能够保存视频流...例如使用ipcamera-for-android及其nanohttp服务器在服务器端使用:

ffmpeg-i "http://{ip of android phone}:8080/live.flv" /my/server/path/stream.flv
Run Code Online (Sandbox Code Playgroud)

但是,只能在局域网中使用,我需要那个移动连接服务器而不是反之亦然.

更新2

一些进展..在服务器端使用此脚本

#!/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);
?>
Run Code Online (Sandbox Code Playgroud)

但是,3gp文件还没有播放..

更新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);
?>
Run Code Online (Sandbox Code Playgroud)

经过一些实验,这次vlc/mplayer似乎可以播放它..仍然有一些音频问题(但我觉得我在android方面有问题)

hoh*_*ner 3

您可能想要使用 PHP 的服务器套接字功能

这是一个方便的教程,它介绍了实现数据流需要执行的操作。