OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 18 matching lines...) Expand all Loading... | |
29 : clock_(clock), | 29 : clock_(clock), |
30 callback_(nullptr), | 30 callback_(nullptr), |
31 max_target_bitrate_kbps_(-1), | 31 max_target_bitrate_kbps_(-1), |
32 last_encode_time_ms_(0) { | 32 last_encode_time_ms_(0) { |
33 // Generate some arbitrary not-all-zero data | 33 // Generate some arbitrary not-all-zero data |
34 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { | 34 for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { |
35 encoded_buffer_[i] = static_cast<uint8_t>(i); | 35 encoded_buffer_[i] = static_cast<uint8_t>(i); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 FakeEncoder::~FakeEncoder() {} | |
40 | |
41 void FakeEncoder::SetMaxBitrate(int max_kbps) { | 39 void FakeEncoder::SetMaxBitrate(int max_kbps) { |
42 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. | 40 RTC_DCHECK_GE(max_kbps, -1); // max_kbps == -1 disables it. |
43 rtc::CritScope cs(&crit_sect_); | 41 rtc::CritScope cs(&crit_sect_); |
44 max_target_bitrate_kbps_ = max_kbps; | 42 max_target_bitrate_kbps_ = max_kbps; |
45 } | 43 } |
46 | 44 |
47 int32_t FakeEncoder::InitEncode(const VideoCodec* config, | 45 int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
48 int32_t number_of_cores, | 46 int32_t number_of_cores, |
49 size_t max_payload_size) { | 47 size_t max_payload_size) { |
50 rtc::CritScope cs(&crit_sect_); | 48 rtc::CritScope cs(&crit_sect_); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 CodecSpecificInfo specifics; | 245 CodecSpecificInfo specifics; |
248 memset(&specifics, 0, sizeof(specifics)); | 246 memset(&specifics, 0, sizeof(specifics)); |
249 specifics.codecType = kVideoCodecH264; | 247 specifics.codecType = kVideoCodecH264; |
250 specifics.codecSpecific.H264.packetization_mode = | 248 specifics.codecSpecific.H264.packetization_mode = |
251 H264PacketizationMode::NonInterleaved; | 249 H264PacketizationMode::NonInterleaved; |
252 RTC_DCHECK(callback); | 250 RTC_DCHECK(callback); |
253 return callback->OnEncodedImage(encoded_image, &specifics, &fragmentation); | 251 return callback->OnEncodedImage(encoded_image, &specifics, &fragmentation); |
254 } | 252 } |
255 | 253 |
256 DelayedEncoder::DelayedEncoder(Clock* clock, int delay_ms) | 254 DelayedEncoder::DelayedEncoder(Clock* clock, int delay_ms) |
257 : test::FakeEncoder(clock), | 255 : test::FakeEncoder(clock), delay_ms_(delay_ms) { |
258 delay_ms_(delay_ms) {} | 256 // The encoder could be created on a different thread than it being used on. |
257 sequence_checker_.Detach(); | |
258 } | |
259 | 259 |
260 void DelayedEncoder::SetDelay(int delay_ms) { | 260 void DelayedEncoder::SetDelay(int delay_ms) { |
261 rtc::CritScope cs(&local_crit_sect_); | 261 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
262 delay_ms_ = delay_ms; | 262 delay_ms_ = delay_ms; |
263 } | 263 } |
264 | 264 |
265 int32_t DelayedEncoder::Encode(const VideoFrame& input_image, | 265 int32_t DelayedEncoder::Encode(const VideoFrame& input_image, |
266 const CodecSpecificInfo* codec_specific_info, | 266 const CodecSpecificInfo* codec_specific_info, |
267 const std::vector<FrameType>* frame_types) { | 267 const std::vector<FrameType>* frame_types) { |
268 int delay_ms = 0; | 268 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
269 { | 269 |
270 rtc::CritScope cs(&local_crit_sect_); | 270 SleepMs(delay_ms_); |
271 delay_ms = delay_ms_; | 271 |
272 } | |
273 SleepMs(delay_ms); | |
274 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); | 272 return FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
275 } | 273 } |
276 | 274 |
277 MultithreadedFakeH264Encoder::MultithreadedFakeH264Encoder(Clock* clock) | 275 MultithreadedFakeH264Encoder::MultithreadedFakeH264Encoder(Clock* clock) |
278 : test::FakeH264Encoder(clock), | 276 : test::FakeH264Encoder(clock), |
279 current_queue_(0), | 277 current_queue_(0), |
280 queue1_("Queue 1"), | 278 queue1_(nullptr), |
281 queue2_("Queue 2") {} | 279 queue2_(nullptr) { |
280 // The encoder could be created on a different thread than it being used on. | |
281 sequence_checker_.Detach(); | |
282 } | |
282 | 283 |
283 MultithreadedFakeH264Encoder::~MultithreadedFakeH264Encoder() = default; | 284 int32_t MultithreadedFakeH264Encoder::InitEncode(const VideoCodec* config, |
285 int32_t number_of_cores, | |
286 size_t max_payload_size) { | |
287 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | |
288 | |
289 queue1_.reset(new rtc::TaskQueue("Queue 1")); | |
290 queue2_.reset(new rtc::TaskQueue("Queue 2")); | |
291 | |
292 return FakeH264Encoder::InitEncode(config, number_of_cores, max_payload_size); | |
293 } | |
284 | 294 |
285 class MultithreadedFakeH264Encoder::EncodeTask : public rtc::QueuedTask { | 295 class MultithreadedFakeH264Encoder::EncodeTask : public rtc::QueuedTask { |
286 public: | 296 public: |
287 EncodeTask(MultithreadedFakeH264Encoder* encoder, | 297 EncodeTask(MultithreadedFakeH264Encoder* encoder, |
288 const VideoFrame& input_image, | 298 const VideoFrame& input_image, |
289 const CodecSpecificInfo* codec_specific_info, | 299 const CodecSpecificInfo* codec_specific_info, |
290 const std::vector<FrameType>* frame_types) | 300 const std::vector<FrameType>* frame_types) |
291 : encoder_(encoder), | 301 : encoder_(encoder), |
292 input_image_(input_image), | 302 input_image_(input_image), |
293 codec_specific_info_(), | 303 codec_specific_info_(), |
(...skipping 12 matching lines...) Expand all Loading... | |
306 MultithreadedFakeH264Encoder* const encoder_; | 316 MultithreadedFakeH264Encoder* const encoder_; |
307 VideoFrame input_image_; | 317 VideoFrame input_image_; |
308 CodecSpecificInfo codec_specific_info_; | 318 CodecSpecificInfo codec_specific_info_; |
309 std::vector<FrameType> frame_types_; | 319 std::vector<FrameType> frame_types_; |
310 }; | 320 }; |
311 | 321 |
312 int32_t MultithreadedFakeH264Encoder::Encode( | 322 int32_t MultithreadedFakeH264Encoder::Encode( |
313 const VideoFrame& input_image, | 323 const VideoFrame& input_image, |
314 const CodecSpecificInfo* codec_specific_info, | 324 const CodecSpecificInfo* codec_specific_info, |
315 const std::vector<FrameType>* frame_types) { | 325 const std::vector<FrameType>* frame_types) { |
316 int current_queue = rtc::AtomicOps::Increment(¤t_queue_); | 326 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
317 rtc::TaskQueue& queue = (current_queue % 2 == 0) ? queue1_ : queue2_; | |
318 | 327 |
319 queue.PostTask(std::unique_ptr<rtc::QueuedTask>( | 328 std::unique_ptr<rtc::TaskQueue>& queue = |
329 (current_queue_++ % 2 == 0) ? queue1_ : queue2_; | |
330 | |
331 if (!queue) { | |
332 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | |
333 } | |
334 | |
335 queue->PostTask(std::unique_ptr<rtc::QueuedTask>( | |
320 new EncodeTask(this, input_image, codec_specific_info, frame_types))); | 336 new EncodeTask(this, input_image, codec_specific_info, frame_types))); |
321 | 337 |
322 return 0; | 338 return WEBRTC_VIDEO_CODEC_OK; |
323 } | 339 } |
324 | 340 |
325 int32_t MultithreadedFakeH264Encoder::EncodeCallback( | 341 int32_t MultithreadedFakeH264Encoder::EncodeCallback( |
326 const VideoFrame& input_image, | 342 const VideoFrame& input_image, |
327 const CodecSpecificInfo* codec_specific_info, | 343 const CodecSpecificInfo* codec_specific_info, |
328 const std::vector<FrameType>* frame_types) { | 344 const std::vector<FrameType>* frame_types) { |
329 return FakeH264Encoder::Encode(input_image, codec_specific_info, frame_types); | 345 return FakeH264Encoder::Encode(input_image, codec_specific_info, frame_types); |
330 } | 346 } |
331 | 347 |
348 int32_t MultithreadedFakeH264Encoder::Release() { | |
349 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | |
350 | |
351 queue1_.reset(); | |
brandtr
2017/02/10 09:15:35
This should stop the posted tasks being executed:
| |
352 queue2_.reset(); | |
353 | |
354 return FakeH264Encoder::Release(); | |
355 } | |
356 | |
332 } // namespace test | 357 } // namespace test |
333 } // namespace webrtc | 358 } // namespace webrtc |
OLD | NEW |