blob: 5f8656df8d82f2179a3fe5a752f98e3b6646b0f4 [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
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000101 // Returns a decoder specified by |payload_type|. The decoded frame callback
102 // of the encoder is set to |decoded_frame_callback|. If no such decoder
103 // already exists an instance will be created and initialized.
104 // NULL is returned if no encoder with the specified payload type was found
105 // and the function failed to create one.
106 VCMGenericDecoder* GetDecoder(
Peter Boströme997a7d2015-11-27 14:23:21 +0100107 const VCMEncodedFrame& frame,
108 VCMDecodedFrameCallback* decoded_frame_callback);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000109
tommi5b7fc8c2017-07-05 16:45:57 -0700110 // Returns the current decoder (i.e. the same value as was last returned from
111 // GetDecoder();
112 VCMGenericDecoder* GetCurrentDecoder();
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000113
perkj796cfaf2015-12-10 09:27:38 -0800114 // Returns true if the currently active decoder prefer to decode frames late.
115 // That means that frames must be decoded near the render times stamp.
116 bool PrefersLateDecoding() const;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000117
pbos@webrtc.org67a9e402015-03-05 13:57:37 +0000118 bool MatchesCurrentResolution(int width, int height) const;
119
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000120 private:
121 typedef std::map<uint8_t, VCMDecoderMapItem*> DecoderMap;
122 typedef std::map<uint8_t, VCMExtDecoderMapItem*> ExternalDecoderMap;
123
tommi5b7fc8c2017-07-05 16:45:57 -0700124 std::unique_ptr<VCMGenericDecoder> CreateAndInitDecoder(
125 const VCMEncodedFrame& frame,
126 VideoCodec* new_codec) const;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000127
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +0000128 // Determines whether a new codec has to be created or not.
129 // Checks every setting apart from maxFramerate and startBitrate.
130 bool RequiresEncoderReset(const VideoCodec& send_codec);
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000131
132 void DeleteEncoder();
133
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000134 const VCMDecoderMapItem* FindDecoderItem(uint8_t payload_type) const;
135
136 const VCMExtDecoderMapItem* FindExternalDecoderItem(
137 uint8_t payload_type) const;
138
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000139 int number_of_cores_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000140 size_t max_payload_size_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000141 bool periodic_key_frames_;
pbos@webrtc.orgb9bb3d12013-05-16 18:40:48 +0000142 bool pending_encoder_reset_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000143 VideoCodec send_codec_;
144 VideoCodec receive_codec_;
Peter Boströmab73d132015-10-15 12:01:38 +0200145 uint8_t encoder_payload_type_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000146 VideoEncoder* external_encoder_;
147 bool internal_source_;
Peter Boström4f5db112015-10-29 16:53:59 +0100148 VCMEncodedFrameCallback* const encoded_frame_callback_;
kwiberg3f55dea2016-02-29 05:51:59 -0800149 std::unique_ptr<VCMGenericEncoder> ptr_encoder_;
tommi5b7fc8c2017-07-05 16:45:57 -0700150 std::unique_ptr<VCMGenericDecoder> ptr_decoder_;
stefan@webrtc.orgfa7e8682012-10-11 11:21:38 +0000151 DecoderMap dec_map_;
152 ExternalDecoderMap dec_external_map_;
153}; // VCMCodecDataBase
154
155} // namespace webrtc
156
Henrik Kjellander2557b862015-11-18 22:00:21 +0100157#endif // WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_