From c1d2adebaeb1bf4b9d2f9351c63283a50a90115a Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Thu, 28 Apr 2022 14:13:09 +0800 Subject: [PATCH] =?UTF-8?q?rtmp=E6=8B=89=E6=B5=81=E7=9A=84=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=80=A7=E6=94=B9=E8=BF=9B=20(#1595)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复play指令的bug * 修改MSG_AGGREGATE的时间戳处理逻辑 这里参考了ffmpeg的方式来计算时间戳. * add bom --- src/Rtmp/RtmpPlayer.cpp | 5 +++-- src/Rtmp/RtmpProtocol.cpp | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index bc454b49..fa3ec496 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -214,7 +214,7 @@ inline void RtmpPlayer::send_createStream() { inline void RtmpPlayer::send_play() { AMFEncoder enc; - enc << "play" << ++_send_req_id << nullptr << _stream_id << (double) _stream_index; + enc << "play" << ++_send_req_id << nullptr << _stream_id << "-2000"; sendRequest(MSG_CMD, enc.data()); auto fun = [](AMFValue &val) { //TraceL << "play onStatus"; @@ -297,7 +297,8 @@ void RtmpPlayer::onCmd_onStatus(AMFDecoder &dec) { auto level = val["level"]; auto code = val["code"].as_string(); if (level.type() == AMF_STRING) { - if (level.as_string() != "status") { + // warning 不应该断开 + if (level.as_string() != "status" && level.as_string() != "warning") { throw std::runtime_error(StrPrinter << "onStatus 失败:" << level.as_string() << " " << code << endl); } } diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index 246734fc..5eedb291 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -766,6 +766,9 @@ void RtmpProtocol::handle_chunk(RtmpPacket::Ptr packet) { case MSG_AGGREGATE: { auto ptr = (uint8_t *) chunk_data.buffer.data(); auto ptr_tail = ptr + chunk_data.buffer.size(); + uint32_t latest_ts, timestamp; + timestamp = chunk_data.time_stamp; + bool first_message = true; while (ptr + 8 + 3 < ptr_tail) { auto type = *ptr; ptr += 1; @@ -781,12 +784,17 @@ void RtmpProtocol::handle_chunk(RtmpPacket::Ptr packet) { if (ptr + size > ptr_tail) { break; } + if (!first_message) { + timestamp += ts - latest_ts; + } + first_message = false; + latest_ts = ts; auto sub_packet_ptr = RtmpPacket::create(); auto &sub_packet = *sub_packet_ptr; sub_packet.buffer.assign((char *)ptr, size); sub_packet.type_id = type; sub_packet.body_size = size; - sub_packet.time_stamp = ts; + sub_packet.time_stamp = timestamp; sub_packet.stream_index = chunk_data.stream_index; sub_packet.chunk_id = chunk_data.chunk_id; handle_chunk(std::move(sub_packet_ptr));