 Chromium Code Reviews
 Chromium Code Reviews Issue 1636083003:
  H264 HW encode using VideoToolbox  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1636083003:
  H264 HW encode using VideoToolbox  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: content/common/gpu/media/vt_video_encode_accelerator.h | 
| diff --git a/media/cast/sender/h264_vt_encoder.h b/content/common/gpu/media/vt_video_encode_accelerator.h | 
| similarity index 27% | 
| copy from media/cast/sender/h264_vt_encoder.h | 
| copy to content/common/gpu/media/vt_video_encode_accelerator.h | 
| index c3b414da8a60901600af19124d60fa9778f2022a..d2bc5ebd4c786ab4377d652f28ef2d09a6f79e5d 100644 | 
| --- a/media/cast/sender/h264_vt_encoder.h | 
| +++ b/content/common/gpu/media/vt_video_encode_accelerator.h | 
| @@ -1,67 +1,59 @@ | 
| -// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// Copyright 2016 The Chromium Authors. All rights reserved. | 
| // Use of this source code is governed by a BSD-style license that can be | 
| // found in the LICENSE file. | 
| -#ifndef MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 
| -#define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 
| - | 
| -#include <stdint.h> | 
| +#ifndef CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_H_ | 
| +#define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_H_ | 
| #include "base/mac/scoped_cftyperef.h" | 
| -#include "base/macros.h" | 
| -#include "base/power_monitor/power_observer.h" | 
| -#include "base/threading/thread_checker.h" | 
| +#include "content/common/content_export.h" | 
| #include "media/base/mac/videotoolbox_glue.h" | 
| -#include "media/cast/sender/size_adaptable_video_encoder_base.h" | 
| -#include "media/cast/sender/video_encoder.h" | 
| - | 
| -namespace media { | 
| -namespace cast { | 
| - | 
| -// VideoToolbox implementation of the media::cast::VideoEncoder interface. | 
| -// VideoToolbox makes no guarantees that it is thread safe, so this object is | 
| -// pinned to the thread on which it is constructed. Supports changing frame | 
| -// sizes directly. Implements the base::PowerObserver interface to reset the | 
| -// compression session when the host process is suspended. | 
| -class H264VideoToolboxEncoder : public VideoEncoder, | 
| - public base::PowerObserver { | 
| - typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | 
| - typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | 
| - typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | 
| +#include "media/video/video_encode_accelerator.h" | 
| + | 
| +namespace content { | 
| +// VideoToolbox.framework implementation of the VideoEncodeAccelerator | 
| +// interface for MacOSX. | 
| +class CONTENT_EXPORT VTVideoEncodeAccelerator | 
| + : public media::VideoEncodeAccelerator { | 
| public: | 
| - // Returns true if the current platform and system configuration supports | 
| - // using H264VideoToolboxEncoder with the given |video_config|. | 
| - static bool IsSupported(const VideoSenderConfig& video_config); | 
| - | 
| - H264VideoToolboxEncoder( | 
| - const scoped_refptr<CastEnvironment>& cast_environment, | 
| - const VideoSenderConfig& video_config, | 
| - const StatusChangeCallback& status_change_cb); | 
| - ~H264VideoToolboxEncoder() final; | 
| - | 
| - // media::cast::VideoEncoder implementation | 
| - bool EncodeVideoFrame( | 
| - const scoped_refptr<media::VideoFrame>& video_frame, | 
| - const base::TimeTicks& reference_time, | 
| - const FrameEncodedCallback& frame_encoded_callback) final; | 
| - void SetBitRate(int new_bit_rate) final; | 
| - void GenerateKeyFrame() final; | 
| - scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() final; | 
| - void EmitFrames() final; | 
| - | 
| - // base::PowerObserver | 
| - void OnSuspend() final; | 
| - void OnResume() final; | 
| + VTVideoEncodeAccelerator(); | 
| + ~VTVideoEncodeAccelerator() override; | 
| + | 
| + // media::VideoEncodeAccelerator implementation. | 
| + media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() | 
| + override; | 
| + bool Initialize(media::VideoPixelFormat format, | 
| + const gfx::Size& input_visible_size, | 
| + media::VideoCodecProfile output_profile, | 
| + uint32_t initial_bitrate, | 
| + Client* client) override; | 
| + void Encode(const scoped_refptr<media::VideoFrame>& frame, | 
| + bool force_keyframe) override; | 
| + void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | 
| + void RequestEncodingParametersChange(uint32_t bitrate, | 
| + uint32_t framerate) override; | 
| + void Destroy() override; | 
| + | 
| + // Holds output buffers coming from the client ready to be filled. | 
| + struct BitstreamBufferRef; | 
| private: | 
| - // VideoFrameFactory tied to the VideoToolbox encoder. | 
| - class VideoFrameFactoryImpl; | 
| + using CMSampleBufferRef = CoreMediaGlue::CMSampleBufferRef; | 
| + using VTCompressionSessionRef = VideoToolboxGlue::VTCompressionSessionRef; | 
| + using VTEncodeInfoFlags = VideoToolboxGlue::VTEncodeInfoFlags; | 
| + | 
| + // Compression session callback function to handle compressed frames. | 
| + static void CompressionCallback(void* encoder_opaque, | 
| + void* request_opaque, | 
| + OSStatus status, | 
| + VTEncodeInfoFlags info, | 
| + CMSampleBufferRef sbuf); | 
| // Reset the encoder's compression session by destroying the existing one | 
| // using DestroyCompressionSession() and creating a new one. The new session | 
| // is configured using ConfigureCompressionSession(). | 
| - void ResetCompressionSession(); | 
| + bool ResetCompressionSession(); | 
| // Configure the current compression session using current encoder settings. | 
| void ConfigureCompressionSession(); | 
| @@ -71,65 +63,33 @@ class H264VideoToolboxEncoder : public VideoEncoder, | 
| // encoding work). | 
| void DestroyCompressionSession(); | 
| - // Update the encoder's target frame size by resetting the compression | 
| - // session. This will also update the video frame factory. | 
| - 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
 | 
| - | 
| - // Set a compression session property. | 
| - bool SetSessionProperty(CFStringRef key, int32_t value); | 
| - bool SetSessionProperty(CFStringRef key, bool value); | 
| - bool SetSessionProperty(CFStringRef key, CFStringRef value); | 
| - | 
| - // Compression session callback function to handle compressed frames. | 
| - static void CompressionCallback(void* encoder_opaque, | 
| - void* request_opaque, | 
| - OSStatus status, | 
| - VTEncodeInfoFlags info, | 
| - CMSampleBufferRef sbuf); | 
| - | 
| - // The cast environment (contains worker threads & more). | 
| - const scoped_refptr<CastEnvironment> cast_environment_; | 
| - | 
| // VideoToolboxGlue provides access to VideoToolbox at runtime. | 
| const VideoToolboxGlue* const videotoolbox_glue_; | 
| - | 
| - // VideoSenderConfig copy so we can create compression sessions on demand. | 
| - // This is needed to recover from backgrounding and other events that can | 
| - // invalidate compression sessions. | 
| - const VideoSenderConfig video_config_; | 
| - | 
| - // Frame size of the current compression session. Can be changed by submitting | 
| - // a frame of a different size, which will cause a compression session reset. | 
| - gfx::Size frame_size_; | 
| - | 
| - // Callback used to report initialization status and runtime errors. | 
| - const StatusChangeCallback status_change_cb_; | 
| - | 
| - // Thread checker to enforce that this object is used on a specific thread. | 
| - base::ThreadChecker thread_checker_; | 
| - | 
| - // The compression session. | 
| base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | 
| - // Video frame factory tied to the encoder. | 
| - scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; | 
| + // 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.
 | 
| + 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.
 | 
| + gfx::Size input_visible_size_; | 
| - // The ID of the last frame that was emitted. | 
| - uint32_t last_frame_id_; | 
| + // Bitstream buffers ready to be used to return encoded output, as a LIFO | 
| + // since we don't care about ordering. | 
| + 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
 | 
| - // Force next frame to be a keyframe. | 
| - bool encode_next_frame_as_keyframe_; | 
| + // Our original calling task runner for the child thread. | 
| + const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; | 
| - // Power suspension state. | 
| - bool power_suspended_; | 
| + // To expose client callbacks from VideoEncodeAccelerator. | 
| + // NOTE: all calls to this object *MUST* be executed on | 
| + // |client_task_runner_|. | 
| + scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; | 
| + base::WeakPtr<Client> client_; | 
| - // NOTE: Weak pointers must be invalidated before all other member variables. | 
| - base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_; | 
| + // Thread checker to enforce that this object is used on a specific thread. | 
| + base::ThreadChecker thread_checker_; | 
| - DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); | 
| + DISALLOW_COPY_AND_ASSIGN(VTVideoEncodeAccelerator); | 
| }; | 
| -} // namespace cast | 
| -} // namespace media | 
| +} // namespace content | 
| -#endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 
| +#endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_H_ |