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
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <assert.h>
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <stdlib.h>
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <string>
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1849d62206ededc5905d6121d42fdcce8ed665b2c0kjellander@webrtc.org#include "testing/gmock/include/gmock/gmock.h"
1949d62206ededc5905d6121d42fdcce8ed665b2c0kjellander@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
207537dde7698d151f5c50b5f160e4ed28347b0c90henrike@webrtc.org
21e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h"
227537dde7698d151f5c50b5f160e4ed28347b0c90henrike@webrtc.org#include "webrtc/test/testsupport/gtest_disable.h"
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, CreateAndDestroy) {
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(0, db.Size());
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.Empty());
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, InsertAndRemove) {
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadType = 0;
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadType, kDecoderPCMu));
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(1, db.Size());
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.Empty());
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(0, db.Size());
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.Empty());
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, GetDecoderInfo) {
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadType = 0;
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadType, kDecoderPCMu));
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const DecoderDatabase::DecoderInfo* info;
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  info = db.GetDecoderInfo(kPayloadType);
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_TRUE(info != NULL);
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kDecoderPCMu, info->codec_type);
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(NULL, info->decoder);
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(8000, info->fs_hz);
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(info->external);
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  info = db.GetDecoderInfo(kPayloadType + 1);  // Other payload type.
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(info == NULL);  // Should not be found.
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, GetRtpPayloadType) {
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadType = 0;
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadType, kDecoderPCMu));
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kPayloadType, db.GetRtpPayloadType(kDecoderPCMu));
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t expected_value = DecoderDatabase::kRtpPayloadTypeError;
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(expected_value,
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.GetRtpPayloadType(kDecoderISAC));  // iSAC is not registered.
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
718a35df18480899fca1911395e5e3414fa9454c61henrik.lundin@webrtc.orgTEST(DecoderDatabase, GetDecoder) {
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadType = 0;
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
758a35df18480899fca1911395e5e3414fa9454c61henrik.lundin@webrtc.org            db.RegisterPayload(kPayloadType, kDecoderPCM16B));
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  AudioDecoder* dec = db.GetDecoder(kPayloadType);
779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_TRUE(dec != NULL);
789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, TypeTests) {
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadTypePcmU = 0;
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadTypeCng = 13;
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadTypeDtmf = 100;
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadTypeRed = 101;
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadNotUsed = 102;
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Load into database.
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadTypePcmU, kDecoderPCMu));
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadTypeCng, kDecoderCNGnb));
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadTypeDtmf, kDecoderAVT));
949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.RegisterPayload(kPayloadTypeRed, kDecoderRED));
969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(4, db.Size());
979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Test.
989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsComfortNoise(kPayloadNotUsed));
999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsDtmf(kPayloadNotUsed));
1009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsRed(kPayloadNotUsed));
1019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsComfortNoise(kPayloadTypePcmU));
1029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsDtmf(kPayloadTypePcmU));
1039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsRed(kPayloadTypePcmU));
1049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(db.IsType(kPayloadTypePcmU, kDecoderISAC));
1059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.IsType(kPayloadTypePcmU, kDecoderPCMu));
1069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.IsComfortNoise(kPayloadTypeCng));
1079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.IsDtmf(kPayloadTypeDtmf));
1089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.IsRed(kPayloadTypeRed));
1099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, ExternalDecoder) {
1129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
1139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const uint8_t kPayloadType = 0;
1149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  MockAudioDecoder decoder;
1159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Load into database.
1169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK,
1179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.InsertExternal(kPayloadType, kDecoderPCMu, 8000,
1189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                               &decoder));
1199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(1, db.Size());
1209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Get decoder and make sure we get the external one.
1219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(&decoder, db.GetDecoder(kPayloadType));
1229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Get the decoder info struct and check it too.
1239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const DecoderDatabase::DecoderInfo* info;
1249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  info = db.GetDecoderInfo(kPayloadType);
1259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_TRUE(info != NULL);
1269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kDecoderPCMu, info->codec_type);
1279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(&decoder, info->decoder);
1289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(8000, info->fs_hz);
1299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(info->external);
1309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Expect not to delete the decoder when removing it from the database, since
1319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // it was declared externally.
1329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder, Die()).Times(0);
1339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
1349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(db.Empty());
1359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_CALL(decoder, Die()).Times(1);  // Will be called when |db| is deleted.
1379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, CheckPayloadTypes) {
1409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
1419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Load a number of payloads into the database. Payload types are 0, 1, ...,
1429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // while the decoder type is the same for all payload types (this does not
1439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // matter for the test).
1449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const int kNumPayloads = 10;
1459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (uint8_t payload_type = 0; payload_type < kNumPayloads; ++payload_type) {
1469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    EXPECT_EQ(DecoderDatabase::kOK,
1479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org              db.RegisterPayload(payload_type, kDecoderArbitrary));
1489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList packet_list;
1509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 0; i < kNumPayloads + 1; ++i) {
1519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Create packet with payload type |i|. The last packet will have a payload
1529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // type that is not registered in the decoder database.
1539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    Packet* packet = new Packet;
1549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet->header.payloadType = i;
1559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_list.push_back(packet);
1569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Expect to return false, since the last packet is of an unknown type.
1599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
1609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.CheckPayloadTypes(packet_list));
1619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  delete packet_list.back();
1639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  packet_list.pop_back();  // Remove the unknown one.
1649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.CheckPayloadTypes(packet_list));
1669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Delete all packets.
1689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  PacketList::iterator it = packet_list.begin();
1699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (it != packet_list.end()) {
1709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete packet_list.front();
1719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    it = packet_list.erase(it);
1729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
1749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Test the methods for setting and getting active speech and CNG decoders.
1769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgTEST(DecoderDatabase, ActiveDecoders) {
1779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase db;
1789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Load payload types.
1799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(DecoderDatabase::kOK, db.RegisterPayload(0, kDecoderPCMu));
1809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(DecoderDatabase::kOK, db.RegisterPayload(103, kDecoderISAC));
1819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_EQ(DecoderDatabase::kOK, db.RegisterPayload(13, kDecoderCNGnb));
1829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Verify that no decoders are active from the start.
1839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(NULL, db.GetActiveDecoder());
1849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(NULL, db.GetActiveCngDecoder());
1859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Set active speech codec.
1879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool changed;  // Should be true when the active decoder changed.
1889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
1899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(changed);
1909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  AudioDecoder* decoder = db.GetActiveDecoder();
1919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
1929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kDecoderPCMu, decoder->codec_type());
1939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Set the same again. Expect no change.
1959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
1969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_FALSE(changed);
1979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder = db.GetActiveDecoder();
1989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
1999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kDecoderPCMu, decoder->codec_type());
2009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Change active decoder.
2029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(103, &changed));
2039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(changed);
2049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder = db.GetActiveDecoder();
2059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
2069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kDecoderISAC, decoder->codec_type());
2079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Remove the active decoder, and verify that the active becomes NULL.
2099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.Remove(103));
2109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(NULL, db.GetActiveDecoder());
2119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Set active CNG codec.
2139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveCngDecoder(13));
2149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  decoder = db.GetActiveCngDecoder();
2159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
2169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kDecoderCNGnb, decoder->codec_type());
2179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Remove the active CNG decoder, and verify that the active becomes NULL.
2199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kOK, db.Remove(13));
2209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(NULL, db.GetActiveCngDecoder());
2219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
2229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Try to set non-existing codecs as active.
2239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
2249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.SetActiveDecoder(17, &changed));
2259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
2269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org            db.SetActiveCngDecoder(17));
2279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
2289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
229