Writing H.264 data to a file

1,778 views
Skip to first unread message

austin...@gmail.com

unread,
Mar 29, 2018, 11:33:59 PM3/29/18
to discuss-webrtc
Hi All
In a webrtc call with H.264 video codec, I need to write received RTP (H.264) data to a file that I can play using VLC / avplay / ffmpeg etc.

My understanding is, the received RTP packet contains RTP packetization data + H264 encoded data.
I need to remove RTP packetization data and then using ffmpeg av_write_* apis, I can write H.264 encoded data to a mp4 or mkv file.

I am trying to remove RTP packetization data. I referred to RFC 6184 (RTP Payload Format for H.264 Video).
Its quite big and complex also.

Can somebody explain the logic at high level how to strip the RTP packetization data.
OR is there any reference implementation available in C.

Best Regards,
Austin

Lorenzo Miniero

unread,
Mar 30, 2018, 4:06:23 AM3/30/18
to discuss-webrtc
You can have a look at the code we wrote for the recordings postprocessor in Janus. In Janus we record streams by basically dumping RTP packets as they are to a file in a structured way: the postprocessor then just goes through the packets in order, depacketizes RTP and saves the actual payload to file. For video formats we do make use of ffmpeg. The H.264 code (which saves to MP4) is available here: https://github.com/meetecho/janus-gateway/blob/master/postprocessing/pp-h264.c

L.

Austin Einter

unread,
Mar 31, 2018, 10:54:33 AM3/31/18
to discuss...@googlegroups.com

Hi 
I looked at pp-h264.c file,  specially janus_pp_h264_process function.
I did not understand complete logic.

So again I checked with RFC, read through relevant sections in rfc 6184 for packet formats.

Now I have somewhat working understanding on NALU, STAP-A, STAP-B, MTAP16, MTAP24, FU-A and FU-B packets.

At a higher level we have 

1. NAL Header as shown below
       +---------------+
       |0|1|2|3|4|5|6|7|
       +-+-+-+-+-+-+-+-+
       |F|NRI|  Type   |
       +---------------+

2. DON
3. NALU size
4. NALU TS offset
5. NALU header
6. NALU data


My question is, from an incoming RTP packet, I can get relevant data available (depending on packet type).

So when I call av_write_* apis of ffmpeg, do I need to pass only NALU data or I need to pass DON, NALU size, NALU TS offsets etc.

I understand this part will not be there in RFC, if somebody knows please this part help.


Thanks
Austin







--

---
You received this message because you are subscribed to a topic in the Google Groups "discuss-webrtc" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/discuss-webrtc/By57wV7IWaA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/87009e38-542d-42e0-9917-24873fb2c5be%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Harald Alvestrand

unread,
Mar 31, 2018, 11:30:38 AM3/31/18
to WebRTC-discuss
You probably need to check the H.264 standards for exact details. These are available for free download from the ITU.
Not easy reading, though!


--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/CANXt1k8HS4NNTF0w0w%2BUTvcuJfKRDoovO5gRjMw14Y1Lf4OpnQ%40mail.gmail.com.

Austin Einter

unread,
Apr 1, 2018, 9:32:10 PM4/1/18
to discuss...@googlegroups.com
Dear Harald Alvestrand
Thanks for response.

I am of different opinion. It is about what data should be written in container file (mp4 or mkv), not the internals of h264. So probably reading ITU may not be required.

Anyone who has written h264 data to a container file, please help.

Thanks
Austin






Austin Einter

unread,
Apr 2, 2018, 9:53:20 AM4/2/18
to discuss...@googlegroups.com
Dear All
On further debug, it looks from Chrome I received Single NAL units.

So for each incoming packet, I prepared buffer like 
1. first 3 bytes 0x00 0x00 0x01
2. Removed first byte from received data
3. Then received bytes I appended to buffer

Then that buffer I wrote to file using ffmpeg.

However this file is not playing in system.


Can somebody point out whats the wrong here....

Thanks
Austin




Sergio Garcia Murillo

unread,
Apr 3, 2018, 2:30:26 AM4/3/18
to discuss...@googlegroups.com
I think you should ask on the ffmpeg  list better than here..

Best regards
Sergio
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/CANXt1k_kexFBvP2AMiKy0BS5ncMjU56ADaZmye-i%2BgZNcdwJPA%40mail.gmail.com.

