blob: 6c5fa3f859a67798328fc263f95b412e619f1a88 [file] [log] [blame]
nisseeed52bf2017-05-19 06:15:19 -07001/*
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "call/rtx_receive_stream.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
nisseeed52bf2017-05-19 06:15:19 -070015#include <utility>
16
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/rtp_rtcp/include/receive_statistics.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
nisseeed52bf2017-05-19 06:15:19 -070023
24namespace webrtc {
25
nisseca5706d2017-09-11 02:32:16 -070026RtxReceiveStream::RtxReceiveStream(
27 RtpPacketSinkInterface* media_sink,
28 std::map<int, int> associated_payload_types,
29 uint32_t media_ssrc,
30 ReceiveStatistics* rtp_receive_statistics /* = nullptr */)
nisseeed52bf2017-05-19 06:15:19 -070031 : media_sink_(media_sink),
nisse38644992017-08-30 04:16:40 -070032 associated_payload_types_(std::move(associated_payload_types)),
nisseca5706d2017-09-11 02:32:16 -070033 media_ssrc_(media_ssrc),
34 rtp_receive_statistics_(rtp_receive_statistics) {
Tommi13b9f812022-08-16 10:23:47 +020035 packet_checker_.Detach();
nisse38644992017-08-30 04:16:40 -070036 if (associated_payload_types_.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010037 RTC_LOG(LS_WARNING)
nisse38644992017-08-30 04:16:40 -070038 << "RtxReceiveStream created with empty payload type mapping.";
39 }
40}
nisseeed52bf2017-05-19 06:15:19 -070041
nisse76e62b02017-05-31 02:24:52 -070042RtxReceiveStream::~RtxReceiveStream() = default;
43
Tommi13b9f812022-08-16 10:23:47 +020044void 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
nisseeed52bf2017-05-19 06:15:19 -070050void RtxReceiveStream::OnRtpPacket(const RtpPacketReceived& rtx_packet) {
Tommi13b9f812022-08-16 10:23:47 +020051 RTC_DCHECK_RUN_ON(&packet_checker_);
nisseca5706d2017-09-11 02:32:16 -070052 if (rtp_receive_statistics_) {
Niels Möller1f3206c2018-09-14 08:26:32 +020053 rtp_receive_statistics_->OnRtpPacket(rtx_packet);
nisseca5706d2017-09-11 02:32:16 -070054 }
nisseeed52bf2017-05-19 06:15:19 -070055 rtc::ArrayView<const uint8_t> payload = rtx_packet.payload();
56
57 if (payload.size() < kRtxHeaderSize) {
58 return;
59 }
60
nisse38644992017-08-30 04:16:40 -070061 auto it = associated_payload_types_.find(rtx_packet.PayloadType());
62 if (it == associated_payload_types_.end()) {
Tommi13b9f812022-08-16 10:23:47 +020063 RTC_DLOG(LS_VERBOSE) << "Unknown payload type "
64 << static_cast<int>(rtx_packet.PayloadType())
65 << " on rtx ssrc " << rtx_packet.Ssrc();
nisseeed52bf2017-05-19 06:15:19 -070066 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);
nisse38644992017-08-30 04:16:40 -070074 media_packet.set_recovered(true);
Tommi2497a272021-05-05 12:33:00 +020075 media_packet.set_arrival_time(rtx_packet.arrival_time());
nisseeed52bf2017-05-19 06:15:19 -070076
77 // Skip the RTX header.
Yves Gerey665174f2018-06-19 15:03:05 +020078 rtc::ArrayView<const uint8_t> rtx_payload = payload.subview(kRtxHeaderSize);
nisseeed52bf2017-05-19 06:15:19 -070079
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