| Index: content/common/gpu/media/vt_video_encode_accelerator_mac.h
|
| diff --git a/media/cast/sender/h264_vt_encoder.h b/content/common/gpu/media/vt_video_encode_accelerator_mac.h
|
| similarity index 25%
|
| copy from media/cast/sender/h264_vt_encoder.h
|
| copy to content/common/gpu/media/vt_video_encode_accelerator_mac.h
|
| index c3b414da8a60901600af19124d60fa9778f2022a..868953706863ba51419ed5acba97a2e79a6650cf 100644
|
| --- a/media/cast/sender/h264_vt_encoder.h
|
| +++ b/content/common/gpu/media/vt_video_encode_accelerator_mac.h
|
| @@ -1,84 +1,55 @@
|
| -// 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_MAC_H_
|
| +#define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_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/base/mac/videotoolbox_helpers.h"
|
| +#include "media/video/video_encode_accelerator.h"
|
| +
|
| +namespace content {
|
|
|
| +// VideoToolbox.framework implementation of the VideoEncodeAccelerator
|
| +// interface for MacOSX. VideoToolbox makes no guarantees that it is thread
|
| +// safe, so this object is pinned to the thread on which it is constructed.
|
| +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;
|
|
|
| private:
|
| - // VideoFrameFactory tied to the VideoToolbox encoder.
|
| - class VideoFrameFactoryImpl;
|
| + using CMSampleBufferRef = CoreMediaGlue::CMSampleBufferRef;
|
| + using VTCompressionSessionRef = VideoToolboxGlue::VTCompressionSessionRef;
|
| + using VTEncodeInfoFlags = VideoToolboxGlue::VTEncodeInfoFlags;
|
|
|
| - // 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();
|
| + // Holds the associated data of a video frame being processed.
|
| + struct InProgressFrameEncode;
|
|
|
| - // Configure the current compression session using current encoder settings.
|
| - void ConfigureCompressionSession();
|
| + // Holds output buffers coming from the encoder.
|
| + struct EncodeOutput;
|
|
|
| - // Destroy the current compression session if any. Blocks until all pending
|
| - // frames have been flushed out (similar to EmitFrames without doing any
|
| - // 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);
|
| -
|
| - // Set a compression session property.
|
| - bool SetSessionProperty(CFStringRef key, int32_t value);
|
| - bool SetSessionProperty(CFStringRef key, bool value);
|
| - bool SetSessionProperty(CFStringRef key, CFStringRef value);
|
| + // Holds output buffers coming from the client ready to be filled.
|
| + struct BitstreamBufferRef;
|
|
|
| // Compression session callback function to handle compressed frames.
|
| static void CompressionCallback(void* encoder_opaque,
|
| @@ -86,50 +57,67 @@ class H264VideoToolboxEncoder : public VideoEncoder,
|
| OSStatus status,
|
| VTEncodeInfoFlags info,
|
| CMSampleBufferRef sbuf);
|
| + void CompressionCallbackTask(VTEncodeInfoFlags info, CMSampleBufferRef sbuf);
|
|
|
| - // The cast environment (contains worker threads & more).
|
| - const scoped_refptr<CastEnvironment> cast_environment_;
|
| + // Copy CMSampleBuffer into a BitstreamBuffer and return it to the |client_|.
|
| + void ReturnBitstreamBuffer(
|
| + VTEncodeInfoFlags info,
|
| + CMSampleBufferRef sbuf,
|
| + scoped_ptr<VTVideoEncodeAccelerator::BitstreamBufferRef> buffer_ref);
|
|
|
| - // 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_;
|
| + // 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().
|
| + bool ResetCompressionSession();
|
|
|
| - // 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_;
|
| + // Create a compression session, with HW encoder enforced if
|
| + // |require_hw_encoding| is set.
|
| + bool CreateCompressionSession(
|
| + base::ScopedCFTypeRef<CFDictionaryRef> attributes,
|
| + bool require_hw_encoding);
|
|
|
| - // Callback used to report initialization status and runtime errors.
|
| - const StatusChangeCallback status_change_cb_;
|
| + // Configure the current compression session using current encoder settings.
|
| + bool ConfigureCompressionSession();
|
|
|
| - // Thread checker to enforce that this object is used on a specific thread.
|
| - base::ThreadChecker thread_checker_;
|
| + // Destroy the current compression session if any. Blocks until all pending
|
| + // frames have been flushed out (similar to EmitFrames without doing any
|
| + // encoding work).
|
| + void DestroyCompressionSession();
|
|
|
| - // The compression session.
|
| + // VideoToolboxGlue provides access to VideoToolbox at runtime.
|
| + const VideoToolboxGlue* videotoolbox_glue_;
|
| base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_;
|
|
|
| - // Video frame factory tied to the encoder.
|
| - scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_;
|
| + gfx::Size input_visible_size_;
|
| + size_t bitstream_buffer_size_;
|
| + int32_t frame_rate_;
|
| + int32_t average_bitrate_;
|
| + int32_t max_bitrate_;
|
| +
|
| + // Set parameters for VTCompressionSession.
|
| + media::video_toolbox::SessionPropertySetter session_property_setter_;
|
|
|
| - // 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 FIFO.
|
| + std::deque<scoped_ptr<BitstreamBufferRef>> bitstream_buffer_queue_;
|
|
|
| - // Force next frame to be a keyframe.
|
| - bool encode_next_frame_as_keyframe_;
|
| + // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO.
|
| + std::deque<scoped_ptr<EncodeOutput>> encoder_output_queue_;
|
|
|
| - // Power suspension state.
|
| - bool power_suspended_;
|
| + // Our original calling task runner for the child thread.
|
| + const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_;
|
|
|
| - // NOTE: Weak pointers must be invalidated before all other member variables.
|
| - base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_;
|
| + // 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_;
|
| +
|
| + // 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_MAC_H_
|
|
|