xref: /aosp_15_r20/external/webrtc/call/rtx_receive_stream.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "call/rtx_receive_stream.h"
12 
13 #include <string.h>
14 
15 #include <utility>
16 
17 #include "api/array_view.h"
18 #include "modules/rtp_rtcp/include/receive_statistics.h"
19 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
20 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/logging.h"
23 
24 namespace webrtc {
25 
RtxReceiveStream(RtpPacketSinkInterface * media_sink,std::map<int,int> associated_payload_types,uint32_t media_ssrc,ReceiveStatistics * rtp_receive_statistics)26 RtxReceiveStream::RtxReceiveStream(
27     RtpPacketSinkInterface* media_sink,
28     std::map<int, int> associated_payload_types,
29     uint32_t media_ssrc,
30     ReceiveStatistics* rtp_receive_statistics /* = nullptr */)
31     : media_sink_(media_sink),
32       associated_payload_types_(std::move(associated_payload_types)),
33       media_ssrc_(media_ssrc),
34       rtp_receive_statistics_(rtp_receive_statistics) {
35   packet_checker_.Detach();
36   if (associated_payload_types_.empty()) {
37     RTC_LOG(LS_WARNING)
38         << "RtxReceiveStream created with empty payload type mapping.";
39   }
40 }
41 
42 RtxReceiveStream::~RtxReceiveStream() = default;
43 
SetAssociatedPayloadTypes(std::map<int,int> associated_payload_types)44 void RtxReceiveStream::SetAssociatedPayloadTypes(
45     std::map<int, int> associated_payload_types) {
46   RTC_DCHECK_RUN_ON(&packet_checker_);
47   associated_payload_types_ = std::move(associated_payload_types);
48 }
49 
OnRtpPacket(const RtpPacketReceived & rtx_packet)50 void RtxReceiveStream::OnRtpPacket(const RtpPacketReceived& rtx_packet) {
51   RTC_DCHECK_RUN_ON(&packet_checker_);
52   if (rtp_receive_statistics_) {
53     rtp_receive_statistics_->OnRtpPacket(rtx_packet);
54   }
55   rtc::ArrayView<const uint8_t> payload = rtx_packet.payload();
56 
57   if (payload.size() < kRtxHeaderSize) {
58     return;
59   }
60 
61   auto it = associated_payload_types_.find(rtx_packet.PayloadType());
62   if (it == associated_payload_types_.end()) {
63     RTC_DLOG(LS_VERBOSE) << "Unknown payload type "
64                          << static_cast<int>(rtx_packet.PayloadType())
65                          << " on rtx ssrc " << rtx_packet.Ssrc();
66     return;
67   }
68   RtpPacketReceived media_packet;
69   media_packet.CopyHeaderFrom(rtx_packet);
70 
71   media_packet.SetSsrc(media_ssrc_);
72   media_packet.SetSequenceNumber((payload[0] << 8) + payload[1]);
73   media_packet.SetPayloadType(it->second);
74   media_packet.set_recovered(true);
75   media_packet.set_arrival_time(rtx_packet.arrival_time());
76 
77   // Skip the RTX header.
78   rtc::ArrayView<const uint8_t> rtx_payload = payload.subview(kRtxHeaderSize);
79 
80   uint8_t* media_payload = media_packet.AllocatePayload(rtx_payload.size());
81   RTC_DCHECK(media_payload != nullptr);
82 
83   memcpy(media_payload, rtx_payload.data(), rtx_payload.size());
84 
85   media_sink_->OnRtpPacket(media_packet);
86 }
87 
88 }  // namespace webrtc
89