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_GENERIC_ENCODER_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_
13 
14 #include <stdio.h>
15 #include <vector>
16 
17 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
18 #include "webrtc/modules/video_coding/include/video_coding_defines.h"
19 
20 #include "webrtc/base/criticalsection.h"
21 #include "webrtc/base/scoped_ptr.h"
22 
23 namespace webrtc {
24 class CriticalSectionWrapper;
25 
26 namespace media_optimization {
27 class MediaOptimization;
28 }  // namespace media_optimization
29 
30 struct EncoderParameters {
31   uint32_t target_bitrate;
32   uint8_t loss_rate;
33   int64_t rtt;
34   uint32_t input_frame_rate;
35 };
36 
37 /*************************************/
38 /* VCMEncodeFrameCallback class     */
39 /***********************************/
40 class VCMEncodedFrameCallback : public EncodedImageCallback {
41  public:
42     explicit VCMEncodedFrameCallback(
43       EncodedImageCallback* post_encode_callback);
44     virtual ~VCMEncodedFrameCallback();
45 
46     /*
47     * Callback implementation - codec encode complete
48     */
49     int32_t Encoded(
50         const EncodedImage& encodedImage,
51         const CodecSpecificInfo* codecSpecificInfo = NULL,
52         const RTPFragmentationHeader* fragmentationHeader = NULL);
53     /*
54     * Callback implementation - generic encoder encode complete
55     */
56     int32_t SetTransportCallback(VCMPacketizationCallback* transport);
57     /**
58     * Set media Optimization
59     */
60     void SetMediaOpt(media_optimization::MediaOptimization* mediaOpt);
61 
SetPayloadType(uint8_t payloadType)62     void SetPayloadType(uint8_t payloadType) {
63       _payloadType = payloadType;
64     }
65 
SetInternalSource(bool internalSource)66     void SetInternalSource(bool internalSource) {
67       _internalSource = internalSource;
68     }
69 
SetRotation(VideoRotation rotation)70     void SetRotation(VideoRotation rotation) { _rotation = rotation; }
71     void SignalLastEncoderImplementationUsed(
72         const char* encoder_implementation_name);
73 
74  private:
75     VCMPacketizationCallback* send_callback_;
76     media_optimization::MediaOptimization* _mediaOpt;
77     uint8_t _payloadType;
78     bool _internalSource;
79     VideoRotation _rotation;
80 
81     EncodedImageCallback* post_encode_callback_;
82 
83 #ifdef DEBUG_ENCODER_BIT_STREAM
84     FILE* _bitStreamAfterEncoder;
85 #endif
86 };  // end of VCMEncodeFrameCallback class
87 
88 /******************************/
89 /* VCMGenericEncoder class    */
90 /******************************/
91 class VCMGenericEncoder {
92   friend class VCMCodecDataBase;
93 
94  public:
95   VCMGenericEncoder(VideoEncoder* encoder,
96                     VideoEncoderRateObserver* rate_observer,
97                     VCMEncodedFrameCallback* encoded_frame_callback,
98                     bool internalSource);
99   ~VCMGenericEncoder();
100   /**
101   * Free encoder memory
102   */
103   int32_t Release();
104   /**
105   * Initialize the encoder with the information from the VideoCodec
106   */
107   int32_t InitEncode(const VideoCodec* settings,
108                      int32_t numberOfCores,
109                      size_t maxPayloadSize);
110   /**
111   * Encode raw image
112   * inputFrame        : Frame containing raw image
113   * codecSpecificInfo : Specific codec data
114   * cameraFrameRate   : Request or information from the remote side
115   * frameType         : The requested frame type to encode
116   */
117   int32_t Encode(const VideoFrame& inputFrame,
118                  const CodecSpecificInfo* codecSpecificInfo,
119                  const std::vector<FrameType>& frameTypes);
120 
121   void SetEncoderParameters(const EncoderParameters& params);
122   EncoderParameters GetEncoderParameters() const;
123 
124   int32_t SetPeriodicKeyFrames(bool enable);
125 
126   int32_t RequestFrame(const std::vector<FrameType>& frame_types);
127 
128   bool InternalSource() const;
129 
130   void OnDroppedFrame();
131 
132   bool SupportsNativeHandle() const;
133 
134   int GetTargetFramerate();
135 
136  private:
137   VideoEncoder* const encoder_;
138   VideoEncoderRateObserver* const rate_observer_;
139   VCMEncodedFrameCallback* const vcm_encoded_frame_callback_;
140   const bool internal_source_;
141   mutable rtc::CriticalSection params_lock_;
142   EncoderParameters encoder_params_ GUARDED_BY(params_lock_);
143   VideoRotation rotation_;
144   bool is_screenshare_;
145 };  // end of VCMGenericEncoder class
146 
147 }  // namespace webrtc
148 
149 #endif  // WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_
150