blob: ec8a5d69bc27dfb1b19e472dc5121dbadedffbbf [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <map>
kwiberg84be5112016-04-27 01:19:58 -070015#include <memory>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080016#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000018#include "webrtc/base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/common_types.h" // NULL
ossu97ba30e2016-04-25 07:55:58 -070020#include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000021#include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000022#include "webrtc/modules/audio_coding/neteq/packet.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023#include "webrtc/typedefs.h"
24
25namespace webrtc {
26
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027class DecoderDatabase {
28 public:
29 enum DatabaseReturnCodes {
30 kOK = 0,
31 kInvalidRtpPayloadType = -1,
32 kCodecNotSupported = -2,
33 kInvalidSampleRate = -3,
34 kDecoderExists = -4,
35 kDecoderNotFound = -5,
36 kInvalidPointer = -6
37 };
38
kwiberg0fa0a972016-04-19 05:03:45 -070039 // Class that stores decoder info in the database.
40 class DecoderInfo {
41 public:
henrik.lundin4cf61dd2015-12-09 06:20:58 -080042 DecoderInfo(NetEqDecoder ct,
43 const std::string& nm,
44 int fs,
kwiberg0fa0a972016-04-19 05:03:45 -070045 AudioDecoder* ext_dec);
46 DecoderInfo(DecoderInfo&&);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047 ~DecoderInfo();
48
kwiberg0fa0a972016-04-19 05:03:45 -070049 // Get the AudioDecoder object, creating it first if necessary.
50 AudioDecoder* GetDecoder();
51
52 // Delete the AudioDecoder object, unless it's external. (This means we can
53 // always recreate it later if we need it.)
54 void DropDecoder() { decoder_.reset(); }
55
56 const NetEqDecoder codec_type;
57 const std::string name;
58 const int fs_hz;
kwiberg0fa0a972016-04-19 05:03:45 -070059 AudioDecoder* const external_decoder;
60
61 private:
62 std::unique_ptr<AudioDecoder> decoder_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063 };
64
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
66 // only 7 bits).
67 static const uint8_t kRtpPayloadTypeError = 0xFF;
68
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000069 DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000071 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
73 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000074 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075
76 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000077 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078
79 // Resets the database, erasing all registered payload types, and deleting
80 // any AudioDecoder objects that were not externally created and inserted
81 // using InsertExternal().
82 virtual void Reset();
83
henrik.lundin4cf61dd2015-12-09 06:20:58 -080084 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
85 // is only used to populate the name field in the DecoderInfo struct in the
86 // database, and can be arbitrary (including empty). Returns kOK on success;
87 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080089 NetEqDecoder codec_type,
90 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091
92 // Registers an externally created AudioDecoder object, and associates it
93 // as a decoder of type |codec_type| with |rtp_payload_type|.
94 virtual int InsertExternal(uint8_t rtp_payload_type,
95 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080096 const std::string& codec_name,
97 int fs_hz,
98 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099
100 // Removes the entry for |rtp_payload_type| from the database.
101 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
102 virtual int Remove(uint8_t rtp_payload_type);
103
104 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
105 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
106 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
107
108 // Returns one RTP payload type associated with |codec_type|, or
109 // kDecoderNotFound if no entry exists for that value. Note that one
110 // |codec_type| may be registered with several RTP payload types, and the
111 // method may return any of them.
112 virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const;
113
114 // Returns a pointer to the AudioDecoder object associated with
115 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
116 // object does not exist for that decoder, the object is created.
117 virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type);
118
119 // Returns true if |rtp_payload_type| is registered as a |codec_type|.
120 virtual bool IsType(uint8_t rtp_payload_type,
121 NetEqDecoder codec_type) const;
122
123 // Returns true if |rtp_payload_type| is registered as comfort noise.
124 virtual bool IsComfortNoise(uint8_t rtp_payload_type) const;
125
126 // Returns true if |rtp_payload_type| is registered as DTMF.
127 virtual bool IsDtmf(uint8_t rtp_payload_type) const;
128
129 // Returns true if |rtp_payload_type| is registered as RED.
130 virtual bool IsRed(uint8_t rtp_payload_type) const;
131
132 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
133 // change of active decoder, |new_decoder| is set to true. The previous active
134 // decoder's AudioDecoder object is deleted.
135 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
136
137 // Returns the current active decoder, or NULL if no active decoder exists.
138 virtual AudioDecoder* GetActiveDecoder();
139
140 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
141 // call results in a change of active comfort noise decoder, the previous
142 // active decoder's AudioDecoder object is deleted.
143 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
144
145 // Returns the current active comfort noise decoder, or NULL if no active
146 // comfort noise decoder exists.
ossu97ba30e2016-04-25 07:55:58 -0700147 virtual ComfortNoiseDecoder* GetActiveCngDecoder();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148
149 // Returns kOK if all packets in |packet_list| carry payload types that are
150 // registered in the database. Otherwise, returns kDecoderNotFound.
151 virtual int CheckPayloadTypes(const PacketList& packet_list) const;
152
153 private:
154 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
155
156 DecoderMap decoders_;
ossu97ba30e2016-04-25 07:55:58 -0700157 int active_decoder_type_;
158 int active_cng_decoder_type_;
159 std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160
henrikg3c089d72015-09-16 05:37:44 -0700161 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162};
163
164} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000165#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_