OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 5 #ifndef CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ |
6 #define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 6 #define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ |
7 | |
8 #include <stdint.h> | |
9 | 7 |
10 #include "base/mac/scoped_cftyperef.h" | 8 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/macros.h" | 9 #include "content/common/content_export.h" |
12 #include "base/power_monitor/power_observer.h" | |
13 #include "base/threading/thread_checker.h" | |
14 #include "media/base/mac/videotoolbox_glue.h" | 10 #include "media/base/mac/videotoolbox_glue.h" |
15 #include "media/cast/sender/size_adaptable_video_encoder_base.h" | 11 #include "media/base/mac/videotoolbox_helpers.h" |
16 #include "media/cast/sender/video_encoder.h" | 12 #include "media/video/video_encode_accelerator.h" |
17 | 13 |
18 namespace media { | 14 namespace content { |
19 namespace cast { | |
20 | 15 |
21 // VideoToolbox implementation of the media::cast::VideoEncoder interface. | 16 // VideoToolbox.framework implementation of the VideoEncodeAccelerator |
22 // VideoToolbox makes no guarantees that it is thread safe, so this object is | 17 // interface for MacOSX. VideoToolbox makes no guarantees that it is thread |
23 // pinned to the thread on which it is constructed. Supports changing frame | 18 // safe, so this object is pinned to the thread on which it is constructed. |
24 // sizes directly. Implements the base::PowerObserver interface to reset the | 19 class CONTENT_EXPORT VTVideoEncodeAccelerator |
25 // compression session when the host process is suspended. | 20 : public media::VideoEncodeAccelerator { |
26 class H264VideoToolboxEncoder : public VideoEncoder, | 21 public: |
27 public base::PowerObserver { | 22 VTVideoEncodeAccelerator(); |
28 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | 23 ~VTVideoEncodeAccelerator() override; |
29 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | |
30 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | |
31 | 24 |
32 public: | 25 // media::VideoEncodeAccelerator implementation. |
33 // Returns true if the current platform and system configuration supports | 26 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() |
34 // using H264VideoToolboxEncoder with the given |video_config|. | 27 override; |
35 static bool IsSupported(const VideoSenderConfig& video_config); | 28 bool Initialize(media::VideoPixelFormat format, |
36 | 29 const gfx::Size& input_visible_size, |
37 H264VideoToolboxEncoder( | 30 media::VideoCodecProfile output_profile, |
38 const scoped_refptr<CastEnvironment>& cast_environment, | 31 uint32_t initial_bitrate, |
39 const VideoSenderConfig& video_config, | 32 Client* client) override; |
40 const StatusChangeCallback& status_change_cb); | 33 void Encode(const scoped_refptr<media::VideoFrame>& frame, |
41 ~H264VideoToolboxEncoder() final; | 34 bool force_keyframe) override; |
42 | 35 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; |
43 // media::cast::VideoEncoder implementation | 36 void RequestEncodingParametersChange(uint32_t bitrate, |
44 bool EncodeVideoFrame( | 37 uint32_t framerate) override; |
45 const scoped_refptr<media::VideoFrame>& video_frame, | 38 void Destroy() override; |
46 const base::TimeTicks& reference_time, | |
47 const FrameEncodedCallback& frame_encoded_callback) final; | |
48 void SetBitRate(int new_bit_rate) final; | |
49 void GenerateKeyFrame() final; | |
50 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() final; | |
51 void EmitFrames() final; | |
52 | |
53 // base::PowerObserver | |
54 void OnSuspend() final; | |
55 void OnResume() final; | |
56 | 39 |
57 private: | 40 private: |
58 // VideoFrameFactory tied to the VideoToolbox encoder. | 41 using CMSampleBufferRef = CoreMediaGlue::CMSampleBufferRef; |
59 class VideoFrameFactoryImpl; | 42 using VTCompressionSessionRef = VideoToolboxGlue::VTCompressionSessionRef; |
| 43 using VTEncodeInfoFlags = VideoToolboxGlue::VTEncodeInfoFlags; |
60 | 44 |
61 // Reset the encoder's compression session by destroying the existing one | 45 // Holds the associated data of a video frame being processed. |
62 // using DestroyCompressionSession() and creating a new one. The new session | 46 struct InProgressFrameEncode; |
63 // is configured using ConfigureCompressionSession(). | |
64 void ResetCompressionSession(); | |
65 | 47 |
66 // Configure the current compression session using current encoder settings. | 48 // Holds output buffers coming from the encoder. |
67 void ConfigureCompressionSession(); | 49 struct EncodeOutput; |
68 | 50 |
69 // Destroy the current compression session if any. Blocks until all pending | 51 // Holds output buffers coming from the client ready to be filled. |
70 // frames have been flushed out (similar to EmitFrames without doing any | 52 struct BitstreamBufferRef; |
71 // encoding work). | |
72 void DestroyCompressionSession(); | |
73 | 53 |
74 // Update the encoder's target frame size by resetting the compression | 54 // Encoding tasks to be run on |encoder_thread_|. |
75 // session. This will also update the video frame factory. | 55 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame, |
76 void UpdateFrameSize(const gfx::Size& size_needed); | 56 bool force_keyframe); |
| 57 void UseOutputBitstreamBufferTask(scoped_ptr<BitstreamBufferRef> buffer_ref); |
| 58 void RequestEncodingParametersChangeTask(uint32_t bitrate, |
| 59 uint32_t framerate); |
| 60 void DestroyTask(); |
77 | 61 |
78 // Set a compression session property. | 62 // Helper function to notify the client of an error on |client_task_runner_|. |
79 bool SetSessionProperty(CFStringRef key, int32_t value); | 63 void NotifyError(media::VideoEncodeAccelerator::Error error); |
80 bool SetSessionProperty(CFStringRef key, bool value); | |
81 bool SetSessionProperty(CFStringRef key, CFStringRef value); | |
82 | 64 |
83 // Compression session callback function to handle compressed frames. | 65 // Compression session callback function to handle compressed frames. |
84 static void CompressionCallback(void* encoder_opaque, | 66 static void CompressionCallback(void* encoder_opaque, |
85 void* request_opaque, | 67 void* request_opaque, |
86 OSStatus status, | 68 OSStatus status, |
87 VTEncodeInfoFlags info, | 69 VTEncodeInfoFlags info, |
88 CMSampleBufferRef sbuf); | 70 CMSampleBufferRef sbuf); |
| 71 void CompressionCallbackTask(OSStatus status, |
| 72 scoped_ptr<EncodeOutput> encode_output); |
89 | 73 |
90 // The cast environment (contains worker threads & more). | 74 // Copy CMSampleBuffer into a BitstreamBuffer and return it to the |client_|. |
91 const scoped_refptr<CastEnvironment> cast_environment_; | 75 void ReturnBitstreamBuffer( |
| 76 scoped_ptr<EncodeOutput> encode_output, |
| 77 scoped_ptr<VTVideoEncodeAccelerator::BitstreamBufferRef> buffer_ref); |
| 78 |
| 79 // Reset the encoder's compression session by destroying the existing one |
| 80 // using DestroyCompressionSession() and creating a new one. The new session |
| 81 // is configured using ConfigureCompressionSession(). |
| 82 bool ResetCompressionSession(); |
| 83 |
| 84 // Create a compression session, with HW encoder enforced if |
| 85 // |require_hw_encoding| is set. |
| 86 bool CreateCompressionSession( |
| 87 base::ScopedCFTypeRef<CFDictionaryRef> attributes, |
| 88 const gfx::Size& input_size, |
| 89 bool require_hw_encoding); |
| 90 |
| 91 // Configure the current compression session using current encoder settings. |
| 92 bool ConfigureCompressionSession(); |
| 93 |
| 94 // Destroy the current compression session if any. Blocks until all pending |
| 95 // frames have been flushed out (similar to EmitFrames without doing any |
| 96 // encoding work). |
| 97 void DestroyCompressionSession(); |
92 | 98 |
93 // VideoToolboxGlue provides access to VideoToolbox at runtime. | 99 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
94 const VideoToolboxGlue* const videotoolbox_glue_; | 100 const VideoToolboxGlue* videotoolbox_glue_; |
| 101 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; |
95 | 102 |
96 // VideoSenderConfig copy so we can create compression sessions on demand. | 103 gfx::Size input_visible_size_; |
97 // This is needed to recover from backgrounding and other events that can | 104 size_t bitstream_buffer_size_; |
98 // invalidate compression sessions. | 105 int32_t frame_rate_; |
99 const VideoSenderConfig video_config_; | 106 int32_t target_bitrate_; |
100 | 107 |
101 // Frame size of the current compression session. Can be changed by submitting | 108 // Bitstream buffers ready to be used to return encoded output as a FIFO. |
102 // a frame of a different size, which will cause a compression session reset. | 109 std::deque<scoped_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; |
103 gfx::Size frame_size_; | |
104 | 110 |
105 // Callback used to report initialization status and runtime errors. | 111 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. |
106 const StatusChangeCallback status_change_cb_; | 112 std::deque<scoped_ptr<EncodeOutput>> encoder_output_queue_; |
| 113 |
| 114 // Our original calling task runner for the child thread. |
| 115 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; |
| 116 |
| 117 // To expose client callbacks from VideoEncodeAccelerator. |
| 118 // NOTE: all calls to this object *MUST* be executed on |
| 119 // |client_task_runner_|. |
| 120 base::WeakPtr<Client> client_; |
| 121 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; |
107 | 122 |
108 // Thread checker to enforce that this object is used on a specific thread. | 123 // Thread checker to enforce that this object is used on a specific thread. |
| 124 // It is pinned on |client_task_runner_| thread. |
109 base::ThreadChecker thread_checker_; | 125 base::ThreadChecker thread_checker_; |
110 | 126 |
111 // The compression session. | 127 // This thread services tasks posted from the VEA API entry points by the |
112 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | 128 // GPU child thread and CompressionCallback() posted from device thread. |
| 129 base::Thread encoder_thread_; |
| 130 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; |
113 | 131 |
114 // Video frame factory tied to the encoder. | 132 // Declared last to ensure that all weak pointers are invalidated before |
115 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; | 133 // other destructors run. |
| 134 base::WeakPtr<VTVideoEncodeAccelerator> encoder_weak_ptr_; |
| 135 base::WeakPtrFactory<VTVideoEncodeAccelerator> encoder_task_weak_factory_; |
116 | 136 |
117 // The ID of the last frame that was emitted. | 137 DISALLOW_COPY_AND_ASSIGN(VTVideoEncodeAccelerator); |
118 uint32_t last_frame_id_; | |
119 | |
120 // Force next frame to be a keyframe. | |
121 bool encode_next_frame_as_keyframe_; | |
122 | |
123 // Power suspension state. | |
124 bool power_suspended_; | |
125 | |
126 // NOTE: Weak pointers must be invalidated before all other member variables. | |
127 base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); | |
130 }; | 138 }; |
131 | 139 |
132 } // namespace cast | 140 } // namespace content |
133 } // namespace media | |
134 | 141 |
135 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 142 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ |
OLD | NEW |