spr...@webrtc.org

unread,
Apr 3, 2018, 10:21:52 AM4/3/18
to discuss-webrtc
What are you trying to achieve?
If you're just debugging an issue you might want to hack the webrtc code and provide a file name to the VideoReceiveStream::EnableEncodedFrameRecording() method. This will generate an ivf file that can be played by eg vlc. Note that this won't work from within chrome unless you start with the --no-sandbox flag.
To unsubscribe from this group and all its topics, send an email to discuss-webrt...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.

--

---
You received this message because you are subscribed to a topic in the Google Groups "discuss-webrtc" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/discuss-webrtc/By57wV7IWaA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to discuss-webrt...@googlegroups.com.

Austin Einter

unread,
Apr 3, 2018, 12:49:18 PM4/3/18
to discuss...@googlegroups.com
Hello
My requirement is strait forward.

In a webrtc call, I am receiving RTP packets (H.264 payload).
I need to create a mp4 or a mkv file from received RTP data, which I should be able to play in ffmpeg or avplay.
I have already done similar stuff for VP8 case.

So I have H.264 encoded data (taken from RTP packet), I am using ffmpeg to write it to a mkv file.
Though I am getting output file, but it is not playing (tried with ffmpeg, avplay etc).

So I am trying to find out, if I have h.264 encoded data, how do I write that data to a file that can be played by media players.

Thanks
Austin 



To unsubscribe from this group and all its topics, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/ca19cd37-c1c1-4a60-8e2b-c1aa5baa0eea%40googlegroups.com.

Austin Einter

unread,
Apr 3, 2018, 10:40:34 PM4/3/18
to discuss...@googlegroups.com
I have overcome this issue by writing packets as
0x00 0x00 0x00 0x01 [SPS], 0x00 0x00 0x00 0x01 [PPS], 0x00 0x00 0x00 0x01 [Complete video frame,   0x00 0x00 0x00 0x01 [Complete video frame ...

But video quality is not good.

I can clearly see the bits per second is less during call, meaning chrome sending lesser data.
Is it a known issue .., 

Rgds,
Austin




--

---
You received this message because you are subscribed to a topic in the Google Groups "discuss-webrtc" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/discuss-webrtc/By57wV7IWaA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to discuss-webrtc+unsubscribe@googlegroups.com.

chertov

unread,
Apr 4, 2018, 2:23:05 AM4/4/18
to discuss-webrtc
Hi! Can you tell about how are you receiving raw RTP packets from libwertc? I can't find api for this( Thank you!

вторник, 3 апреля 2018 г., 19:49:18 UTC+3 пользователь Austin Einter написал:

Austin Einter

unread,
Apr 4, 2018, 3:56:13 AM4/4/18
to discuss...@googlegroups.com
Hi chertov
Ours is a proprietary webrtc solution, we have our own RTP stack.
Hence getting packet from RTP is not a difficult task.

I am not sure how to get RTP packet from  libwertc.

-Austin

To unsubscribe from this group and all its topics, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/4fbea9b7-04ce-4a10-a7e3-5a9377fe3954%40googlegroups.com.

Alexandre GOUAILLARD

unread,
Apr 4, 2018, 4:39:40 AM4/4/18
to discuss...@googlegroups.com
if your not using webrtc,
if you're not using SRTP,
if you refuse to follow our advices about reading the H264 bitstream spec, and corresponding RTP payload spec,
there is so much we can do to help you ....

Austin Einter

unread,
Apr 4, 2018, 4:50:39 AM4/4/18
to discuss...@googlegroups.com
Dear Alexandre GOUAILLARD
Are you referring to me ?

Rgds,
Austin

--

---
You received this message because you are subscribed to a topic in the Google Groups "discuss-webrtc" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/discuss-webrtc/By57wV7IWaA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to discuss-webrtc+unsubscribe@googlegroups.com.

Sergio Garcia Murillo

unread,
Apr 4, 2018, 5:29:25 AM4/4/18
to discuss...@googlegroups.com
Indeed, you need to convert the h264 stream to annex b before writing it to a video file.

The problem you are having now with the bits per second is not related with writing the video to a file. Check webrtc-internals for more info.

Best regards
Sergio
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages