blob: 5a7e87564494010fb4237b70911f7c216c9abba0 [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>
kwiberg3f55dea2016-02-29 05:51:59 -080015#include <memory>
stefan@webrtc.orgf27916a2012-01-20 14:04:13 +000016
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
kwiberg3f55dea2016-02-29 05:51:59 -080031 std::unique_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,
perkj796cfaf2015-12-10 09:27:38 -080039 uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000040
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000041 uint8_t payload_type;
42 VideoDecoder* external_decoder_instance;
niklase@google.com470e71d2011-07-07 08:21:25 +000043};
44
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000045class VCMCodecDataBase {
46 public:
perkjf5b2e512016-07-05 08:34:04 -070047 explicit VCMCodecDataBase(VCMEncodedFrameCallback* encoded_frame_callback);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000048 ~VCMCodecDataBase();
niklase@google.com470e71d2011-07-07 08:21:25 +000049
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000050 // Sender Side
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000051 // Returns the default settings for the codec with type |codec_type|.
Peter Boström92f8dbd2015-11-24 13:55:55 +010052 static void Codec(VideoCodecType codec_type, VideoCodec* settings);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000054 // Sets the sender side codec and initiates the desired codec given the
55 // VideoCodec struct.
56 // Returns true if the codec was successfully registered, false otherwise.
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +000057 bool SetSendCodec(const VideoCodec* send_codec,
58 int number_of_cores,
Peter Boström4f5db112015-10-29 16:53:59 +010059 size_t max_payload_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000061 // Gets the current send codec. Relevant for internal codecs only.
62 // Returns true if there is a send codec, false otherwise.
63 bool SendCodec(VideoCodec* current_send_codec) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000064
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000065 // Gets current send side codec type. Relevant for internal codecs only.
66 // Returns kVideoCodecUnknown if there is no send codec.
67 VideoCodecType SendCodec() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000069 // Registers and initializes an external encoder object.
70 // |internal_source| should be set to true if the codec has an internal
71 // video source and doesn't need the user to provide it with frames via
72 // the Encode() method.
73 void RegisterExternalEncoder(VideoEncoder* external_encoder,
74 uint8_t payload_type,
75 bool internal_source);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000077 // Deregisters an external encoder. Returns true if the encoder was
78 // found and deregistered, false otherwise. |was_send_codec| is set to true
79 // if the external encoder was the send codec before being deregistered.
80 bool DeregisterExternalEncoder(uint8_t payload_type, bool* was_send_codec);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +000082 VCMGenericEncoder* GetEncoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000083
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000084 bool SetPeriodicKeyFrames(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000086 // Deregisters an external decoder object specified by |payload_type|.
87 bool DeregisterExternalDecoder(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000089 // Registers an external decoder object to the payload type |payload_type|.
Peter Boström795dbe42015-11-27 14:09:07 +010090 void RegisterExternalDecoder(VideoDecoder* external_decoder,
perkj796cfaf2015-12-10 09:27:38 -080091 uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000092
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000093 bool DecoderRegistered() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +000095 bool RegisterReceiveCodec(const VideoCodec* receive_codec,
96 int number_of_cores,
97 bool require_key_frame);
98
99 bool DeregisterReceiveCodec(uint8_t payload_type);
100
101 // Get current receive side codec. Relevant for internal codecs only.
102 bool ReceiveCodec(VideoCodec* current_receive_codec) const;
103
104 // Get current receive side codec type. Relevant for internal codecs only.
105 VideoCodecType ReceiveCodec() const;
106
107 // Returns a decoder specified by |payload_type|. The decoded frame callback
108 // of the encoder is set to |decoded_frame_callback|. If no such decoder
109 // already exists an instance will be created and initialized.
110 // NULL is returned if no encoder with the specified payload type was found
111 // and the function failed to create one.
112 VCMGenericDecoder* GetDecoder(
Peter Boströme997a7d2015-11-27 14:23:21 +0100113 const VCMEncodedFrame& frame,
114 VCMDecodedFrameCallback* decoded_frame_callback);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000115
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000116 // Deletes the memory of the decoder instance |decoder|. Used to delete
117 // deep copies returned by CreateDecoderCopy().
118 void ReleaseDecoder(VCMGenericDecoder* decoder) const;
119
perkj796cfaf2015-12-10 09:27:38 -0800120 // Returns true if the currently active decoder prefer to decode frames late.
121 // That means that frames must be decoded near the render times stamp.
122 bool PrefersLateDecoding() const;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000123
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000124 bool MatchesCurrentResolution(int width, int height) const;
125
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000126 private:
127 typedef std::map<uint8_t, VCMDecoderMapItem*> DecoderMap;
128 typedef std::map<uint8_t, VCMExtDecoderMapItem*> ExternalDecoderMap;
129
Peter Boströme997a7d2015-11-27 14:23:21 +0100130 VCMGenericDecoder* CreateAndInitDecoder(const VCMEncodedFrame& frame,
Stefan Holmer79064e52015-03-25 15:20:32 +0100131 VideoCodec* new_codec) const;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000132
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +0000133 // Determines whether a new codec has to be created or not.
134 // Checks every setting apart from maxFramerate and startBitrate.
135 bool RequiresEncoderReset(const VideoCodec& send_codec);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000136
137 void DeleteEncoder();
138
139 // Create an internal Decoder given a codec type
140 VCMGenericDecoder* CreateDecoder(VideoCodecType type) const;
141
142 const VCMDecoderMapItem* FindDecoderItem(uint8_t payload_type) const;
143
144 const VCMExtDecoderMapItem* FindExternalDecoderItem(
145 uint8_t payload_type) const;
146
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000147 int number_of_cores_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000148 size_t max_payload_size_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000149 bool periodic_key_frames_;
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +0000150 bool pending_encoder_reset_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000151 VideoCodec send_codec_;
152 VideoCodec receive_codec_;
Peter Boströmab73d132015-10-15 12:01:38 +0200153 uint8_t encoder_payload_type_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000154 VideoEncoder* external_encoder_;
155 bool internal_source_;
Peter Boström4f5db112015-10-29 16:53:59 +0100156 VCMEncodedFrameCallback* const encoded_frame_callback_;
kwiberg3f55dea2016-02-29 05:51:59 -0800157 std::unique_ptr<VCMGenericEncoder> ptr_encoder_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000158 VCMGenericDecoder* ptr_decoder_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000159 DecoderMap dec_map_;
160 ExternalDecoderMap dec_external_map_;
161}; // VCMCodecDataBase
162
163} // namespace webrtc
164
Henrik Kjellander2557b862015-11-18 22:00:21 +0100165#endif // WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_