[Mac] Allow customizing H264 encoder
[WebKit.git] / Source / ThirdParty / libwebrtc / Source / webrtc / sdk / objc / Framework / Classes / h264_video_toolbox_encoder.h
1 /*
2  *  Copyright (c) 2015 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
12 #ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_ENCODER_H_
13 #define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_ENCODER_H_
14
15 #include "webrtc/api/video/video_rotation.h"
16 #include "webrtc/base/criticalsection.h"
17 #include "webrtc/common_video/h264/h264_bitstream_parser.h"
18 #include "webrtc/common_video/include/bitrate_adjuster.h"
19 #include "webrtc/media/base/codec.h"
20 #include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
21 #include "webrtc/modules/video_coding/utility/quality_scaler.h"
22
23 #include <VideoToolbox/VideoToolbox.h>
24 #include <vector>
25
26 // This file provides a H264 encoder implementation using the VideoToolbox
27 // APIs. Since documentation is almost non-existent, this is largely based on
28 // the information in the VideoToolbox header files, a talk from WWDC 2014 and
29 // experimentation.
30
31 namespace webrtc {
32
33 class WEBRTC_DYLIB_EXPORT H264VideoToolboxEncoder : public H264Encoder {
34  public:
35   explicit H264VideoToolboxEncoder(const cricket::VideoCodec& codec);
36
37   ~H264VideoToolboxEncoder() override;
38
39   int InitEncode(const VideoCodec* codec_settings,
40                  int number_of_cores,
41                  size_t max_payload_size) override;
42
43   int Encode(const VideoFrame& input_image,
44              const CodecSpecificInfo* codec_specific_info,
45              const std::vector<FrameType>* frame_types) override;
46
47   int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
48
49   int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
50
51   int SetRates(uint32_t new_bitrate_kbit, uint32_t frame_rate) override;
52
53   int Release() override;
54
55   const char* ImplementationName() const override;
56
57   bool SupportsNativeHandle() const override;
58
59   void OnEncodedFrame(OSStatus status,
60                       VTEncodeInfoFlags info_flags,
61                       CMSampleBufferRef sample_buffer,
62                       CodecSpecificInfo codec_specific_info,
63                       int32_t width,
64                       int32_t height,
65                       int64_t render_time_ms,
66                       uint32_t timestamp,
67                       VideoRotation rotation);
68
69   ScalingSettings GetScalingSettings() const override;
70
71  protected:
72   virtual int CreateCompressionSession(VTCompressionSessionRef&, VTCompressionOutputCallback, int32_t width, int32_t height);
73
74  private:
75   int ResetCompressionSession();
76   void ConfigureCompressionSession();
77   void DestroyCompressionSession();
78   rtc::scoped_refptr<VideoFrameBuffer> GetScaledBufferOnEncode(
79       const rtc::scoped_refptr<VideoFrameBuffer>& frame);
80   void SetBitrateBps(uint32_t bitrate_bps);
81   void SetEncoderBitrateBps(uint32_t bitrate_bps);
82
83   EncodedImageCallback* callback_;
84   VTCompressionSessionRef compression_session_;
85   BitrateAdjuster bitrate_adjuster_;
86   H264PacketizationMode packetization_mode_;
87   uint32_t target_bitrate_bps_;
88   uint32_t encoder_bitrate_bps_;
89   int32_t width_;
90   int32_t height_;
91   const CFStringRef profile_;
92
93   H264BitstreamParser h264_bitstream_parser_;
94   std::vector<uint8_t> nv12_scale_buffer_;
95 };  // H264VideoToolboxEncoder
96
97 }  // namespace webrtc
98
99 #endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_H264_VIDEO_TOOLBOX_ENCODER_H_