• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   if (associated_payload_types_.empty()) {
36     RTC_LOG(LS_WARNING)
37         << "RtxReceiveStream created with empty payload type mapping.";
38   }
39 }
40 
41 RtxReceiveStream::~RtxReceiveStream() = default;
42 
OnRtpPacket(const RtpPacketReceived & rtx_packet)43 void RtxReceiveStream::OnRtpPacket(const RtpPacketReceived& rtx_packet) {
44   if (rtp_receive_statistics_) {
45     rtp_receive_statistics_->OnRtpPacket(rtx_packet);
46   }
47   rtc::ArrayView<const uint8_t> payload = rtx_packet.payload();
48 
49   if (payload.size() < kRtxHeaderSize) {
50     return;
51   }
52 
53   auto it = associated_payload_types_.find(rtx_packet.PayloadType());
54   if (it == associated_payload_types_.end()) {
55     RTC_LOG(LS_VERBOSE) << "Unknown payload type "
56                         << static_cast<int>(rtx_packet.PayloadType())
57                         << " on rtx ssrc " << rtx_packet.Ssrc();
58     return;
59   }
60   RtpPacketReceived media_packet;
61   media_packet.CopyHeaderFrom(rtx_packet);
62 
63   media_packet.SetSsrc(media_ssrc_);
64   media_packet.SetSequenceNumber((payload[0] << 8) + payload[1]);
65   media_packet.SetPayloadType(it->second);
66   media_packet.set_recovered(true);
67   media_packet.set_arrival_time_ms(rtx_packet.arrival_time_ms());
68 
69   // Skip the RTX header.
70   rtc::ArrayView<const uint8_t> rtx_payload = payload.subview(kRtxHeaderSize);
71 
72   uint8_t* media_payload = media_packet.AllocatePayload(rtx_payload.size());
73   RTC_DCHECK(media_payload != nullptr);
74 
75   memcpy(media_payload, rtx_payload.data(), rtx_payload.size());
76 
77   media_sink_->OnRtpPacket(media_packet);
78 }
79 
80 }  // namespace webrtc
81