| 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" | |
| 24 #include "webrtc/base/trace_event.h" | 23 #include "webrtc/base/trace_event.h" |
| 25 #include "webrtc/common_types.h" | 24 #include "webrtc/common_types.h" |
| 26 #include "webrtc/common_video/h264/profile_level_id.h" | 25 #include "webrtc/common_video/h264/profile_level_id.h" |
| 27 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 26 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 28 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | 27 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
| 29 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 28 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 30 #include "webrtc/modules/utility/include/process_thread.h" | 29 #include "webrtc/modules/utility/include/process_thread.h" |
| 31 #include "webrtc/modules/video_coding/frame_object.h" | 30 #include "webrtc/modules/video_coding/frame_object.h" |
| 32 #include "webrtc/modules/video_coding/include/video_coding.h" | 31 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 33 #include "webrtc/modules/video_coding/jitter_estimator.h" | 32 #include "webrtc/modules/video_coding/jitter_estimator.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 const int kDefaultStartBitrate = 300; | 161 const int kDefaultStartBitrate = 300; |
| 163 codec.startBitrate = codec.minBitrate = codec.maxBitrate = | 162 codec.startBitrate = codec.minBitrate = codec.maxBitrate = |
| 164 kDefaultStartBitrate; | 163 kDefaultStartBitrate; |
| 165 | 164 |
| 166 return codec; | 165 return codec; |
| 167 } | 166 } |
| 168 } // namespace | 167 } // namespace |
| 169 | 168 |
| 170 namespace internal { | 169 namespace internal { |
| 171 | 170 |
| 172 VideoReceiveStream::VideoReceiveStream(int num_cpu_cores, | 171 VideoReceiveStream::VideoReceiveStream( |
| 173 PacketRouter* packet_router, | 172 int num_cpu_cores, |
| 174 VideoReceiveStream::Config config, | 173 PacketRouter* packet_router, |
| 175 ProcessThread* process_thread, | 174 VideoReceiveStream::Config config, |
| 176 CallStats* call_stats, | 175 ProcessThread* process_thread, |
| 177 VieRemb* remb) | 176 CallStats* call_stats, |
| 177 VieRemb* remb) |
| 178 : transport_adapter_(config.rtcp_send_transport), | 178 : transport_adapter_(config.rtcp_send_transport), |
| 179 config_(std::move(config)), | 179 config_(std::move(config)), |
| 180 num_cpu_cores_(num_cpu_cores), | 180 num_cpu_cores_(num_cpu_cores), |
| 181 process_thread_(process_thread), | 181 process_thread_(process_thread), |
| 182 clock_(Clock::GetRealTimeClock()), | 182 clock_(Clock::GetRealTimeClock()), |
| 183 decode_thread_(&DecodeThreadFunction, | 183 decode_thread_(DecodeThreadFunction, this, "DecodingThread"), |
| 184 this, | |
| 185 "DecodingThread", | |
| 186 rtc::kHighestPriority), | |
| 187 call_stats_(call_stats), | 184 call_stats_(call_stats), |
| 188 timing_(new VCMTiming(clock_)), | 185 timing_(new VCMTiming(clock_)), |
| 189 video_receiver_(clock_, nullptr, this, timing_.get(), this, this), | 186 video_receiver_(clock_, nullptr, this, timing_.get(), this, this), |
| 190 stats_proxy_(&config_, clock_), | 187 stats_proxy_(&config_, clock_), |
| 191 rtp_stream_receiver_(&transport_adapter_, | 188 rtp_stream_receiver_(&transport_adapter_, |
| 192 call_stats_->rtcp_rtt_stats(), | 189 call_stats_->rtcp_rtt_stats(), |
| 193 packet_router, | 190 packet_router, |
| 194 remb, | 191 remb, |
| 195 &config_, | 192 &config_, |
| 196 &stats_proxy_, | 193 &stats_proxy_, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 217 << ") for different decoders."; | 214 << ") for different decoders."; |
| 218 decoder_payload_types.insert(decoder.payload_type); | 215 decoder_payload_types.insert(decoder.payload_type); |
| 219 } | 216 } |
| 220 | 217 |
| 221 video_receiver_.SetRenderDelay(config.render_delay_ms); | 218 video_receiver_.SetRenderDelay(config.render_delay_ms); |
| 222 | 219 |
| 223 jitter_estimator_.reset(new VCMJitterEstimator(clock_)); | 220 jitter_estimator_.reset(new VCMJitterEstimator(clock_)); |
| 224 frame_buffer_.reset(new video_coding::FrameBuffer( | 221 frame_buffer_.reset(new video_coding::FrameBuffer( |
| 225 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_)); | 222 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_)); |
| 226 | 223 |
| 224 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); |
| 227 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); | 225 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); |
| 228 } | 226 } |
| 229 | 227 |
| 230 VideoReceiveStream::~VideoReceiveStream() { | 228 VideoReceiveStream::~VideoReceiveStream() { |
| 231 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 229 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 232 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); | 230 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); |
| 233 Stop(); | 231 Stop(); |
| 234 | 232 |
| 235 process_thread_->DeRegisterModule(&rtp_stream_sync_); | 233 process_thread_->DeRegisterModule(&rtp_stream_sync_); |
| 234 process_thread_->DeRegisterModule(&video_receiver_); |
| 236 } | 235 } |
| 237 | 236 |
| 238 void VideoReceiveStream::SignalNetworkState(NetworkState state) { | 237 void VideoReceiveStream::SignalNetworkState(NetworkState state) { |
| 239 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 238 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 240 rtp_stream_receiver_.SignalNetworkState(state); | 239 rtp_stream_receiver_.SignalNetworkState(state); |
| 241 } | 240 } |
| 242 | 241 |
| 242 |
| 243 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { | 243 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 244 return rtp_stream_receiver_.DeliverRtcp(packet, length); | 244 return rtp_stream_receiver_.DeliverRtcp(packet, length); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void VideoReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) { | 247 void VideoReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) { |
| 248 rtp_stream_receiver_.OnRtpPacket(packet); | 248 rtp_stream_receiver_.OnRtpPacket(packet); |
| 249 } | 249 } |
| 250 | 250 |
| 251 bool VideoReceiveStream::OnRecoveredPacket(const uint8_t* packet, | 251 bool VideoReceiveStream::OnRecoveredPacket(const uint8_t* packet, |
| 252 size_t length) { | 252 size_t length) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec( | 295 RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec( |
| 296 &codec, num_cpu_cores_, false)); | 296 &codec, num_cpu_cores_, false)); |
| 297 } | 297 } |
| 298 | 298 |
| 299 video_stream_decoder_.reset(new VideoStreamDecoder( | 299 video_stream_decoder_.reset(new VideoStreamDecoder( |
| 300 &video_receiver_, &rtp_stream_receiver_, &rtp_stream_receiver_, | 300 &video_receiver_, &rtp_stream_receiver_, &rtp_stream_receiver_, |
| 301 rtp_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec, | 301 rtp_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec, |
| 302 &stats_proxy_, renderer)); | 302 &stats_proxy_, renderer)); |
| 303 // Register the channel to receive stats updates. | 303 // Register the channel to receive stats updates. |
| 304 call_stats_->RegisterStatsObserver(video_stream_decoder_.get()); | 304 call_stats_->RegisterStatsObserver(video_stream_decoder_.get()); |
| 305 | |
| 306 video_receiver_.DecoderThreadStarting(); | |
| 307 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); | |
| 308 | |
| 309 // Start the decode thread | 305 // Start the decode thread |
| 310 decode_thread_.Start(); | 306 decode_thread_.Start(); |
| 307 decode_thread_.SetPriority(rtc::kHighestPriority); |
| 311 rtp_stream_receiver_.StartReceive(); | 308 rtp_stream_receiver_.StartReceive(); |
| 312 } | 309 } |
| 313 | 310 |
| 314 void VideoReceiveStream::Stop() { | 311 void VideoReceiveStream::Stop() { |
| 315 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 312 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 316 rtp_stream_receiver_.StopReceive(); | 313 rtp_stream_receiver_.StopReceive(); |
| 314 // TriggerDecoderShutdown will release any waiting decoder thread and make it |
| 315 // stop immediately, instead of waiting for a timeout. Needs to be called |
| 316 // before joining the decoder thread thread. |
| 317 video_receiver_.TriggerDecoderShutdown(); |
| 317 | 318 |
| 318 frame_buffer_->Stop(); | 319 frame_buffer_->Stop(); |
| 319 call_stats_->DeregisterStatsObserver(&rtp_stream_receiver_); | 320 call_stats_->DeregisterStatsObserver(&rtp_stream_receiver_); |
| 320 process_thread_->DeRegisterModule(&video_receiver_); | |
| 321 | 321 |
| 322 if (decode_thread_.IsRunning()) { | 322 if (decode_thread_.IsRunning()) { |
| 323 // TriggerDecoderShutdown will release any waiting decoder thread and make | |
| 324 // it stop immediately, instead of waiting for a timeout. Needs to be called | |
| 325 // before joining the decoder thread thread. | |
| 326 video_receiver_.TriggerDecoderShutdown(); | |
| 327 | |
| 328 decode_thread_.Stop(); | 323 decode_thread_.Stop(); |
| 329 video_receiver_.DecoderThreadStopped(); | |
| 330 // Deregister external decoders so they are no longer running during | 324 // Deregister external decoders so they are no longer running during |
| 331 // destruction. This effectively stops the VCM since the decoder thread is | 325 // destruction. This effectively stops the VCM since the decoder thread is |
| 332 // stopped, the VCM is deregistered and no asynchronous decoder threads are | 326 // stopped, the VCM is deregistered and no asynchronous decoder threads are |
| 333 // running. | 327 // running. |
| 334 for (const Decoder& decoder : config_.decoders) | 328 for (const Decoder& decoder : config_.decoders) |
| 335 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); | 329 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); |
| 336 } | 330 } |
| 337 | 331 |
| 338 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); | 332 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); |
| 339 video_stream_decoder_.reset(); | 333 video_stream_decoder_.reset(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { | 457 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { |
| 464 RTC_NOTREACHED(); | 458 RTC_NOTREACHED(); |
| 465 return 0; | 459 return 0; |
| 466 } | 460 } |
| 467 | 461 |
| 468 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { | 462 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { |
| 469 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); | 463 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 470 video_receiver_.SetMinimumPlayoutDelay(delay_ms); | 464 video_receiver_.SetMinimumPlayoutDelay(delay_ms); |
| 471 } | 465 } |
| 472 | 466 |
| 473 void VideoReceiveStream::DecodeThreadFunction(void* ptr) { | 467 bool VideoReceiveStream::DecodeThreadFunction(void* ptr) { |
| 474 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) { | 468 return static_cast<VideoReceiveStream*>(ptr)->Decode(); |
| 475 } | |
| 476 } | 469 } |
| 477 | 470 |
| 478 bool VideoReceiveStream::Decode() { | 471 bool VideoReceiveStream::Decode() { |
| 479 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); | 472 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); |
| 480 static const int kMaxWaitForFrameMs = 3000; | 473 static const int kMaxWaitForFrameMs = 3000; |
| 481 std::unique_ptr<video_coding::FrameObject> frame; | 474 std::unique_ptr<video_coding::FrameObject> frame; |
| 475 video_coding::FrameBuffer::ReturnReason res = |
| 476 frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); |
| 482 | 477 |
| 483 video_coding::FrameBuffer::ReturnReason res; | 478 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) { |
| 484 #if defined(WEBRTC_ANDROID) | 479 video_receiver_.DecodingStopped(); |
| 485 // This is a temporary workaround for video capture on Android in order to | |
| 486 // deliver asynchronously delivered frames, on the decoder thread. | |
| 487 // More details here: | |
| 488 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7361 | |
| 489 static const int kPollIntervalMs = 10; | |
| 490 int time_remaining = kMaxWaitForFrameMs; | |
| 491 do { | |
| 492 res = frame_buffer_->NextFrame(kPollIntervalMs, &frame); | |
| 493 if (res != video_coding::FrameBuffer::ReturnReason::kTimeout) | |
| 494 break; | |
| 495 time_remaining -= kPollIntervalMs; | |
| 496 video_receiver_.PollDecodedFrames(); | |
| 497 } while (time_remaining > 0); | |
| 498 #else | |
| 499 res = frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); | |
| 500 #endif | |
| 501 | |
| 502 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) | |
| 503 return false; | 480 return false; |
| 481 } |
| 504 | 482 |
| 505 if (frame) { | 483 if (frame) { |
| 506 if (video_receiver_.Decode(frame.get()) == VCM_OK) | 484 if (video_receiver_.Decode(frame.get()) == VCM_OK) |
| 507 rtp_stream_receiver_.FrameDecoded(frame->picture_id); | 485 rtp_stream_receiver_.FrameDecoded(frame->picture_id); |
| 508 } else { | 486 } else { |
| 509 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kTimeout); | |
| 510 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs | 487 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs |
| 511 << " ms, requesting keyframe."; | 488 << " ms, requesting keyframe."; |
| 512 RequestKeyFrame(); | 489 RequestKeyFrame(); |
| 513 } | 490 } |
| 514 | |
| 515 return true; | 491 return true; |
| 516 } | 492 } |
| 517 } // namespace internal | 493 } // namespace internal |
| 518 } // namespace webrtc | 494 } // namespace webrtc |
| OLD | NEW |