Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: content/common/gpu/media/vt_video_encode_accelerator_mac.h

Issue 1636083003: H264 HW encode using VideoToolbox (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix cast tests. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.
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 53
74 // Update the encoder's target frame size by resetting the compression 54 // Encoding tasks to be run on |encode_thread|.
Pawel Osciak 2016/03/10 05:25:35 s/encode_thread/encoder_thread_/
emircan 2016/03/11 00:21:07 Done.
75 // session. This will also update the video frame factory. 55 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame,
76 void UpdateFrameSize(const gfx::Size& size_needed); 56 bool force_keyframe);
Pawel Osciak 2016/03/10 05:25:35 Incorrect indent?
emircan 2016/03/11 00:21:07 Done.
57 void UseOutputBitstreamBufferTask(scoped_ptr<BitstreamBufferRef> buffer_ref);
58 void DestroyTask();
77 59
78 // Set a compression session property. 60 // Helper function to notify the client of an error on |client_task_runner_|.
79 bool SetSessionProperty(CFStringRef key, int32_t value); 61 void NotifyError(media::VideoEncodeAccelerator::Error error);
80 bool SetSessionProperty(CFStringRef key, bool value);
81 bool SetSessionProperty(CFStringRef key, CFStringRef value);
82 62
83 // Compression session callback function to handle compressed frames. 63 // Compression session callback function to handle compressed frames.
84 static void CompressionCallback(void* encoder_opaque, 64 static void CompressionCallback(void* encoder_opaque,
85 void* request_opaque, 65 void* request_opaque,
86 OSStatus status, 66 OSStatus status,
87 VTEncodeInfoFlags info, 67 VTEncodeInfoFlags info,
88 CMSampleBufferRef sbuf); 68 CMSampleBufferRef sbuf);
69 void CompressionCallbackTask(OSStatus status,
70 scoped_ptr<EncodeOutput> encode_output);
89 71
90 // The cast environment (contains worker threads & more). 72 // Copy CMSampleBuffer into a BitstreamBuffer and return it to the |client_|.
91 const scoped_refptr<CastEnvironment> cast_environment_; 73 void ReturnBitstreamBuffer(
74 scoped_ptr<EncodeOutput> encode_output,
75 scoped_ptr<VTVideoEncodeAccelerator::BitstreamBufferRef> buffer_ref);
76
77 // Reset the encoder's compression session by destroying the existing one
78 // using DestroyCompressionSession() and creating a new one. The new session
79 // is configured using ConfigureCompressionSession().
80 bool ResetCompressionSession();
81
82 // Create a compression session, with HW encoder enforced if
83 // |require_hw_encoding| is set.
84 bool CreateCompressionSession(
85 base::ScopedCFTypeRef<CFDictionaryRef> attributes,
86 const gfx::Size& input_size,
87 bool require_hw_encoding);
88
89 // Configure the current compression session using current encoder settings.
90 bool ConfigureCompressionSession();
91
92 // Destroy the current compression session if any. Blocks until all pending
93 // frames have been flushed out (similar to EmitFrames without doing any
94 // encoding work).
95 void DestroyCompressionSession();
92 96
93 // VideoToolboxGlue provides access to VideoToolbox at runtime. 97 // VideoToolboxGlue provides access to VideoToolbox at runtime.
94 const VideoToolboxGlue* const videotoolbox_glue_; 98 const VideoToolboxGlue* videotoolbox_glue_;
99 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_;
95 100
96 // VideoSenderConfig copy so we can create compression sessions on demand. 101 gfx::Size input_visible_size_;
97 // This is needed to recover from backgrounding and other events that can 102 size_t bitstream_buffer_size_;
98 // invalidate compression sessions. 103 int32_t frame_rate_;
99 const VideoSenderConfig video_config_; 104 int32_t target_bitrate_;
100 105
101 // Frame size of the current compression session. Can be changed by submitting 106 // 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. 107 std::deque<scoped_ptr<BitstreamBufferRef>> bitstream_buffer_queue_;
103 gfx::Size frame_size_;
104 108
105 // Callback used to report initialization status and runtime errors. 109 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO.
106 const StatusChangeCallback status_change_cb_; 110 std::deque<scoped_ptr<EncodeOutput>> encoder_output_queue_;
111
112 // Our original calling task runner for the child thread.
113 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_;
114
115 // To expose client callbacks from VideoEncodeAccelerator.
116 // NOTE: all calls to this object *MUST* be executed on
117 // |client_task_runner_|.
118 base::WeakPtr<Client> client_;
119 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_;
107 120
108 // Thread checker to enforce that this object is used on a specific thread. 121 // Thread checker to enforce that this object is used on a specific thread.
122 // It is pinned on |client_task_runner_| thread.
109 base::ThreadChecker thread_checker_; 123 base::ThreadChecker thread_checker_;
110 124
111 // The compression session. 125 // This thread services tasks posted from the VEA API entry points by the
112 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; 126 // GPU child thread and CompressionCallback() posted from device thread.
127 base::Thread encoder_thread_;
113 128
114 // Video frame factory tied to the encoder. 129 // Declared last to ensure that all weak pointers are invalidated before
115 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; 130 // other destructors run.
131 base::WeakPtrFactory<VTVideoEncodeAccelerator> encoder_task_weak_factory_;
116 132
117 // The ID of the last frame that was emitted. 133 DISALLOW_COPY_AND_ASSIGN(VTVideoEncodeAccelerator);
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 }; 134 };
131 135
132 } // namespace cast 136 } // namespace content
133 } // namespace media
134 137
135 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ 138 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_ENCODE_ACCELERATOR_MAC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698