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 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 // |degradation_preference| control whether or not resolution or frame rate |
| 81 // may be reduced. |
| 82 void SetSource( |
| 83 rtc::VideoSourceInterface<VideoFrame>* source, |
| 84 const VideoSendStream::DegradationPreference& degradation_preference); |
| 85 |
| 86 // Sets the |sink| that gets the encoded frames. |rotation_applied| means |
| 87 // that the source must support rotation. Only set |rotation_applied| if the |
| 88 // remote side does not support the rotation extension. |
| 89 void SetSink(EncoderSink* sink, bool rotation_applied); |
79 | 90 |
80 // TODO(perkj): Can we remove VideoCodec.startBitrate ? | 91 // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
81 void SetStartBitrate(int start_bitrate_bps); | 92 void SetStartBitrate(int start_bitrate_bps); |
82 | 93 |
83 void ConfigureEncoder(VideoEncoderConfig config, | 94 void ConfigureEncoder(VideoEncoderConfig config, |
84 size_t max_data_payload_length); | 95 size_t max_data_payload_length); |
85 | 96 |
86 // Permanently stop encoding. After this method has returned, it is | 97 // Permanently stop encoding. After this method has returned, it is |
87 // guaranteed that no encoded frames will be delivered to the sink. | 98 // guaranteed that no encoded frames will be delivered to the sink. |
88 void Stop(); | 99 void Stop(); |
89 | 100 |
90 void SendKeyFrame(); | 101 void SendKeyFrame(); |
91 | 102 |
92 // virtual to test EncoderStateFeedback with mocks. | 103 // virtual to test EncoderStateFeedback with mocks. |
93 virtual void OnReceivedIntraFrameRequest(size_t stream_index); | 104 virtual void OnReceivedIntraFrameRequest(size_t stream_index); |
94 virtual void OnReceivedSLI(uint8_t picture_id); | 105 virtual void OnReceivedSLI(uint8_t picture_id); |
95 virtual void OnReceivedRPSI(uint64_t picture_id); | 106 virtual void OnReceivedRPSI(uint64_t picture_id); |
96 | 107 |
97 void OnBitrateUpdated(uint32_t bitrate_bps, | 108 void OnBitrateUpdated(uint32_t bitrate_bps, |
98 uint8_t fraction_lost, | 109 uint8_t fraction_lost, |
99 int64_t round_trip_time_ms); | 110 int64_t round_trip_time_ms); |
100 | 111 |
| 112 protected: |
| 113 // Used for testing. For example the |CpuOveruseObserver| methods must be |
| 114 // called on |encoder_queue_|. |
| 115 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; } |
| 116 |
| 117 // webrtc::CpuOveruseObserver implementation. |
| 118 // These methods are protected for easier testing. |
| 119 void OveruseDetected() override; |
| 120 void NormalUsage() override; |
| 121 |
101 private: | 122 private: |
102 class ConfigureEncoderTask; | 123 class ConfigureEncoderTask; |
103 class EncodeTask; | 124 class EncodeTask; |
104 class VideoSourceProxy; | 125 class VideoSourceProxy; |
105 | 126 |
106 struct VideoFrameInfo { | 127 struct VideoFrameInfo { |
107 VideoFrameInfo(int width, | 128 VideoFrameInfo(int width, |
108 int height, | 129 int height, |
109 VideoRotation rotation, | 130 VideoRotation rotation, |
110 bool is_texture) | 131 bool is_texture) |
(...skipping 20 matching lines...) Expand all Loading... |
131 | 152 |
132 void EncodeVideoFrame(const VideoFrame& frame, | 153 void EncodeVideoFrame(const VideoFrame& frame, |
133 int64_t time_when_posted_in_ms); | 154 int64_t time_when_posted_in_ms); |
134 | 155 |
135 // Implements EncodedImageCallback. | 156 // Implements EncodedImageCallback. |
136 EncodedImageCallback::Result OnEncodedImage( | 157 EncodedImageCallback::Result OnEncodedImage( |
137 const EncodedImage& encoded_image, | 158 const EncodedImage& encoded_image, |
138 const CodecSpecificInfo* codec_specific_info, | 159 const CodecSpecificInfo* codec_specific_info, |
139 const RTPFragmentationHeader* fragmentation) override; | 160 const RTPFragmentationHeader* fragmentation) override; |
140 | 161 |
141 // webrtc::CpuOveruseObserver implementation. | |
142 void OveruseDetected() override; | |
143 void NormalUsage() override; | |
144 | |
145 bool EncoderPaused() const; | 162 bool EncoderPaused() const; |
146 void TraceFrameDropStart(); | 163 void TraceFrameDropStart(); |
147 void TraceFrameDropEnd(); | 164 void TraceFrameDropEnd(); |
148 | 165 |
149 rtc::Event shutdown_event_; | 166 rtc::Event shutdown_event_; |
150 | 167 |
151 const uint32_t number_of_cores_; | 168 const uint32_t number_of_cores_; |
152 | 169 |
153 const std::unique_ptr<VideoSourceProxy> source_proxy_; | 170 const std::unique_ptr<VideoSourceProxy> source_proxy_; |
154 EncoderSink* sink_; | 171 EncoderSink* sink_; |
155 const VideoSendStream::Config::EncoderSettings settings_; | 172 const VideoSendStream::Config::EncoderSettings settings_; |
156 const VideoCodecType codec_type_; | 173 const VideoCodecType codec_type_; |
157 | 174 |
158 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); | 175 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); |
159 | |
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_); |
(...skipping 10 matching lines...) Expand all Loading... |
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_; |
191 | 206 |
192 rtc::RaceChecker incoming_frame_race_checker_; | 207 VideoSendStream::DegradationPreference degradation_preference_ |
| 208 ACCESS_ON(&encoder_queue_); |
| 209 // Counter used for deciding if the video resolution is currently |
| 210 // restricted by CPU usage. |
| 211 int cpu_restricted_counter_ ACCESS_ON(&encoder_queue_); |
| 212 |
| 213 int last_frame_width_ ACCESS_ON(&encoder_queue_); |
| 214 int last_frame_height_ ACCESS_ON(&encoder_queue_); |
| 215 // Pixel count last time the resolution was requested to be changed down. |
| 216 rtc::Optional<int> max_pixel_count_ ACCESS_ON(&encoder_queue_); |
| 217 // Pixel count last time the resolution was requested to be changed up. |
| 218 rtc::Optional<int> max_pixel_count_step_up_ ACCESS_ON(&encoder_queue_); |
| 219 |
| 220 rtc::RaceChecker incoming_frame_race_checker_ |
| 221 GUARDED_BY(incoming_frame_race_checker_); |
193 Atomic32 posted_frames_waiting_for_encode_; | 222 Atomic32 posted_frames_waiting_for_encode_; |
194 // Used to make sure incoming time stamp is increasing for every frame. | 223 // Used to make sure incoming time stamp is increasing for every frame. |
195 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); | 224 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); |
196 // Delta used for translating between NTP and internal timestamps. | 225 // Delta used for translating between NTP and internal timestamps. |
197 const int64_t delta_ntp_internal_ms_; | 226 const int64_t delta_ntp_internal_ms_ GUARDED_BY(incoming_frame_race_checker_); |
198 | 227 |
199 int64_t last_frame_log_ms_ GUARDED_BY(incoming_frame_race_checker_); | 228 int64_t last_frame_log_ms_ GUARDED_BY(incoming_frame_race_checker_); |
200 int captured_frame_count_ ACCESS_ON(&encoder_queue_); | 229 int captured_frame_count_ ACCESS_ON(&encoder_queue_); |
201 int dropped_frame_count_ ACCESS_ON(&encoder_queue_); | 230 int dropped_frame_count_ ACCESS_ON(&encoder_queue_); |
202 | 231 |
203 // All public methods are proxied to |encoder_queue_|. It must must be | 232 // 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. | 233 // destroyed first to make sure no tasks are run that use other members. |
205 rtc::TaskQueue encoder_queue_; | 234 rtc::TaskQueue encoder_queue_; |
206 | 235 |
207 RTC_DISALLOW_COPY_AND_ASSIGN(ViEEncoder); | 236 RTC_DISALLOW_COPY_AND_ASSIGN(ViEEncoder); |
208 }; | 237 }; |
209 | 238 |
210 } // namespace webrtc | 239 } // namespace webrtc |
211 | 240 |
212 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ | 241 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ |
OLD | NEW |