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_CODEC_DATABASE_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_
13 
14 #include <map>
15 
16 #include "webrtc/base/scoped_ptr.h"
17 #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"
21 #include "webrtc/typedefs.h"
22 
23 namespace webrtc {
24 
25 struct VCMDecoderMapItem {
26  public:
27   VCMDecoderMapItem(VideoCodec* settings,
28                     int number_of_cores,
29                     bool require_key_frame);
30 
31   rtc::scoped_ptr<VideoCodec> settings;
32   int number_of_cores;
33   bool require_key_frame;
34 };
35 
36 struct VCMExtDecoderMapItem {
37  public:
38   VCMExtDecoderMapItem(VideoDecoder* external_decoder_instance,
39                        uint8_t payload_type);
40 
41   uint8_t payload_type;
42   VideoDecoder* external_decoder_instance;
43 };
44 
45 class VCMCodecDataBase {
46  public:
47   VCMCodecDataBase(VideoEncoderRateObserver* encoder_rate_observer,
48                    VCMEncodedFrameCallback* encoded_frame_callback);
49   ~VCMCodecDataBase();
50 
51   // Sender Side
52   // Returns the default settings for the codec with type |codec_type|.
53   static void Codec(VideoCodecType codec_type, VideoCodec* settings);
54 
55   // Sets the sender side codec and initiates the desired codec given the
56   // VideoCodec struct.
57   // Returns true if the codec was successfully registered, false otherwise.
58   bool SetSendCodec(const VideoCodec* send_codec,
59                     int number_of_cores,
60                     size_t max_payload_size);
61 
62   // Gets the current send codec. Relevant for internal codecs only.
63   // Returns true if there is a send codec, false otherwise.
64   bool SendCodec(VideoCodec* current_send_codec) const;
65 
66   // Gets current send side codec type. Relevant for internal codecs only.
67   // Returns kVideoCodecUnknown if there is no send codec.
68   VideoCodecType SendCodec() const;
69 
70   // Registers and initializes an external encoder object.
71   // |internal_source| should be set to true if the codec has an internal
72   // video source and doesn't need the user to provide it with frames via
73   // the Encode() method.
74   void RegisterExternalEncoder(VideoEncoder* external_encoder,
75                                uint8_t payload_type,
76                                bool internal_source);
77 
78   // Deregisters an external encoder. Returns true if the encoder was
79   // found and deregistered, false otherwise. |was_send_codec| is set to true
80   // if the external encoder was the send codec before being deregistered.
81   bool DeregisterExternalEncoder(uint8_t payload_type, bool* was_send_codec);
82 
83   VCMGenericEncoder* GetEncoder();
84 
85   bool SetPeriodicKeyFrames(bool enable);
86 
87   // Deregisters an external decoder object specified by |payload_type|.
88   bool DeregisterExternalDecoder(uint8_t payload_type);
89 
90   // Registers an external decoder object to the payload type |payload_type|.
91   void RegisterExternalDecoder(VideoDecoder* external_decoder,
92                                uint8_t payload_type);
93 
94   bool DecoderRegistered() const;
95 
96   bool RegisterReceiveCodec(const VideoCodec* receive_codec,
97                             int number_of_cores,
98                             bool require_key_frame);
99 
100   bool DeregisterReceiveCodec(uint8_t payload_type);
101 
102   // Get current receive side codec. Relevant for internal codecs only.
103   bool ReceiveCodec(VideoCodec* current_receive_codec) const;
104 
105   // Get current receive side codec type. Relevant for internal codecs only.
106   VideoCodecType ReceiveCodec() const;
107 
108   // Returns a decoder specified by |payload_type|. The decoded frame callback
109   // of the encoder is set to |decoded_frame_callback|. If no such decoder
110   // already exists an instance will be created and initialized.
111   // NULL is returned if no encoder with the specified payload type was found
112   // and the function failed to create one.
113   VCMGenericDecoder* GetDecoder(
114       const VCMEncodedFrame& frame,
115       VCMDecodedFrameCallback* decoded_frame_callback);
116 
117   // Deletes the memory of the decoder instance |decoder|. Used to delete
118   // deep copies returned by CreateDecoderCopy().
119   void ReleaseDecoder(VCMGenericDecoder* decoder) const;
120 
121   // Returns true if the currently active decoder prefer to decode frames late.
122   // That means that frames must be decoded near the render times stamp.
123   bool PrefersLateDecoding() const;
124 
125   bool MatchesCurrentResolution(int width, int height) const;
126 
127  private:
128   typedef std::map<uint8_t, VCMDecoderMapItem*> DecoderMap;
129   typedef std::map<uint8_t, VCMExtDecoderMapItem*> ExternalDecoderMap;
130 
131   VCMGenericDecoder* CreateAndInitDecoder(const VCMEncodedFrame& frame,
132                                           VideoCodec* new_codec) const;
133 
134   // Determines whether a new codec has to be created or not.
135   // Checks every setting apart from maxFramerate and startBitrate.
136   bool RequiresEncoderReset(const VideoCodec& send_codec);
137 
138   void DeleteEncoder();
139 
140   // Create an internal Decoder given a codec type
141   VCMGenericDecoder* CreateDecoder(VideoCodecType type) const;
142 
143   const VCMDecoderMapItem* FindDecoderItem(uint8_t payload_type) const;
144 
145   const VCMExtDecoderMapItem* FindExternalDecoderItem(
146       uint8_t payload_type) const;
147 
148   int number_of_cores_;
149   size_t max_payload_size_;
150   bool periodic_key_frames_;
151   bool pending_encoder_reset_;
152   VideoCodec send_codec_;
153   VideoCodec receive_codec_;
154   uint8_t encoder_payload_type_;
155   VideoEncoder* external_encoder_;
156   bool internal_source_;
157   VideoEncoderRateObserver* const encoder_rate_observer_;
158   VCMEncodedFrameCallback* const encoded_frame_callback_;
159   rtc::scoped_ptr<VCMGenericEncoder> ptr_encoder_;
160   VCMGenericDecoder* ptr_decoder_;
161   DecoderMap dec_map_;
162   ExternalDecoderMap dec_external_map_;
163 };  // VCMCodecDataBase
164 
165 }  // namespace webrtc
166 
167 #endif  // WEBRTC_MODULES_VIDEO_CODING_CODEC_DATABASE_H_
168