title: Video streaming from Android device to LAMP Server
body: <p>starting from this point: <a href="http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system" rel="nofollow">http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system</a>
  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..)</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>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:</p>
  
  <pre><code>ffmpeg-i "http://{ip of android phone}:8080/live.flv" /my/server/path/stream.flv
  </code></pre>
  
  <p>However, can only be used in LAN,
  I need that mobile connect server and not viceversa.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Some progress.. using this script on server side </p>
  
  <pre><code>#!/usr/bin/php5
  &lt;?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);
  ?&gt;
  </code></pre>
  
  <p>however, 3gp file is not yet playable..</p>
  
  <p><strong>UPDATE 3</strong></p>
  
  <pre><code>#!/usr/bin/php5
  &lt;?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);
  ?&gt;
  </code></pre>
  
  <p>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)</p>