OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 public: | 53 public: |
54 // Interface for receiving encoded video frames and notifications about | 54 // Interface for receiving encoded video frames and notifications about |
55 // configuration changes. | 55 // configuration changes. |
56 class EncoderSink : public EncodedImageCallback { | 56 class EncoderSink : public EncodedImageCallback { |
57 public: | 57 public: |
58 virtual void OnEncoderConfigurationChanged( | 58 virtual void OnEncoderConfigurationChanged( |
59 std::vector<VideoStream> streams, | 59 std::vector<VideoStream> streams, |
60 int min_transmit_bitrate_bps) = 0; | 60 int min_transmit_bitrate_bps) = 0; |
61 }; | 61 }; |
62 | 62 |
| 63 // Down grade resolution at most 2 times for CPU reasons. |
| 64 static const int kMaxCpuDowngrades = 2; |
| 65 |
63 ViEEncoder(uint32_t number_of_cores, | 66 ViEEncoder(uint32_t number_of_cores, |
64 SendStatisticsProxy* stats_proxy, | 67 SendStatisticsProxy* stats_proxy, |
65 const webrtc::VideoSendStream::Config::EncoderSettings& settings, | 68 const webrtc::VideoSendStream::Config::EncoderSettings& settings, |
66 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 69 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
67 LoadObserver* overuse_callback, | |
68 EncodedFrameObserver* encoder_timing); | 70 EncodedFrameObserver* encoder_timing); |
69 ~ViEEncoder(); | 71 ~ViEEncoder(); |
70 // RegisterProcessThread register |module_process_thread| with those objects | 72 // RegisterProcessThread register |module_process_thread| with those objects |
71 // that use it. Registration has to happen on the thread where | 73 // that use it. Registration has to happen on the thread where |
72 // |module_process_thread| was created (libjingle's worker thread). | 74 // |module_process_thread| was created (libjingle's worker thread). |
73 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue. | 75 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue. |
74 void RegisterProcessThread(ProcessThread* module_process_thread); | 76 void RegisterProcessThread(ProcessThread* module_process_thread); |
75 void DeRegisterProcessThread(); | 77 void DeRegisterProcessThread(); |
76 | 78 |
77 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source); | 79 // Sets the source that will provide I420 video frames. |
78 void SetSink(EncoderSink* sink); | 80 // |disable_resolution| means that this encoder will not request |source| to |
| 81 // lower the resolution due to CPU adaptation. |
| 82 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 83 bool disable_resolution_scaling); |
| 84 |
| 85 // Sets the |sink| that gets the encoded frames. |rotation_applied| means |
| 86 // that the source must support rotation. Only set |rotation_applied| if the |
| 87 // remote side does not support the rotation extension. |
| 88 void SetSink(EncoderSink* sink, bool rotation_applied); |
79 | 89 |
80 // TODO(perkj): Can we remove VideoCodec.startBitrate ? | 90 // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
81 void SetStartBitrate(int start_bitrate_bps); | 91 void SetStartBitrate(int start_bitrate_bps); |
82 | 92 |
83 void ConfigureEncoder(VideoEncoderConfig config, | 93 void ConfigureEncoder(VideoEncoderConfig config, |
84 size_t max_data_payload_length); | 94 size_t max_data_payload_length); |
85 | 95 |
86 // Permanently stop encoding. After this method has returned, it is | 96 // Permanently stop encoding. After this method has returned, it is |
87 // guaranteed that no encoded frames will be delivered to the sink. | 97 // guaranteed that no encoded frames will be delivered to the sink. |
88 void Stop(); | 98 void Stop(); |
89 | 99 |
90 void SendKeyFrame(); | 100 void SendKeyFrame(); |
91 | 101 |
92 // virtual to test EncoderStateFeedback with mocks. | 102 // virtual to test EncoderStateFeedback with mocks. |
93 virtual void OnReceivedIntraFrameRequest(size_t stream_index); | 103 virtual void OnReceivedIntraFrameRequest(size_t stream_index); |
94 virtual void OnReceivedSLI(uint8_t picture_id); | 104 virtual void OnReceivedSLI(uint8_t picture_id); |
95 virtual void OnReceivedRPSI(uint64_t picture_id); | 105 virtual void OnReceivedRPSI(uint64_t picture_id); |
96 | 106 |
97 void OnBitrateUpdated(uint32_t bitrate_bps, | 107 void OnBitrateUpdated(uint32_t bitrate_bps, |
98 uint8_t fraction_lost, | 108 uint8_t fraction_lost, |
99 int64_t round_trip_time_ms); | 109 int64_t round_trip_time_ms); |
100 | 110 |
| 111 protected: |
| 112 // Used for testing. For example the |CpuOveruseObserver| methods must be |
| 113 // called on |encoder_queue_|. |
| 114 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; } |
| 115 |
| 116 // webrtc::CpuOveruseObserver implementation. |
| 117 // These methods are protected for easier testing. |
| 118 void OveruseDetected() override; |
| 119 void NormalUsage() override; |
| 120 |
101 private: | 121 private: |
102 class ConfigureEncoderTask; | 122 class ConfigureEncoderTask; |
103 class EncodeTask; | 123 class EncodeTask; |
104 class VideoSourceProxy; | 124 class VideoSourceProxy; |
105 | 125 |
106 struct VideoFrameInfo { | 126 struct VideoFrameInfo { |
107 VideoFrameInfo(int width, | 127 VideoFrameInfo(int width, |
108 int height, | 128 int height, |
109 VideoRotation rotation, | 129 VideoRotation rotation, |
110 bool is_texture) | 130 bool is_texture) |
(...skipping 20 matching lines...) Expand all Loading... |
131 | 151 |
132 void EncodeVideoFrame(const VideoFrame& frame, | 152 void EncodeVideoFrame(const VideoFrame& frame, |
133 int64_t time_when_posted_in_ms); | 153 int64_t time_when_posted_in_ms); |
134 | 154 |
135 // Implements EncodedImageCallback. | 155 // Implements EncodedImageCallback. |
136 EncodedImageCallback::Result OnEncodedImage( | 156 EncodedImageCallback::Result OnEncodedImage( |
137 const EncodedImage& encoded_image, | 157 const EncodedImage& encoded_image, |
138 const CodecSpecificInfo* codec_specific_info, | 158 const CodecSpecificInfo* codec_specific_info, |
139 const RTPFragmentationHeader* fragmentation) override; | 159 const RTPFragmentationHeader* fragmentation) override; |
140 | 160 |
141 // webrtc::CpuOveruseObserver implementation. | |
142 void OveruseDetected() override; | |
143 void NormalUsage() override; | |
144 | |
145 bool EncoderPaused() const; | 161 bool EncoderPaused() const; |
146 void TraceFrameDropStart(); | 162 void TraceFrameDropStart(); |
147 void TraceFrameDropEnd(); | 163 void TraceFrameDropEnd(); |
148 | 164 |
149 rtc::Event shutdown_event_; | 165 rtc::Event shutdown_event_; |
150 | 166 |
151 const uint32_t number_of_cores_; | 167 const uint32_t number_of_cores_; |
152 | 168 |
153 const std::unique_ptr<VideoSourceProxy> source_proxy_; | 169 const std::unique_ptr<VideoSourceProxy> source_proxy_; |
154 EncoderSink* sink_; | 170 EncoderSink* sink_; |
155 const VideoSendStream::Config::EncoderSettings settings_; | 171 const VideoSendStream::Config::EncoderSettings settings_; |
156 const VideoCodecType codec_type_; | 172 const VideoCodecType codec_type_; |
157 | 173 |
158 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); | 174 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); |
159 | 175 bool disable_resolution_scaling_ ACCESS_ON(&encoder_queue_); |
160 OveruseFrameDetector overuse_detector_ ACCESS_ON(&encoder_queue_); | 176 OveruseFrameDetector overuse_detector_ ACCESS_ON(&encoder_queue_); |
161 LoadObserver* const load_observer_ ACCESS_ON(&encoder_queue_); | |
162 | 177 |
163 SendStatisticsProxy* const stats_proxy_; | 178 SendStatisticsProxy* const stats_proxy_; |
164 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; | 179 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; |
165 ProcessThread* module_process_thread_; | 180 ProcessThread* module_process_thread_; |
166 rtc::ThreadChecker module_process_thread_checker_; | 181 rtc::ThreadChecker module_process_thread_checker_; |
167 // |thread_checker_| checks that public methods that are related to lifetime | 182 // |thread_checker_| checks that public methods that are related to lifetime |
168 // of ViEEncoder are called on the same thread. | 183 // of ViEEncoder are called on the same thread. |
169 rtc::ThreadChecker thread_checker_; | 184 rtc::ThreadChecker thread_checker_; |
170 | 185 |
171 VideoEncoderConfig encoder_config_ ACCESS_ON(&encoder_queue_); | 186 VideoEncoderConfig encoder_config_ ACCESS_ON(&encoder_queue_); |
172 // TODO(sprang): Change |rate_allocator_| to be a codec type | 187 // TODO(sprang): Change |rate_allocator_| to be a codec type |
173 // agnostic interface. It is currently VP8 simulcast specific if more than | 188 // agnostic interface. It is currently VP8 simulcast specific if more than |
174 // one layer is specified. | 189 // one layer is specified. |
175 std::unique_ptr<SimulcastRateAllocator> rate_allocator_ | 190 std::unique_ptr<SimulcastRateAllocator> rate_allocator_ |
176 ACCESS_ON(&encoder_queue_); | 191 ACCESS_ON(&encoder_queue_); |
177 | 192 |
178 // Set when ConfigureEncoder has been called in order to lazy reconfigure the | 193 // Set when ConfigureEncoder has been called in order to lazy reconfigure the |
179 // encoder on the next frame. | 194 // encoder on the next frame. |
180 bool pending_encoder_reconfiguration_ ACCESS_ON(&encoder_queue_); | 195 bool pending_encoder_reconfiguration_ ACCESS_ON(&encoder_queue_); |
181 rtc::Optional<VideoFrameInfo> last_frame_info_ ACCESS_ON(&encoder_queue_); | 196 rtc::Optional<VideoFrameInfo> last_frame_info_ ACCESS_ON(&encoder_queue_); |
182 uint32_t encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_); | 197 uint32_t encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
183 size_t max_data_payload_length_ ACCESS_ON(&encoder_queue_); | 198 size_t max_data_payload_length_ ACCESS_ON(&encoder_queue_); |
184 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_); | 199 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
185 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_); | 200 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_); |
186 bool has_received_sli_ ACCESS_ON(&encoder_queue_); | 201 bool has_received_sli_ ACCESS_ON(&encoder_queue_); |
187 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_); | 202 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_); |
188 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_); | 203 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_); |
189 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_); | 204 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_); |
190 Clock* const clock_; | 205 Clock* const clock_; |
| 206 // Counter used for deciding if the video resolution is currently |
| 207 // restricted by CPU usage. It is reset if |source_| is changed. |
| 208 int cpu_restricted_counter_; // ACCESS_ON(&encoder_queue_); |
| 209 |
| 210 int last_frame_width_; // ACCESS_ON(&encoder_queue_); |
| 211 int last_frame_height_; // ACCESS_ON(&encoder_queue_); |
| 212 // Resolution has been requested to be less than |max_pixel_count_|. |
| 213 rtc::Optional<int> max_pixel_count_; // ACCESS_ON(&encoder_queue_); |
| 214 // Resolution has been requested to higher than |max_pixel_count_step_up_|. |
| 215 rtc::Optional<int> max_pixel_count_step_up_; // ACCESS_ON(&encoder_queue_); |
191 | 216 |
192 rtc::RaceChecker incoming_frame_race_checker_; | 217 rtc::RaceChecker incoming_frame_race_checker_; |
193 Atomic32 posted_frames_waiting_for_encode_; | 218 Atomic32 posted_frames_waiting_for_encode_; |
194 // Used to make sure incoming time stamp is increasing for every frame. | 219 // Used to make sure incoming time stamp is increasing for every frame. |
195 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); | 220 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); |
196 // Delta used for translating between NTP and internal timestamps. | 221 // Delta used for translating between NTP and internal timestamps. |
197 const int64_t delta_ntp_internal_ms_; | 222 const int64_t delta_ntp_internal_ms_; |
198 | 223 |
199 int64_t last_frame_log_ms_ GUARDED_BY(incoming_frame_race_checker_); | 224 int64_t last_frame_log_ms_ GUARDED_BY(incoming_frame_race_checker_); |
200 int captured_frame_count_ ACCESS_ON(&encoder_queue_); | 225 int captured_frame_count_ ACCESS_ON(&encoder_queue_); |
201 int dropped_frame_count_ ACCESS_ON(&encoder_queue_); | 226 int dropped_frame_count_ ACCESS_ON(&encoder_queue_); |
202 | 227 |
203 // All public methods are proxied to |encoder_queue_|. It must must be | 228 // All public methods are proxied to |encoder_queue_|. It must must be |
204 // destroyed first to make sure no tasks are run that use other members. | 229 // destroyed first to make sure no tasks are run that use other members. |
205 rtc::TaskQueue encoder_queue_; | 230 rtc::TaskQueue encoder_queue_; |
206 | 231 |
207 RTC_DISALLOW_COPY_AND_ASSIGN(ViEEncoder); | 232 RTC_DISALLOW_COPY_AND_ASSIGN(ViEEncoder); |
208 }; | 233 }; |
209 | 234 |
210 } // namespace webrtc | 235 } // namespace webrtc |
211 | 236 |
212 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ | 237 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ |
OLD | NEW |