Chromium Code Reviews| 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. Supports changing frame | 20 // pinned to the thread on which it is constructed. Supports changing frame |
| 20 // sizes directly. | 21 // sizes directly. |
| 21 class H264VideoToolboxEncoder : public VideoEncoder { | 22 class H264VideoToolboxEncoder : public VideoEncoder { |
| 22 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | 23 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; |
| 23 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | 24 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; |
| 24 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | 25 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; |
| 25 | 26 |
| 26 public: | 27 public: |
| 27 // VideoFrameFactory tied to the VideoToolbox encoder. | |
| 28 class VideoFrameFactoryImpl; | |
| 29 | |
| 30 // Returns true if the current platform and system configuration supports | 28 // Returns true if the current platform and system configuration supports |
| 31 // using H264VideoToolboxEncoder with the given |video_config|. | 29 // using H264VideoToolboxEncoder with the given |video_config|. |
| 32 static bool IsSupported(const VideoSenderConfig& video_config); | 30 static bool IsSupported(const VideoSenderConfig& video_config); |
| 33 | 31 |
| 34 H264VideoToolboxEncoder( | 32 H264VideoToolboxEncoder( |
| 35 const scoped_refptr<CastEnvironment>& cast_environment, | 33 const scoped_refptr<CastEnvironment>& cast_environment, |
| 36 const VideoSenderConfig& video_config, | 34 const VideoSenderConfig& video_config, |
| 37 const StatusChangeCallback& status_change_cb); | 35 const StatusChangeCallback& status_change_cb); |
| 38 ~H264VideoToolboxEncoder() override; | 36 ~H264VideoToolboxEncoder() override; |
| 39 | 37 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 bool SetSessionProperty(CFStringRef key, bool value); | 69 bool SetSessionProperty(CFStringRef key, bool value); |
| 72 bool SetSessionProperty(CFStringRef key, CFStringRef value); | 70 bool SetSessionProperty(CFStringRef key, CFStringRef value); |
| 73 | 71 |
| 74 // Compression session callback function to handle compressed frames. | 72 // Compression session callback function to handle compressed frames. |
| 75 static void CompressionCallback(void* encoder_opaque, | 73 static void CompressionCallback(void* encoder_opaque, |
| 76 void* request_opaque, | 74 void* request_opaque, |
| 77 OSStatus status, | 75 OSStatus status, |
| 78 VTEncodeInfoFlags info, | 76 VTEncodeInfoFlags info, |
| 79 CMSampleBufferRef sbuf); | 77 CMSampleBufferRef sbuf); |
| 80 | 78 |
| 79 // VideoFrameFactory tied to the encoder. | |
| 80 class VideoFrameFactoryImpl; | |
| 81 | |
| 82 // PowerObserver tied to the encoder. Emits all frames and destroys the | |
| 83 // compression session on suspend and resets the compression session on | |
| 84 // resume. | |
| 85 class PowerObserver : public base::PowerObserver { | |
|
miu
2015/04/22 04:02:58
Instead of a nested class, how about making H264Vi
jfroy
2015/04/22 04:18:45
I considered that but decided to use an auxiliary
| |
| 86 public: | |
| 87 explicit PowerObserver(H264VideoToolboxEncoder* encoder); | |
| 88 virtual ~PowerObserver(); | |
| 89 void OnSuspend() override; | |
| 90 void OnResume() override; | |
| 91 | |
| 92 private: | |
| 93 H264VideoToolboxEncoder* encoder_; | |
| 94 DISALLOW_COPY_AND_ASSIGN(PowerObserver); | |
| 95 }; | |
| 96 | |
| 81 // The cast environment (contains worker threads & more). | 97 // The cast environment (contains worker threads & more). |
| 82 const scoped_refptr<CastEnvironment> cast_environment_; | 98 const scoped_refptr<CastEnvironment> cast_environment_; |
| 83 | 99 |
| 84 // VideoToolboxGlue provides access to VideoToolbox at runtime. | 100 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
| 85 const VideoToolboxGlue* const videotoolbox_glue_; | 101 const VideoToolboxGlue* const videotoolbox_glue_; |
| 86 | 102 |
| 87 // VideoSenderConfig copy so we can create compression sessions on demand. | 103 // VideoSenderConfig copy so we can create compression sessions on demand. |
| 88 // This is needed to recover from backgrounding and other events that can | 104 // This is needed to recover from backgrounding and other events that can |
| 89 // invalidate compression sessions. | 105 // invalidate compression sessions. |
| 90 const VideoSenderConfig video_config_; | 106 const VideoSenderConfig video_config_; |
| 91 | 107 |
| 92 // Frame size of the current compression session. Can be changed by submitting | 108 // 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. | 109 // a frame of a different size, which will cause a compression session reset. |
| 94 gfx::Size frame_size_; | 110 gfx::Size frame_size_; |
| 95 | 111 |
| 96 // Callback used to report initialization status and runtime errors. | 112 // Callback used to report initialization status and runtime errors. |
| 97 const StatusChangeCallback status_change_cb_; | 113 const StatusChangeCallback status_change_cb_; |
| 98 | 114 |
| 99 // Thread checker to enforce that this object is used on a specific thread. | 115 // Thread checker to enforce that this object is used on a specific thread. |
| 100 base::ThreadChecker thread_checker_; | 116 base::ThreadChecker thread_checker_; |
| 101 | 117 |
| 118 // PowerObserver tied to the encoder. | |
| 119 const PowerObserver power_observer_; | |
| 120 | |
| 102 // The compression session. | 121 // The compression session. |
| 103 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | 122 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; |
| 104 | 123 |
| 105 // Video frame factory tied to the encoder. | 124 // Video frame factory tied to the encoder. |
| 106 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; | 125 scoped_refptr<VideoFrameFactoryImpl> video_frame_factory_; |
| 107 | 126 |
| 108 // The ID of the last frame that was emitted. | 127 // The ID of the last frame that was emitted. |
| 109 uint32 last_frame_id_; | 128 uint32 last_frame_id_; |
| 110 | 129 |
| 111 // Force next frame to be a keyframe. | 130 // Force next frame to be a keyframe. |
| 112 bool encode_next_frame_as_keyframe_; | 131 bool encode_next_frame_as_keyframe_; |
| 113 | 132 |
| 114 // NOTE: Weak pointers must be invalidated before all other member variables. | 133 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 115 base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_; | 134 base::WeakPtrFactory<H264VideoToolboxEncoder> weak_factory_; |
| 116 | 135 |
| 117 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); | 136 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); |
| 118 }; | 137 }; |
| 119 | 138 |
| 120 } // namespace cast | 139 } // namespace cast |
| 121 } // namespace media | 140 } // namespace media |
| 122 | 141 |
| 123 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 142 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
| OLD | NEW |