LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/modules/audio_coding/neteq - red_payload_splitter.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 66 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13