| 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(scoped_ptr<BitstreamBufferRef> buffer_ref); | |
| 58 void RequestEncodingParametersChangeTask(uint32_t bitrate, | |
| 59 uint32_t framerate); | |
| 60 void DestroyTask(); | |
| 61 | |
| 62 // Helper function to notify the client of an error on |client_task_runner_|. | |
| 63 void NotifyError(media::VideoEncodeAccelerator::Error error); | |
| 64 | |
| 65 // Compression session callback function to handle compressed frames. | |
| 66 static void CompressionCallback(void* encoder_opaque, | |
| 67 void* request_opaque, | |
| 68 OSStatus status, | |
| 69 VTEncodeInfoFlags info, | |
| 70 CMSampleBufferRef sbuf); | |
| 71 void CompressionCallbackTask(OSStatus status, | |
| 72 scoped_ptr<EncodeOutput> encode_output); | |
| 73 | |
| 74 // Copy CMSampleBuffer into a BitstreamBuffer and return it to the |client_|. | |
| 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(); | |
| 98 | |
| 99 // VideoToolboxGlue provides access to VideoToolbox at runtime. | |
| 100 const VideoToolboxGlue* videotoolbox_glue_; | |
| 101 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
| 102 | |
| 103 gfx::Size input_visible_size_; | |
| 104 size_t bitstream_buffer_size_; | |
| 105 int32_t frame_rate_; | |
| 106 int32_t target_bitrate_; | |
| 107 | |
| 108 // Bitstream buffers ready to be used to return encoded output as a FIFO. | |
| 109 std::deque<scoped_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; | |
| 110 | |
| 111 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. | |
| 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_; | |
| 122 | |
| 123 // Thread checker to enforce that this object is used on a specific thread. | |
| 124 // It is pinned on |client_task_runner_| thread. | |
| 125 base::ThreadChecker thread_checker_; | |
| 126 | |
| 127 // This thread services tasks posted from the VEA API entry points by the | |
| 128 // GPU child thread and CompressionCallback() posted from device thread. | |
| 129 base::Thread encoder_thread_; | |
| 130 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; | |
| 131 | |
| 132 // Declared last to ensure that all weak pointers are invalidated before | |
| 133 // other destructors run. | |
| 134 base::WeakPtr<VTVideoEncodeAccelerator> encoder_weak_ptr_; | |
| 135 base::WeakPtrFactory<VTVideoEncodeAccelerator> encoder_task_weak_factory_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(VTVideoEncodeAccelerator); | |
| 138 }; | |
| 139 | |
| 140 } // namespace content | |
| 141 | |
| 142 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ | |
| OLD | NEW |