如何去code从parcelfiledescriptor收到的H.264视频流 [英] How to decode the H.264 video stream received from parcelfiledescriptor

查看:700
本文介绍了如何去code从parcelfiledescriptor收到的H.264视频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的视频直播两款Android手机的Andr​​oid应用程序。我已经建立了这些设备之间的套接字连接。我在一台设备捕获视频和流发送到其他设备,但目前我只是想在接收端移动设备来保存和保存。我在一个设备中使用 MediaRecorder 录音,所以要传输到接收我,用 parcelfiledescriptor 对象通过设置中号数据

客户端code

  mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
    mediaRecorder.setVideoEn codeR(MediaRecorder.VideoEn coder.H263);
    mediaRecorder.setOutputFile(pfd.getFileDescriptor());

接收方code

  PFD = ParcelFileDescriptor.fromSocket(S);
  在的InputStream =新的FileInputStream(pfd.getFileDescriptor());
  DataInputStream以clientData =新DataInputStream所(中);
  的OutputStream newDatabase =新的FileOutputStream(文件);
  INT可用= in.available();
  字节[]缓冲区=新的字节[可]
  INT长;
                而((长度= in.read(缓冲液))大于0)
                {
                    newDatabase.write(缓冲液,0,长度);                }
                newDatabase.close();

被在接收器侧移动创建的视频文件,但它不能接收任何字节。那么,我已经脱code在接收端未来的流,因为发送的视频流是连接codeD录制时。所以,我怎么能去code被接收的信号流?我发现像MediaExtractor和媒体codeC的一些解决方案...但会与现场视频采集这项工作,而且我对测试 Android版本2.3.6姜饼
是否有可能取消从 code中的视频流媒体codeC 为2.3.6版或其他方法可用?


解决方案

  

被在接收器侧移动创建的视频文件,但它不能接收任何字节。


如果我理解你的权利,你没有得到从插座中的数据。这是一个单独的问题,它有无关的视频格式,解码或编码。

要调试您的插座,它可能有助于使用一个单独的应用程序,它只是转储收到的数据。一旦数据看起来不错,你可以去下一个步骤 - 对视频进行解码


问题的第二部分是视频格式。您使用的mp4,这不是用于流可用。 这里是关于格式结构的更多信息。可以使用的MP4视频记录到本地文件,然后将整个文件超过插座的地方转移,但真正的实时流媒体不能因为插座的非可搜索性来完成(如链接的文章中介绍)。有在文件,其作为一个目录,没有它的开始元数据的块时,previous数据只是垃圾。问题是,你可以组装目录仅之后你得到了所有内容。但在那一刻,数据已经通过socket发送,你不能在它的开头插入任何东西。

有几个walkarounds,但这只是为你将来的研究,我没有使用过他们没有。

的最直观的方式将是从的mp4切换到MPEG-TS,设计为流的容器。参加 MediaRecorder.OutputFormat 看一个隐藏的常量值为8。

另一种选择是将原始的H.264数据打包到的RTP / RTCP分组,而这又是被设计为流的协议。此外,您的应用程序将能够传输到支持此协议(例如VLC运行在PC)的设备。为了进一步reasearch,看看 Spydroid IP摄像机,这不正是的东西。

I'm creating an Android application of live video streaming between two android phone. I've already established a socket connection between these devices. I'm capturing video in one device and send the stream to other device but currently I just want to save in the receiver side mobile device and save it. I'm recording using MediaRecorder in one device , so to stream to the receiver I,m using parcelfiledescriptor object by setting the data

Client side code

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
    mediaRecorder.setOutputFile(pfd.getFileDescriptor());

Receiver side code

  pfd= ParcelFileDescriptor.fromSocket(s);
  InputStream in = new FileInputStream(pfd.getFileDescriptor()); 
  DataInputStream clientData = new DataInputStream(in);  
  OutputStream newDatabase = new FileOutputStream(file);
  int available=in.available();
  byte[] buffer = new byte[available];
  int length;


                while((length = in.read(buffer)) > 0)
                {
                    newDatabase.write(buffer, 0, length);

                }
                newDatabase.close();

The video file is being created on the receiver side mobile, but it's not able to receive any bytes. So Do I've to decode the coming stream on the receiver side since the video stream sent is encoded while recording. So how can I decode the stream that is received ? I found some solution like MediaExtractor and MediaCodec...but will this work with live video capturing and moreover I'm testing on android version 2.3.6 GingerBread Is it possible to decode the video stream from MediaCodec for version 2.3.6 or some other method is available ?

解决方案

The video file is being created on the receiver side mobile, but it's not able to receive any bytes.

If I understand you right, you are getting no data from the socket. That is a separate problem, which has nothing to do with the video format, decoding or encoding.

To debug your sockets, it may be helpful to use a separate application which just dumps the recieved data. Once the data looks fine, you can go to the next step - decoding the video.


Second part of the problem is the video format. You are using mp4, which is not usable for streaming. Here is more info about the format structure. You can use mp4 to record a video into a local file and then transfer the whole file over socket somewhere, but true realtime streaming cannot be done because of the non-seekable nature of the socket (as described in the linked article). There is a block of metadata at the beginning of the file, which acts as a "table of contents" and without it, the previous data are just junk. The problem is, you can assemble a "table of contents" only after you got all the contents. But at that moment, the data was already sent through the socket and you cannot insert anything at its beginning.

There are few walkarounds, but that's just for your future research and I haven't used them yet.

The most intuitive way would be to switch from mp4 to mpeg-ts, a container designed for streaming. Take a look at a hidden constant in MediaRecorder.OutputFormat with value 8.

Another option is to pack the raw H.264 data into RTP/RTCP packets, which is again a protocol designed for streaming. Also your application would be able to stream to any device that support this protocol (for example a PC running VLC). To further reasearch, take a look at Spydroid IP camera, which does exactly the thing.

这篇关于如何去code从parcelfiledescriptor收到的H.264视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