19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Unit tests for PayloadSplitter class.
129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
13e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <assert.h>
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <utility>  // pair
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1949d62206ededc5905d6121d42fdcce8ed665b2c0kjellander@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
20e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
21e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/packet.h"
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/system_wrappers/interface/scoped_ptr.h"
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgusing ::testing::Return;
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgusing ::testing::ReturnNull;
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstatic const int kRedPayloadType = 100;
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstatic const int kPayloadLength = 10;
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstatic const int kRedHeaderLength = 4;  // 4 bytes RED header.
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstatic const uint16_t kSequenceNumber = 0;
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstatic const uint32_t kBaseTimestamp = 0x12345678;
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// RED headers (according to RFC 2198):
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//    0                   1                   2                   3
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//    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
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//   |F|   block PT  |  timestamp offset         |   block length    |
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Last RED header:
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//    0 1 2 3 4 5 6 7
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//   +-+-+-+-+-+-+-+-+
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//   |0|   Block PT  |
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//   +-+-+-+-+-+-+-+-+
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Creates a RED packet, with |num_payloads| payloads, with payload types given
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// by the values in array |payload_types| (which must be of length
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// |num_payloads|). Each redundant payload is |timestamp_offset| samples
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// "behind" the the previous payload.
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgPacket* CreateRedPayload(int num_payloads,
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                         uint8_t* payload_types,
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                         int timestamp_offset) {
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = new Packet;
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.payloadType = kRedPayloadType;
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.timestamp = kBaseTimestamp;
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.sequenceNumber = kSequenceNumber;
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->payload_length = (kPayloadLength + 1) +
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      (num_payloads - 1) * (kPayloadLength + kRedHeaderLength);
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t* payload = new uint8_t[packet->payload_length];
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t* payload_ptr = payload;
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i < num_payloads; ++i) {
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Write the RED headers.
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (i == num_payloads - 1) {
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Special case for last payload.
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      *payload_ptr = payload_types[i] & 0x7F;  // F = 0;
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      ++payload_ptr;
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      break;
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *payload_ptr = payload_types[i] & 0x7F;
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Not the last block; set F = 1.
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *payload_ptr |= 0x80;
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++payload_ptr;
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int this_offset = (num_payloads - i - 1) * timestamp_offset;
779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *payload_ptr = this_offset >> 6;
789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++payload_ptr;
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    assert(kPayloadLength <= 1023);  // Max length described by 10 bits.
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *payload_ptr = ((this_offset & 0x3F) << 2) | (kPayloadLength >> 8);
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++payload_ptr;
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    *payload_ptr = kPayloadLength & 0xFF;
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++payload_ptr;
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i < num_payloads; ++i) {
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Write |i| to all bytes in each payload.
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    memset(payload_ptr, i, kPayloadLength);
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    payload_ptr += kPayloadLength;
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->payload = payload;
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return packet;
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
94835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
95835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// A possible Opus packet that contains FEC is the following.
96835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// The frame is 20 ms in duration.
97835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org//
98835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// 0                   1                   2                   3
99835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// 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
100835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// |0|0|0|0|1|0|0|0|x|1|x|x|x|x|x|x|x|                             |
102835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                             |
103835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// |                    Compressed frame 1 (N-2 bytes)...          :
104835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// :                                                               |
105835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// |                                                               |
106835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.orgPacket* CreateOpusFecPacket(uint8_t payload_type, int payload_length,
108835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org                            uint8_t payload_value) {
109835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  Packet* packet = new Packet;
110835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet->header.payloadType = payload_type;
111835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet->header.timestamp = kBaseTimestamp;
112835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet->header.sequenceNumber = kSequenceNumber;
113835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet->payload_length = payload_length;
114835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  uint8_t* payload = new uint8_t[packet->payload_length];
115835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  payload[0] = 0x08;
116835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  payload[1] = 0x40;
117835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  memset(&payload[2], payload_value, payload_length - 2);
118835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet->payload = payload;
119835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  return packet;
120835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org}
121835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
1229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Create a packet with all payload bytes set to |payload_value|.
1239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgPacket* CreatePacket(uint8_t payload_type, int payload_length,
1249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                     uint8_t payload_value) {
1259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = new Packet;
1269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.payloadType = payload_type;
1279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.timestamp = kBaseTimestamp;
1289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.sequenceNumber = kSequenceNumber;
1299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->payload_length = payload_length;
1309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t* payload = new uint8_t[packet->payload_length];
1319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  memset(payload, payload_value, payload_length);
1329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->payload = payload;
1339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return packet;
1349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Checks that |packet| has the attributes given in the remaining parameters.
1379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgvoid VerifyPacket(const Packet* packet,
1389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                  int payload_length,
1399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                  uint8_t payload_type,
1409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                  uint16_t sequence_number,
1419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                  uint32_t timestamp,
1429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                  uint8_t payload_value,
1439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                  bool primary = true) {
1449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(payload_length, packet->payload_length);
1459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(payload_type, packet->header.payloadType);
1469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(sequence_number, packet->header.sequenceNumber);
1479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(timestamp, packet->header.timestamp);
1489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(primary, packet->primary);
1499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_FALSE(packet->payload == NULL);
1509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i < packet->payload_length; ++i) {
1519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(payload_value, packet->payload[i]);
1529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Start of test definitions.
1569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(PayloadSplitter, CreateAndDestroy) {
1589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter* splitter = new PayloadSplitter;
1599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete splitter;
1609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Packet A is split into A1 and A2.
1639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(RedPayloadSplitter, OnePacketTwoPayloads) {
1649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t payload_types[] = {0, 0};
1659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int kTimestampOffset = 160;
1669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreateRedPayload(2, payload_types, kTimestampOffset);
1679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
1689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
1699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
1709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kOK, splitter.SplitRed(&packet_list));
1719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(2u, packet_list.size());
1729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check first packet. The first in list should always be the primary payload.
1739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
1749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[1], kSequenceNumber,
1759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp, 1, true);
1769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
1779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
1789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
1799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check second packet.
1809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
1819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
1829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp - kTimestampOffset, 0, false);
1839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
1849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
1859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Packets A and B are not split at all. Only the RED header in each packet is
1889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// removed.
1899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(RedPayloadSplitter, TwoPacketsOnePayload) {
1909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t payload_types[] = {0};
1919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int kTimestampOffset = 160;
1929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Create first packet, with a single RED payload.
1939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreateRedPayload(1, payload_types, kTimestampOffset);
1949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
1959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
1969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Create second packet, with a single RED payload.
1979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = CreateRedPayload(1, payload_types, kTimestampOffset);
1989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Manually change timestamp and sequence number of second packet.
1999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.timestamp += kTimestampOffset;
2009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.sequenceNumber++;
2019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
2029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
2039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kOK, splitter.SplitRed(&packet_list));
2049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(2u, packet_list.size());
2059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check first packet.
2069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
2089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp, 0, true);
2099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
2129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check second packet.
2139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber + 1,
2159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp + kTimestampOffset, 0, true);
2169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
2199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Packets A and B are split into packets A1, A2, A3, B1, B2, B3, with
2219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// attributes as follows:
2229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//
2239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//                  A1*   A2    A3    B1*   B2    B3
2249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Payload type     0     1     2     0     1     2
2259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Timestamp        b     b-o   b-2o  b+o   b     b-o
2269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Sequence number  0     0     0     1     1     1
2279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//
2289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// b = kBaseTimestamp, o = kTimestampOffset, * = primary.
2299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(RedPayloadSplitter, TwoPacketsThreePayloads) {
2309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t payload_types[] = {2, 1, 0};  // Primary is the last one.
2319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int kTimestampOffset = 160;
2329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Create first packet, with 3 RED payloads.
2339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreateRedPayload(3, payload_types, kTimestampOffset);
2349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
2359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
2369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Create first packet, with 3 RED payloads.
2379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = CreateRedPayload(3, payload_types, kTimestampOffset);
2389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Manually change timestamp and sequence number of second packet.
2399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.timestamp += kTimestampOffset;
2409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->header.sequenceNumber++;
2419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
2429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
2439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kOK, splitter.SplitRed(&packet_list));
2449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(6u, packet_list.size());
2459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check first packet, A1.
2469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[2], kSequenceNumber,
2489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp, 2, true);
2499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
2529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check second packet, A2.
2539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[1], kSequenceNumber,
2559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp - kTimestampOffset, 1, false);
2569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
2599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check third packet, A3.
2609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
2629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp - 2 * kTimestampOffset, 0, false);
2639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
2669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check fourth packet, B1.
2679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[2], kSequenceNumber + 1,
2699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp + kTimestampOffset, 2, true);
2709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
2739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check fifth packet, B2.
2749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[1], kSequenceNumber + 1,
2769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp, 1, false);
2779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
2809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check sixth packet, B3.
2819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
2829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber + 1,
2839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp - kTimestampOffset, 0, false);
2849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
2859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
2869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
2879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Creates a list with 4 packets with these payload types:
2899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// 0 = CNGnb
2909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// 1 = PCMu
2919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// 2 = DTMF (AVT)
2929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// 3 = iLBC
2939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// We expect the method CheckRedPayloads to discard the iLBC packet, since it
2949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// is a non-CNG, non-DTMF payload of another type than the first speech payload
2959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// found in the list (which is PCMu).
2969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(RedPayloadSplitter, CheckRedPayloads) {
2979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
2989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i <= 3; ++i) {
2999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Create packet with payload type |i|, payload length 10 bytes, all 0.
3009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    Packet* packet = CreatePacket(i, 10, 0);
3019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_list.push_back(packet);
3029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
3039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Use a real DecoderDatabase object here instead of a mock, since it is
3059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // easier to just register the payload types and let the actual implementation
3069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // do its job.
3079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase decoder_database;
3089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder_database.RegisterPayload(0, kDecoderCNGnb);
3099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder_database.RegisterPayload(1, kDecoderPCMu);
3109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder_database.RegisterPayload(2, kDecoderAVT);
3119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder_database.RegisterPayload(3, kDecoderILBC);
3129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
3149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  splitter.CheckRedPayloads(&packet_list, decoder_database);
3159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(3u, packet_list.size());  // Should have dropped the last packet.
3179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Verify packets. The loop verifies that payload types 0, 1, and 2 are in the
3189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // list.
3199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i <= 2; ++i) {
3209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    Packet* packet = packet_list.front();
3219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    VerifyPacket(packet, 10, i, kSequenceNumber, kBaseTimestamp, 0, true);
3229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] packet->payload;
3239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete packet;
3249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_list.pop_front();
3259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
3269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(packet_list.empty());
3279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
3289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Packet A is split into A1, A2 and A3. But the length parameter is off, so
3309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// the last payloads should be discarded.
3319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(RedPayloadSplitter, WrongPayloadLength) {
3329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t payload_types[] = {0, 0, 0};
3339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int kTimestampOffset = 160;
3349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreateRedPayload(3, payload_types, kTimestampOffset);
3359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Manually tamper with the payload length of the packet.
3369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // This is one byte too short for the second payload (out of three).
3379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // We expect only the first payload to be returned.
3389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet->payload_length -= kPayloadLength + 1;
3399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
3409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
3419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
3429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kRedLengthMismatch,
3439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            splitter.SplitRed(&packet_list));
3449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(1u, packet_list.size());
3459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check first packet.
3469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet = packet_list.front();
3479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  VerifyPacket(packet, kPayloadLength, payload_types[0], kSequenceNumber,
3489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               kBaseTimestamp - 2 * kTimestampOffset, 0, false);
3499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete [] packet->payload;
3509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet;
3519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_front();
3529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
3539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test that iSAC, iSAC-swb, RED, DTMF, CNG, and "Arbitrary" payloads do not
3559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// get split.
3569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(AudioPayloadSplitter, NonSplittable) {
3579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Set up packets with different RTP payload types. The actual values do not
3589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // matter, since we are mocking the decoder database anyway.
3599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
3609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i < 6; ++i) {
3619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Let the payload type be |i|, and the payload value 10 * |i|.
3629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_list.push_back(CreatePacket(i, kPayloadLength, 10 * i));
3639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
3649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockDecoderDatabase decoder_database;
3669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Tell the mock decoder database to return DecoderInfo structs with different
3679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // codec types.
3689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Use scoped pointers to avoid having to delete them later.
3699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info0(
3709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderISAC, 16000, NULL, false));
3719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(0))
3729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info0.get()));
3739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info1(
3749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderISACswb, 32000, NULL, false));
3759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(1))
3769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info1.get()));
3779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info2(
3789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderRED, 8000, NULL, false));
3799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(2))
3809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info2.get()));
3819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info3(
3829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderAVT, 8000, NULL, false));
3839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(3))
3849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info3.get()));
3859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info4(
3869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderCNGnb, 8000, NULL, false));
3879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(4))
3889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info4.get()));
3899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info5(
3909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderArbitrary, 8000, NULL, false));
3919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(5))
3929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info5.get()));
3939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
3959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(0, splitter.SplitAudio(&packet_list, decoder_database));
3969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(6u, packet_list.size());
3979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
3989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Check that all payloads are intact.
3999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t payload_type = 0;
4009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
4019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
4029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    VerifyPacket((*it), kPayloadLength, payload_type, kSequenceNumber,
4039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                 kBaseTimestamp, 10 * payload_type);
4049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++payload_type;
4059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] (*it)->payload;
4069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete (*it);
4079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
4089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
4099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The destructor is called when decoder_database goes out of scope.
4119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, Die());
4129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
4139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test unknown payload type.
4159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(AudioPayloadSplitter, UnknownPayloadType) {
4169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
4179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const uint8_t kPayloadType = 17;  // Just a random number.
4189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int kPayloadLengthBytes = 4711;  // Random number.
4199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(CreatePacket(kPayloadType, kPayloadLengthBytes, 0));
4209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockDecoderDatabase decoder_database;
4229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Tell the mock decoder database to return NULL when asked for decoder info.
4239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // This signals that the decoder database does not recognize the payload type.
4249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(kPayloadType))
4259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(ReturnNull());
4269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
4289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kUnknownPayloadType,
4299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            splitter.SplitAudio(&packet_list, decoder_database));
4309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(1u, packet_list.size());
4319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Delete the packets and payloads to avoid having the test leak memory.
4349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
4359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
4369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] (*it)->payload;
4379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete (*it);
4389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
4399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
4409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The destructor is called when decoder_database goes out of scope.
4429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, Die());
4439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
4449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
4459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass SplitBySamplesTest : public ::testing::TestWithParam<NetEqDecoder> {
4469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org protected:
4479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  virtual void SetUp() {
4489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    decoder_type_ = GetParam();
4499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    switch (decoder_type_) {
4509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCMu:
4519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCMa:
4529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 8;
4539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 8;
4549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCMu_2ch:
4569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCMa_2ch:
4579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 2 * 8;
4589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 8;
4599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderG722:
4619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 8;
4629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 16;
4639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16B:
4659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 16;
4669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 8;
4679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16Bwb:
4699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 32;
4709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 16;
4719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16Bswb32kHz:
4739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 64;
4749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 32;
4759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16Bswb48kHz:
4779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 96;
4789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 48;
4799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16B_2ch:
4819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 2 * 16;
4829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 8;
4839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16Bwb_2ch:
4859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 2 * 32;
4869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 16;
4879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16Bswb32kHz_2ch:
4899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 2 * 64;
4909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 32;
4919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16Bswb48kHz_2ch:
4939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 2 * 96;
4949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 48;
4959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
4969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      case kDecoderPCM16B_5ch:
4979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        bytes_per_ms_ = 5 * 16;
4989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        samples_per_ms_ = 8;
4999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
5009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      default:
5019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        assert(false);
5029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        break;
5039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
5049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
5059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int bytes_per_ms_;
5069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int samples_per_ms_;
5079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  NetEqDecoder decoder_type_;
5089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org};
5099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test splitting sample-based payloads.
5119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST_P(SplitBySamplesTest, PayloadSizes) {
5129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
5139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const uint8_t kPayloadType = 17;  // Just a random number.
5149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int payload_size_ms = 10; payload_size_ms <= 60; payload_size_ms += 10) {
5159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // The payload values are set to be the same as the payload_size, so that
5169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // one can distinguish from which packet the split payloads come from.
5179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int payload_size_bytes = payload_size_ms * bytes_per_ms_;
5189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_list.push_back(CreatePacket(kPayloadType, payload_size_bytes,
5199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                       payload_size_ms));
5209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
5219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockDecoderDatabase decoder_database;
5239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Tell the mock decoder database to return DecoderInfo structs with different
5249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // codec types.
5259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Use scoped pointers to avoid having to delete them later.
5269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // (Sample rate is set to 8000 Hz, but does not matter.)
5279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info(
5289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(decoder_type_, 8000, NULL, false));
5299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(kPayloadType))
5309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info.get()));
5319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
5339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(0, splitter.SplitAudio(&packet_list, decoder_database));
5349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The payloads are expected to be split as follows:
5359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // 10 ms -> 10 ms
5369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // 20 ms -> 20 ms
5379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // 30 ms -> 30 ms
5389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // 40 ms -> 20 + 20 ms
5399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // 50 ms -> 25 + 25 ms
5409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // 60 ms -> 30 + 30 ms
5419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int expected_size_ms[] = {10, 20, 30, 20, 20, 25, 25, 30, 30};
5429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int expected_payload_value[] = {10, 20, 30, 40, 40, 50, 50, 60, 60};
5439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int expected_timestamp_offset_ms[] = {0, 0, 0, 0, 20, 0, 25, 0, 30};
5449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t expected_num_packets =
5459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      sizeof(expected_size_ms) / sizeof(expected_size_ms[0]);
5469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(expected_num_packets, packet_list.size());
5479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
5499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int i = 0;
5509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
5519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int length_bytes = expected_size_ms[i] * bytes_per_ms_;
5529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    uint32_t expected_timestamp = kBaseTimestamp +
5539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        expected_timestamp_offset_ms[i] * samples_per_ms_;
5549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    VerifyPacket((*it), length_bytes, kPayloadType, kSequenceNumber,
5559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                 expected_timestamp, expected_payload_value[i]);
5569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] (*it)->payload;
5579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete (*it);
5589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
5599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++i;
5609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
5619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The destructor is called when decoder_database goes out of scope.
5639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, Die());
5649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
5659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgINSTANTIATE_TEST_CASE_P(
5679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    PayloadSplitter, SplitBySamplesTest,
5689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ::testing::Values(kDecoderPCMu, kDecoderPCMa, kDecoderPCMu_2ch,
5699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      kDecoderPCMa_2ch, kDecoderG722, kDecoderPCM16B,
5709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      kDecoderPCM16Bwb, kDecoderPCM16Bswb32kHz,
5719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      kDecoderPCM16Bswb48kHz, kDecoderPCM16B_2ch,
5729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      kDecoderPCM16Bwb_2ch, kDecoderPCM16Bswb32kHz_2ch,
5739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      kDecoderPCM16Bswb48kHz_2ch, kDecoderPCM16B_5ch));
5749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass SplitIlbcTest : public ::testing::TestWithParam<std::pair<int, int> > {
5779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org protected:
5789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  virtual void SetUp() {
5799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    const std::pair<int, int> parameters = GetParam();
5809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    num_frames_ = parameters.first;
5819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    frame_length_ms_ = parameters.second;
5829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    frame_length_bytes_ = (frame_length_ms_ == 20) ? 38 : 50;
5839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
5849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t num_frames_;
5859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int frame_length_ms_;
5869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int frame_length_bytes_;
5879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org};
5889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
5899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test splitting sample-based payloads.
5909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST_P(SplitIlbcTest, NumFrames) {
5919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
5929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const uint8_t kPayloadType = 17;  // Just a random number.
5939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int frame_length_samples = frame_length_ms_ * 8;
5949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int payload_length_bytes = frame_length_bytes_ * num_frames_;
5959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreatePacket(kPayloadType, payload_length_bytes, 0);
5969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Fill payload with increasing integers {0, 1, 2, ...}.
5979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i < packet->payload_length; ++i) {
5989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet->payload[i] = static_cast<uint8_t>(i);
5999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
6009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
6019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockDecoderDatabase decoder_database;
6039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Tell the mock decoder database to return DecoderInfo structs with different
6049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // codec types.
6059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Use scoped pointers to avoid having to delete them later.
6069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info(
6079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderILBC, 8000, NULL, false));
6089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(kPayloadType))
6099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info.get()));
6109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
6129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(0, splitter.SplitAudio(&packet_list, decoder_database));
6139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(num_frames_, packet_list.size());
6149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
6169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int frame_num = 0;
6179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t payload_value = 0;
6189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
6199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    Packet* packet = (*it);
6209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(kBaseTimestamp + frame_length_samples * frame_num,
6219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org              packet->header.timestamp);
6229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(frame_length_bytes_, packet->payload_length);
6239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(kPayloadType, packet->header.payloadType);
6249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(kSequenceNumber, packet->header.sequenceNumber);
6259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(true, packet->primary);
6269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ASSERT_FALSE(packet->payload == NULL);
6279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    for (int i = 0; i < packet->payload_length; ++i) {
6289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      EXPECT_EQ(payload_value, packet->payload[i]);
6299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      ++payload_value;
6309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
6319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] (*it)->payload;
6329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete (*it);
6339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
6349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ++frame_num;
6359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
6369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The destructor is called when decoder_database goes out of scope.
6389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, Die());
6399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
6409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test 1 through 5 frames of 20 and 30 ms size.
6429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Also test the maximum number of frames in one packet for 20 and 30 ms.
6439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// The maximum is defined by the largest payload length that can be uniquely
6449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// resolved to a frame size of either 38 bytes (20 ms) or 50 bytes (30 ms).
6459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgINSTANTIATE_TEST_CASE_P(
6469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    PayloadSplitter, SplitIlbcTest,
6479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    ::testing::Values(std::pair<int, int>(1, 20),  // 1 frame, 20 ms.
6489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(2, 20),  // 2 frames, 20 ms.
6499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(3, 20),  // And so on.
6509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(4, 20),
6519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(5, 20),
6529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(24, 20),
6539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(1, 30),
6549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(2, 30),
6559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(3, 30),
6569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(4, 30),
6579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(5, 30),
6589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                      std::pair<int, int>(18, 30)));
6599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test too large payload size.
6619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(IlbcPayloadSplitter, TooLargePayload) {
6629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
6639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const uint8_t kPayloadType = 17;  // Just a random number.
6649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int kPayloadLengthBytes = 950;
6659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreatePacket(kPayloadType, kPayloadLengthBytes, 0);
6669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
6679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockDecoderDatabase decoder_database;
6699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info(
6709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderILBC, 8000, NULL, false));
6719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(kPayloadType))
6729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info.get()));
6739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
6759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kTooLargePayload,
6769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            splitter.SplitAudio(&packet_list, decoder_database));
6779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(1u, packet_list.size());
6789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Delete the packets and payloads to avoid having the test leak memory.
6809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
6819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
6829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] (*it)->payload;
6839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete (*it);
6849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
6859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
6869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The destructor is called when decoder_database goes out of scope.
6889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, Die());
6899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
6909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Payload not an integer number of frames.
6929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(IlbcPayloadSplitter, UnevenPayload) {
6939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
6949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const uint8_t kPayloadType = 17;  // Just a random number.
6959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int kPayloadLengthBytes = 39;  // Not an even number of frames.
6969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet* packet = CreatePacket(kPayloadType, kPayloadLengthBytes, 0);
6979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.push_back(packet);
6989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
6999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockDecoderDatabase decoder_database;
7009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  scoped_ptr<DecoderDatabase::DecoderInfo> info(
7019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      new DecoderDatabase::DecoderInfo(kDecoderILBC, 8000, NULL, false));
7029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, GetDecoderInfo(kPayloadType))
7039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      .WillRepeatedly(Return(info.get()));
7049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
7059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PayloadSplitter splitter;
7069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(PayloadSplitter::kFrameSplitError,
7079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            splitter.SplitAudio(&packet_list, decoder_database));
7089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(1u, packet_list.size());
7099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
7109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Delete the packets and payloads to avoid having the test leak memory.
7119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
7129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
7139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete [] (*it)->payload;
7149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete (*it);
7159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
7169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
7179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
7189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The destructor is called when decoder_database goes out of scope.
7199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder_database, Die());
7209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
7219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
722835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.orgTEST(FecPayloadSplitter, MixedPayload) {
723835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  PacketList packet_list;
724835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  DecoderDatabase decoder_database;
725835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
726835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  decoder_database.RegisterPayload(0, kDecoderOpus);
727835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  decoder_database.RegisterPayload(1, kDecoderPCMu);
728835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
729835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  Packet* packet = CreateOpusFecPacket(0, 10, 0xFF);
730835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet_list.push_back(packet);
731835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
732835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet = CreatePacket(0, 10, 0); // Non-FEC Opus payload.
733835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet_list.push_back(packet);
734835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
735835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet = CreatePacket(1, 10, 0); // Non-Opus payload.
736835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet_list.push_back(packet);
737835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
738835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  PayloadSplitter splitter;
739835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(PayloadSplitter::kOK,
740835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org            splitter.SplitFec(&packet_list, &decoder_database));
741835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(4u, packet_list.size());
742835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
743835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  // Check first packet.
744835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet = packet_list.front();
745835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(0, packet->header.payloadType);
746254879d7e969771c28d0882582e02b1a0b0eeb14minyue@webrtc.org  EXPECT_EQ(kBaseTimestamp - 20 * 48, packet->header.timestamp);
747835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(10, packet->payload_length);
748835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_FALSE(packet->primary);
749835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete [] packet->payload;
750835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete packet;
751835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet_list.pop_front();
752835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
753835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  // Check second packet.
754835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet = packet_list.front();
755835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(0, packet->header.payloadType);
756835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(kBaseTimestamp, packet->header.timestamp);
757835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_EQ(10, packet->payload_length);
758835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  EXPECT_TRUE(packet->primary);
759835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete [] packet->payload;
760835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete packet;
761835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet_list.pop_front();
762835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
763835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  // Check third packet.
764835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet = packet_list.front();
765835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  VerifyPacket(packet, 10, 0, kSequenceNumber, kBaseTimestamp, 0, true);
766835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete [] packet->payload;
767835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete packet;
768835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet_list.pop_front();
769835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
770835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  // Check fourth packet.
771835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  packet = packet_list.front();
772835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  VerifyPacket(packet, 10, 1, kSequenceNumber, kBaseTimestamp, 0, true);
773835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete [] packet->payload;
774835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org  delete packet;
775835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org}
776835b016d936aeb6c48b42c211ef257d6d3057c4fminyue@webrtc.org
7779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
778