| 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 |
| 11 #include "webrtc/video/video_receive_stream.h" | 11 #include "webrtc/video/video_receive_stream.h" |
| 12 | 12 |
| 13 #include <stdlib.h> | 13 #include <stdlib.h> |
| 14 | 14 |
| 15 #include <set> | 15 #include <set> |
| 16 #include <string> | 16 #include <string> |
| 17 #include <utility> | 17 #include <utility> |
| 18 | 18 |
| 19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/location.h" | 20 #include "webrtc/base/location.h" |
| 21 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/optional.h" | 22 #include "webrtc/base/optional.h" |
| 23 #include "webrtc/base/timeutils.h" |
| 23 #include "webrtc/base/trace_event.h" | 24 #include "webrtc/base/trace_event.h" |
| 24 #include "webrtc/common_video/h264/profile_level_id.h" | 25 #include "webrtc/common_video/h264/profile_level_id.h" |
| 25 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 26 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 26 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | 27 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
| 27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 28 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 28 #include "webrtc/modules/utility/include/process_thread.h" | 29 #include "webrtc/modules/utility/include/process_thread.h" |
| 29 #include "webrtc/modules/video_coding/frame_object.h" | 30 #include "webrtc/modules/video_coding/frame_object.h" |
| 30 #include "webrtc/modules/video_coding/include/video_coding.h" | 31 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 31 #include "webrtc/modules/video_coding/jitter_estimator.h" | 32 #include "webrtc/modules/video_coding/jitter_estimator.h" |
| 32 #include "webrtc/modules/video_coding/timing.h" | 33 #include "webrtc/modules/video_coding/timing.h" |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 230 |
| 230 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); | 231 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); |
| 231 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); | 232 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); |
| 232 } | 233 } |
| 233 | 234 |
| 234 VideoReceiveStream::~VideoReceiveStream() { | 235 VideoReceiveStream::~VideoReceiveStream() { |
| 235 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 236 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 236 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); | 237 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); |
| 237 Stop(); | 238 Stop(); |
| 238 | 239 |
| 240 LOG(LS_INFO) << "~VideoReceiveStream: DeRegisterModule rtp_stream_sync_."; |
| 239 process_thread_->DeRegisterModule(&rtp_stream_sync_); | 241 process_thread_->DeRegisterModule(&rtp_stream_sync_); |
| 242 LOG(LS_INFO) << "~VideoReceiveStream: DeRegisterModule video_receiver_."; |
| 240 process_thread_->DeRegisterModule(&video_receiver_); | 243 process_thread_->DeRegisterModule(&video_receiver_); |
| 244 |
| 245 LOG(LS_INFO) << "~VideoReceiveStream: Modules DeRegistered."; |
| 241 } | 246 } |
| 242 | 247 |
| 243 void VideoReceiveStream::SignalNetworkState(NetworkState state) { | 248 void VideoReceiveStream::SignalNetworkState(NetworkState state) { |
| 244 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 249 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 245 rtp_stream_receiver_.SignalNetworkState(state); | 250 rtp_stream_receiver_.SignalNetworkState(state); |
| 246 } | 251 } |
| 247 | 252 |
| 248 | 253 |
| 249 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { | 254 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 250 return rtp_stream_receiver_.DeliverRtcp(packet, length); | 255 return rtp_stream_receiver_.DeliverRtcp(packet, length); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 308 } |
| 304 | 309 |
| 305 video_stream_decoder_.reset(new VideoStreamDecoder( | 310 video_stream_decoder_.reset(new VideoStreamDecoder( |
| 306 &video_receiver_, &rtp_stream_receiver_, &rtp_stream_receiver_, | 311 &video_receiver_, &rtp_stream_receiver_, &rtp_stream_receiver_, |
| 307 rtp_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec, | 312 rtp_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec, |
| 308 &stats_proxy_, renderer)); | 313 &stats_proxy_, renderer)); |
| 309 // Register the channel to receive stats updates. | 314 // Register the channel to receive stats updates. |
| 310 call_stats_->RegisterStatsObserver(video_stream_decoder_.get()); | 315 call_stats_->RegisterStatsObserver(video_stream_decoder_.get()); |
| 311 // Start the decode thread | 316 // Start the decode thread |
| 312 decode_thread_.Start(); | 317 decode_thread_.Start(); |
| 313 #if !defined(WEBRTC_ANDROID) | |
| 314 // On android, the decoding happens on a different thread and frames | |
| 315 // are delivered on that thread (that in itself needs to be fixed). | |
| 316 // In any event, the actual decoding work is higher priority than the | |
| 317 // |decode_thread_| on Android, so we only raise the deocode priority on | |
| 318 // platforms other than Android. | |
| 319 decode_thread_.SetPriority(rtc::kHighestPriority); | |
| 320 #endif | |
| 321 rtp_stream_receiver_.StartReceive(); | 318 rtp_stream_receiver_.StartReceive(); |
| 322 } | 319 } |
| 323 | 320 |
| 324 void VideoReceiveStream::Stop() { | 321 void VideoReceiveStream::Stop() { |
| 325 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 322 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 323 LOG(LS_INFO) << "VideoReceiveStream::Stop: StopReceive"; |
| 326 rtp_stream_receiver_.StopReceive(); | 324 rtp_stream_receiver_.StopReceive(); |
| 327 // TriggerDecoderShutdown will release any waiting decoder thread and make it | 325 // TriggerDecoderShutdown will release any waiting decoder thread and make it |
| 328 // stop immediately, instead of waiting for a timeout. Needs to be called | 326 // stop immediately, instead of waiting for a timeout. Needs to be called |
| 329 // before joining the decoder thread thread. | 327 // before joining the decoder thread thread. |
| 328 LOG(LS_INFO) << "VideoReceiveStream::Stop: TriggerDecoderShutdown"; |
| 330 video_receiver_.TriggerDecoderShutdown(); | 329 video_receiver_.TriggerDecoderShutdown(); |
| 331 | 330 |
| 331 LOG(LS_INFO) << "VideoReceiveStream::Stop: frame_buffer_"; |
| 332 frame_buffer_->Stop(); | 332 frame_buffer_->Stop(); |
| 333 LOG(LS_INFO) << "VideoReceiveStream::Stop: call_stats_"; |
| 333 call_stats_->DeregisterStatsObserver(&rtp_stream_receiver_); | 334 call_stats_->DeregisterStatsObserver(&rtp_stream_receiver_); |
| 334 | 335 |
| 335 if (decode_thread_.IsRunning()) { | 336 if (decode_thread_.IsRunning()) { |
| 337 LOG(LS_INFO) << "VideoReceiveStream::Stop: decode_thread_"; |
| 336 decode_thread_.Stop(); | 338 decode_thread_.Stop(); |
| 337 // Deregister external decoders so they are no longer running during | 339 // Deregister external decoders so they are no longer running during |
| 338 // destruction. This effectively stops the VCM since the decoder thread is | 340 // destruction. This effectively stops the VCM since the decoder thread is |
| 339 // stopped, the VCM is deregistered and no asynchronous decoder threads are | 341 // stopped, the VCM is deregistered and no asynchronous decoder threads are |
| 340 // running. | 342 // running. |
| 341 for (const Decoder& decoder : config_.decoders) | 343 for (const Decoder& decoder : config_.decoders) |
| 342 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); | 344 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); |
| 343 } | 345 } |
| 344 | 346 |
| 347 LOG(LS_INFO) << "VideoReceiveStream::Stop: call_stats_"; |
| 345 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); | 348 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); |
| 349 LOG(LS_INFO) << "VideoReceiveStream::Stop: video_stream_decoder_"; |
| 346 video_stream_decoder_.reset(); | 350 video_stream_decoder_.reset(); |
| 351 LOG(LS_INFO) << "VideoReceiveStream::Stop: incoming_video_stream_"; |
| 347 incoming_video_stream_.reset(); | 352 incoming_video_stream_.reset(); |
| 353 LOG(LS_INFO) << "VideoReceiveStream::Stop: transport_adapter_"; |
| 348 transport_adapter_.Disable(); | 354 transport_adapter_.Disable(); |
| 355 LOG(LS_INFO) << "VideoReceiveStream::Stop: done"; |
| 349 } | 356 } |
| 350 | 357 |
| 351 VideoReceiveStream::Stats VideoReceiveStream::GetStats() const { | 358 VideoReceiveStream::Stats VideoReceiveStream::GetStats() const { |
| 352 return stats_proxy_.GetStats(); | 359 return stats_proxy_.GetStats(); |
| 353 } | 360 } |
| 354 | 361 |
| 355 void VideoReceiveStream::EnableEncodedFrameRecording(rtc::PlatformFile file, | 362 void VideoReceiveStream::EnableEncodedFrameRecording(rtc::PlatformFile file, |
| 356 size_t byte_limit) { | 363 size_t byte_limit) { |
| 357 { | 364 { |
| 358 rtc::CritScope lock(&ivf_writer_lock_); | 365 rtc::CritScope lock(&ivf_writer_lock_); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { | 477 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { |
| 471 RTC_NOTREACHED(); | 478 RTC_NOTREACHED(); |
| 472 return 0; | 479 return 0; |
| 473 } | 480 } |
| 474 | 481 |
| 475 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { | 482 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { |
| 476 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); | 483 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 477 video_receiver_.SetMinimumPlayoutDelay(delay_ms); | 484 video_receiver_.SetMinimumPlayoutDelay(delay_ms); |
| 478 } | 485 } |
| 479 | 486 |
| 480 bool VideoReceiveStream::DecodeThreadFunction(void* ptr) { | 487 void VideoReceiveStream::DecodeThreadFunction(void* ptr) { |
| 481 return static_cast<VideoReceiveStream*>(ptr)->Decode(); | 488 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) { |
| 489 } |
| 482 } | 490 } |
| 483 | 491 |
| 484 bool VideoReceiveStream::Decode() { | 492 bool VideoReceiveStream::Decode() { |
| 485 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); | 493 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); |
| 486 static const int kMaxWaitForFrameMs = 3000; | 494 static const int kMaxWaitForFrameMs = 3000; |
| 487 std::unique_ptr<video_coding::FrameObject> frame; | 495 std::unique_ptr<video_coding::FrameObject> frame; |
| 488 video_coding::FrameBuffer::ReturnReason res = | 496 |
| 489 frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); | 497 video_coding::FrameBuffer::ReturnReason res; |
| 498 #if defined(WEBRTC_ANDROID) |
| 499 static const int kPollIntervalMs = 10; |
| 500 int time_remaining = kMaxWaitForFrameMs; |
| 501 do { |
| 502 res = frame_buffer_->NextFrame(kPollIntervalMs, &frame); |
| 503 if (res != video_coding::FrameBuffer::ReturnReason::kTimeout) |
| 504 break; |
| 505 time_remaining -= kPollIntervalMs; |
| 506 video_receiver_.PollDecodedFrames(); |
| 507 } while (time_remaining > 0); |
| 508 #else |
| 509 res = frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); |
| 510 #endif |
| 490 | 511 |
| 491 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) | 512 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) |
| 492 return false; | 513 return false; |
| 493 | 514 |
| 494 if (frame) { | 515 if (frame) { |
| 495 if (video_receiver_.Decode(frame.get()) == VCM_OK) | 516 auto ret = video_receiver_.Decode(frame.get()); |
| 517 if (ret == VCM_OK) { |
| 496 rtp_stream_receiver_.FrameDecoded(frame->picture_id); | 518 rtp_stream_receiver_.FrameDecoded(frame->picture_id); |
| 519 } else { |
| 520 printf("ret = %i\n", ret); |
| 521 } |
| 497 } else { | 522 } else { |
| 523 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kTimeout); |
| 498 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs | 524 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs |
| 499 << " ms, requesting keyframe."; | 525 << " ms, requesting keyframe."; |
| 500 RequestKeyFrame(); | 526 RequestKeyFrame(); |
| 501 } | 527 } |
| 528 |
| 502 return true; | 529 return true; |
| 503 } | 530 } |
| 504 } // namespace internal | 531 } // namespace internal |
| 505 } // namespace webrtc | 532 } // namespace webrtc |
| OLD | NEW |