2

I have a problem with my streamed video from Android to my php server. I can't play my video whatever player i use because the moov atom is missing. I followed the tutorial of Mattakis to stream my video and a little php script to re-encode the header of the received 3gp video.

Android side:

s = new Socket("192.168.1.5", 6000);
        ParcelFileDescriptor pfd= ParcelFileDescriptor.fromSocket(s);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setVideoEncodingBitRate(512 * 1000);
        recorder.setVideoFrameRate(30);
        recorder.setVideoSize(320, 240);
        recorder.setOutputFile(pfd.getFileDescriptor());

Php Script : I used the script in the third Update of this link

Code :

 #!/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);
 ?>

i tried also to re-encode the header manually after saving the whole file but it doesn't work.

Community
  • 1
  • 1
113408
  • 3,364
  • 6
  • 27
  • 54
  • Please show `var_dump($output);` immediately after the line `$output = shell_exec('grep -aobE "moov" '.$file);` – DaveRandom Jul 16 '12 at 11:28
  • the result of `var_dump($output)` is : string(12) "179788:moov" – 113408 Jul 16 '12 at 13:17
  • Right well in that case $moov_pos will be empty, so you will be placing an empty offset (`\x00\x00\x00\x00`) back into the file. How large is the file you are recieving? Is there any way you can reasonably post a hex dump of it? – DaveRandom Jul 16 '12 at 13:42
  • the file size is at least 150ko the hex dump is large. i don't understand what is wrong with the code, it works in the other topic. – 113408 Jul 16 '12 at 13:54
  • It's not the code (necessarily) but the file. The code above is expecting `moov` to have at least 1 digit after it, but you can see from `var_dump()` it doesn't. It's possible the digits *before* the `moov` are the ones required (although I doubt it), in which case changing the regex to `/(\d+):moov/i` would fix it. But like I say, I doubt this is the case - but I'm having trouble laying my hands on a spec that explains exactly what all this actually *means* in terms of how the header should be layed out. I suspect using a static set of 28+4 bytes for the header in every 3GP file is wrong. – DaveRandom Jul 16 '12 at 13:59
  • Can i just edit the Hex manually and replace the moov ? – 113408 Jul 16 '12 at 16:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13985/discussion-between-hamza-karmouda-and-daverandom) – 113408 Jul 17 '12 at 09:51
  • Have you done something workable finally ? – Gilles Quénot Sep 09 '15 at 17:28
  • Not really, moved on to another project. But the problem if I remember well was from the phone. Not sure though – 113408 Sep 10 '15 at 02:50

0 Answers0