通过LocalSocket流从MediaRecorder视频视频、LocalSocket、MediaRecorder

2023-09-05 04:06:01 作者:負袮、叕哬妨

我想从Android的MediaRecorder通过本地的Socket发送H264 / AAC视频。我们的目标是到视频发送到WOWZA服务器throught RTMP或RTSP,但它给了我很多的麻烦,现在我只是想将数据写入到从LocalServerSocket的文件。

I'm trying to send h264/AAC video from Android's MediaRecorder through a local Socket. The goal is to to send video to a WOWZA server throught RTMP or RTSP, but it's giving me a lot of trouble and for now I'm just trying to write the data to a file from the LocalServerSocket.

下面是一些code。很抱歉,这不是真的干净,但我花了几个小时测试的很多事情,我的项目是一个烂摊子,现在。

Here is some code. Sorry it's not really clean, but I spent hours testing many things and my project is a mess right now.

在摄像头活动,输出文件设置:

In the Camera activity, the output file setup:

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实现:

The 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) {}
    }
}

的问题是,由此产生的文件不是由任何媒体播放器可读的。你认为这是因为编码的问题?这code应生成的二进制文件,如果我的理解呢?!

The problem is that the file resulting from this is not readable by any media player. Do you think this is because of an encoding issue? This code should generate a binary file if I understand well?!

在此先感谢,干杯。

推荐答案

好吧,我发现为什么文件无法播放。在MP4和3GPP文件,还有包含字节的报头:

Ok, I've found why the files couldn't play. In MP4 and 3GPP files, there is a header containing the bytes:

ftyp3gp4 3gp43gp6宽MDAT

ftyp3gp4 3gp43gp6 wide mdat

在HEX

0000001866747970336770340000030033677034336770360000000877696465000392D86D6461740000

0000001866747970336770340000030033677034336770360000000877696465000392D86D6461740000

在MDAT标签重新present坐落在文件末尾另一种MOOV标签的位置之前的4个字节。位置通常被设定在记录结束了,但作为MediaRecorder不能谋求插座,它可以不设置这些字节在我们的情况下,正确的值。

The 4 bytes before the 'mdat' tag represent the position of another 'moov' tag situated at the end of the file. The position is usually set when the recording is over, but as MediaRecorder can't seek sockets, it can't set these bytes to the correct value in our case.

我现在的问题是要找到一种方法,使这样的文件可流,因为它涉及到它要播放的记录结束之前。

My problem now is to find a way to make such a file streamable, as it involves for it to be played before the recording is over.