blob: 4feaffe6f3f569e6caf4121d76385f3712c7f3ab [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.orgf27916a2012-01-20 14:04:13 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Kjellander2557b862015-11-18 22:00:21 +010011#ifndef WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_
12#define WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
stefan@webrtc.orgf27916a2012-01-20 14:04:13 +000014#include <map>
15
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000016#include "webrtc/base/scoped_ptr.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/include/video_codec_interface.h"
18#include "webrtc/modules/video_coding/include/video_coding.h"
19#include "webrtc/modules/video_coding/generic_decoder.h"
20#include "webrtc/modules/video_coding/generic_encoder.h"
pbos@webrtc.orga4407322013-07-16 12:32:05 +000021#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000025struct VCMDecoderMapItem {
26 public:
27 VCMDecoderMapItem(VideoCodec* settings,
28 int number_of_cores,
29 bool require_key_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +000030
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000031 rtc::scoped_ptr<VideoCodec> settings;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000032 int number_of_cores;
33 bool require_key_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +000034};
35
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000036struct VCMExtDecoderMapItem {
37 public:
38 VCMExtDecoderMapItem(VideoDecoder* external_decoder_instance,
39 uint8_t payload_type,
40 bool internal_render_timing);
niklase@google.com470e71d2011-07-07 08:21:25 +000041
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000042 uint8_t payload_type;
43 VideoDecoder* external_decoder_instance;
44 bool internal_render_timing;
niklase@google.com470e71d2011-07-07 08:21:25 +000045};
46
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000047class VCMCodecDataBase {
48 public:
Peter Boström4f5db112015-10-29 16:53:59 +010049 VCMCodecDataBase(VideoEncoderRateObserver* encoder_rate_observer,
50 VCMEncodedFrameCallback* encoded_frame_callback);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000051 ~VCMCodecDataBase();
niklase@google.com470e71d2011-07-07 08:21:25 +000052
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000053 // Sender Side
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000054 // Returns the default settings for the codec with type |codec_type|.
Peter Boström92f8dbd2015-11-24 13:55:55 +010055 static void Codec(VideoCodecType codec_type, VideoCodec* settings);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000057 void ResetSender();
niklase@google.com470e71d2011-07-07 08:21:25 +000058
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000059 // Sets the sender side codec and initiates the desired codec given the
60 // VideoCodec struct.
61 // Returns true if the codec was successfully registered, false otherwise.
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +000062 bool SetSendCodec(const VideoCodec* send_codec,
63 int number_of_cores,
Peter Boström4f5db112015-10-29 16:53:59 +010064 size_t max_payload_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000066 // Gets the current send codec. Relevant for internal codecs only.
67 // Returns true if there is a send codec, false otherwise.
68 bool SendCodec(VideoCodec* current_send_codec) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000070 // Gets current send side codec type. Relevant for internal codecs only.
71 // Returns kVideoCodecUnknown if there is no send codec.
72 VideoCodecType SendCodec() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000073
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000074 // Registers and initializes an external encoder object.
75 // |internal_source| should be set to true if the codec has an internal
76 // video source and doesn't need the user to provide it with frames via
77 // the Encode() method.
78 void RegisterExternalEncoder(VideoEncoder* external_encoder,
79 uint8_t payload_type,
80 bool internal_source);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000082 // Deregisters an external encoder. Returns true if the encoder was
83 // found and deregistered, false otherwise. |was_send_codec| is set to true
84 // if the external encoder was the send codec before being deregistered.
85 bool DeregisterExternalEncoder(uint8_t payload_type, bool* was_send_codec);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +000087 VCMGenericEncoder* GetEncoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000089 bool SetPeriodicKeyFrames(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000091 // Receiver Side
92 void ResetReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +000093
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000094 // Deregisters an external decoder object specified by |payload_type|.
95 bool DeregisterExternalDecoder(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000096
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000097 // Registers an external decoder object to the payload type |payload_type|.
98 // |internal_render_timing| is set to true if the |external_decoder| has
99 // built in rendering which is able to obey the render timestamps of the
100 // encoded frames.
101 bool RegisterExternalDecoder(VideoDecoder* external_decoder,
102 uint8_t payload_type,
103 bool internal_render_timing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000105 bool DecoderRegistered() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000107 bool RegisterReceiveCodec(const VideoCodec* receive_codec,
108 int number_of_cores,
109 bool require_key_frame);
110
111 bool DeregisterReceiveCodec(uint8_t payload_type);
112
113 // Get current receive side codec. Relevant for internal codecs only.
114 bool ReceiveCodec(VideoCodec* current_receive_codec) const;
115
116 // Get current receive side codec type. Relevant for internal codecs only.
117 VideoCodecType ReceiveCodec() const;
118
119 // Returns a decoder specified by |payload_type|. The decoded frame callback
120 // of the encoder is set to |decoded_frame_callback|. If no such decoder
121 // already exists an instance will be created and initialized.
122 // NULL is returned if no encoder with the specified payload type was found
123 // and the function failed to create one.
124 VCMGenericDecoder* GetDecoder(
125 uint8_t payload_type, VCMDecodedFrameCallback* decoded_frame_callback);
126
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000127 // Deletes the memory of the decoder instance |decoder|. Used to delete
128 // deep copies returned by CreateDecoderCopy().
129 void ReleaseDecoder(VCMGenericDecoder* decoder) const;
130
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000131 // Returns true if the currently active decoder supports render scheduling,
132 // that is, it is able to render frames according to the render timestamp of
133 // the encoded frames.
134 bool SupportsRenderScheduling() const;
135
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000136 bool MatchesCurrentResolution(int width, int height) const;
137
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000138 private:
139 typedef std::map<uint8_t, VCMDecoderMapItem*> DecoderMap;
140 typedef std::map<uint8_t, VCMExtDecoderMapItem*> ExternalDecoderMap;
141
142 VCMGenericDecoder* CreateAndInitDecoder(uint8_t payload_type,
Stefan Holmer79064e52015-03-25 15:20:32 +0100143 VideoCodec* new_codec) const;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000144
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +0000145 // Determines whether a new codec has to be created or not.
146 // Checks every setting apart from maxFramerate and startBitrate.
147 bool RequiresEncoderReset(const VideoCodec& send_codec);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000148
149 void DeleteEncoder();
150
151 // Create an internal Decoder given a codec type
152 VCMGenericDecoder* CreateDecoder(VideoCodecType type) const;
153
154 const VCMDecoderMapItem* FindDecoderItem(uint8_t payload_type) const;
155
156 const VCMExtDecoderMapItem* FindExternalDecoderItem(
157 uint8_t payload_type) const;
158
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000159 int number_of_cores_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000160 size_t max_payload_size_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000161 bool periodic_key_frames_;
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +0000162 bool pending_encoder_reset_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000163 VideoCodec send_codec_;
164 VideoCodec receive_codec_;
Peter Boströmab73d132015-10-15 12:01:38 +0200165 uint8_t encoder_payload_type_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000166 VideoEncoder* external_encoder_;
167 bool internal_source_;
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000168 VideoEncoderRateObserver* const encoder_rate_observer_;
Peter Boström4f5db112015-10-29 16:53:59 +0100169 VCMEncodedFrameCallback* const encoded_frame_callback_;
Peter Boströmab73d132015-10-15 12:01:38 +0200170 rtc::scoped_ptr<VCMGenericEncoder> ptr_encoder_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000171 VCMGenericDecoder* ptr_decoder_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000172 DecoderMap dec_map_;
173 ExternalDecoderMap dec_external_map_;
174}; // VCMCodecDataBase
175
176} // namespace webrtc
177
Henrik Kjellander2557b862015-11-18 22:00:21 +0100178#endif // WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_