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