FFmpeg broadcast a black screen problem analysis solution

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/myvest/article/details/91361005

background

FFmpeg is a very good open-source framework, using its secondary development and adaptation process, will inevitably encounter various problems.
The record is based on FFmpeg development player, live black screen problem analysis of ideas and solutions. This article does not involve a detailed procedure code, mainly to provide analysis of ideas.
Questions are as follows:
in the course of the live broadcast, a playback source from some black screen after sowing, but there are sound, live broadcasts based on HLS.

analysis of idea

For the analysis of such problems, and the need to acquire network packets corresponding log, in order to better position the cause of the problem.
Analysis of the following steps:
1. Make sure the stream format, to ensure the normal flow itself, support video encoding.
Can dump flow, filtration or directly from the network packet in response to a media stream in the code, I prefer second.
Flow can play with VLC PC side, clear stream itself is not the problem. While viewing the codec information, confirmed that the video formats we support (Tools -> codec information, screenshots are only examples, not the problem of flow)
Here Insert Picture Description
2, see FFmpeg resolve to the correct audio and video formats.
After av_find_stream_info we can function, use av_dump_format function will dump out the audio and video information to confirm whether the FFmpeg parsed correctly.
Can be seen, audio (PID [0xb3]), the corresponding information has been parsed Audio: mp2, 48000 Hz, stereo , s16, 192 kb / s
and another data (PID [0xb2]), the specific information is not clear .

03-28 09:17:55.073 I/ffmpeg( 3431):   Duration: 
03-28 09:17:55.073 I/ffmpeg( 3431): N/A
03-28 09:17:55.073 I/ffmpeg( 3431): , start: 
03-28 09:17:55.073 I/ffmpeg( 3431): 75309.284411
03-28 09:17:55.073 I/ffmpeg( 3431): , bitrate: 
03-28 09:17:55.073 I/ffmpeg( 3431): N/A
03-28 09:17:55.073 I/ffmpeg( 3431): 
03-28 09:17:55.073 I/ffmpeg( 3431):   Program 5 
03-28 09:17:55.073 I/ffmpeg( 3431):     Metadata:
03-28 09:17:55.073 I/ffmpeg( 3431):       service_name    : CCTV2 HD
03-28 09:17:55.073 I/ffmpeg( 3431):   No Program
03-28 09:17:55.083 I/ffmpeg( 3431):     Stream #0.0
03-28 09:17:55.083 I/ffmpeg( 3431): [0xb2]
03-28 09:17:55.083 I/ffmpeg( 3431): : Data: [0][0][0][0] / 0x0000
03-28 09:17:55.083 I/ffmpeg( 3431): 
03-28 09:17:55.083 I/ffmpeg( 3431):     Stream #0.1
03-28 09:17:55.083 I/ffmpeg( 3431): [0xb3]
03-28 09:17:55.083 I/ffmpeg( 3431): : Audio: mp2, 48000 Hz, stereo, s16, 192 kb/s
03-28 09:17:55.083 I/ffmpeg( 3431): 

3, using analytical tools to parse and compare TS stream normal play, you can see during normal playback dump information, video information has been parsed, TS analytical tool, PID [0xb2] that is, video PID. It is almost certain that no parsing the PMT FFmpeg correct.

03-28 09:18:51.983 I/ffmpeg( 3431): N/A
03-28 09:18:51.983 I/ffmpeg( 3431): , start: 
03-28 09:18:51.983 I/ffmpeg( 3431): 75369.524411
03-28 09:18:51.983 I/ffmpeg( 3431): , bitrate: 
03-28 09:18:51.983 I/ffmpeg( 3431): N/A
03-28 09:18:51.983 I/ffmpeg( 3431): 
03-28 09:18:51.983 I/ffmpeg( 3431):   Program 5 
03-28 09:18:51.983 I/ffmpeg( 3431):     Stream #0.0
03-28 09:18:51.983 I/ffmpeg( 3431): [0xb2]
03-28 09:18:51.983 I/ffmpeg( 3431): : Video: h264 (High), 1920x1088
03-28 09:18:51.983 I/ffmpeg( 3431): , 90k tbr
03-28 09:18:51.983 I/ffmpeg( 3431): , 90k tbn
03-28 09:18:51.983 I/ffmpeg( 3431): 
03-28 09:18:51.983 I/ffmpeg( 3431):     Stream #0.1
03-28 09:18:51.983 I/ffmpeg( 3431): [0xb3]
03-28 09:18:51.983 I/ffmpeg( 3431): (eng)
03-28 09:18:51.983 I/ffmpeg( 3431): : Audio: mp2, 48000 Hz, stereo, s16, 192 kb/s

4, in conjunction with code analysis.
For TS stream parsing PAT / PMT tables, that is, the demux process, the flow in the avformat_open_input.
View mpegts corresponding demux module. For avformat_open_input process, generally call read_head function, namely mpegts_read_header.

AVInputFormat ff_mpegts_demuxer = {
    "mpegts",
    NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
    sizeof(MpegTSContext),
    mpegts_probe,
    mpegts_read_header,
    mpegts_read_packet,
    mpegts_read_close,
    .read_seek = mpegts_read_seek,
    .read_timestamp = mpegts_get_dts,
    .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT| AVFMT_GENERIC_INDEX,
};

It can be seen from sowing to accelerate the speed, probe_size set of small (256kB), handle_packets function according to probe_size set to read a certain number of ts packet parsing, which resulted in a situation that may not read PAT table.

static int mpegts_read_header(AVFormatContext *s,
                              AVFormatParameters *ap)
{
   ....省略
            //find PMT, would stop parse
			 probe_size =256 * 1024;//fast play
			 handle_packets(ts, probe_size / ts->raw_packet_size);
   ....省略
        }

5, using EasyICE analysis TS slices.
256kB corresponding offset 0x40000, the first take m3u8 downloaded slicing (note that the first fragmentation downloaded, not the first in the list, as not necessarily start from scratch).
Use EasyICE analysis, can be seen, the PAT table offset 0x6C3A8, greater than 0x40000, so demux stage, and not the acquired PAT / PMT tables, it can not resolve the correct video format.
Why can resolve the audio out of it, because there av_find_stream_info pop process, you can parse out the audio format, but the video in this case can not be fully resolved out.
Here Insert Picture Description
Summary: The problem is that FFmpeg not read PAT / PMT, resulting in a parse error. Air flow may be due to large, in this case, the media data is large, PSI / SI information in the proportion may be small, too small to read data on the potential for error in the TS.

Solutions

Methods to resolve this problem in two ways:
1, from the client to resolve, namely to increase the size of the data parsing stage of demux, but in fact no guarantee that a larger stream or PSI / SI distributed under too sparse normal, and the network than under poor circumstances, from sowing speed will have some impact.
2, solve the CDN, live streaming media server protocol for HLS, may be the beginning of each slice is inserted PSI / SI information, which can ensure that the client will be able to get the right.

Guess you like

Origin blog.csdn.net/myvest/article/details/91361005
Recommended