OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
6 #define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 6 #define MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
7 | 7 |
8 #include "base/mac/scoped_cftyperef.h" | 8 #include "base/mac/scoped_cftyperef.h" |
9 #include "base/threading/thread_checker.h" | 9 #include "base/threading/thread_checker.h" |
10 #include "media/base/mac/videotoolbox_glue.h" | 10 #include "media/base/mac/videotoolbox_glue.h" |
11 #include "media/cast/sender/size_adaptable_video_encoder_base.h" | 11 #include "media/cast/sender/size_adaptable_video_encoder_base.h" |
12 #include "media/cast/sender/video_encoder.h" | 12 #include "media/cast/sender/video_encoder.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 namespace cast { | 15 namespace cast { |
16 | 16 |
17 // VideoToolbox implementation of the media::cast::VideoEncoder interface. | 17 // VideoToolbox implementation of the media::cast::VideoEncoder interface. |
18 // VideoToolbox makes no guarantees that it is thread safe, so this object is | 18 // VideoToolbox makes no guarantees that it is thread safe, so this object is |
19 // pinned to the thread on which it is constructed. | 19 // pinned to the thread on which it is constructed. Supports changing frame |
| 20 // sizes directly. |
20 class H264VideoToolboxEncoder : public VideoEncoder { | 21 class H264VideoToolboxEncoder : public VideoEncoder { |
21 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | 22 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; |
22 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | 23 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; |
23 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | 24 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; |
24 | 25 |
25 public: | 26 public: |
26 // Returns true if the current platform and system configuration supports | 27 // Returns true if the current platform and system configuration supports |
27 // using H264VideoToolboxEncoder with the given |video_config|. | 28 // using H264VideoToolboxEncoder with the given |video_config|. |
28 static bool IsSupported(const VideoSenderConfig& video_config); | 29 static bool IsSupported(const VideoSenderConfig& video_config); |
29 | 30 |
30 H264VideoToolboxEncoder( | 31 H264VideoToolboxEncoder( |
31 const scoped_refptr<CastEnvironment>& cast_environment, | 32 const scoped_refptr<CastEnvironment>& cast_environment, |
32 const VideoSenderConfig& video_config, | 33 const VideoSenderConfig& video_config, |
33 const gfx::Size& frame_size, | |
34 uint32 first_frame_id, | |
35 const StatusChangeCallback& status_change_cb); | 34 const StatusChangeCallback& status_change_cb); |
36 ~H264VideoToolboxEncoder() override; | 35 ~H264VideoToolboxEncoder() override; |
37 | 36 |
38 // media::cast::VideoEncoder implementation | 37 // media::cast::VideoEncoder implementation |
39 bool EncodeVideoFrame( | 38 bool EncodeVideoFrame( |
40 const scoped_refptr<media::VideoFrame>& video_frame, | 39 const scoped_refptr<media::VideoFrame>& video_frame, |
41 const base::TimeTicks& reference_time, | 40 const base::TimeTicks& reference_time, |
42 const FrameEncodedCallback& frame_encoded_callback) override; | 41 const FrameEncodedCallback& frame_encoded_callback) override; |
43 void SetBitRate(int new_bit_rate) override; | 42 void SetBitRate(int new_bit_rate) override; |
44 void GenerateKeyFrame() override; | 43 void GenerateKeyFrame() override; |
45 void LatestFrameIdToReference(uint32 frame_id) override; | 44 void LatestFrameIdToReference(uint32 frame_id) override; |
46 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; | 45 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; |
47 void EmitFrames() override; | 46 void EmitFrames() override; |
48 | 47 |
49 private: | 48 private: |
50 // Initialize the compression session. | 49 // VideoFrameFactory tied to the VideoToolbox encoder. |
51 bool Initialize(const VideoSenderConfig& video_config); | 50 class VideoFrameFactoryImpl; |
52 | 51 |
53 // Configure the compression session. | 52 // Reset the encoder's compression session by destroying the existing one |
54 void ConfigureSession(const VideoSenderConfig& video_config); | 53 // using DestroyCompressionSession() and creating a new one. The new session |
| 54 // is configured using ConfigureCompressionSession(). |
| 55 void ResetCompressionSession(); |
55 | 56 |
56 // Teardown the encoder. | 57 // Configure the current compression session using current encoder settings. |
57 void Teardown(); | 58 void ConfigureCompressionSession(); |
| 59 |
| 60 // Destroy the current compression session if any. Blocks until all pending |
| 61 // frames have been flushed out (similar to EmitFrames without doing any |
| 62 // encoding work). |
| 63 void DestroyCompressionSession(); |
| 64 |
| 65 // Update the encoder's target frame size by resetting the compression |
| 66 // session. This will also update the video frame factory. |
| 67 void UpdateFrameSize(const gfx::Size& size_needed); |
58 | 68 |
59 // Set a compression session property. | 69 // Set a compression session property. |
60 bool SetSessionProperty(CFStringRef key, int32_t value); | 70 bool SetSessionProperty(CFStringRef key, int32_t value); |
61 bool SetSessionProperty(CFStringRef key, bool value); | 71 bool SetSessionProperty(CFStringRef key, bool value); |
62 bool SetSessionProperty(CFStringRef key, CFStringRef value); | 72 bool SetSessionProperty(CFStringRef key, CFStringRef value); |
63 | 73 |
64 // Compression session callback function to handle compressed frames. | 74 // Compression session callback function to handle compressed frames. |
65 static void CompressionCallback(void* encoder_opaque, | 75 static void CompressionCallback(void* encoder_opaque, |
66 void* request_opaque, | 76 void* request_opaque, |
67 OSStatus status, | 77 OSStatus status, |
68 VTEncodeInfoFlags info, | 78 VTEncodeInfoFlags info, |
69 CMSampleBufferRef sbuf); | 79 CMSampleBufferRef sbuf); |
70 | 80 |
71 // The cast environment (contains worker threads & more). | 81 // The cast environment (contains worker threads & more). |
72 const scoped_refptr<CastEnvironment> cast_environment_; | 82 const scoped_refptr<CastEnvironment> cast_environment_; |
73 | 83 |
74 // VideoToolboxGlue provides access to VideoToolbox at runtime. | 84 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
75 const VideoToolboxGlue* const videotoolbox_glue_; | 85 const VideoToolboxGlue* const videotoolbox_glue_; |
76 | 86 |
77 // The size of the visible region of the video frames to be encoded. | 87 // VideoSenderConfig copy so we can create compression sessions on demand. |
78 const gfx::Size frame_size_; | 88 // This is needed to recover from backgrounding and other events that can |
| 89 // invalidate compression sessions. |
| 90 const VideoSenderConfig video_config_; |
| 91 |
| 92 // Frame size of the current compression session. Can be changed by submitting |
| 93 // a frame of a different size, which will cause a compression session reset. |
| 94 gfx::Size frame_size_; |
79 | 95 |
80 // Callback used to report initialization status and runtime errors. | 96 // Callback used to report initialization status and runtime errors. |
81 const StatusChangeCallback status_change_cb_; | 97 const StatusChangeCallback status_change_cb_; |
82 | 98 |
83 // Thread checker to enforce that this object is used on a specific thread. | 99 // Thread checker to enforce that this object is used on a specific thread. |
84 base::ThreadChecker thread_checker_; | 100 base::ThreadChecker thread_checker_; |
85 | 101 |
86 // The compression session. | 102 // The compression session. |
87 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | 103 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; |
88 | 104 |
89 // Frame identifier counter. | 105 // Video frame factory tied to the encoder. |
90 uint32 next_frame_id_; | 106 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; |
| 107 |
| 108 // The ID of the last frame that was emitted. |
| 109 uint32 last_frame_id_; |
91 | 110 |
92 // Force next frame to be a keyframe. | 111 // Force next frame to be a keyframe. |
93 bool encode_next_frame_as_keyframe_; | 112 bool encode_next_frame_as_keyframe_; |
94 | 113 |
| 114 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 115 base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_; |
| 116 |
95 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); | 117 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); |
96 }; | 118 }; |
97 | 119 |
98 // An implementation of SizeAdaptableVideoEncoderBase to proxy for | |
99 // H264VideoToolboxEncoder instances. | |
100 class SizeAdaptableH264VideoToolboxVideoEncoder | |
101 : public SizeAdaptableVideoEncoderBase { | |
102 public: | |
103 SizeAdaptableH264VideoToolboxVideoEncoder( | |
104 const scoped_refptr<CastEnvironment>& cast_environment, | |
105 const VideoSenderConfig& video_config, | |
106 const StatusChangeCallback& status_change_cb); | |
107 | |
108 ~SizeAdaptableH264VideoToolboxVideoEncoder() override; | |
109 | |
110 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; | |
111 | |
112 protected: | |
113 scoped_ptr<VideoEncoder> CreateEncoder() override; | |
114 void OnEncoderReplaced(VideoEncoder* replacement_encoder) override; | |
115 void DestroyEncoder() override; | |
116 | |
117 private: | |
118 struct FactoryHolder; | |
119 class VideoFrameFactoryProxy; | |
120 | |
121 const scoped_refptr<FactoryHolder> holder_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(SizeAdaptableH264VideoToolboxVideoEncoder); | |
124 }; | |
125 | |
126 } // namespace cast | 120 } // namespace cast |
127 } // namespace media | 121 } // namespace media |
128 | 122 |
129 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 123 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
OLD | NEW |