| 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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 const int kDefaultStartBitrate = 300; | 161 const int kDefaultStartBitrate = 300; |
| 162 codec.startBitrate = codec.minBitrate = codec.maxBitrate = | 162 codec.startBitrate = codec.minBitrate = codec.maxBitrate = |
| 163 kDefaultStartBitrate; | 163 kDefaultStartBitrate; |
| 164 | 164 |
| 165 return codec; | 165 return codec; |
| 166 } | 166 } |
| 167 } // namespace | 167 } // namespace |
| 168 | 168 |
| 169 namespace internal { | 169 namespace internal { |
| 170 | 170 |
| 171 VideoReceiveStream::VideoReceiveStream( | 171 VideoReceiveStream::VideoReceiveStream(int num_cpu_cores, |
| 172 int num_cpu_cores, | 172 PacketRouter* packet_router, |
| 173 PacketRouter* packet_router, | 173 VideoReceiveStream::Config config, |
| 174 VideoReceiveStream::Config config, | 174 ProcessThread* process_thread, |
| 175 ProcessThread* process_thread, | 175 CallStats* call_stats) |
| 176 CallStats* call_stats) | |
| 177 : transport_adapter_(config.rtcp_send_transport), | 176 : transport_adapter_(config.rtcp_send_transport), |
| 178 config_(std::move(config)), | 177 config_(std::move(config)), |
| 179 num_cpu_cores_(num_cpu_cores), | 178 num_cpu_cores_(num_cpu_cores), |
| 180 process_thread_(process_thread), | 179 process_thread_(process_thread), |
| 181 clock_(Clock::GetRealTimeClock()), | 180 clock_(Clock::GetRealTimeClock()), |
| 182 decode_thread_(DecodeThreadFunction, this, "DecodingThread"), | 181 decode_thread_(&DecodeThreadFunction, |
| 182 this, |
| 183 "DecodingThread", |
| 184 rtc::kHighestPriority), |
| 183 call_stats_(call_stats), | 185 call_stats_(call_stats), |
| 184 timing_(new VCMTiming(clock_)), | 186 timing_(new VCMTiming(clock_)), |
| 185 video_receiver_(clock_, nullptr, this, timing_.get(), this, this), | 187 video_receiver_(clock_, nullptr, this, timing_.get(), this, this), |
| 186 stats_proxy_(&config_, clock_), | 188 stats_proxy_(&config_, clock_), |
| 187 rtp_video_stream_receiver_(&transport_adapter_, | 189 rtp_video_stream_receiver_(&transport_adapter_, |
| 188 call_stats_->rtcp_rtt_stats(), | 190 call_stats_->rtcp_rtt_stats(), |
| 189 packet_router, | 191 packet_router, |
| 190 &config_, | 192 &config_, |
| 191 &stats_proxy_, | 193 &stats_proxy_, |
| 192 process_thread_, | 194 process_thread_, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 212 << ") for different decoders."; | 214 << ") for different decoders."; |
| 213 decoder_payload_types.insert(decoder.payload_type); | 215 decoder_payload_types.insert(decoder.payload_type); |
| 214 } | 216 } |
| 215 | 217 |
| 216 video_receiver_.SetRenderDelay(config.render_delay_ms); | 218 video_receiver_.SetRenderDelay(config.render_delay_ms); |
| 217 | 219 |
| 218 jitter_estimator_.reset(new VCMJitterEstimator(clock_)); | 220 jitter_estimator_.reset(new VCMJitterEstimator(clock_)); |
| 219 frame_buffer_.reset(new video_coding::FrameBuffer( | 221 frame_buffer_.reset(new video_coding::FrameBuffer( |
| 220 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_)); | 222 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_)); |
| 221 | 223 |
| 222 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); | |
| 223 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); | 224 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); |
| 224 } | 225 } |
| 225 | 226 |
| 226 VideoReceiveStream::~VideoReceiveStream() { | 227 VideoReceiveStream::~VideoReceiveStream() { |
| 227 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 228 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 228 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); | 229 LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); |
| 229 Stop(); | 230 Stop(); |
| 230 | 231 |
| 231 process_thread_->DeRegisterModule(&rtp_stream_sync_); | 232 process_thread_->DeRegisterModule(&rtp_stream_sync_); |
| 232 process_thread_->DeRegisterModule(&video_receiver_); | |
| 233 } | 233 } |
| 234 | 234 |
| 235 void VideoReceiveStream::SignalNetworkState(NetworkState state) { | 235 void VideoReceiveStream::SignalNetworkState(NetworkState state) { |
| 236 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 236 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 237 rtp_video_stream_receiver_.SignalNetworkState(state); | 237 rtp_video_stream_receiver_.SignalNetworkState(state); |
| 238 } | 238 } |
| 239 | 239 |
| 240 | |
| 241 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { | 240 bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 242 return rtp_video_stream_receiver_.DeliverRtcp(packet, length); | 241 return rtp_video_stream_receiver_.DeliverRtcp(packet, length); |
| 243 } | 242 } |
| 244 | 243 |
| 245 void VideoReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) { | 244 void VideoReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) { |
| 246 rtp_video_stream_receiver_.OnRtpPacket(packet); | 245 rtp_video_stream_receiver_.OnRtpPacket(packet); |
| 247 } | 246 } |
| 248 | 247 |
| 249 void VideoReceiveStream::SetSync(Syncable* audio_syncable) { | 248 void VideoReceiveStream::SetSync(Syncable* audio_syncable) { |
| 250 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 249 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 &codec, num_cpu_cores_, false)); | 289 &codec, num_cpu_cores_, false)); |
| 291 } | 290 } |
| 292 | 291 |
| 293 video_stream_decoder_.reset(new VideoStreamDecoder( | 292 video_stream_decoder_.reset(new VideoStreamDecoder( |
| 294 &video_receiver_, &rtp_video_stream_receiver_, | 293 &video_receiver_, &rtp_video_stream_receiver_, |
| 295 &rtp_video_stream_receiver_, | 294 &rtp_video_stream_receiver_, |
| 296 rtp_video_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec, | 295 rtp_video_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec, |
| 297 &stats_proxy_, renderer)); | 296 &stats_proxy_, renderer)); |
| 298 // Register the channel to receive stats updates. | 297 // Register the channel to receive stats updates. |
| 299 call_stats_->RegisterStatsObserver(video_stream_decoder_.get()); | 298 call_stats_->RegisterStatsObserver(video_stream_decoder_.get()); |
| 299 |
| 300 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE); |
| 301 |
| 300 // Start the decode thread | 302 // Start the decode thread |
| 301 decode_thread_.Start(); | 303 decode_thread_.Start(); |
| 302 decode_thread_.SetPriority(rtc::kHighestPriority); | |
| 303 rtp_video_stream_receiver_.StartReceive(); | 304 rtp_video_stream_receiver_.StartReceive(); |
| 304 } | 305 } |
| 305 | 306 |
| 306 void VideoReceiveStream::Stop() { | 307 void VideoReceiveStream::Stop() { |
| 307 RTC_DCHECK_RUN_ON(&worker_thread_checker_); | 308 RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 308 rtp_video_stream_receiver_.StopReceive(); | 309 rtp_video_stream_receiver_.StopReceive(); |
| 309 // TriggerDecoderShutdown will release any waiting decoder thread and make it | |
| 310 // stop immediately, instead of waiting for a timeout. Needs to be called | |
| 311 // before joining the decoder thread thread. | |
| 312 video_receiver_.TriggerDecoderShutdown(); | |
| 313 | 310 |
| 314 frame_buffer_->Stop(); | 311 frame_buffer_->Stop(); |
| 315 call_stats_->DeregisterStatsObserver(&rtp_video_stream_receiver_); | 312 call_stats_->DeregisterStatsObserver(&rtp_video_stream_receiver_); |
| 313 process_thread_->DeRegisterModule(&video_receiver_); |
| 316 | 314 |
| 317 if (decode_thread_.IsRunning()) { | 315 if (decode_thread_.IsRunning()) { |
| 316 // TriggerDecoderShutdown will release any waiting decoder thread and make |
| 317 // it stop immediately, instead of waiting for a timeout. Needs to be called |
| 318 // before joining the decoder thread. |
| 319 video_receiver_.TriggerDecoderShutdown(); |
| 320 |
| 318 decode_thread_.Stop(); | 321 decode_thread_.Stop(); |
| 319 // Deregister external decoders so they are no longer running during | 322 // Deregister external decoders so they are no longer running during |
| 320 // destruction. This effectively stops the VCM since the decoder thread is | 323 // destruction. This effectively stops the VCM since the decoder thread is |
| 321 // stopped, the VCM is deregistered and no asynchronous decoder threads are | 324 // stopped, the VCM is deregistered and no asynchronous decoder threads are |
| 322 // running. | 325 // running. |
| 323 for (const Decoder& decoder : config_.decoders) | 326 for (const Decoder& decoder : config_.decoders) |
| 324 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); | 327 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); |
| 325 } | 328 } |
| 326 | 329 |
| 327 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); | 330 call_stats_->DeregisterStatsObserver(video_stream_decoder_.get()); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { | 455 uint32_t VideoReceiveStream::GetPlayoutTimestamp() const { |
| 453 RTC_NOTREACHED(); | 456 RTC_NOTREACHED(); |
| 454 return 0; | 457 return 0; |
| 455 } | 458 } |
| 456 | 459 |
| 457 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { | 460 void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { |
| 458 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); | 461 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 459 video_receiver_.SetMinimumPlayoutDelay(delay_ms); | 462 video_receiver_.SetMinimumPlayoutDelay(delay_ms); |
| 460 } | 463 } |
| 461 | 464 |
| 462 bool VideoReceiveStream::DecodeThreadFunction(void* ptr) { | 465 void VideoReceiveStream::DecodeThreadFunction(void* ptr) { |
| 463 return static_cast<VideoReceiveStream*>(ptr)->Decode(); | 466 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) { |
| 467 } |
| 464 } | 468 } |
| 465 | 469 |
| 466 bool VideoReceiveStream::Decode() { | 470 bool VideoReceiveStream::Decode() { |
| 467 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); | 471 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode"); |
| 468 static const int kMaxWaitForFrameMs = 3000; | 472 static const int kMaxWaitForFrameMs = 3000; |
| 469 std::unique_ptr<video_coding::FrameObject> frame; | 473 std::unique_ptr<video_coding::FrameObject> frame; |
| 470 video_coding::FrameBuffer::ReturnReason res = | 474 video_coding::FrameBuffer::ReturnReason res = |
| 471 frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); | 475 frame_buffer_->NextFrame(kMaxWaitForFrameMs, &frame); |
| 472 | 476 |
| 473 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) { | 477 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) { |
| 474 video_receiver_.DecodingStopped(); | 478 video_receiver_.DecodingStopped(); |
| 475 return false; | 479 return false; |
| 476 } | 480 } |
| 477 | 481 |
| 478 if (frame) { | 482 if (frame) { |
| 483 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kFrameFound); |
| 479 if (video_receiver_.Decode(frame.get()) == VCM_OK) | 484 if (video_receiver_.Decode(frame.get()) == VCM_OK) |
| 480 rtp_video_stream_receiver_.FrameDecoded(frame->picture_id); | 485 rtp_video_stream_receiver_.FrameDecoded(frame->picture_id); |
| 481 } else { | 486 } else { |
| 487 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kTimeout); |
| 482 int64_t now_ms = clock_->TimeInMilliseconds(); | 488 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 483 rtc::Optional<int64_t> last_packet_ms = | 489 rtc::Optional<int64_t> last_packet_ms = |
| 484 rtp_video_stream_receiver_.LastReceivedPacketMs(); | 490 rtp_video_stream_receiver_.LastReceivedPacketMs(); |
| 485 rtc::Optional<int64_t> last_keyframe_packet_ms = | 491 rtc::Optional<int64_t> last_keyframe_packet_ms = |
| 486 rtp_video_stream_receiver_.LastReceivedKeyframePacketMs(); | 492 rtp_video_stream_receiver_.LastReceivedKeyframePacketMs(); |
| 487 | 493 |
| 488 // To avoid spamming keyframe requests for a stream that is not active we | 494 // To avoid spamming keyframe requests for a stream that is not active we |
| 489 // check if we have received a packet within the last 5 seconds. | 495 // check if we have received a packet within the last 5 seconds. |
| 490 bool stream_is_active = last_packet_ms && now_ms - *last_packet_ms < 5000; | 496 bool stream_is_active = last_packet_ms && now_ms - *last_packet_ms < 5000; |
| 491 | 497 |
| 492 // If we recently (within |kMaxWaitForFrameMs|) have been receiving packets | 498 // If we recently (within |kMaxWaitForFrameMs|) have been receiving packets |
| 493 // belonging to a keyframe then we assume a keyframe is being received right | 499 // belonging to a keyframe then we assume a keyframe is being received right |
| 494 // now. | 500 // now. |
| 495 bool receiving_keyframe = | 501 bool receiving_keyframe = |
| 496 last_keyframe_packet_ms && | 502 last_keyframe_packet_ms && |
| 497 now_ms - *last_keyframe_packet_ms < kMaxWaitForFrameMs; | 503 now_ms - *last_keyframe_packet_ms < kMaxWaitForFrameMs; |
| 498 | 504 |
| 499 if (stream_is_active && !receiving_keyframe) { | 505 if (stream_is_active && !receiving_keyframe) { |
| 500 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs | 506 LOG(LS_WARNING) << "No decodable frame in " << kMaxWaitForFrameMs |
| 501 << " ms, requesting keyframe."; | 507 << " ms, requesting keyframe."; |
| 502 RequestKeyFrame(); | 508 RequestKeyFrame(); |
| 503 } | 509 } |
| 504 } | 510 } |
| 505 return true; | 511 return true; |
| 506 } | 512 } |
| 507 } // namespace internal | 513 } // namespace internal |
| 508 } // namespace webrtc | 514 } // namespace webrtc |
| OLD | NEW |