• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 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 "modules/audio_coding/neteq/red_payload_splitter.h"
12 
13 #include <assert.h>
14 #include <stddef.h>
15 
16 #include <cstdint>
17 #include <list>
18 #include <utility>
19 #include <vector>
20 
21 #include "modules/audio_coding/neteq/decoder_database.h"
22 #include "modules/audio_coding/neteq/packet.h"
23 #include "rtc_base/buffer.h"
24 #include "rtc_base/logging.h"
25 #include "rtc_base/numerics/safe_conversions.h"
26 
27 namespace webrtc {
28 
29 // The method loops through a list of packets {A, B, C, ...}. Each packet is
30 // split into its corresponding RED payloads, {A1, A2, ...}, which is
31 // temporarily held in the list |new_packets|.
32 // When the first packet in |packet_list| has been processed, the orignal packet
33 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes:
34 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all
35 // the original packets have been replaced by their split payloads.
SplitRed(PacketList * packet_list)36 bool RedPayloadSplitter::SplitRed(PacketList* packet_list) {
37   // Too many RED blocks indicates that something is wrong. Clamp it at some
38   // reasonable value.
39   const size_t kMaxRedBlocks = 32;
40   bool ret = true;
41   PacketList::iterator it = packet_list->begin();
42   while (it != packet_list->end()) {
43     const Packet& red_packet = *it;
44     assert(!red_packet.payload.empty());
45     const uint8_t* payload_ptr = red_packet.payload.data();
46 
47     // Read RED headers (according to RFC 2198):
48     //
49     //    0                   1                   2                   3
50     //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51     //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52     //   |F|   block PT  |  timestamp offset         |   block length    |
53     //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54     // Last RED header:
55     //    0 1 2 3 4 5 6 7
56     //   +-+-+-+-+-+-+-+-+
57     //   |0|   Block PT  |
58     //   +-+-+-+-+-+-+-+-+
59 
60     struct RedHeader {
61       uint8_t payload_type;
62       uint32_t timestamp;
63       size_t payload_length;
64     };
65 
66     std::vector<RedHeader> new_headers;
67     bool last_block = false;
68     size_t sum_length = 0;
69     while (!last_block) {
70       RedHeader new_header;
71       // Check the F bit. If F == 0, this was the last block.
72       last_block = ((*payload_ptr & 0x80) == 0);
73       // Bits 1 through 7 are payload type.
74       new_header.payload_type = payload_ptr[0] & 0x7F;
75       if (last_block) {
76         // No more header data to read.
77         ++sum_length;  // Account for RED header size of 1 byte.
78         new_header.timestamp = red_packet.timestamp;
79         new_header.payload_length = red_packet.payload.size() - sum_length;
80         payload_ptr += 1;  // Advance to first payload byte.
81       } else {
82         // Bits 8 through 21 are timestamp offset.
83         int timestamp_offset =
84             (payload_ptr[1] << 6) + ((payload_ptr[2] & 0xFC) >> 2);
85         new_header.timestamp = red_packet.timestamp - timestamp_offset;
86         // Bits 22 through 31 are payload length.
87         new_header.payload_length =
88             ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3];
89         payload_ptr += 4;  // Advance to next RED header.
90       }
91       sum_length += new_header.payload_length;
92       sum_length += 4;  // Account for RED header size of 4 bytes.
93       // Store in new list of packets.
94       new_headers.push_back(new_header);
95     }
96 
97     if (new_headers.size() <= kMaxRedBlocks) {
98       // Populate the new packets with payload data.
99       // |payload_ptr| now points at the first payload byte.
100       PacketList new_packets;  // An empty list to store the split packets in.
101       for (size_t i = 0; i != new_headers.size(); ++i) {
102         const auto& new_header = new_headers[i];
103         size_t payload_length = new_header.payload_length;
104         if (payload_ptr + payload_length >
105             red_packet.payload.data() + red_packet.payload.size()) {
106           // The block lengths in the RED headers do not match the overall
107           // packet length. Something is corrupt. Discard this and the remaining
108           // payloads from this packet.
109           RTC_LOG(LS_WARNING) << "SplitRed length mismatch";
110           ret = false;
111           break;
112         }
113 
114         Packet new_packet;
115         new_packet.timestamp = new_header.timestamp;
116         new_packet.payload_type = new_header.payload_type;
117         new_packet.sequence_number = red_packet.sequence_number;
118         new_packet.priority.red_level =
119             rtc::dchecked_cast<int>((new_headers.size() - 1) - i);
120         new_packet.payload.SetData(payload_ptr, payload_length);
121         new_packet.packet_info = RtpPacketInfo(
122             /*ssrc=*/red_packet.packet_info.ssrc(),
123             /*csrcs=*/std::vector<uint32_t>(),
124             /*rtp_timestamp=*/new_packet.timestamp,
125             /*audio_level=*/absl::nullopt,
126             /*absolute_capture_time=*/absl::nullopt,
127             /*receive_time_ms=*/red_packet.packet_info.receive_time_ms());
128         new_packets.push_front(std::move(new_packet));
129         payload_ptr += payload_length;
130       }
131       // Insert new packets into original list, before the element pointed to by
132       // iterator |it|.
133       packet_list->splice(it, std::move(new_packets));
134     } else {
135       RTC_LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size();
136       ret = false;
137     }
138     // Remove |it| from the packet list. This operation effectively moves the
139     // iterator |it| to the next packet in the list. Thus, we do not have to
140     // increment it manually.
141     it = packet_list->erase(it);
142   }
143   return ret;
144 }
145 
CheckRedPayloads(PacketList * packet_list,const DecoderDatabase & decoder_database)146 void RedPayloadSplitter::CheckRedPayloads(
147     PacketList* packet_list,
148     const DecoderDatabase& decoder_database) {
149   int main_payload_type = -1;
150   for (auto it = packet_list->begin(); it != packet_list->end(); /* */) {
151     uint8_t this_payload_type = it->payload_type;
152     if (decoder_database.IsRed(this_payload_type)) {
153       it = packet_list->erase(it);
154       continue;
155     }
156     if (!decoder_database.IsDtmf(this_payload_type) &&
157         !decoder_database.IsComfortNoise(this_payload_type)) {
158       if (main_payload_type == -1) {
159         // This is the first packet in the list which is non-DTMF non-CNG.
160         main_payload_type = this_payload_type;
161       } else {
162         if (this_payload_type != main_payload_type) {
163           // We do not allow redundant payloads of a different type.
164           // Remove |it| from the packet list. This operation effectively
165           // moves the iterator |it| to the next packet in the list. Thus, we
166           // do not have to increment it manually.
167           it = packet_list->erase(it);
168           continue;
169         }
170       }
171     }
172     ++it;
173   }
174 }
175 
176 }  // namespace webrtc
177