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

          Line data    Source code
       1             : /*
       2             :  *  Copyright (c) 2016 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/rtp_rtcp/source/rtcp_packet/sdes.h"
      12             : 
      13             : #include <utility>
      14             : 
      15             : #include "webrtc/base/checks.h"
      16             : #include "webrtc/base/logging.h"
      17             : #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
      18             : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
      19             : 
      20             : namespace webrtc {
      21             : namespace rtcp {
      22             : constexpr uint8_t Sdes::kPacketType;
      23             : // Source Description (SDES) (RFC 3550).
      24             : //
      25             : //         0                   1                   2                   3
      26             : //         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
      27             : //        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      28             : // header |V=2|P|    SC   |  PT=SDES=202  |             length            |
      29             : //        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
      30             : // chunk  |                          SSRC/CSRC_1                          |
      31             : //   1    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      32             : //        |                           SDES items                          |
      33             : //        |                              ...                              |
      34             : //        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
      35             : // chunk  |                          SSRC/CSRC_2                          |
      36             : //   2    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      37             : //        |                           SDES items                          |
      38             : //        |                              ...                              |
      39             : //        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
      40             : //
      41             : // Canonical End-Point Identifier SDES Item (CNAME)
      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             : //   |    CNAME=1    |     length    | user and domain name        ...
      47             : //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      48             : namespace {
      49             : const uint8_t kTerminatorTag = 0;
      50             : const uint8_t kCnameTag = 1;
      51             : 
      52           0 : size_t ChunkSize(const Sdes::Chunk& chunk) {
      53             :   // Chunk:
      54             :   // SSRC/CSRC (4 bytes) | CNAME=1 (1 byte) | length (1 byte) | cname | padding.
      55           0 :   size_t chunk_payload_size = 4 + 1 + 1 + chunk.cname.size();
      56           0 :   size_t padding_size = 4 - (chunk_payload_size % 4);  // Minimum 1.
      57           0 :   return chunk_payload_size + padding_size;
      58             : }
      59             : }  // namespace
      60             : 
      61           0 : Sdes::Sdes() : block_length_(RtcpPacket::kHeaderLength) {}
      62             : 
      63           0 : Sdes::~Sdes() {}
      64             : 
      65           0 : bool Sdes::Parse(const CommonHeader& packet) {
      66           0 :   RTC_DCHECK_EQ(packet.type(), kPacketType);
      67             : 
      68           0 :   uint8_t number_of_chunks = packet.count();
      69           0 :   std::vector<Chunk> chunks;  // Read chunk into temporary array, so that in
      70             :                               // case of an error original array would stay
      71             :                               // unchanged.
      72           0 :   size_t block_length = kHeaderLength;
      73             : 
      74           0 :   if (packet.payload_size_bytes() % 4 != 0) {
      75           0 :     LOG(LS_WARNING) << "Invalid payload size " << packet.payload_size_bytes()
      76             :                     << " bytes for a valid Sdes packet. Size should be"
      77           0 :                        " multiple of 4 bytes";
      78             :   }
      79             :   const uint8_t* const payload_end =
      80           0 :       packet.payload() + packet.payload_size_bytes();
      81           0 :   const uint8_t* looking_at = packet.payload();
      82           0 :   chunks.resize(number_of_chunks);
      83           0 :   for (size_t i = 0; i < number_of_chunks;) {
      84             :     // Each chunk consumes at least 8 bytes.
      85           0 :     if (payload_end - looking_at < 8) {
      86           0 :       LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1);
      87           0 :       return false;
      88             :     }
      89           0 :     chunks[i].ssrc = ByteReader<uint32_t>::ReadBigEndian(looking_at);
      90           0 :     looking_at += sizeof(uint32_t);
      91           0 :     bool cname_found = false;
      92             : 
      93             :     uint8_t item_type;
      94           0 :     while ((item_type = *(looking_at++)) != kTerminatorTag) {
      95           0 :       if (looking_at >= payload_end) {
      96           0 :         LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #"
      97           0 :                         << (i + 1) << ". Expected to find size of the text.";
      98           0 :         return false;
      99             :       }
     100           0 :       uint8_t item_length = *(looking_at++);
     101           0 :       const size_t kTerminatorSize = 1;
     102           0 :       if (looking_at + item_length + kTerminatorSize > payload_end) {
     103           0 :         LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #"
     104           0 :                         << (i + 1) << ". Expected to find text of size "
     105           0 :                         << item_length;
     106           0 :         return false;
     107             :       }
     108           0 :       if (item_type == kCnameTag) {
     109           0 :         if (cname_found) {
     110           0 :           LOG(LS_WARNING) << "Found extra CNAME for same ssrc in chunk #"
     111           0 :                           << (i + 1);
     112           0 :           return false;
     113             :         }
     114           0 :         cname_found = true;
     115           0 :         chunks[i].cname.assign(reinterpret_cast<const char*>(looking_at),
     116           0 :                                item_length);
     117             :       }
     118           0 :       looking_at += item_length;
     119             :     }
     120           0 :     if (cname_found) {
     121             :       // block_length calculates length of the packet that would be generated by
     122             :       // Build/Create functions. Adjust it same way WithCName function does.
     123           0 :       block_length += ChunkSize(chunks[i]);
     124           0 :       ++i;
     125             :     } else {
     126             :       // RFC states CNAME item is mandatory.
     127             :       // But same time it allows chunk without items.
     128             :       // So while parsing, ignore all chunks without cname,
     129             :       // but do not fail the parse.
     130           0 :       LOG(LS_WARNING) << "CNAME not found for ssrc " << chunks[i].ssrc;
     131           0 :       --number_of_chunks;
     132           0 :       chunks.resize(number_of_chunks);
     133             :     }
     134             :     // Adjust to 32bit boundary.
     135           0 :     looking_at += (payload_end - looking_at) % 4;
     136             :   }
     137             : 
     138           0 :   chunks_ = std::move(chunks);
     139           0 :   block_length_ = block_length;
     140           0 :   return true;
     141             : }
     142             : 
     143           0 : bool Sdes::AddCName(uint32_t ssrc, std::string cname) {
     144           0 :   RTC_DCHECK_LE(cname.length(), 0xffu);
     145           0 :   if (chunks_.size() >= kMaxNumberOfChunks) {
     146           0 :     LOG(LS_WARNING) << "Max SDES chunks reached.";
     147           0 :     return false;
     148             :   }
     149           0 :   Chunk chunk;
     150           0 :   chunk.ssrc = ssrc;
     151           0 :   chunk.cname = std::move(cname);
     152           0 :   chunks_.push_back(chunk);
     153           0 :   block_length_ += ChunkSize(chunk);
     154           0 :   return true;
     155             : }
     156             : 
     157           0 : bool Sdes::Create(uint8_t* packet,
     158             :                   size_t* index,
     159             :                   size_t max_length,
     160             :                   RtcpPacket::PacketReadyCallback* callback) const {
     161           0 :   while (*index + BlockLength() > max_length) {
     162           0 :     if (!OnBufferFull(packet, index, callback))
     163           0 :       return false;
     164             :   }
     165           0 :   const size_t index_end = *index + BlockLength();
     166           0 :   CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index);
     167             : 
     168           0 :   for (const Sdes::Chunk& chunk : chunks_) {
     169           0 :     ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], chunk.ssrc);
     170           0 :     ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 4], kCnameTag);
     171           0 :     ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 5],
     172           0 :                                         chunk.cname.size());
     173           0 :     memcpy(&packet[*index + 6], chunk.cname.data(), chunk.cname.size());
     174           0 :     *index += (6 + chunk.cname.size());
     175             : 
     176             :     // In each chunk, the list of items must be terminated by one or more null
     177             :     // octets. The next chunk must start on a 32-bit boundary.
     178             :     // CNAME (1 byte) | length (1 byte) | name | padding.
     179           0 :     size_t padding_size = 4 - ((6 + chunk.cname.size()) % 4);
     180           0 :     const int kPadding = 0;
     181           0 :     memset(packet + *index, kPadding, padding_size);
     182           0 :     *index += padding_size;
     183             :   }
     184             : 
     185           0 :   RTC_CHECK_EQ(*index, index_end);
     186           0 :   return true;
     187             : }
     188             : }  // namespace rtcp
     189             : }  // namespace webrtc

Generated by: LCOV version 1.13