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.
Simon source share