OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ | |
6 #define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ | |
7 | |
8 #include "base/mac/scoped_cftyperef.h" | |
9 #include "content/common/content_export.h" | |
10 #include "media/base/mac/videotoolbox_glue.h" | |
11 #include "media/base/mac/videotoolbox_helpers.h" | |
12 #include "media/video/video_encode_accelerator.h" | |
13 | |
14 namespace content { | |
15 | |
16 // VideoToolbox.framework implementation of the VideoEncodeAccelerator | |
17 // interface for MacOSX. VideoToolbox makes no guarantees that it is thread | |
18 // safe, so this object is pinned to the thread on which it is constructed. | |
19 class CONTENT_EXPORT VTVideoEncodeAccelerator | |
20 : public media::VideoEncodeAccelerator { | |
21 public: | |
22 VTVideoEncodeAccelerator(); | |
23 ~VTVideoEncodeAccelerator() override; | |
24 | |
25 // media::VideoEncodeAccelerator implementation. | |
26 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() | |
27 override; | |
28 bool Initialize(media::VideoPixelFormat format, | |
29 const gfx::Size& input_visible_size, | |
30 media::VideoCodecProfile output_profile, | |
31 uint32_t initial_bitrate, | |
32 Client* client) override; | |
33 void Encode(const scoped_refptr<media::VideoFrame>& frame, | |
34 bool force_keyframe) override; | |
35 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | |
36 void RequestEncodingParametersChange(uint32_t bitrate, | |
37 uint32_t framerate) override; | |
38 void Destroy() override; | |
39 | |
40 private: | |
41 using CMSampleBufferRef = CoreMediaGlue::CMSampleBufferRef; | |
42 using VTCompressionSessionRef = VideoToolboxGlue::VTCompressionSessionRef; | |
43 using VTEncodeInfoFlags = VideoToolboxGlue::VTEncodeInfoFlags; | |
44 | |
45 // Holds the associated data of a video frame being processed. | |
46 struct InProgressFrameEncode; | |
47 | |
48 // Holds output buffers coming from the encoder. | |
49 struct EncodeOutput; | |
50 | |
51 // Holds output buffers coming from the client ready to be filled. | |
52 struct BitstreamBufferRef; | |
53 | |
54 // Encoding tasks to be run on |encoder_thread_|. | |
55 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame, | |
56 bool force_keyframe); | |
57 void UseOutputBitstreamBufferTask( | |
58 std::unique_ptr<BitstreamBufferRef> buffer_ref); | |
59 void RequestEncodingParametersChangeTask(uint32_t bitrate, | |
60 uint32_t framerate); | |
61 void DestroyTask(); | |
62 | |
63 // Helper function to notify the client of an error on |client_task_runner_|. | |
64 void NotifyError(media::VideoEncodeAccelerator::Error error); | |
65 | |
66 // Compression session callback function to handle compressed frames. | |
67 static void CompressionCallback(void* encoder_opaque, | |
68 void* request_opaque, | |
69 OSStatus status, | |
70 VTEncodeInfoFlags info, | |
71 CMSampleBufferRef sbuf); | |
72 void CompressionCallbackTask(OSStatus status, | |
73 std::unique_ptr<EncodeOutput> encode_output); | |
74 | |
75 // Copy CMSampleBuffer into a BitstreamBuffer and return it to the |client_|. | |
76 void ReturnBitstreamBuffer( | |
77 std::unique_ptr<EncodeOutput> encode_output, | |
78 std::unique_ptr<VTVideoEncodeAccelerator::BitstreamBufferRef> buffer_ref); | |
79 | |
80 // Reset the encoder's compression session by destroying the existing one | |
81 // using DestroyCompressionSession() and creating a new one. The new session | |
82 // is configured using ConfigureCompressionSession(). | |
83 bool ResetCompressionSession(); | |
84 | |
85 // Create a compression session, with HW encoder enforced if | |
86 // |require_hw_encoding| is set. | |
87 bool CreateCompressionSession( | |
88 base::ScopedCFTypeRef<CFDictionaryRef> attributes, | |
89 const gfx::Size& input_size, | |
90 bool require_hw_encoding); | |
91 | |
92 // Configure the current compression session using current encoder settings. | |
93 bool ConfigureCompressionSession(); | |
94 | |
95 // Destroy the current compression session if any. Blocks until all pending | |
96 // frames have been flushed out (similar to EmitFrames without doing any | |
97 // encoding work). | |
98 void DestroyCompressionSession(); | |
99 | |
100 // VideoToolboxGlue provides access to VideoToolbox at runtime. | |
101 const VideoToolboxGlue* videotoolbox_glue_; | |
102 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
103 | |
104 gfx::Size input_visible_size_; | |
105 size_t bitstream_buffer_size_; | |
106 int32_t frame_rate_; | |
107 int32_t target_bitrate_; | |
108 | |
109 // Bitstream buffers ready to be used to return encoded output as a FIFO. | |
110 std::deque<std::unique_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; | |
111 | |
112 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. | |
113 std::deque<std::unique_ptr<EncodeOutput>> encoder_output_queue_; | |
114 | |
115 // Our original calling task runner for the child thread. | |
116 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; | |
117 | |
118 // To expose client callbacks from VideoEncodeAccelerator. | |
119 // NOTE: all calls to this object *MUST* be executed on | |
120 // |client_task_runner_|. | |
121 base::WeakPtr<Client> client_; | |
122 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; | |
123 | |
124 // Thread checker to enforce that this object is used on a specific thread. | |
125 // It is pinned on |client_task_runner_| thread. | |
126 base::ThreadChecker thread_checker_; | |
127 | |
128 // This thread services tasks posted from the VEA API entry points by the | |
129 // GPU child thread and CompressionCallback() posted from device thread. | |
130 base::Thread encoder_thread_; | |
131 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; | |
132 | |
133 // Declared last to ensure that all weak pointers are invalidated before | |
134 // other destructors run. | |
135 base::WeakPtr<VTVideoEncodeAccelerator> encoder_weak_ptr_; | |
136 base::WeakPtrFactory<VTVideoEncodeAccelerator> encoder_task_weak_factory_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(VTVideoEncodeAccelerator); | |
139 }; | |
140 | |
141 } // namespace content | |
142 | |
143 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ | |
OLD | NEW |