Streaming video from MediaRecorder via LocalSocket

I am trying to send h264 / AAC video from Android MediaRecorder through a local socket. The goal is to send the video to the WOWZA server via RTMP or RTSP, but this gives me a lot of problems, and for now I am just trying to write data to a file from LocalServerSocket.

Here is the code. Sorry, this is not very clean, but I tested for many hours, and my project is now a mess.

In the "Camera" operation, setting the output file:

LocalSocket outSocket = new LocalSocket(); try { outSocket.connect(new LocalSocketAddress(LOCAL_SOCKET)); } catch (Exception e) { Log.i(LOG_TAG, "Error connecting socket: "+e); } mMediaRecorder.setOutputFile(outSocket.getFileDescriptor()); 

LocalServerSocket implementation:

 try { mLocalServerSocket = new LocalServerSocket(mName); } catch (Exception e) { Log.e(LOG_TAG, "Error creating server socket: "+e); return; } while (true) { File out = null; FileOutputStream fop = null; try { mLocalClientSocket = mLocalServerSocket.accept(); InputStream in = mLocalClientSocket.getInputStream(); out = new File(mContext.getExternalFilesDir(null), "testfile.mp4"); fop = new FileOutputStream(out); int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) >= 0) { Log.i(LOG_TAG, "Writing "+len+" bytes"); fop.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally{ try { fop.close(); mLocalClientSocket.close(); } catch (Exception e2) {} } } 

The problem is that the file resulting from this is not readable by any media player. Do you think this is due to a coding problem? This code should generate a binary if I understand well ?!

Thanks in advance, greetings.

+6
source share
3 answers

Ok, I found why files cannot play. The MP4 and 3GPP files have a header containing bytes:

ftyp3gp4 3gp43gp6 wide mdat

in hex

0000001866747970336770340000030033677034336770360000000877696465000392D86D6461740000

The 4 bytes before the 'mdat' tag represent the position of another 'moov' tag located at the end of the file. The position is usually set when recording is complete, but since MediaRecorder cannot search for sockets, it cannot set these bytes to the correct value in our case.

Now my problem is to find a way to make such a file more accessible, since it is designed to play it before recording is completed.

+2
source

You can try using mp4box to restructure your file. The moov box gives indexes for each sample of audio and video. If this is at the end of the file, this makes it difficult to stream.

This may help: http://boliston.wordpress.com/tag/moov-box/

Or this: mp4box -inter 0.5 some_file.mp4

(I have no opportunity to try now)

If you need this to work with your application, I do not know any steps to port mp4box to Android.

+1
source

I tried today to do the same, but mp4 is not very easy to transmit (as said before some parts are written at the end). I am not saying that this is impossible, but it seems, at least, quite difficult.

Such a workaround for the new Android APIs (4.3) could be the following:

  • Set the camera preview to SurfaceTexture: camera.setPreviewTexture
  • Record this texture using OpenGL and MediaCodex + Muxer

The disadvantage of this solution is that the size of the cameraโ€™s preview may be smaller than the size of the video. This means that depending on your device, you may not be able to record at the highest resolution. Hint: some cameras say that they do not support higher preview sizes, but they do, and you can try adjusting the camera to set the preview size to the video size. If you do, catch the RuntimeException for camera.setParameters, and if it doesn't work, use only the supported preview sizes.

Some links how to write from SurfaceTexture:

Bigflage: Great examples for MediaCodec content.

The VideoRecorder class from Lablet.

It may also be useful: spydroid-ipcamera transfers data from the MediaRecorder socket as RTP streams, but I have not found a way to transfer this to MediaCodec. (I was already stuck reading the correct NAL block sizes, like them ...)

0
source

Source: https://habr.com/ru/post/916452/


All Articles