1 /* 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 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_CODEC_DATABASE_H_ 12 #define WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_CODEC_DATABASE_H_ 13 14 #include <map> 15 16 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" 17 #include "webrtc/modules/video_coding/main/interface/video_coding.h" 18 #include "webrtc/modules/video_coding/main/source/generic_decoder.h" 19 #include "webrtc/modules/video_coding/main/source/generic_encoder.h" 20 #include "webrtc/system_wrappers/interface/scoped_ptr.h" 21 #include "webrtc/typedefs.h" 22 23 namespace webrtc { 24 25 enum VCMCodecDBProperties { 26 kDefaultPayloadSize = 1440 27 }; 28 29 struct VCMDecoderMapItem { 30 public: 31 VCMDecoderMapItem(VideoCodec* settings, 32 int number_of_cores, 33 bool require_key_frame); 34 35 scoped_ptr<VideoCodec> settings; 36 int number_of_cores; 37 bool require_key_frame; 38 }; 39 40 struct VCMExtDecoderMapItem { 41 public: 42 VCMExtDecoderMapItem(VideoDecoder* external_decoder_instance, 43 uint8_t payload_type, 44 bool internal_render_timing); 45 46 uint8_t payload_type; 47 VideoDecoder* external_decoder_instance; 48 bool internal_render_timing; 49 }; 50 51 class VCMCodecDataBase { 52 public: 53 VCMCodecDataBase(); 54 ~VCMCodecDataBase(); 55 56 // Sender Side 57 // Returns the number of supported codecs (or -1 in case of error). 58 static int NumberOfCodecs(); 59 60 // Returns the default settings for the codec with id |list_id|. 61 static bool Codec(int list_id, VideoCodec* settings); 62 63 // Returns the default settings for the codec with type |codec_type|. 64 static bool Codec(VideoCodecType codec_type, VideoCodec* settings); 65 66 void ResetSender(); 67 68 // Sets the sender side codec and initiates the desired codec given the 69 // VideoCodec struct. 70 // Returns true if the codec was successfully registered, false otherwise. 71 bool SetSendCodec(const VideoCodec* send_codec, 72 int number_of_cores, 73 int max_payload_size, 74 VCMEncodedFrameCallback* encoded_frame_callback); 75 76 // Gets the current send codec. Relevant for internal codecs only. 77 // Returns true if there is a send codec, false otherwise. 78 bool SendCodec(VideoCodec* current_send_codec) const; 79 80 // Gets current send side codec type. Relevant for internal codecs only. 81 // Returns kVideoCodecUnknown if there is no send codec. 82 VideoCodecType SendCodec() const; 83 84 // Registers and initializes an external encoder object. 85 // |internal_source| should be set to true if the codec has an internal 86 // video source and doesn't need the user to provide it with frames via 87 // the Encode() method. 88 void RegisterExternalEncoder(VideoEncoder* external_encoder, 89 uint8_t payload_type, 90 bool internal_source); 91 92 // Deregisters an external encoder. Returns true if the encoder was 93 // found and deregistered, false otherwise. |was_send_codec| is set to true 94 // if the external encoder was the send codec before being deregistered. 95 bool DeregisterExternalEncoder(uint8_t payload_type, bool* was_send_codec); 96 97 VCMGenericEncoder* GetEncoder(); 98 99 bool SetPeriodicKeyFrames(bool enable); 100 101 // Receiver Side 102 void ResetReceiver(); 103 104 // Deregisters an external decoder object specified by |payload_type|. 105 bool DeregisterExternalDecoder(uint8_t payload_type); 106 107 // Registers an external decoder object to the payload type |payload_type|. 108 // |internal_render_timing| is set to true if the |external_decoder| has 109 // built in rendering which is able to obey the render timestamps of the 110 // encoded frames. 111 bool RegisterExternalDecoder(VideoDecoder* external_decoder, 112 uint8_t payload_type, 113 bool internal_render_timing); 114 115 bool DecoderRegistered() const; 116 117 bool RegisterReceiveCodec(const VideoCodec* receive_codec, 118 int number_of_cores, 119 bool require_key_frame); 120 121 bool DeregisterReceiveCodec(uint8_t payload_type); 122 123 // Get current receive side codec. Relevant for internal codecs only. 124 bool ReceiveCodec(VideoCodec* current_receive_codec) const; 125 126 // Get current receive side codec type. Relevant for internal codecs only. 127 VideoCodecType ReceiveCodec() const; 128 129 // Returns a decoder specified by |payload_type|. The decoded frame callback 130 // of the encoder is set to |decoded_frame_callback|. If no such decoder 131 // already exists an instance will be created and initialized. 132 // NULL is returned if no encoder with the specified payload type was found 133 // and the function failed to create one. 134 VCMGenericDecoder* GetDecoder( 135 uint8_t payload_type, VCMDecodedFrameCallback* decoded_frame_callback); 136 137 // Returns a deep copy of the currently active decoder. 138 VCMGenericDecoder* CreateDecoderCopy() const; 139 140 // Deletes the memory of the decoder instance |decoder|. Used to delete 141 // deep copies returned by CreateDecoderCopy(). 142 void ReleaseDecoder(VCMGenericDecoder* decoder) const; 143 144 // Creates a deep copy of |decoder| and replaces the currently used decoder 145 // with it. 146 void CopyDecoder(const VCMGenericDecoder& decoder); 147 148 // Returns true if the currently active decoder supports render scheduling, 149 // that is, it is able to render frames according to the render timestamp of 150 // the encoded frames. 151 bool SupportsRenderScheduling() const; 152 153 private: 154 typedef std::map<uint8_t, VCMDecoderMapItem*> DecoderMap; 155 typedef std::map<uint8_t, VCMExtDecoderMapItem*> ExternalDecoderMap; 156 157 VCMGenericDecoder* CreateAndInitDecoder(uint8_t payload_type, 158 VideoCodec* new_codec, 159 bool* external) const; 160 161 // Determines whether a new codec has to be created or not. 162 // Checks every setting apart from maxFramerate and startBitrate. 163 bool RequiresEncoderReset(const VideoCodec& send_codec); 164 // Create an internal encoder given a codec type. 165 VCMGenericEncoder* CreateEncoder(const VideoCodecType type) const; 166 167 void DeleteEncoder(); 168 169 // Create an internal Decoder given a codec type 170 VCMGenericDecoder* CreateDecoder(VideoCodecType type) const; 171 172 const VCMDecoderMapItem* FindDecoderItem(uint8_t payload_type) const; 173 174 const VCMExtDecoderMapItem* FindExternalDecoderItem( 175 uint8_t payload_type) const; 176 177 int number_of_cores_; 178 int max_payload_size_; 179 bool periodic_key_frames_; 180 bool pending_encoder_reset_; 181 bool current_enc_is_external_; 182 VideoCodec send_codec_; 183 VideoCodec receive_codec_; 184 uint8_t external_payload_type_; 185 VideoEncoder* external_encoder_; 186 bool internal_source_; 187 VCMGenericEncoder* ptr_encoder_; 188 VCMGenericDecoder* ptr_decoder_; 189 bool current_dec_is_external_; 190 DecoderMap dec_map_; 191 ExternalDecoderMap dec_external_map_; 192 }; // VCMCodecDataBase 193 194 } // namespace webrtc 195 196 #endif // WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_CODEC_DATABASE_H_ 197