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/video_encoder.h" | 12 #include "media/cast/sender/video_encoder.h" |
12 | 13 |
13 namespace media { | 14 namespace media { |
14 namespace cast { | 15 namespace cast { |
15 | 16 |
16 // VideoToolbox implementation of the media::cast::VideoEncoder interface. | 17 // VideoToolbox implementation of the media::cast::VideoEncoder interface. |
17 // 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 |
18 // pinned to the thread on which it is constructed. | 19 // pinned to the thread on which it is constructed. |
19 class H264VideoToolboxEncoder : public VideoEncoder { | 20 class H264VideoToolboxEncoder : public VideoEncoder { |
20 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; | 21 typedef CoreMediaGlue::CMSampleBufferRef CMSampleBufferRef; |
21 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; | 22 typedef VideoToolboxGlue::VTCompressionSessionRef VTCompressionSessionRef; |
22 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; | 23 typedef VideoToolboxGlue::VTEncodeInfoFlags VTEncodeInfoFlags; |
23 | 24 |
24 public: | 25 public: |
| 26 // Returns true if the current platform and system configuration supports |
| 27 // using H264VideoToolboxEncoder with the given |video_config|. |
| 28 static bool IsSupported(const VideoSenderConfig& video_config); |
| 29 |
25 H264VideoToolboxEncoder( | 30 H264VideoToolboxEncoder( |
26 const scoped_refptr<CastEnvironment>& cast_environment, | 31 const scoped_refptr<CastEnvironment>& cast_environment, |
27 const VideoSenderConfig& video_config, | 32 const VideoSenderConfig& video_config, |
28 const gfx::Size& frame_size, | 33 const gfx::Size& frame_size, |
| 34 uint32 first_frame_id, |
29 const StatusChangeCallback& status_change_cb); | 35 const StatusChangeCallback& status_change_cb); |
30 ~H264VideoToolboxEncoder() override; | 36 ~H264VideoToolboxEncoder() override; |
31 | 37 |
32 // media::cast::VideoEncoder implementation | 38 // media::cast::VideoEncoder implementation |
33 bool CanEncodeVariedFrameSizes() const override; | |
34 bool EncodeVideoFrame( | 39 bool EncodeVideoFrame( |
35 const scoped_refptr<media::VideoFrame>& video_frame, | 40 const scoped_refptr<media::VideoFrame>& video_frame, |
36 const base::TimeTicks& reference_time, | 41 const base::TimeTicks& reference_time, |
37 const FrameEncodedCallback& frame_encoded_callback) override; | 42 const FrameEncodedCallback& frame_encoded_callback) override; |
38 void SetBitRate(int new_bit_rate) override; | 43 void SetBitRate(int new_bit_rate) override; |
39 void GenerateKeyFrame() override; | 44 void GenerateKeyFrame() override; |
40 void LatestFrameIdToReference(uint32 frame_id) override; | 45 void LatestFrameIdToReference(uint32 frame_id) override; |
41 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; | 46 scoped_ptr<VideoFrameFactory> CreateVideoFrameFactory() override; |
42 void EmitFrames() override; | 47 void EmitFrames() override; |
43 | 48 |
44 private: | 49 private: |
45 // Initialize the compression session. | 50 // Initialize the compression session. |
46 bool Initialize(const VideoSenderConfig& video_config, | 51 bool Initialize(const VideoSenderConfig& video_config); |
47 const gfx::Size& frame_size); | |
48 | 52 |
49 // Configure the compression session. | 53 // Configure the compression session. |
50 void ConfigureSession(const VideoSenderConfig& video_config); | 54 void ConfigureSession(const VideoSenderConfig& video_config); |
51 | 55 |
52 // Teardown the encoder. | 56 // Teardown the encoder. |
53 void Teardown(); | 57 void Teardown(); |
54 | 58 |
55 // Set a compression session property. | 59 // Set a compression session property. |
56 bool SetSessionProperty(CFStringRef key, int32_t value); | 60 bool SetSessionProperty(CFStringRef key, int32_t value); |
57 bool SetSessionProperty(CFStringRef key, bool value); | 61 bool SetSessionProperty(CFStringRef key, bool value); |
58 bool SetSessionProperty(CFStringRef key, CFStringRef value); | 62 bool SetSessionProperty(CFStringRef key, CFStringRef value); |
59 | 63 |
60 // Compression session callback function to handle compressed frames. | 64 // Compression session callback function to handle compressed frames. |
61 static void CompressionCallback(void* encoder_opaque, | 65 static void CompressionCallback(void* encoder_opaque, |
62 void* request_opaque, | 66 void* request_opaque, |
63 OSStatus status, | 67 OSStatus status, |
64 VTEncodeInfoFlags info, | 68 VTEncodeInfoFlags info, |
65 CMSampleBufferRef sbuf); | 69 CMSampleBufferRef sbuf); |
66 | 70 |
67 // The cast environment (contains worker threads & more). | 71 // The cast environment (contains worker threads & more). |
68 const scoped_refptr<CastEnvironment> cast_environment_; | 72 const scoped_refptr<CastEnvironment> cast_environment_; |
69 | 73 |
70 // VideoToolboxGlue provides access to VideoToolbox at runtime. | 74 // VideoToolboxGlue provides access to VideoToolbox at runtime. |
71 const VideoToolboxGlue* videotoolbox_glue_; | 75 const VideoToolboxGlue* const videotoolbox_glue_; |
| 76 |
| 77 // The size of the visible region of the video frames to be encoded. |
| 78 const gfx::Size frame_size_; |
| 79 |
| 80 // Callback used to report initialization status and runtime errors. |
| 81 const StatusChangeCallback status_change_cb_; |
72 | 82 |
73 // Thread checker to enforce that this object is used on a specific thread. | 83 // Thread checker to enforce that this object is used on a specific thread. |
74 base::ThreadChecker thread_checker_; | 84 base::ThreadChecker thread_checker_; |
75 | 85 |
76 // The compression session. | 86 // The compression session. |
77 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; | 87 base::ScopedCFTypeRef<VTCompressionSessionRef> compression_session_; |
78 | 88 |
79 // Frame identifier counter. | 89 // Frame identifier counter. |
80 uint32 frame_id_; | 90 uint32 next_frame_id_; |
81 | 91 |
82 // Force next frame to be a keyframe. | 92 // Force next frame to be a keyframe. |
83 bool encode_next_frame_as_keyframe_; | 93 bool encode_next_frame_as_keyframe_; |
84 | 94 |
85 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); | 95 DISALLOW_COPY_AND_ASSIGN(H264VideoToolboxEncoder); |
86 }; | 96 }; |
87 | 97 |
| 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 |
88 } // namespace cast | 126 } // namespace cast |
89 } // namespace media | 127 } // namespace media |
90 | 128 |
91 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ | 129 #endif // MEDIA_CAST_SENDER_H264_VT_ENCODER_H_ |
OLD | NEW |