| 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_types.h" | 25 #include "webrtc/common_types.h" |
| 25 #include "webrtc/common_video/h264/profile_level_id.h" | 26 #include "webrtc/common_video/h264/profile_level_id.h" |
| 26 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 27 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 27 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | 28 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
| 28 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 29 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 29 #include "webrtc/modules/utility/include/process_thread.h" | 30 #include "webrtc/modules/utility/include/process_thread.h" |
| 30 #include "webrtc/modules/video_coding/frame_object.h" | 31 #include "webrtc/modules/video_coding/frame_object.h" |
| 31 #include "webrtc/modules/video_coding/include/video_coding.h" | 32 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 32 #include "webrtc/modules/video_coding/jitter_estimator.h" | 33 #include "webrtc/modules/video_coding/jitter_estimator.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 const int kDefaultStartBitrate = 300; | 162 const int kDefaultStartBitrate = 300; |
| 162 codec.startBitrate = codec.minBitrate = codec.maxBitrate = | 163 codec.startBitrate = codec.minBitrate = codec.maxBitrate = |
| 163 kDefaultStartBitrate; | 164 kDefaultStartBitrate; |
| 164 | 165 |
| 165 return codec; | 166 return codec; |
| 166 } | 167 } |
| 167 } // namespace | 168 } // namespace |
| 168 | 169 |
| 169 namespace internal { | 170 namespace internal { |
| 170 | 171 |
| 171 VideoReceiveStream::VideoReceiveStream( | 172 VideoReceiveStream::VideoReceiveStream(int num_cpu_cores, |
| 172 int num_cpu_cores, | 173 PacketRouter* packet_router, |
| 173 PacketRouter* packet_router, | 174 VideoReceiveStream::Config config, |
| 174 VideoReceiveStream::Config config, | 175 ProcessThread* process_thread, |
| 175 ProcessThread* process_thread, | 176 CallStats* call_stats, |
| 176 CallStats* call_stats, | 177 VieRemb* remb) |
| 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, this, "DecodingThread"), | 183 decode_thread_(&DecodeThreadFunction, |
| 184 this, |
| 185 "DecodingThread", |
| 186 rtc::kHighestPriority), |
| 184 call_stats_(call_stats), | 187 call_stats_(call_stats), |
| 185 timing_(new VCMTiming(clock_)), | 188 timing_(new VCMTiming(clock_)), |
| 186 video_receiver_(clock_, nullptr, this, timing_.get(), this, this), | 189 video_receiver_(clock_, nullptr, this, timing_.get(), this, this), |
| 187 stats_proxy_(&config_, clock_), | 190 stats_proxy_(&config_, clock_), |
| 188 rtp_stream_receiver_(&transport_adapter_, | 191 rtp_stream_receiver_(&transport_adapter_, |
| 189 call_stats_->rtcp_rtt_stats(), | 192 call_stats_->rtcp_rtt_stats(), |
| 190 packet_router, | 193 packet_router, |
| 191 remb, | 194 remb, |
| 192 &config_, | 195 &config_, |
| 193 &stats_proxy_, | 196 &stats_proxy_, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 214 << ") for different decoders."; | 217 << ") for different decoders."; |
| 215 decoder_payload_types.insert(decoder.payload_type); | 218 decoder_payload_types.insert(decoder.payload_type); |
| 216 } | 219 } |
| 217 | 220 |
| 218 video_receiver_.SetRenderDelay(config.render_delay_ms); | 221 video_receiver_.SetRenderDelay(config.render_delay_ms); |
| 219 | 222 |
| 220 jitter_estimator_.reset(new VCMJitterEstimator(clock_)); | 223 jitter_estimator_.reset(new VCMJitterEstimator(clock_)); |
| 221 frame_buffer_.reset(new video_coding::FrameBuffer( | 224 frame_buffer_.reset(new video_coding::FrameBuffer( |
| 222 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_)); | 225 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_)); |
| 223 | 226 |
| 224 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); | |
| 225 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); | 227 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); |
| 226 } | 228 } |
| 227 | 229 |
| 228 VideoReceiveStream::~VideoReceiveStream() { | 230 VideoReceiveStream::~VideoReceiveStream() { |
| 229 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 231 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 230 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); | 232 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); |
| 231 Stop(); | 233 Stop(); |
| 232 | 234 |
| 233 process_thread_->DeRegisterModule(&rtp_stream_sync_); | 235 process_thread_->DeRegisterModule(&rtp_stream_sync_); |
| 234 process_thread_->DeRegisterModule(&video_receiver_); | |
| 235 } | 236 } |
| 236 | 237 |
| 237 void VideoReceiveStream::SignalNetworkState(NetworkState state) { | 238 void VideoReceiveStream::SignalNetworkState(NetworkState state) { |
| 238 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 239 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 239 rtp_stream_receiver_.SignalNetworkState(state); | 240 rtp_stream_receiver_.SignalNetworkState(state); |
| 240 } | 241 } |
| 241 | 242 |
| 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 |
| 305 // Start the decode thread | 309 // Start the decode thread |
| 306 decode_thread_.Start(); | 310 decode_thread_.Start(); |
| 307 decode_thread_.SetPriority(rtc::kHighestPriority); | |
| 308 rtp_stream_receiver_.StartReceive(); | 311 rtp_stream_receiver_.StartReceive(); |
| 309 } | 312 } |
| 310 | 313 |
| 311 void VideoReceiveStream::Stop() { | 314 void VideoReceiveStream::Stop() { |
| 312 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 315 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 313 rtp_stream_receiver_.StopReceive(); | 316 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(); | |
| 318 | 317 |
| 319 frame_buffer_->Stop(); | 318 frame_buffer_->Stop(); |
| 320 call_stats_->DeregisterStatsObserver(&rtp_stream_receiver_); | 319 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 |
| 323 decode_thread_.Stop(); | 328 decode_thread_.Stop(); |
| 329 video_receiver_.DecoderThreadStopped(); |
| 324 // Deregister external decoders so they are no longer running during | 330 // Deregister external decoders so they are no longer running during |
| 325 // destruction. This effectively stops the VCM since the decoder thread is | 331 // destruction. This effectively stops the VCM since the decoder thread is |
| 326 // stopped, the VCM is deregistered and no asynchronous decoder threads are | 332 // stopped, the VCM is deregistered and no asynchronous decoder threads are |
| 327 // running. | 333 // running. |
| 328 for (const Decoder& decoder : config_.decoders) | 334 for (const Decoder& decoder : config_.decoders) |
| 329 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); | 335 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); |
| 330 } | 336 } |
| 331 | 337 |
| 332 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); | 338 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); |
| 333 video_stream_decoder_.reset(); | 339 video_stream_decoder_.reset(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { | 463 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { |
| 458 RTC_NOTREACHED(); | 464 RTC_NOTREACHED(); |
| 459 return 0; | 465 return 0; |
| 460 } | 466 } |
| 461 | 467 |
| 462 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { | 468 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { |
| 463 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); | 469 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 464 video_receiver_.SetMinimumPlayoutDelay(delay_ms); | 470 video_receiver_.SetMinimumPlayoutDelay(delay_ms); |
| 465 } | 471 } |
| 466 | 472 |
| 467 bool VideoReceiveStream::DecodeThreadFunction(void* ptr) { | 473 void VideoReceiveStream::DecodeThreadFunction(void* ptr) { |
| 468 return static_cast<VideoReceiveStream*>(ptr)->Decode(); | 474 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) { |
| 475 } |
| 469 } | 476 } |
| 470 | 477 |
| 471 bool VideoReceiveStream::Decode() { | 478 bool VideoReceiveStream::Decode() { |
| 472 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); | 479 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); |
| 473 static const int kMaxWaitForFrameMs = 3000; | 480 static const int kMaxWaitForFrameMs = 3000; |
| 474 std::unique_ptr<video_coding::FrameObject> frame; | 481 std::unique_ptr<video_coding::FrameObject> frame; |
| 475 video_coding::FrameBuffer::ReturnReason res = | |
| 476 frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); | |
| 477 | 482 |
| 478 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) { | 483 video_coding::FrameBuffer::ReturnReason res; |
| 479 video_receiver_.DecodingStopped(); | 484 #if defined(WEBRTC_ANDROID) |
| 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) |
| 480 return false; | 503 return false; |
| 481 } | |
| 482 | 504 |
| 483 if (frame) { | 505 if (frame) { |
| 484 if (video_receiver_.Decode(frame.get()) == VCM_OK) | 506 if (video_receiver_.Decode(frame.get()) == VCM_OK) |
| 485 rtp_stream_receiver_.FrameDecoded(frame->picture_id); | 507 rtp_stream_receiver_.FrameDecoded(frame->picture_id); |
| 486 } else { | 508 } else { |
| 509 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kTimeout); |
| 487 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs | 510 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs |
| 488 << " ms, requesting keyframe."; | 511 << " ms, requesting keyframe."; |
| 489 RequestKeyFrame(); | 512 RequestKeyFrame(); |
| 490 } | 513 } |
| 514 |
| 491 return true; | 515 return true; |
| 492 } | 516 } |
| 493 } // namespace internal | 517 } // namespace internal |
| 494 } // namespace webrtc | 518 } // namespace webrtc |
| OLD | NEW |