ZLMediaKit / ZLMediaKit

WebRTC/RTSP/RTMP/HTTP/HLS/HTTP-FLV/WebSocket-FLV/HTTP-TS/HTTP-fMP4/WebSocket-TS/WebSocket-fMP4/GB28181/SRT server and client framework based on C++11
https://docs.zlmediakit.com
Other
13.96k stars 3.42k forks source link

API接口按需拉流问题反馈 #352

Closed llatecomers closed 4 years ago

llatecomers commented 4 years ago

项目中需要按需拉流秒内播放,在二次开发的时候遇到一个问题。 收到有人观看的时候,流发布调用以下接口

mk_media_init_video(rtsp_client->Media, 0,1920, 1080, 15);
mk_media_init_audio(rtsp_client->Media, 2, sample_rate, channel, sample_bit);
mk_media_set_on_close(rtsp_client->Media, rg_on_mk_media_close, rtsp_client);
mk_media_init_complete(rtsp_client->Media);

同时拉取摄像头的流作为发布流的数据。

按需拉流测试demo代码运行时间线:

rtsp_client->RtspUrl = rtsp://172.20.122.20:8554/live/stream (拉取live/stream流)
111111****************rg_rtsp_client_start 1592449533454
on_mk_media_not_found client info, local: 172.20.122.20:8554, peer: 172.20.122.20:42377
rtsp/__defaultVhost__/live/stream, url params:
rtsp_meida_not_found
22222SERVER_STREAME_SUBSCRIBE =  1592449533554  (收到有人观看请求 时间:1592449533554  )
mk_media_create success cc219010   (创建live/stream流)
rtsp_client->RtspUrl = rtsp://admin:123456@192.168.23.245/live/av0  (拉取摄像头流作为数据流)
333333SERVER_STREAME_SUBSCRIBE 1592449533556  (执行完上述两个步骤,时间:1592449533556)
mk_play_event err_code = 0 err_msg = rtsp play success
rg_rtsp_event_cb 1592449533645
CLIENT_STATUS_SUCCESS = 0
4444444**************test_mk_play_data 1592449533712 (拉取摄像头数据时间:1592449533712)
on_mk_media_changed 1 rtsp/__defaultVhost__/live/stream 1592449538571(live/stream流发布时间:1592449533712 ,耗时5秒)
on_mk_media_changed 1 rtmp/__defaultVhost__/live/stream 1592449538573
mk_play_event err_code = 0 err_msg = rtsp play success
rg_rtsp_event_cb 1592449538590 
CLIENT_STATUS_SUCCESS = 0
5555555**************test_mk_play_data 1592449538594 (拉到live/stream流 时间:1592449538594 )
5555555**************test_mk_play_data 1592449538594
5555555**************test_mk_play_data 1592449538594
5555555**************test_mk_play_data 1592449538594

后面查询代码发现MediaSink.cpp中,

//最多等待未初始化的Track 10秒,超时之后会忽略未初始化的Track
#define MAX_WAIT_MS_READY 5000

//如果添加Track,最多等待3秒
#define MAX_WAIT_MS_ADD_TRACK 100

发布时间与 MAX_WAIT_MS_READY 定义的时间一致。修改为2秒即2秒发布,修改成5秒即5秒发布。请问这块是否有bug?

xia-chu commented 4 years ago

没有,MAX_WAIT_MS_READY这个宏是控制忽略track的超时时间, 也就是视频如果在这个时间内未获取到sps pps,就会被忽略,音频同理。 你强行减少这个时间,只会导致track被忽略

llatecomers commented 4 years ago

但是摄像头帧率30帧,GOP间隔就50。 在收到摄像头流后,打印第一帧是PPS和SPS。我这边就是没有输入音频就是,不知道跟这个有没有关系。 rtsp_client->RtspUrl = rtsp://172.20.122.20:8554/live/stream 111111****rg_rtsp_client_start 1592452606061 on_mk_media_not_found client info, local: 172.20.122.20:8554, peer: 172.20.122.20:45381 rtsp/defaultVhost/live/stream, url params: rtsp_meida_not_found 22222SERVER_STREAME_SUBSCRIBE = 1592452606160 mk_media_create success 13416010 rtsp_client->RtspUrl = rtsp://admin:123456@192.168.23.245/live/av0 333333SERVER_STREAME_SUBSCRIBE 1592452606162 mk_play_event err_code = 0 err_msg = rtsp play success rg_rtsp_event_cb 1592452606259 CLIENT_STATUS_SUCCESS = 0 4444444**test_mk_play_data 1592452606330 0 0 0 1 67 (摄像头第一帧数据为sps pps) on_mk_media_changed 1 rtsp/defaultVhost/live/stream 1592452611166 on_mk_media_changed 1 rtmp/defaultVhost/live/stream 1592452611169 mk_play_event err_code = 0 err_msg = rtsp play success rg_rtsp_event_cb 1592452611186 CLIENT_STATUS_SUCCESS = 0 5555555**test_mk_play_data 1592452611191

llatecomers commented 4 years ago

如果我不调用这个mk_media_init_audio(rtsp_client->Media, 2, sample_rate, channel, sample_bit);那么流发布很快。

xia-chu commented 4 years ago

在on_mk_play_event(播放成功事件)回调中再根据mk_player_video_codecId和mk_player_audio_codecId判断有没有音频 有没有视频,有音频就mk_media_init_audio,有视频就mk_media_init_video