mwang7
October 9, 2024, 1:20pm
1
Hi, I’m trying to build an app that record the desktop and later the audio and stream it over the network.
Basically you can break it down like so :
Grab the desktop frames with DXGI desktop duplication API in BGRA format
Encode with NvEnc to H264
Packetize into RTP packets with FFmpeg
Send to the network
I currently am able to stream with a good image quality.
I used multiple samples i found to understand and mix it to do what I wanted,
The issue is that there is a time delay issue and I believe it could come from the encoder’s parameters related to timestamps
For test purpuse here i stream locally using VLC :
As you can see in there, on the difference between the desktop and the stream is significant.
I tried to look into the documentation and it’s saying this :
I tried to look into my code and I was using the default settings set by the sample, modified a few parameters like GOP length. enablePTD in NV_ENC_INITIALIZE_PARAMS and , change the delay to 1.5 sec and defining all the frames as IDR (stilll too much)
Currently i’m having some confusions :
Is the parameter enablePTD a way to not manage ourselves if the frame is a IDR, P-frame or I-frame and let the NVENCODE API decide ?
In my case of where i’m streaming should I enablePTD ?
If i’m setting enablePTD to true, the stream is slow and end up having a 5-10-15 sec delay over the time
If i’m setting enablePTD to false and set these parameters in the Pic params :
picParams.inputTimeStamp = timestamp;
picParams.pictureType = NV_ENC_PIC_TYPE_IDR;
picParams.encodePicFlags |= NV_ENC_PIC_FLAG_FORCEIDR;
end up creating a delay too but since it tries to recaliber itself it’s less but still too much (~3 sec)
mwang7
October 9, 2024, 2:16pm
2
Also tried to set the IDR and I frames and P frames manually just to test it out,
if (m_iToSend == 0)
{
picParams.pictureType = NV_ENC_PIC_TYPE_IDR; // Start with IDR
std::cout << "IDR" << std::endl;
}
else if (m_iToSend % 30 == 0) // every 30 frames, use I frame
{
picParams.pictureType = NV_ENC_PIC_TYPE_I; // Use I-frame
}
else
{
picParams.pictureType = NV_ENC_PIC_TYPE_P; // Use P-frame for others
}
But it streams a black screen on VLC and it outputs this error on ffplay without displaying anything :
./ffplay.exe -protocol_whitelist file,crypto,data,rtp,udp -loglevel debug -i stream.sdp
ffplay version 2024-06-27-git-9a3bc59a38-essentials_build-www.gyan.dev Copyright (c) 2003-2024 the F
Fmpeg developers
built with gcc 13.2.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autod
etect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzli
b --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --en
able-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enab
le-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enabl
e-libfribidi --enable-libharfbuzz --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf
–enable-cuda-llvm --enable-cuvid --enable-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcod
ec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-libgme --enable-libopenmpt
–enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-l
ibgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-libr
ubberband
libavutil 59. 26.100 / 59. 26.100
libavcodec 61. 8.100 / 61. 8.100
libavformat 61. 4.100 / 61. 4.100
libavdevice 61. 2.100 / 61. 2.100
libavfilter 10. 2.102 / 10. 2.102
libswscale 8. 2.100 / 8. 2.100
libswresample 5. 2.100 / 5. 2.100
libpostproc 58. 2.100 / 58. 2.100
Initialized direct3d renderer.
[AVFormatContext @ 000001e52a2f33c0] Opening ‘stream.sdp’ for reading
[sdp @ 000001e52a2f33c0] Format sdp probed with size=2048 and score=50
[sdp @ 000001e52a2f33c0] video codec set to: h264
[sdp @ 000001e52a2f33c0] RTP Packetization Mode: 1
[sdp @ 000001e52a2f33c0] Extradata set to 000001e52a2f3980 (size: 21)
[udp @ 000001e52a2efe00] end receive buffer size reported is 393216
[udp @ 000001e52a2fc2c0] end receive buffer size reported is 393216
[sdp @ 000001e52a2f33c0] setting jitter buffer size to 500
[sdp @ 000001e52a2f33c0] Before avformat_find_stream_info() pos: 225 bytes read:225 seeks:0 nb_strea
ms:1
[h264 @ 000001e52a2fbd00] nal_unit_type: 7(SPS), nal_ref_idc: 3
[h264 @ 000001e52a2fbd00] nal_unit_type: 8(PPS), nal_ref_idc: 3
[h264 @ 000001e52a2fbd00] nal_unit_type: 7(SPS), nal_ref_idc: 3
[h264 @ 000001e52a2fbd00] nal_unit_type: 8(PPS), nal_ref_idc: 3
[h264 @ 000001e52a2fbd00] nal_unit_type: 5(IDR), nal_ref_idc: 0
Last message repeated 3 times
[h264 @ 000001e52a2fbd00] deblocking filter parameters 0 -7 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] Format yuv420p chosen by get_format().
[h264 @ 000001e52a2fbd00] Reinit context to 320x240, pix_fmt: yuv420p
[h264 @ 000001e52a2fbd00] first_mb_in_slice overflow
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] deblocking filter parameters 0 -7 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] deblocking_filter_idc 14 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] nal_unit_type: 5(IDR), nal_ref_idc: 0
Last message repeated 3 times
[h264 @ 000001e52a2fbd00] deblocking_filter_idc 14 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] deblocking filter parameters 0 -7 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] deblocking_filter_idc 14 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] first_mb_in_slice overflow= 0B
[h264 @ 000001e52a2fbd00] decode_slice_header error
[sdp @ 000001e52a2f33c0] jitter buffer full 0KB sq= 0B
[sdp @ 000001e52a2f33c0] RTP: missed 28 packets
[h264 @ 000001e52a2fbd00] nal_unit_type: 5(IDR), nal_ref_idc: 0
Last message repeated 3 times
[h264 @ 000001e52a2fbd00] deblocking filter parameters 0 -7 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] first_mb_in_slice overflow
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] deblocking filter parameters 0 -7 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] deblocking_filter_idc 14 out of range
[h264 @ 000001e52a2fbd00] decode_slice_header error
[h264 @ 000001e52a2fbd00] nal_unit_type: 5(IDR), nal_ref_idc: 0
Last message repeated 3 times
mwang7
October 10, 2024, 1:41pm
3
Ok found the reason why the delay, i was from another part, where in FFmpeg I packetize into rtp packets I’ve set 30 fps in initialization of FFmpeg whereas i’ve set 60 fps in nvEnc initialization.