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/video/video_encode_accelerator.h" |
| 16 #include "media/cast/sender/video_encoder.h" | |
| 17 | 12 |
| 18 namespace media { | 13 namespace content { |
| 19 namespace cast { | |
| 20 | 14 |
| 21 // VideoToolbox implementation of the media::cast::VideoEncoder interface. | 15 // VideoToolbox.framework implementation of the VideoEncodeAccelerator |
| 22 // VideoToolbox makes no guarantees that it is thread safe, so this object is | 16 // interface for MacOSX. |
| 23 // pinned to the thread on which it is constructed. Supports changing frame | 17 class CONTENT_EXPORT VTVideoEncodeAccelerator |
| 24 // sizes directly. Implements the base::PowerObserver interface to reset the | 18 : public media::VideoEncodeAccelerator { |
| 25 // compression session when the host process is suspended. | 19 public: |
| 26 class H264VideoToolboxEncoder : public VideoEncoder, | 20 VTVideoEncodeAccelerator(); |
| 27 public base::PowerObserver { | 21 ~VTVideoEncodeAccelerator() override; |
| 28 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | |
| 29 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | |
| 30 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | |
| 31 | 22 |
| 32 public: | 23 // media::VideoEncodeAccelerator implementation. |
| 33 // Returns true if the current platform and system configuration supports | 24 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() |
| 34 // using H264VideoToolboxEncoder with the given |video_config|. | 25 override; |
| 35 static bool IsSupported(const VideoSenderConfig& video_config); | 26 bool Initialize(media::VideoPixelFormat format, |
| 27 const gfx::Size& input_visible_size, | |
| 28 media::VideoCodecProfile output_profile, | |
| 29 uint32_t initial_bitrate, | |
| 30 Client* client) override; | |
| 31 void Encode(const scoped_refptr<media::VideoFrame>& frame, | |
| 32 bool force_keyframe) override; | |
| 33 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | |
| 34 void RequestEncodingParametersChange(uint32_t bitrate, | |
| 35 uint32_t framerate) override; | |
| 36 void Destroy() override; | |
| 36 | 37 |
| 37 H264VideoToolboxEncoder( | 38 // Holds output buffers coming from the client ready to be filled. |
| 38 const scoped_refptr<CastEnvironment>& cast_environment, | 39 struct BitstreamBufferRef; |
| 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 | 40 |
| 57 private: | 41 private: |
| 58 // VideoFrameFactory tied to the VideoToolbox encoder. | 42 using CMSampleBufferRef = CoreMediaGlue::CMSampleBufferRef; |
| 59 class VideoFrameFactoryImpl; | 43 using VTCompressionSessionRef = VideoToolboxGlue::VTCompressionSessionRef; |
| 44 using VTEncodeInfoFlags = VideoToolboxGlue::VTEncodeInfoFlags; | |
| 45 | |
| 46 // Compression session callback function to handle compressed frames. | |
| 47 static void CompressionCallback(void* encoder_opaque, | |
| 48 void* request_opaque, | |
| 49 OSStatus status, | |
| 50 VTEncodeInfoFlags info, | |
| 51 CMSampleBufferRef sbuf); | |
| 60 | 52 |
| 61 // Reset the encoder's compression session by destroying the existing one | 53 // Reset the encoder's compression session by destroying the existing one |
| 62 // using DestroyCompressionSession() and creating a new one. The new session | 54 // using DestroyCompressionSession() and creating a new one. The new session |
| 63 // is configured using ConfigureCompressionSession(). | 55 // is configured using ConfigureCompressionSession(). |
| 64 void ResetCompressionSession(); | 56 bool ResetCompressionSession(); |
| 65 | 57 |
| 66 // Configure the current compression session using current encoder settings. | 58 // Configure the current compression session using current encoder settings. |
| 67 void ConfigureCompressionSession(); | 59 void ConfigureCompressionSession(); |
| 68 | 60 |
| 69 // Destroy the current compression session if any. Blocks until all pending | 61 // Destroy the current compression session if any. Blocks until all pending |
| 70 // frames have been flushed out (similar to EmitFrames without doing any | 62 // frames have been flushed out (similar to EmitFrames without doing any |
| 71 // encoding work). | 63 // encoding work). |
| 72 void DestroyCompressionSession(); | 64 void DestroyCompressionSession(); |
| 73 | 65 |
| 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); | |
|
miu
2016/02/04 22:23:20
It's unfortunate that VEA only works with fixed in
emircan
2016/02/07 04:24:06
It might be a good fit for V4L2 and VAAPI but Vide
| |
| 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. | 66 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
| 94 const VideoToolboxGlue* const videotoolbox_glue_; | 67 const VideoToolboxGlue* const videotoolbox_glue_; |
| 68 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | |
| 95 | 69 |
| 96 // VideoSenderConfig copy so we can create compression sessions on demand. | 70 // Initial parameters given by media::VideoEncodeAccelerator::Initialize() |
|
miu
2016/02/04 22:23:20
This comment is not accurate: |bitrate_| may chang
emircan
2016/02/07 04:24:06
Removed it.
| |
| 97 // This is needed to recover from backgrounding and other events that can | 71 uint32_t bitrate_; |
|
miu
2016/02/04 22:23:20
Type should be int. Unsigneds are for index count
emircan
2016/02/07 04:24:06
Done.
| |
| 98 // invalidate compression sessions. | 72 gfx::Size input_visible_size_; |
| 99 const VideoSenderConfig video_config_; | |
| 100 | 73 |
| 101 // Frame size of the current compression session. Can be changed by submitting | 74 // Bitstream buffers ready to be used to return encoded output, as a LIFO |
| 102 // a frame of a different size, which will cause a compression session reset. | 75 // since we don't care about ordering. |
| 103 gfx::Size frame_size_; | 76 std::deque<scoped_ptr<BitstreamBufferRef>> encoder_output_queue_; |
|
miu
2016/02/04 22:23:20
nit: If this is a LIFO, seems like you want a std:
emircan
2016/02/07 04:24:06
Actually it is a FIFO at this point, changing the
| |
| 104 | 77 |
| 105 // Callback used to report initialization status and runtime errors. | 78 // Our original calling task runner for the child thread. |
| 106 const StatusChangeCallback status_change_cb_; | 79 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; |
| 80 | |
| 81 // To expose client callbacks from VideoEncodeAccelerator. | |
| 82 // NOTE: all calls to this object *MUST* be executed on | |
| 83 // |client_task_runner_|. | |
| 84 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; | |
| 85 base::WeakPtr<Client> client_; | |
| 107 | 86 |
| 108 // Thread checker to enforce that this object is used on a specific thread. | 87 // Thread checker to enforce that this object is used on a specific thread. |
| 109 base::ThreadChecker thread_checker_; | 88 base::ThreadChecker thread_checker_; |
| 110 | 89 |
| 111 // The compression session. | 90 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 }; | 91 }; |
| 131 | 92 |
| 132 } // namespace cast | 93 } // namespace content |
| 133 } // namespace media | |
| 134 | 94 |
| 135 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 95 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_H_ |
| OLD | NEW |