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