| 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 #include "webrtc/video/video_send_stream.h" | 10 #include "webrtc/video/video_send_stream.h" |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <sstream> | 13 #include <sstream> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <utility> | 15 #include <utility> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "webrtc/common_types.h" |
| 18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/file.h" |
| 19 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/trace_event.h" | 22 #include "webrtc/base/trace_event.h" |
| 21 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 23 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 22 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 24 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
| 23 #include "webrtc/modules/pacing/packet_router.h" | 25 #include "webrtc/modules/pacing/packet_router.h" |
| 24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 26 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 25 #include "webrtc/modules/utility/include/process_thread.h" | 27 #include "webrtc/modules/utility/include/process_thread.h" |
| 26 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | 28 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" |
| 27 #include "webrtc/video/call_stats.h" | 29 #include "webrtc/video/call_stats.h" |
| 28 #include "webrtc/video/vie_remb.h" | 30 #include "webrtc/video/vie_remb.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 void DeRegisterProcessThread(); | 262 void DeRegisterProcessThread(); |
| 261 | 263 |
| 262 void SignalNetworkState(NetworkState state); | 264 void SignalNetworkState(NetworkState state); |
| 263 bool DeliverRtcp(const uint8_t* packet, size_t length); | 265 bool DeliverRtcp(const uint8_t* packet, size_t length); |
| 264 void Start(); | 266 void Start(); |
| 265 void Stop(); | 267 void Stop(); |
| 266 | 268 |
| 267 void SignalEncoderConfigurationChanged(const VideoEncoderConfig& config); | 269 void SignalEncoderConfigurationChanged(const VideoEncoderConfig& config); |
| 268 VideoSendStream::RtpStateMap GetRtpStates() const; | 270 VideoSendStream::RtpStateMap GetRtpStates() const; |
| 269 | 271 |
| 272 void EnableEncodedFrameRecording(const std::vector<rtc::PlatformFile>& files, |
| 273 size_t byte_limit); |
| 274 |
| 270 private: | 275 private: |
| 271 class CheckEncoderActivityTask; | 276 class CheckEncoderActivityTask; |
| 272 | 277 |
| 273 // Implements BitrateAllocatorObserver. | 278 // Implements BitrateAllocatorObserver. |
| 274 uint32_t OnBitrateUpdated(uint32_t bitrate_bps, | 279 uint32_t OnBitrateUpdated(uint32_t bitrate_bps, |
| 275 uint8_t fraction_loss, | 280 uint8_t fraction_loss, |
| 276 int64_t rtt) override; | 281 int64_t rtt) override; |
| 277 | 282 |
| 278 // Implements webrtc::VCMProtectionCallback. | 283 // Implements webrtc::VCMProtectionCallback. |
| 279 int ProtectionRequest(const FecProtectionParams* delta_params, | 284 int ProtectionRequest(const FecProtectionParams* delta_params, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 304 rtc::TaskQueue* const worker_queue_; | 309 rtc::TaskQueue* const worker_queue_; |
| 305 | 310 |
| 306 rtc::CriticalSection encoder_activity_crit_sect_; | 311 rtc::CriticalSection encoder_activity_crit_sect_; |
| 307 CheckEncoderActivityTask* check_encoder_activity_task_ | 312 CheckEncoderActivityTask* check_encoder_activity_task_ |
| 308 GUARDED_BY(encoder_activity_crit_sect_); | 313 GUARDED_BY(encoder_activity_crit_sect_); |
| 309 CallStats* const call_stats_; | 314 CallStats* const call_stats_; |
| 310 CongestionController* const congestion_controller_; | 315 CongestionController* const congestion_controller_; |
| 311 BitrateAllocator* const bitrate_allocator_; | 316 BitrateAllocator* const bitrate_allocator_; |
| 312 VieRemb* const remb_; | 317 VieRemb* const remb_; |
| 313 | 318 |
| 314 static const bool kEnableFrameRecording = false; | 319 rtc::CriticalSection ivf_writers_crit_; |
| 315 static const int kMaxLayers = 3; | 320 std::unique_ptr<IvfFileWriter> file_writers_[kMaxSimulcastStreams] GUARDED_BY( |
| 316 std::unique_ptr<IvfFileWriter> file_writers_[kMaxLayers]; | 321 ivf_writers_crit_); |
| 317 | 322 |
| 318 int max_padding_bitrate_; | 323 int max_padding_bitrate_; |
| 319 int encoder_min_bitrate_bps_; | 324 int encoder_min_bitrate_bps_; |
| 320 uint32_t encoder_max_bitrate_bps_; | 325 uint32_t encoder_max_bitrate_bps_; |
| 321 uint32_t encoder_target_rate_bps_; | 326 uint32_t encoder_target_rate_bps_; |
| 322 | 327 |
| 323 ViEEncoder* const vie_encoder_; | 328 ViEEncoder* const vie_encoder_; |
| 324 EncoderStateFeedback encoder_feedback_; | 329 EncoderStateFeedback encoder_feedback_; |
| 325 ProtectionBitrateCalculator protection_bitrate_calculator_; | 330 ProtectionBitrateCalculator protection_bitrate_calculator_; |
| 326 | 331 |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 &state_map, std::move(send_stream_), &thread_sync_event_))); | 596 &state_map, std::move(send_stream_), &thread_sync_event_))); |
| 592 thread_sync_event_.Wait(rtc::Event::kForever); | 597 thread_sync_event_.Wait(rtc::Event::kForever); |
| 593 return state_map; | 598 return state_map; |
| 594 } | 599 } |
| 595 | 600 |
| 596 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { | 601 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 597 // Called on a network thread. | 602 // Called on a network thread. |
| 598 return send_stream_->DeliverRtcp(packet, length); | 603 return send_stream_->DeliverRtcp(packet, length); |
| 599 } | 604 } |
| 600 | 605 |
| 606 void VideoSendStream::EnableEncodedFrameRecording( |
| 607 const std::vector<rtc::PlatformFile>& files, |
| 608 size_t byte_limit) { |
| 609 send_stream_->EnableEncodedFrameRecording(files, byte_limit); |
| 610 } |
| 611 |
| 601 VideoSendStreamImpl::VideoSendStreamImpl( | 612 VideoSendStreamImpl::VideoSendStreamImpl( |
| 602 SendStatisticsProxy* stats_proxy, | 613 SendStatisticsProxy* stats_proxy, |
| 603 rtc::TaskQueue* worker_queue, | 614 rtc::TaskQueue* worker_queue, |
| 604 CallStats* call_stats, | 615 CallStats* call_stats, |
| 605 CongestionController* congestion_controller, | 616 CongestionController* congestion_controller, |
| 606 BitrateAllocator* bitrate_allocator, | 617 BitrateAllocator* bitrate_allocator, |
| 607 SendDelayStats* send_delay_stats, | 618 SendDelayStats* send_delay_stats, |
| 608 VieRemb* remb, | 619 VieRemb* remb, |
| 609 ViEEncoder* vie_encoder, | 620 ViEEncoder* vie_encoder, |
| 610 RtcEventLog* event_log, | 621 RtcEventLog* event_log, |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 { | 868 { |
| 858 rtc::CritScope lock(&encoder_activity_crit_sect_); | 869 rtc::CritScope lock(&encoder_activity_crit_sect_); |
| 859 if (check_encoder_activity_task_) | 870 if (check_encoder_activity_task_) |
| 860 check_encoder_activity_task_->UpdateEncoderActivity(); | 871 check_encoder_activity_task_->UpdateEncoderActivity(); |
| 861 } | 872 } |
| 862 | 873 |
| 863 protection_bitrate_calculator_.UpdateWithEncodedData(encoded_image); | 874 protection_bitrate_calculator_.UpdateWithEncodedData(encoded_image); |
| 864 EncodedImageCallback::Result result = payload_router_.OnEncodedImage( | 875 EncodedImageCallback::Result result = payload_router_.OnEncodedImage( |
| 865 encoded_image, codec_specific_info, fragmentation); | 876 encoded_image, codec_specific_info, fragmentation); |
| 866 | 877 |
| 867 if (kEnableFrameRecording) { | 878 RTC_DCHECK(codec_specific_info); |
| 868 int layer = codec_specific_info->codecType == kVideoCodecVP8 | 879 |
| 869 ? codec_specific_info->codecSpecific.VP8.simulcastIdx | 880 int layer = codec_specific_info->codecType == kVideoCodecVP8 |
| 870 : 0; | 881 ? codec_specific_info->codecSpecific.VP8.simulcastIdx |
| 871 IvfFileWriter* file_writer; | 882 : 0; |
| 872 { | 883 { |
| 873 if (file_writers_[layer] == nullptr) { | 884 rtc::CritScope lock(&ivf_writers_crit_); |
| 874 std::ostringstream oss; | 885 if (file_writers_[layer].get()) { |
| 875 oss << "send_bitstream_ssrc"; | 886 bool ok = file_writers_[layer]->WriteFrame( |
| 876 for (uint32_t ssrc : config_->rtp.ssrcs) | 887 encoded_image, codec_specific_info->codecType); |
| 877 oss << "_" << ssrc; | |
| 878 oss << "_layer" << layer << ".ivf"; | |
| 879 file_writers_[layer] = | |
| 880 IvfFileWriter::Open(oss.str(), codec_specific_info->codecType); | |
| 881 } | |
| 882 file_writer = file_writers_[layer].get(); | |
| 883 } | |
| 884 if (file_writer) { | |
| 885 bool ok = file_writer->WriteFrame(encoded_image); | |
| 886 RTC_DCHECK(ok); | 888 RTC_DCHECK(ok); |
| 887 } | 889 } |
| 888 } | 890 } |
| 889 | 891 |
| 890 return result; | 892 return result; |
| 891 } | 893 } |
| 892 | 894 |
| 893 void VideoSendStreamImpl::ConfigureProtection() { | 895 void VideoSendStreamImpl::ConfigureProtection() { |
| 894 RTC_DCHECK_RUN_ON(worker_queue_); | 896 RTC_DCHECK_RUN_ON(worker_queue_); |
| 895 // Enable NACK, FEC or both. | 897 // Enable NACK, FEC or both. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1030 bitrate_bps, stats_proxy_->GetSendFrameRate(), fraction_loss, rtt); | 1032 bitrate_bps, stats_proxy_->GetSendFrameRate(), fraction_loss, rtt); |
| 1031 uint32_t protection_bitrate = bitrate_bps - encoder_target_rate_bps_; | 1033 uint32_t protection_bitrate = bitrate_bps - encoder_target_rate_bps_; |
| 1032 | 1034 |
| 1033 encoder_target_rate_bps_ = | 1035 encoder_target_rate_bps_ = |
| 1034 std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps_); | 1036 std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps_); |
| 1035 vie_encoder_->OnBitrateUpdated(encoder_target_rate_bps_, fraction_loss, rtt); | 1037 vie_encoder_->OnBitrateUpdated(encoder_target_rate_bps_, fraction_loss, rtt); |
| 1036 stats_proxy_->OnSetEncoderTargetRate(encoder_target_rate_bps_); | 1038 stats_proxy_->OnSetEncoderTargetRate(encoder_target_rate_bps_); |
| 1037 return protection_bitrate; | 1039 return protection_bitrate; |
| 1038 } | 1040 } |
| 1039 | 1041 |
| 1042 void VideoSendStreamImpl::EnableEncodedFrameRecording( |
| 1043 const std::vector<rtc::PlatformFile>& files, |
| 1044 size_t byte_limit) { |
| 1045 { |
| 1046 rtc::CritScope lock(&ivf_writers_crit_); |
| 1047 for (unsigned int i = 0; i < kMaxSimulcastStreams; ++i) { |
| 1048 if (i < files.size()) { |
| 1049 file_writers_[i] = IvfFileWriter::Wrap(rtc::File(files[i]), byte_limit); |
| 1050 } else { |
| 1051 file_writers_[i].reset(); |
| 1052 } |
| 1053 } |
| 1054 } |
| 1055 |
| 1056 if (!files.empty()) { |
| 1057 // Make a keyframe appear as early as possible in the logs, to give actually |
| 1058 // decodable output. |
| 1059 vie_encoder_->SendKeyFrame(); |
| 1060 } |
| 1061 } |
| 1062 |
| 1040 int VideoSendStreamImpl::ProtectionRequest( | 1063 int VideoSendStreamImpl::ProtectionRequest( |
| 1041 const FecProtectionParams* delta_params, | 1064 const FecProtectionParams* delta_params, |
| 1042 const FecProtectionParams* key_params, | 1065 const FecProtectionParams* key_params, |
| 1043 uint32_t* sent_video_rate_bps, | 1066 uint32_t* sent_video_rate_bps, |
| 1044 uint32_t* sent_nack_rate_bps, | 1067 uint32_t* sent_nack_rate_bps, |
| 1045 uint32_t* sent_fec_rate_bps) { | 1068 uint32_t* sent_fec_rate_bps) { |
| 1046 RTC_DCHECK_RUN_ON(worker_queue_); | 1069 RTC_DCHECK_RUN_ON(worker_queue_); |
| 1047 *sent_video_rate_bps = 0; | 1070 *sent_video_rate_bps = 0; |
| 1048 *sent_nack_rate_bps = 0; | 1071 *sent_nack_rate_bps = 0; |
| 1049 *sent_fec_rate_bps = 0; | 1072 *sent_fec_rate_bps = 0; |
| 1050 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 1073 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 1051 uint32_t not_used = 0; | 1074 uint32_t not_used = 0; |
| 1052 uint32_t module_video_rate = 0; | 1075 uint32_t module_video_rate = 0; |
| 1053 uint32_t module_fec_rate = 0; | 1076 uint32_t module_fec_rate = 0; |
| 1054 uint32_t module_nack_rate = 0; | 1077 uint32_t module_nack_rate = 0; |
| 1055 rtp_rtcp->SetFecParameters(delta_params, key_params); | 1078 rtp_rtcp->SetFecParameters(delta_params, key_params); |
| 1056 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, | 1079 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, |
| 1057 &module_nack_rate); | 1080 &module_nack_rate); |
| 1058 *sent_video_rate_bps += module_video_rate; | 1081 *sent_video_rate_bps += module_video_rate; |
| 1059 *sent_nack_rate_bps += module_nack_rate; | 1082 *sent_nack_rate_bps += module_nack_rate; |
| 1060 *sent_fec_rate_bps += module_fec_rate; | 1083 *sent_fec_rate_bps += module_fec_rate; |
| 1061 } | 1084 } |
| 1062 return 0; | 1085 return 0; |
| 1063 } | 1086 } |
| 1064 | 1087 |
| 1065 } // namespace internal | 1088 } // namespace internal |
| 1066 } // namespace webrtc | 1089 } // namespace webrtc |
| OLD | NEW |