Chromium Code Reviews| 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. |
|
Pawel Osciak
2016/02/18 11:16:14
We should always avoid doing things on GPU Child t
emircan
2016/03/02 05:28:13
Ok, I am adding an encoder thread. I was trying to
| |
| 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 | |
| 74 // Update the encoder's target frame size by resetting the compression | |
| 75 // session. This will also update the video frame factory. | |
| 76 void UpdateFrameSize(const gfx::Size& size_needed); | |
| 77 | |
| 78 // Set a compression session property. | |
| 79 bool SetSessionProperty(CFStringRef key, int32_t value); | |
| 80 bool SetSessionProperty(CFStringRef key, bool value); | |
| 81 bool SetSessionProperty(CFStringRef key, CFStringRef value); | |
| 82 | 53 |
| 83 // Compression session callback function to handle compressed frames. | 54 // Compression session callback function to handle compressed frames. |
| 84 static void CompressionCallback(void* encoder_opaque, | 55 static void CompressionCallback(void* encoder_opaque, |
| 85 void* request_opaque, | 56 void* request_opaque, |
| 86 OSStatus status, | 57 OSStatus status, |
| 87 VTEncodeInfoFlags info, | 58 VTEncodeInfoFlags info, |
| 88 CMSampleBufferRef sbuf); | 59 CMSampleBufferRef sbuf); |
| 60 void CompressionCallbackTask(VTEncodeInfoFlags info, CMSampleBufferRef sbuf); | |
| 89 | 61 |
| 90 // The cast environment (contains worker threads & more). | 62 // Copy CMSampleBuffer into a BitstreamBuffer and return it to the |client_|. |
| 91 const scoped_refptr<CastEnvironment> cast_environment_; | 63 void ReturnBitstreamBuffer( |
| 64 VTEncodeInfoFlags info, | |
| 65 CMSampleBufferRef sbuf, | |
| 66 scoped_ptr<VTVideoEncodeAccelerator::BitstreamBufferRef> buffer_ref); | |
| 67 | |
| 68 // Reset the encoder's compression session by destroying the existing one | |
| 69 // using DestroyCompressionSession() and creating a new one. The new session | |
| 70 // is configured using ConfigureCompressionSession(). | |
| 71 bool ResetCompressionSession(); | |
| 72 | |
| 73 // Create a compression session, with HW encoder enforced if | |
| 74 // |require_hw_encoding| is set. | |
| 75 bool CreateCompressionSession( | |
| 76 base::ScopedCFTypeRef<CFDictionaryRef> attributes, | |
| 77 bool require_hw_encoding); | |
| 78 | |
| 79 // Configure the current compression session using current encoder settings. | |
| 80 bool ConfigureCompressionSession(); | |
| 81 | |
| 82 // Destroy the current compression session if any. Blocks until all pending | |
|
Pawel Osciak
2016/02/18 11:16:14
This sounds especially concerning if blocking GPU
emircan
2016/03/02 05:28:13
I will post this on the |encoder_thread| during dt
| |
| 83 // frames have been flushed out (similar to EmitFrames without doing any | |
| 84 // encoding work). | |
| 85 void DestroyCompressionSession(); | |
| 92 | 86 |
| 93 // VideoToolboxGlue provides access to VideoToolbox at runtime. | 87 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
| 94 const VideoToolboxGlue* const videotoolbox_glue_; | 88 const VideoToolboxGlue* videotoolbox_glue_; |
| 89 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
| 95 | 90 |
| 96 // VideoSenderConfig copy so we can create compression sessions on demand. | 91 gfx::Size input_visible_size_; |
| 97 // This is needed to recover from backgrounding and other events that can | 92 size_t bitstream_buffer_size_; |
| 98 // invalidate compression sessions. | 93 int32_t frame_rate_; |
| 99 const VideoSenderConfig video_config_; | 94 int32_t target_bitrate_; |
| 95 bool set_data_rate_limit_; | |
|
Pawel Osciak
2016/02/18 11:16:14
Please document this field.
emircan
2016/03/02 05:28:13
Done.
| |
| 100 | 96 |
| 101 // Frame size of the current compression session. Can be changed by submitting | 97 // 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. | 98 std::deque<scoped_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; |
| 103 gfx::Size frame_size_; | |
| 104 | 99 |
| 105 // Callback used to report initialization status and runtime errors. | 100 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. |
| 106 const StatusChangeCallback status_change_cb_; | 101 std::deque<scoped_ptr<EncodeOutput>> encoder_output_queue_; |
| 102 | |
| 103 // Our original calling task runner for the child thread. | |
| 104 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; | |
| 105 | |
| 106 // To expose client callbacks from VideoEncodeAccelerator. | |
| 107 // NOTE: all calls to this object *MUST* be executed on | |
| 108 // |client_task_runner_|. | |
| 109 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; | |
| 110 base::WeakPtr<Client> client_; | |
| 107 | 111 |
| 108 // Thread checker to enforce that this object is used on a specific thread. | 112 // Thread checker to enforce that this object is used on a specific thread. |
| 109 base::ThreadChecker thread_checker_; | 113 base::ThreadChecker thread_checker_; |
| 110 | 114 |
| 111 // The compression session. | 115 DISALLOW_COPY_AND_ASSIGN(VTVideoEncodeAccelerator); |
| 112 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
| 113 | |
| 114 // Video frame factory tied to the encoder. | |
| 115 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; | |
| 116 | |
| 117 // The ID of the last frame that was emitted. | |
| 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 }; | 116 }; |
| 131 | 117 |
| 132 } // namespace cast | 118 } // namespace content |
| 133 } // namespace media | |
| 134 | 119 |
| 135 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 120 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_ |
| OLD | NEW |