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_H_ |
| 6 #define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 6 #define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_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. |
|
Pawel Osciak
2016/02/08 04:33:43
Should we keep threading comment, or is VT no long
emircan
2016/02/08 23:41:24
I will keep the threading comment.
| |
| 23 // pinned to the thread on which it is constructed. Supports changing frame | 18 class CONTENT_EXPORT VTVideoEncodeAccelerator |
| 24 // sizes directly. Implements the base::PowerObserver interface to reset the | 19 : public media::VideoEncodeAccelerator { |
| 25 // compression session when the host process is suspended. | 20 public: |
| 26 class H264VideoToolboxEncoder : public VideoEncoder, | 21 VTVideoEncodeAccelerator(); |
| 27 public base::PowerObserver { | 22 ~VTVideoEncodeAccelerator() override; |
| 28 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | |
| 29 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | |
| 30 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | |
| 31 | 23 |
| 32 public: | 24 // media::VideoEncodeAccelerator implementation. |
| 33 // Returns true if the current platform and system configuration supports | 25 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() |
| 34 // using H264VideoToolboxEncoder with the given |video_config|. | 26 override; |
| 35 static bool IsSupported(const VideoSenderConfig& video_config); | 27 bool Initialize(media::VideoPixelFormat format, |
| 28 const gfx::Size& input_visible_size, | |
| 29 media::VideoCodecProfile output_profile, | |
| 30 uint32_t initial_bitrate, | |
| 31 Client* client) override; | |
| 32 void Encode(const scoped_refptr<media::VideoFrame>& frame, | |
| 33 bool force_keyframe) override; | |
| 34 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | |
| 35 void RequestEncodingParametersChange(uint32_t bitrate, | |
| 36 uint32_t framerate) override; | |
| 37 void Destroy() override; | |
| 36 | 38 |
| 37 H264VideoToolboxEncoder( | 39 // Holds output buffers coming from the client ready to be filled. |
| 38 const scoped_refptr<CastEnvironment>& cast_environment, | 40 struct BitstreamBufferRef; |
|
Pawel Osciak
2016/02/08 04:33:43
Could this be private?
emircan
2016/02/08 23:41:24
Done.
| |
| 39 const VideoSenderConfig& video_config, | |
| 40 const StatusChangeCallback& status_change_cb); | |
| 41 ~H264VideoToolboxEncoder() final; | |
| 42 | |
| 43 // media::cast::VideoEncoder implementation | |
| 44 bool EncodeVideoFrame( | |
| 45 const scoped_refptr<media::VideoFrame>& video_frame, | |
| 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 | 41 |
| 57 private: | 42 private: |
| 58 // VideoFrameFactory tied to the VideoToolbox encoder. | 43 using CMSampleBufferRef = CoreMediaGlue::CMSampleBufferRef; |
| 59 class VideoFrameFactoryImpl; | 44 using VTCompressionSessionRef = VideoToolboxGlue::VTCompressionSessionRef; |
| 45 using VTEncodeInfoFlags = VideoToolboxGlue::VTEncodeInfoFlags; | |
| 46 | |
| 47 // Compression session callback function to handle compressed frames. | |
| 48 static void CompressionCallback(void* encoder_opaque, | |
| 49 void* request_opaque, | |
| 50 OSStatus status, | |
| 51 VTEncodeInfoFlags info, | |
| 52 CMSampleBufferRef sbuf); | |
| 60 | 53 |
| 61 // Reset the encoder's compression session by destroying the existing one | 54 // Reset the encoder's compression session by destroying the existing one |
| 62 // using DestroyCompressionSession() and creating a new one. The new session | 55 // using DestroyCompressionSession() and creating a new one. The new session |
| 63 // is configured using ConfigureCompressionSession(). | 56 // is configured using ConfigureCompressionSession(). |
| 64 void ResetCompressionSession(); | 57 bool ResetCompressionSession(); |
| 65 | 58 |
| 66 // Configure the current compression session using current encoder settings. | 59 // Configure the current compression session using current encoder settings. |
| 67 void ConfigureCompressionSession(); | 60 void ConfigureCompressionSession(); |
| 68 | 61 |
| 69 // Destroy the current compression session if any. Blocks until all pending | 62 // Destroy the current compression session if any. Blocks until all pending |
| 70 // frames have been flushed out (similar to EmitFrames without doing any | 63 // frames have been flushed out (similar to EmitFrames without doing any |
| 71 // encoding work). | 64 // encoding work). |
| 72 void DestroyCompressionSession(); | 65 void DestroyCompressionSession(); |
| 73 | 66 |
| 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 | |
| 83 // Compression session callback function to handle compressed frames. | |
| 84 static void CompressionCallback(void* encoder_opaque, | |
| 85 void* request_opaque, | |
| 86 OSStatus status, | |
| 87 VTEncodeInfoFlags info, | |
| 88 CMSampleBufferRef sbuf); | |
| 89 | |
| 90 // The cast environment (contains worker threads & more). | |
| 91 const scoped_refptr<CastEnvironment> cast_environment_; | |
| 92 | |
| 93 // VideoToolboxGlue provides access to VideoToolbox at runtime. | 67 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
| 94 const VideoToolboxGlue* const videotoolbox_glue_; | 68 const VideoToolboxGlue* const videotoolbox_glue_; |
| 69 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
| 95 | 70 |
| 96 // VideoSenderConfig copy so we can create compression sessions on demand. | 71 int32_t bitrate_; |
|
Pawel Osciak
2016/02/08 04:33:43
uint32_t?
emircan
2016/02/08 23:41:24
It was uint32_t but miu@ asked me to change it on
miu
2016/02/09 23:29:22
Chromium C++ style guide discusses when and when n
| |
| 97 // This is needed to recover from backgrounding and other events that can | 72 gfx::Size input_visible_size_; |
| 98 // invalidate compression sessions. | |
| 99 const VideoSenderConfig video_config_; | |
| 100 | 73 |
| 101 // Frame size of the current compression session. Can be changed by submitting | 74 // Set parameters for VTCompressionSession. |
| 102 // a frame of a different size, which will cause a compression session reset. | 75 scoped_ptr<media::video_toolbox::SessionPropertySetter> |
| 103 gfx::Size frame_size_; | 76 session_property_setter_; |
| 104 | 77 |
| 105 // Callback used to report initialization status and runtime errors. | 78 // Bitstream buffers ready to be used to return encoded output as a FIFO. |
| 106 const StatusChangeCallback status_change_cb_; | 79 std::deque<scoped_ptr<BitstreamBufferRef>> encoder_output_queue_; |
| 80 | |
| 81 // Our original calling task runner for the child thread. | |
| 82 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; | |
| 83 | |
| 84 // To expose client callbacks from VideoEncodeAccelerator. | |
| 85 // NOTE: all calls to this object *MUST* be executed on | |
| 86 // |client_task_runner_|. | |
| 87 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; | |
| 88 base::WeakPtr<Client> client_; | |
| 107 | 89 |
| 108 // Thread checker to enforce that this object is used on a specific thread. | 90 // Thread checker to enforce that this object is used on a specific thread. |
| 109 base::ThreadChecker thread_checker_; | 91 base::ThreadChecker thread_checker_; |
| 110 | 92 |
| 111 // The compression session. | 93 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 }; | 94 }; |
| 131 | 95 |
| 132 } // namespace cast | 96 } // namespace content |
| 133 } // namespace media | |
| 134 | 97 |
| 135 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 98 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_H_ |
| OLD | NEW |