1 /*
2  *  Copyright (c) 2018 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 MODULES_VIDEO_CODING_DECODER_DATABASE_H_
12 #define MODULES_VIDEO_CODING_DECODER_DATABASE_H_
13 
14 #include <map>
15 #include <memory>
16 
17 #include "modules/video_coding/generic_decoder.h"
18 
19 namespace webrtc {
20 
21 struct VCMDecoderMapItem {
22  public:
23   VCMDecoderMapItem(VideoCodec* settings,
24                     int number_of_cores,
25                     bool require_key_frame);
26   ~VCMDecoderMapItem();
27 
28   std::unique_ptr<VideoCodec> settings;
29   int number_of_cores;
30   bool require_key_frame;
31 };
32 
33 struct VCMExtDecoderMapItem {
34  public:
35   VCMExtDecoderMapItem(VideoDecoder* external_decoder_instance,
36                        uint8_t payload_type);
37 
38   uint8_t payload_type;
39   VideoDecoder* external_decoder_instance;
40 };
41 
42 class VCMDecoderDataBase {
43  public:
44   VCMDecoderDataBase();
45   ~VCMDecoderDataBase();
46 
47   bool DeregisterExternalDecoder(uint8_t payload_type);
48   void RegisterExternalDecoder(VideoDecoder* external_decoder,
49                                uint8_t payload_type);
50 
51   bool RegisterReceiveCodec(const VideoCodec* receive_codec,
52                             int number_of_cores,
53                             bool require_key_frame);
54   bool DeregisterReceiveCodec(uint8_t payload_type);
55 
56   // Returns a decoder specified by frame.PayloadType. The decoded frame
57   // callback of the decoder is set to |decoded_frame_callback|. If no such
58   // decoder already exists an instance will be created and initialized.
59   // nullptr is returned if no decoder with the specified payload type was found
60   // and the function failed to create one.
61   VCMGenericDecoder* GetDecoder(
62       const VCMEncodedFrame& frame,
63       VCMDecodedFrameCallback* decoded_frame_callback);
64 
65   // Returns true if the currently active decoder prefer to decode frames late.
66   // That means that frames must be decoded near the render times stamp.
67   bool PrefersLateDecoding() const;
68 
69  private:
70   typedef std::map<uint8_t, VCMDecoderMapItem*> DecoderMap;
71   typedef std::map<uint8_t, VCMExtDecoderMapItem*> ExternalDecoderMap;
72 
73   std::unique_ptr<VCMGenericDecoder> CreateAndInitDecoder(
74       const VCMEncodedFrame& frame,
75       VideoCodec* new_codec) const;
76 
77   const VCMDecoderMapItem* FindDecoderItem(uint8_t payload_type) const;
78 
79   const VCMExtDecoderMapItem* FindExternalDecoderItem(
80       uint8_t payload_type) const;
81 
82   VideoCodec receive_codec_;
83   std::unique_ptr<VCMGenericDecoder> ptr_decoder_;
84   DecoderMap dec_map_;
85   ExternalDecoderMap dec_external_map_;
86 };
87 
88 }  // namespace webrtc
89 
90 #endif  // MODULES_VIDEO_CODING_DECODER_DATABASE_H_
91