ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 10 | #include "webrtc/video/video_quality_test.h" |
perkj | 9fdbda6 | 2016-09-15 16:19:20 | [diff] [blame] | 11 | |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 12 | #include <stdio.h> |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <deque> |
| 15 | #include <map> |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 16 | #include <sstream> |
mflodman | d1590b2 | 2015-12-09 15:07:59 | [diff] [blame] | 17 | #include <string> |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 20 | #include "webrtc/base/checks.h" |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 21 | #include "webrtc/base/event.h" |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 22 | #include "webrtc/base/format_macros.h" |
nisse | c7fe3c2 | 2016-04-20 10:25:36 | [diff] [blame] | 23 | #include "webrtc/base/optional.h" |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 24 | #include "webrtc/base/platform_file.h" |
sprang | e1f2f1f | 2016-02-01 10:04:52 | [diff] [blame] | 25 | #include "webrtc/base/timeutils.h" |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 26 | #include "webrtc/call.h" |
| 27 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 28 | #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
Henrik Kjellander | ff761fb | 2015-11-04 07:31:52 | [diff] [blame] | 29 | #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 30 | #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 17:17:40 | [diff] [blame] | 31 | #include "webrtc/system_wrappers/include/cpu_info.h" |
kwiberg | ac9f876 | 2016-10-01 05:29:43 | [diff] [blame] | 32 | #include "webrtc/test/gtest.h" |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 33 | #include "webrtc/test/layer_filtering_transport.h" |
| 34 | #include "webrtc/test/run_loop.h" |
| 35 | #include "webrtc/test/statistics.h" |
| 36 | #include "webrtc/test/testsupport/fileutils.h" |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 37 | #include "webrtc/test/vcm_capturer.h" |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 38 | #include "webrtc/test/video_renderer.h" |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 39 | #include "webrtc/voice_engine/include/voe_base.h" |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
| 43 | constexpr int kSendStatsPollingIntervalMs = 1000; |
| 44 | constexpr int kPayloadTypeH264 = 122; |
| 45 | constexpr int kPayloadTypeVP8 = 123; |
| 46 | constexpr int kPayloadTypeVP9 = 124; |
| 47 | constexpr size_t kMaxComparisons = 10; |
| 48 | constexpr char kSyncGroup[] = "av_sync"; |
minyue | 10cbb46 | 2016-11-07 17:29:22 | [diff] [blame] | 49 | constexpr int kOpusMinBitrateBps = 6000; |
| 50 | constexpr int kOpusBitrateFbBps = 32000; |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 51 | |
| 52 | struct VoiceEngineState { |
| 53 | VoiceEngineState() |
| 54 | : voice_engine(nullptr), |
| 55 | base(nullptr), |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 56 | send_channel_id(-1), |
| 57 | receive_channel_id(-1) {} |
| 58 | |
| 59 | webrtc::VoiceEngine* voice_engine; |
| 60 | webrtc::VoEBase* base; |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 61 | int send_channel_id; |
| 62 | int receive_channel_id; |
| 63 | }; |
| 64 | |
| 65 | void CreateVoiceEngine(VoiceEngineState* voe, |
| 66 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> |
| 67 | decoder_factory) { |
| 68 | voe->voice_engine = webrtc::VoiceEngine::Create(); |
| 69 | voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine); |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 70 | EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory)); |
solenberg | 88499ec | 2016-09-07 14:34:41 | [diff] [blame] | 71 | webrtc::VoEBase::ChannelConfig config; |
| 72 | config.enable_voice_pacing = true; |
| 73 | voe->send_channel_id = voe->base->CreateChannel(config); |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 74 | EXPECT_GE(voe->send_channel_id, 0); |
| 75 | voe->receive_channel_id = voe->base->CreateChannel(); |
| 76 | EXPECT_GE(voe->receive_channel_id, 0); |
| 77 | } |
| 78 | |
| 79 | void DestroyVoiceEngine(VoiceEngineState* voe) { |
| 80 | voe->base->DeleteChannel(voe->send_channel_id); |
| 81 | voe->send_channel_id = -1; |
| 82 | voe->base->DeleteChannel(voe->receive_channel_id); |
| 83 | voe->receive_channel_id = -1; |
| 84 | voe->base->Release(); |
| 85 | voe->base = nullptr; |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 86 | |
| 87 | webrtc::VoiceEngine::Delete(voe->voice_engine); |
| 88 | voe->voice_engine = nullptr; |
| 89 | } |
| 90 | |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 91 | class VideoStreamFactory |
| 92 | : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface { |
| 93 | public: |
| 94 | explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams) |
| 95 | : streams_(streams) {} |
| 96 | |
| 97 | private: |
| 98 | std::vector<webrtc::VideoStream> CreateEncoderStreams( |
| 99 | int width, |
| 100 | int height, |
| 101 | const webrtc::VideoEncoderConfig& encoder_config) override { |
| 102 | return streams_; |
| 103 | } |
| 104 | |
| 105 | std::vector<webrtc::VideoStream> streams_; |
| 106 | }; |
| 107 | |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 108 | } // namespace |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 109 | |
| 110 | namespace webrtc { |
| 111 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 112 | class VideoAnalyzer : public PacketReceiver, |
pbos | 2d56668 | 2015-09-28 16:59:31 | [diff] [blame] | 113 | public Transport, |
nisse | 7ade7b3 | 2016-03-23 11:48:10 | [diff] [blame] | 114 | public rtc::VideoSinkInterface<VideoFrame>, |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 115 | public EncodedFrameObserver { |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 116 | public: |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 117 | VideoAnalyzer(test::LayerFilteringTransport* transport, |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 118 | const std::string& test_label, |
| 119 | double avg_psnr_threshold, |
| 120 | double avg_ssim_threshold, |
| 121 | int duration_frames, |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 122 | FILE* graph_data_output_file, |
| 123 | const std::string& graph_title, |
| 124 | uint32_t ssrc_to_analyze) |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 125 | : transport_(transport), |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 126 | receiver_(nullptr), |
| 127 | send_stream_(nullptr), |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 128 | captured_frame_forwarder_(this), |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 129 | test_label_(test_label), |
| 130 | graph_data_output_file_(graph_data_output_file), |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 131 | graph_title_(graph_title), |
| 132 | ssrc_to_analyze_(ssrc_to_analyze), |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 133 | pre_encode_proxy_(this), |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 134 | encode_timing_proxy_(this), |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 135 | frames_to_process_(duration_frames), |
| 136 | frames_recorded_(0), |
| 137 | frames_processed_(0), |
| 138 | dropped_frames_(0), |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 139 | dropped_frames_before_first_encode_(0), |
| 140 | dropped_frames_before_rendering_(0), |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 141 | last_render_time_(0), |
| 142 | rtp_timestamp_delta_(0), |
| 143 | avg_psnr_threshold_(avg_psnr_threshold), |
| 144 | avg_ssim_threshold_(avg_ssim_threshold), |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 145 | stats_polling_thread_(&PollStatsThread, this, "StatsPoller"), |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 146 | comparison_available_event_(false, false), |
Peter Boström | dd45eb6 | 2016-01-19 14:22:32 | [diff] [blame] | 147 | done_(true, false) { |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 148 | // Create thread pool for CPU-expensive PSNR/SSIM calculations. |
| 149 | |
| 150 | // Try to use about as many threads as cores, but leave kMinCoresLeft alone, |
| 151 | // so that we don't accidentally starve "real" worker threads (codec etc). |
| 152 | // Also, don't allocate more than kMaxComparisonThreads, even if there are |
| 153 | // spare cores. |
| 154 | |
| 155 | uint32_t num_cores = CpuInfo::DetectNumberOfCores(); |
| 156 | RTC_DCHECK_GE(num_cores, 1u); |
| 157 | static const uint32_t kMinCoresLeft = 4; |
| 158 | static const uint32_t kMaxComparisonThreads = 8; |
| 159 | |
| 160 | if (num_cores <= kMinCoresLeft) { |
| 161 | num_cores = 1; |
| 162 | } else { |
| 163 | num_cores -= kMinCoresLeft; |
| 164 | num_cores = std::min(num_cores, kMaxComparisonThreads); |
| 165 | } |
| 166 | |
| 167 | for (uint32_t i = 0; i < num_cores; ++i) { |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 168 | rtc::PlatformThread* thread = |
| 169 | new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer"); |
| 170 | thread->Start(); |
| 171 | comparison_thread_pool_.push_back(thread); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 172 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | ~VideoAnalyzer() { |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 176 | for (rtc::PlatformThread* thread : comparison_thread_pool_) { |
| 177 | thread->Stop(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 178 | delete thread; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; } |
| 183 | |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 184 | void SetSendStream(VideoSendStream* stream) { |
| 185 | rtc::CritScope lock(&crit_); |
| 186 | RTC_DCHECK(!send_stream_); |
| 187 | send_stream_ = stream; |
| 188 | } |
| 189 | |
| 190 | rtc::VideoSinkInterface<VideoFrame>* InputInterface() { |
| 191 | return &captured_frame_forwarder_; |
| 192 | } |
| 193 | rtc::VideoSourceInterface<VideoFrame>* OutputInterface() { |
| 194 | return &captured_frame_forwarder_; |
| 195 | } |
| 196 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 197 | DeliveryStatus DeliverPacket(MediaType media_type, |
| 198 | const uint8_t* packet, |
| 199 | size_t length, |
| 200 | const PacketTime& packet_time) override { |
Taylor Brandstetter | 433b95a | 2016-03-18 18:41:03 | [diff] [blame] | 201 | // Ignore timestamps of RTCP packets. They're not synchronized with |
| 202 | // RTP packet timestamps and so they would confuse wrap_handler_. |
| 203 | if (RtpHeaderParser::IsRtcp(packet, length)) { |
| 204 | return receiver_->DeliverPacket(media_type, packet, length, packet_time); |
| 205 | } |
| 206 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 207 | RtpUtility::RtpHeaderParser parser(packet, length); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 208 | RTPHeader header; |
danilchap | f6975f4 | 2015-12-28 18:18:46 | [diff] [blame] | 209 | parser.Parse(&header); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 210 | { |
| 211 | rtc::CritScope lock(&crit_); |
sprang | 16daaa5 | 2016-03-09 09:30:24 | [diff] [blame] | 212 | int64_t timestamp = |
| 213 | wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_); |
| 214 | recv_times_[timestamp] = |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 215 | Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
| 216 | } |
| 217 | |
| 218 | return receiver_->DeliverPacket(media_type, packet, length, packet_time); |
| 219 | } |
| 220 | |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 221 | void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) { |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 222 | rtc::CritScope crit(&comparison_lock_); |
| 223 | samples_encode_time_ms_[ntp_time_ms] = encode_time_ms; |
| 224 | } |
| 225 | |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 226 | void PreEncodeOnFrame(const VideoFrame& video_frame) { |
| 227 | rtc::CritScope lock(&crit_); |
| 228 | if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0) { |
| 229 | while (frames_.front().timestamp() != video_frame.timestamp()) { |
| 230 | ++dropped_frames_before_first_encode_; |
| 231 | frames_.pop_front(); |
| 232 | RTC_CHECK(!frames_.empty()); |
| 233 | } |
| 234 | first_send_timestamp_ = rtc::Optional<uint32_t>(video_frame.timestamp()); |
| 235 | } |
| 236 | } |
| 237 | |
stefan | 1d8a506 | 2015-10-02 10:39:33 | [diff] [blame] | 238 | bool SendRtp(const uint8_t* packet, |
| 239 | size_t length, |
| 240 | const PacketOptions& options) override { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 241 | RtpUtility::RtpHeaderParser parser(packet, length); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 242 | RTPHeader header; |
danilchap | f6975f4 | 2015-12-28 18:18:46 | [diff] [blame] | 243 | parser.Parse(&header); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 244 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 245 | int64_t current_time = |
| 246 | Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
| 247 | bool result = transport_->SendRtp(packet, length, options); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 248 | { |
| 249 | rtc::CritScope lock(&crit_); |
sprang | e1f2f1f | 2016-02-01 10:04:52 | [diff] [blame] | 250 | |
Peter Boström | 81cbd924 | 2016-03-22 11:19:07 | [diff] [blame] | 251 | if (rtp_timestamp_delta_ == 0) { |
nisse | c7fe3c2 | 2016-04-20 10:25:36 | [diff] [blame] | 252 | rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_; |
| 253 | first_send_timestamp_ = rtc::Optional<uint32_t>(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 254 | } |
sprang | 1b3530b | 2016-03-10 09:32:53 | [diff] [blame] | 255 | int64_t timestamp = |
| 256 | wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 257 | send_times_[timestamp] = current_time; |
| 258 | if (!transport_->DiscardedLastPacket() && |
| 259 | header.ssrc == ssrc_to_analyze_) { |
| 260 | encoded_frame_sizes_[timestamp] += |
| 261 | length - (header.headerLength + header.paddingLength); |
| 262 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 263 | } |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 264 | return result; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | bool SendRtcp(const uint8_t* packet, size_t length) override { |
| 268 | return transport_->SendRtcp(packet, length); |
| 269 | } |
| 270 | |
| 271 | void EncodedFrameCallback(const EncodedFrame& frame) override { |
| 272 | rtc::CritScope lock(&comparison_lock_); |
| 273 | if (frames_recorded_ < frames_to_process_) |
| 274 | encoded_frame_size_.AddSample(frame.length_); |
| 275 | } |
| 276 | |
nisse | eb83a1a | 2016-03-21 08:27:56 | [diff] [blame] | 277 | void OnFrame(const VideoFrame& video_frame) override { |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 278 | int64_t render_time_ms = |
| 279 | Clock::GetRealTimeClock()->CurrentNtpInMilliseconds(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 280 | |
| 281 | rtc::CritScope lock(&crit_); |
Taylor Brandstetter | 433b95a | 2016-03-18 18:41:03 | [diff] [blame] | 282 | int64_t send_timestamp = |
sprang | 16daaa5 | 2016-03-09 09:30:24 | [diff] [blame] | 283 | wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 284 | |
sprang | 16daaa5 | 2016-03-09 09:30:24 | [diff] [blame] | 285 | while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) { |
nisse | 97f0b93 | 2016-05-26 16:44:40 | [diff] [blame] | 286 | if (!last_rendered_frame_) { |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 287 | // No previous frame rendered, this one was dropped after sending but |
| 288 | // before rendering. |
| 289 | ++dropped_frames_before_rendering_; |
| 290 | frames_.pop_front(); |
| 291 | RTC_CHECK(!frames_.empty()); |
| 292 | continue; |
| 293 | } |
nisse | 97f0b93 | 2016-05-26 16:44:40 | [diff] [blame] | 294 | AddFrameComparison(frames_.front(), *last_rendered_frame_, true, |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 295 | render_time_ms); |
| 296 | frames_.pop_front(); |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 297 | RTC_DCHECK(!frames_.empty()); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | VideoFrame reference_frame = frames_.front(); |
| 301 | frames_.pop_front(); |
sprang | 16daaa5 | 2016-03-09 09:30:24 | [diff] [blame] | 302 | int64_t reference_timestamp = |
| 303 | wrap_handler_.Unwrap(reference_frame.timestamp()); |
| 304 | if (send_timestamp == reference_timestamp - 1) { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 305 | // TODO(ivica): Make this work for > 2 streams. |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 306 | // Look at RTPSender::BuildRTPHeader. |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 307 | ++send_timestamp; |
| 308 | } |
sprang | 16daaa5 | 2016-03-09 09:30:24 | [diff] [blame] | 309 | ASSERT_EQ(reference_timestamp, send_timestamp); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 310 | |
| 311 | AddFrameComparison(reference_frame, video_frame, false, render_time_ms); |
| 312 | |
nisse | 97f0b93 | 2016-05-26 16:44:40 | [diff] [blame] | 313 | last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 314 | } |
| 315 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 316 | void Wait() { |
| 317 | // Frame comparisons can be very expensive. Wait for test to be done, but |
| 318 | // at time-out check if frames_processed is going up. If so, give it more |
| 319 | // time, otherwise fail. Hopefully this will reduce test flakiness. |
| 320 | |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 321 | stats_polling_thread_.Start(); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 322 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 323 | int last_frames_processed = -1; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 324 | int iteration = 0; |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 325 | while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) { |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 326 | int frames_processed; |
| 327 | { |
| 328 | rtc::CritScope crit(&comparison_lock_); |
| 329 | frames_processed = frames_processed_; |
| 330 | } |
| 331 | |
| 332 | // Print some output so test infrastructure won't think we've crashed. |
| 333 | const char* kKeepAliveMessages[3] = { |
| 334 | "Uh, I'm-I'm not quite dead, sir.", |
| 335 | "Uh, I-I think uh, I could pull through, sir.", |
| 336 | "Actually, I think I'm all right to come with you--"}; |
| 337 | printf("- %s\n", kKeepAliveMessages[iteration++ % 3]); |
| 338 | |
| 339 | if (last_frames_processed == -1) { |
| 340 | last_frames_processed = frames_processed; |
| 341 | continue; |
| 342 | } |
Peter Boström | dd45eb6 | 2016-01-19 14:22:32 | [diff] [blame] | 343 | if (frames_processed == last_frames_processed) { |
| 344 | EXPECT_GT(frames_processed, last_frames_processed) |
| 345 | << "Analyzer stalled while waiting for test to finish."; |
| 346 | done_.Set(); |
| 347 | break; |
| 348 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 349 | last_frames_processed = frames_processed; |
| 350 | } |
| 351 | |
| 352 | if (iteration > 0) |
| 353 | printf("- Farewell, sweet Concorde!\n"); |
| 354 | |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 355 | stats_polling_thread_.Stop(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 356 | } |
| 357 | |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 358 | rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() { |
| 359 | return &pre_encode_proxy_; |
| 360 | } |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 361 | EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; } |
| 362 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 363 | test::LayerFilteringTransport* const transport_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 364 | PacketReceiver* receiver_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 365 | |
| 366 | private: |
| 367 | struct FrameComparison { |
| 368 | FrameComparison() |
| 369 | : dropped(false), |
| 370 | send_time_ms(0), |
| 371 | recv_time_ms(0), |
| 372 | render_time_ms(0), |
| 373 | encoded_frame_size(0) {} |
| 374 | |
| 375 | FrameComparison(const VideoFrame& reference, |
| 376 | const VideoFrame& render, |
| 377 | bool dropped, |
| 378 | int64_t send_time_ms, |
| 379 | int64_t recv_time_ms, |
| 380 | int64_t render_time_ms, |
| 381 | size_t encoded_frame_size) |
| 382 | : reference(reference), |
| 383 | render(render), |
| 384 | dropped(dropped), |
| 385 | send_time_ms(send_time_ms), |
| 386 | recv_time_ms(recv_time_ms), |
| 387 | render_time_ms(render_time_ms), |
| 388 | encoded_frame_size(encoded_frame_size) {} |
| 389 | |
| 390 | VideoFrame reference; |
| 391 | VideoFrame render; |
| 392 | bool dropped; |
| 393 | int64_t send_time_ms; |
| 394 | int64_t recv_time_ms; |
| 395 | int64_t render_time_ms; |
| 396 | size_t encoded_frame_size; |
| 397 | }; |
| 398 | |
| 399 | struct Sample { |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 400 | Sample(int dropped, |
| 401 | int64_t input_time_ms, |
| 402 | int64_t send_time_ms, |
| 403 | int64_t recv_time_ms, |
| 404 | int64_t render_time_ms, |
| 405 | size_t encoded_frame_size, |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 406 | double psnr, |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 407 | double ssim) |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 408 | : dropped(dropped), |
| 409 | input_time_ms(input_time_ms), |
| 410 | send_time_ms(send_time_ms), |
| 411 | recv_time_ms(recv_time_ms), |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 412 | render_time_ms(render_time_ms), |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 413 | encoded_frame_size(encoded_frame_size), |
| 414 | psnr(psnr), |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 415 | ssim(ssim) {} |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 416 | |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 417 | int dropped; |
| 418 | int64_t input_time_ms; |
| 419 | int64_t send_time_ms; |
| 420 | int64_t recv_time_ms; |
| 421 | int64_t render_time_ms; |
| 422 | size_t encoded_frame_size; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 423 | double psnr; |
| 424 | double ssim; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 425 | }; |
| 426 | |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 427 | // This class receives the send-side OnEncodeTiming and is provided to not |
| 428 | // conflict with the receiver-side pre_decode_callback. |
| 429 | class OnEncodeTimingProxy : public EncodedFrameObserver { |
| 430 | public: |
| 431 | explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {} |
| 432 | |
| 433 | void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override { |
| 434 | parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms); |
| 435 | } |
| 436 | void EncodedFrameCallback(const EncodedFrame& frame) override {} |
| 437 | |
| 438 | private: |
| 439 | VideoAnalyzer* const parent_; |
| 440 | }; |
| 441 | |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 442 | // This class receives the send-side OnFrame callback and is provided to not |
| 443 | // conflict with the receiver-side renderer callback. |
| 444 | class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> { |
| 445 | public: |
| 446 | explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {} |
| 447 | |
| 448 | void OnFrame(const VideoFrame& video_frame) override { |
| 449 | parent_->PreEncodeOnFrame(video_frame); |
| 450 | } |
| 451 | |
| 452 | private: |
| 453 | VideoAnalyzer* const parent_; |
| 454 | }; |
| 455 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 456 | void AddFrameComparison(const VideoFrame& reference, |
| 457 | const VideoFrame& render, |
| 458 | bool dropped, |
| 459 | int64_t render_time_ms) |
| 460 | EXCLUSIVE_LOCKS_REQUIRED(crit_) { |
sprang | e1f2f1f | 2016-02-01 10:04:52 | [diff] [blame] | 461 | int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp()); |
| 462 | int64_t send_time_ms = send_times_[reference_timestamp]; |
| 463 | send_times_.erase(reference_timestamp); |
| 464 | int64_t recv_time_ms = recv_times_[reference_timestamp]; |
| 465 | recv_times_.erase(reference_timestamp); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 466 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 467 | // TODO(ivica): Make this work for > 2 streams. |
sprang | e1f2f1f | 2016-02-01 10:04:52 | [diff] [blame] | 468 | auto it = encoded_frame_sizes_.find(reference_timestamp); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 469 | if (it == encoded_frame_sizes_.end()) |
sprang | e1f2f1f | 2016-02-01 10:04:52 | [diff] [blame] | 470 | it = encoded_frame_sizes_.find(reference_timestamp - 1); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 471 | size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second; |
| 472 | if (it != encoded_frame_sizes_.end()) |
| 473 | encoded_frame_sizes_.erase(it); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 474 | |
| 475 | VideoFrame reference_copy; |
| 476 | VideoFrame render_copy; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 477 | |
| 478 | rtc::CritScope crit(&comparison_lock_); |
stefan | b179767 | 2016-08-11 14:00:57 | [diff] [blame] | 479 | if (comparisons_.size() < kMaxComparisons) { |
nisse | 67dca9f | 2016-10-31 15:05:50 | [diff] [blame] | 480 | reference_copy = reference; |
| 481 | render_copy = render; |
stefan | b179767 | 2016-08-11 14:00:57 | [diff] [blame] | 482 | } else { |
| 483 | // Copy the time to ensure that delay calculations can still be made. |
| 484 | reference_copy.set_ntp_time_ms(reference.ntp_time_ms()); |
| 485 | render_copy.set_ntp_time_ms(render.ntp_time_ms()); |
| 486 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 487 | comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped, |
| 488 | send_time_ms, recv_time_ms, |
| 489 | render_time_ms, encoded_size)); |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 490 | comparison_available_event_.Set(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static bool PollStatsThread(void* obj) { |
| 494 | return static_cast<VideoAnalyzer*>(obj)->PollStats(); |
| 495 | } |
| 496 | |
| 497 | bool PollStats() { |
Peter Boström | dd45eb6 | 2016-01-19 14:22:32 | [diff] [blame] | 498 | if (done_.Wait(kSendStatsPollingIntervalMs)) |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 499 | return false; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 500 | |
| 501 | VideoSendStream::Stats stats = send_stream_->GetStats(); |
| 502 | |
| 503 | rtc::CritScope crit(&comparison_lock_); |
Peter Boström | b1eaa8d | 2016-02-23 16:30:49 | [diff] [blame] | 504 | // It's not certain that we yet have estimates for any of these stats. Check |
| 505 | // that they are positive before mixing them in. |
| 506 | if (stats.encode_frame_rate > 0) |
| 507 | encode_frame_rate_.AddSample(stats.encode_frame_rate); |
| 508 | if (stats.avg_encode_time_ms > 0) |
| 509 | encode_time_ms.AddSample(stats.avg_encode_time_ms); |
| 510 | if (stats.encode_usage_percent > 0) |
| 511 | encode_usage_percent.AddSample(stats.encode_usage_percent); |
| 512 | if (stats.media_bitrate_bps > 0) |
| 513 | media_bitrate_bps.AddSample(stats.media_bitrate_bps); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 514 | |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | static bool FrameComparisonThread(void* obj) { |
| 519 | return static_cast<VideoAnalyzer*>(obj)->CompareFrames(); |
| 520 | } |
| 521 | |
| 522 | bool CompareFrames() { |
| 523 | if (AllFramesRecorded()) |
| 524 | return false; |
| 525 | |
| 526 | VideoFrame reference; |
| 527 | VideoFrame render; |
| 528 | FrameComparison comparison; |
| 529 | |
| 530 | if (!PopComparison(&comparison)) { |
| 531 | // Wait until new comparison task is available, or test is done. |
| 532 | // If done, wake up remaining threads waiting. |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 533 | comparison_available_event_.Wait(1000); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 534 | if (AllFramesRecorded()) { |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 535 | comparison_available_event_.Set(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | return true; // Try again. |
| 539 | } |
| 540 | |
| 541 | PerformFrameComparison(comparison); |
| 542 | |
| 543 | if (FrameProcessed()) { |
| 544 | PrintResults(); |
| 545 | if (graph_data_output_file_) |
| 546 | PrintSamplesToFile(); |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 547 | done_.Set(); |
| 548 | comparison_available_event_.Set(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 549 | return false; |
| 550 | } |
| 551 | |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | bool PopComparison(FrameComparison* comparison) { |
| 556 | rtc::CritScope crit(&comparison_lock_); |
| 557 | // If AllFramesRecorded() is true, it means we have already popped |
| 558 | // frames_to_process_ frames from comparisons_, so there is no more work |
| 559 | // for this thread to be done. frames_processed_ might still be lower if |
| 560 | // all comparisons are not done, but those frames are currently being |
| 561 | // worked on by other threads. |
| 562 | if (comparisons_.empty() || AllFramesRecorded()) |
| 563 | return false; |
| 564 | |
| 565 | *comparison = comparisons_.front(); |
| 566 | comparisons_.pop_front(); |
| 567 | |
| 568 | FrameRecorded(); |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | // Increment counter for number of frames received for comparison. |
| 573 | void FrameRecorded() { |
| 574 | rtc::CritScope crit(&comparison_lock_); |
| 575 | ++frames_recorded_; |
| 576 | } |
| 577 | |
| 578 | // Returns true if all frames to be compared have been taken from the queue. |
| 579 | bool AllFramesRecorded() { |
| 580 | rtc::CritScope crit(&comparison_lock_); |
| 581 | assert(frames_recorded_ <= frames_to_process_); |
| 582 | return frames_recorded_ == frames_to_process_; |
| 583 | } |
| 584 | |
| 585 | // Increase count of number of frames processed. Returns true if this was the |
| 586 | // last frame to be processed. |
| 587 | bool FrameProcessed() { |
| 588 | rtc::CritScope crit(&comparison_lock_); |
| 589 | ++frames_processed_; |
| 590 | assert(frames_processed_ <= frames_to_process_); |
| 591 | return frames_processed_ == frames_to_process_; |
| 592 | } |
| 593 | |
| 594 | void PrintResults() { |
| 595 | rtc::CritScope crit(&comparison_lock_); |
| 596 | PrintResult("psnr", psnr_, " dB"); |
tnakamura | 3123cbc | 2016-02-10 19:21:51 | [diff] [blame] | 597 | PrintResult("ssim", ssim_, " score"); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 598 | PrintResult("sender_time", sender_time_, " ms"); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 599 | PrintResult("receiver_time", receiver_time_, " ms"); |
| 600 | PrintResult("total_delay_incl_network", end_to_end_, " ms"); |
| 601 | PrintResult("time_between_rendered_frames", rendered_delta_, " ms"); |
| 602 | PrintResult("encoded_frame_size", encoded_frame_size_, " bytes"); |
| 603 | PrintResult("encode_frame_rate", encode_frame_rate_, " fps"); |
| 604 | PrintResult("encode_time", encode_time_ms, " ms"); |
| 605 | PrintResult("encode_usage_percent", encode_usage_percent, " percent"); |
| 606 | PrintResult("media_bitrate", media_bitrate_bps, " bps"); |
| 607 | |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 608 | printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(), |
| 609 | dropped_frames_); |
| 610 | printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n", |
| 611 | test_label_.c_str(), dropped_frames_before_first_encode_); |
| 612 | printf("RESULT dropped_frames_before_rendering: %s = %d frames\n", |
| 613 | test_label_.c_str(), dropped_frames_before_rendering_); |
| 614 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 615 | EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_); |
| 616 | EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_); |
| 617 | } |
| 618 | |
| 619 | void PerformFrameComparison(const FrameComparison& comparison) { |
| 620 | // Perform expensive psnr and ssim calculations while not holding lock. |
stefan | b179767 | 2016-08-11 14:00:57 | [diff] [blame] | 621 | double psnr = -1.0; |
| 622 | double ssim = -1.0; |
| 623 | if (!comparison.reference.IsZeroSize()) { |
| 624 | psnr = I420PSNR(&comparison.reference, &comparison.render); |
| 625 | ssim = I420SSIM(&comparison.reference, &comparison.render); |
| 626 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 627 | |
| 628 | int64_t input_time_ms = comparison.reference.ntp_time_ms(); |
| 629 | |
| 630 | rtc::CritScope crit(&comparison_lock_); |
| 631 | if (graph_data_output_file_) { |
| 632 | samples_.push_back( |
| 633 | Sample(comparison.dropped, input_time_ms, comparison.send_time_ms, |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 634 | comparison.recv_time_ms, comparison.render_time_ms, |
| 635 | comparison.encoded_frame_size, psnr, ssim)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 636 | } |
stefan | b179767 | 2016-08-11 14:00:57 | [diff] [blame] | 637 | if (psnr >= 0.0) |
| 638 | psnr_.AddSample(psnr); |
| 639 | if (ssim >= 0.0) |
| 640 | ssim_.AddSample(ssim); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 641 | |
| 642 | if (comparison.dropped) { |
| 643 | ++dropped_frames_; |
| 644 | return; |
| 645 | } |
| 646 | if (last_render_time_ != 0) |
| 647 | rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_); |
| 648 | last_render_time_ = comparison.render_time_ms; |
| 649 | |
| 650 | sender_time_.AddSample(comparison.send_time_ms - input_time_ms); |
| 651 | receiver_time_.AddSample(comparison.render_time_ms - |
| 652 | comparison.recv_time_ms); |
| 653 | end_to_end_.AddSample(comparison.render_time_ms - input_time_ms); |
| 654 | encoded_frame_size_.AddSample(comparison.encoded_frame_size); |
| 655 | } |
| 656 | |
| 657 | void PrintResult(const char* result_type, |
| 658 | test::Statistics stats, |
| 659 | const char* unit) { |
| 660 | printf("RESULT %s: %s = {%f, %f}%s\n", |
| 661 | result_type, |
| 662 | test_label_.c_str(), |
| 663 | stats.Mean(), |
| 664 | stats.StandardDeviation(), |
| 665 | unit); |
| 666 | } |
| 667 | |
| 668 | void PrintSamplesToFile(void) { |
| 669 | FILE* out = graph_data_output_file_; |
| 670 | rtc::CritScope crit(&comparison_lock_); |
| 671 | std::sort(samples_.begin(), samples_.end(), |
| 672 | [](const Sample& A, const Sample& B) -> bool { |
| 673 | return A.input_time_ms < B.input_time_ms; |
| 674 | }); |
| 675 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 676 | fprintf(out, "%s\n", graph_title_.c_str()); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 677 | fprintf(out, "%" PRIuS "\n", samples_.size()); |
| 678 | fprintf(out, |
| 679 | "dropped " |
| 680 | "input_time_ms " |
| 681 | "send_time_ms " |
| 682 | "recv_time_ms " |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 683 | "render_time_ms " |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 684 | "encoded_frame_size " |
| 685 | "psnr " |
| 686 | "ssim " |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 687 | "encode_time_ms\n"); |
| 688 | int missing_encode_time_samples = 0; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 689 | for (const Sample& sample : samples_) { |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 690 | auto it = samples_encode_time_ms_.find(sample.input_time_ms); |
| 691 | int encode_time_ms; |
| 692 | if (it != samples_encode_time_ms_.end()) { |
| 693 | encode_time_ms = it->second; |
| 694 | } else { |
| 695 | ++missing_encode_time_samples; |
| 696 | encode_time_ms = -1; |
| 697 | } |
| 698 | fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS |
| 699 | " %lf %lf %d\n", |
| 700 | sample.dropped, sample.input_time_ms, sample.send_time_ms, |
| 701 | sample.recv_time_ms, sample.render_time_ms, |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 702 | sample.encoded_frame_size, sample.psnr, sample.ssim, |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 703 | encode_time_ms); |
| 704 | } |
| 705 | if (missing_encode_time_samples) { |
| 706 | fprintf(stderr, |
| 707 | "Warning: Missing encode_time_ms samples for %d frame(s).\n", |
| 708 | missing_encode_time_samples); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 712 | // Implements VideoSinkInterface to receive captured frames from a |
| 713 | // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act |
| 714 | // as a source to VideoSendStream. |
| 715 | // It forwards all input frames to the VideoAnalyzer for later comparison and |
| 716 | // forwards the captured frames to the VideoSendStream. |
| 717 | class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>, |
| 718 | public rtc::VideoSourceInterface<VideoFrame> { |
| 719 | public: |
| 720 | explicit CapturedFrameForwarder(VideoAnalyzer* analyzer) |
| 721 | : analyzer_(analyzer), send_stream_input_(nullptr) {} |
| 722 | |
| 723 | private: |
| 724 | void OnFrame(const VideoFrame& video_frame) override { |
| 725 | VideoFrame copy = video_frame; |
| 726 | copy.set_timestamp(copy.ntp_time_ms() * 90); |
| 727 | |
| 728 | analyzer_->AddCapturedFrameForComparison(video_frame); |
| 729 | rtc::CritScope lock(&crit_); |
| 730 | if (send_stream_input_) |
| 731 | send_stream_input_->OnFrame(video_frame); |
| 732 | } |
| 733 | |
| 734 | // Called when |send_stream_.SetSource()| is called. |
| 735 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 736 | const rtc::VideoSinkWants& wants) override { |
| 737 | rtc::CritScope lock(&crit_); |
| 738 | RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink); |
| 739 | send_stream_input_ = sink; |
| 740 | } |
| 741 | |
| 742 | // Called by |send_stream_| when |send_stream_.SetSource()| is called. |
| 743 | void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override { |
| 744 | rtc::CritScope lock(&crit_); |
| 745 | RTC_DCHECK(sink == send_stream_input_); |
| 746 | send_stream_input_ = nullptr; |
| 747 | } |
| 748 | |
| 749 | VideoAnalyzer* const analyzer_; |
| 750 | rtc::CriticalSection crit_; |
| 751 | rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_); |
| 752 | }; |
| 753 | |
| 754 | void AddCapturedFrameForComparison(const VideoFrame& video_frame) { |
| 755 | rtc::CritScope lock(&crit_); |
| 756 | RTC_DCHECK_EQ(0u, video_frame.timestamp()); |
| 757 | // Frames from the capturer does not have a rtp timestamp. Create one so it |
| 758 | // can be used for comparison. |
| 759 | VideoFrame copy = video_frame; |
| 760 | copy.set_timestamp(copy.ntp_time_ms() * 90); |
| 761 | frames_.push_back(copy); |
| 762 | } |
| 763 | |
| 764 | VideoSendStream* send_stream_; |
| 765 | CapturedFrameForwarder captured_frame_forwarder_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 766 | const std::string test_label_; |
| 767 | FILE* const graph_data_output_file_; |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 768 | const std::string graph_title_; |
| 769 | const uint32_t ssrc_to_analyze_; |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 770 | PreEncodeProxy pre_encode_proxy_; |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 771 | OnEncodeTimingProxy encode_timing_proxy_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 772 | std::vector<Sample> samples_ GUARDED_BY(comparison_lock_); |
ivica | 8d15bd6 | 2015-10-07 09:43:12 | [diff] [blame] | 773 | std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 774 | test::Statistics sender_time_ GUARDED_BY(comparison_lock_); |
| 775 | test::Statistics receiver_time_ GUARDED_BY(comparison_lock_); |
| 776 | test::Statistics psnr_ GUARDED_BY(comparison_lock_); |
| 777 | test::Statistics ssim_ GUARDED_BY(comparison_lock_); |
| 778 | test::Statistics end_to_end_ GUARDED_BY(comparison_lock_); |
| 779 | test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_); |
| 780 | test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_); |
| 781 | test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_); |
| 782 | test::Statistics encode_time_ms GUARDED_BY(comparison_lock_); |
| 783 | test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_); |
| 784 | test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_); |
| 785 | |
| 786 | const int frames_to_process_; |
| 787 | int frames_recorded_; |
| 788 | int frames_processed_; |
| 789 | int dropped_frames_; |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 790 | int dropped_frames_before_first_encode_; |
| 791 | int dropped_frames_before_rendering_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 792 | int64_t last_render_time_; |
| 793 | uint32_t rtp_timestamp_delta_; |
| 794 | |
| 795 | rtc::CriticalSection crit_; |
| 796 | std::deque<VideoFrame> frames_ GUARDED_BY(crit_); |
nisse | 97f0b93 | 2016-05-26 16:44:40 | [diff] [blame] | 797 | rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_); |
sprang | e1f2f1f | 2016-02-01 10:04:52 | [diff] [blame] | 798 | rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_); |
| 799 | std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_); |
| 800 | std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_); |
| 801 | std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_); |
nisse | c7fe3c2 | 2016-04-20 10:25:36 | [diff] [blame] | 802 | rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 803 | const double avg_psnr_threshold_; |
| 804 | const double avg_ssim_threshold_; |
| 805 | |
| 806 | rtc::CriticalSection comparison_lock_; |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 807 | std::vector<rtc::PlatformThread*> comparison_thread_pool_; |
| 808 | rtc::PlatformThread stats_polling_thread_; |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 809 | rtc::Event comparison_available_event_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 810 | std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_); |
Peter Boström | 5811a39 | 2015-12-10 12:02:50 | [diff] [blame] | 811 | rtc::Event done_; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 812 | }; |
| 813 | |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 814 | VideoQualityTest::VideoQualityTest() |
| 815 | : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {} |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 816 | |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 817 | VideoQualityTest::Params::Params() |
| 818 | : call({false, Call::Config::BitrateConfig()}), |
| 819 | video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false, |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 820 | false, "", ""}), |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 821 | audio({false, false}), |
| 822 | screenshare({false, 10, 0}), |
| 823 | analyzer({"", 0.0, 0.0, 0, "", ""}), |
| 824 | pipe(), |
| 825 | logs(false), |
| 826 | ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}) {} |
| 827 | |
| 828 | VideoQualityTest::Params::~Params() = default; |
| 829 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 830 | void VideoQualityTest::TestBody() {} |
| 831 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 832 | std::string VideoQualityTest::GenerateGraphTitle() const { |
| 833 | std::stringstream ss; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 834 | ss << params_.video.codec; |
| 835 | ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps"; |
| 836 | ss << ", " << params_.video.fps << " FPS"; |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 837 | if (params_.screenshare.scroll_duration) |
| 838 | ss << ", " << params_.screenshare.scroll_duration << "s scroll"; |
| 839 | if (params_.ss.streams.size() > 1) |
| 840 | ss << ", Stream #" << params_.ss.selected_stream; |
| 841 | if (params_.ss.num_spatial_layers > 1) |
| 842 | ss << ", Layer #" << params_.ss.selected_sl; |
| 843 | ss << ")"; |
| 844 | return ss.str(); |
| 845 | } |
| 846 | |
| 847 | void VideoQualityTest::CheckParams() { |
| 848 | // Add a default stream in none specified. |
| 849 | if (params_.ss.streams.empty()) |
| 850 | params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_)); |
| 851 | if (params_.ss.num_spatial_layers == 0) |
| 852 | params_.ss.num_spatial_layers = 1; |
| 853 | |
| 854 | if (params_.pipe.loss_percent != 0 || |
| 855 | params_.pipe.queue_length_packets != 0) { |
| 856 | // Since LayerFilteringTransport changes the sequence numbers, we can't |
| 857 | // use that feature with pack loss, since the NACK request would end up |
| 858 | // retransmitting the wrong packets. |
| 859 | RTC_CHECK(params_.ss.selected_sl == -1 || |
sprang | ee37de3 | 2015-11-23 14:10:23 | [diff] [blame] | 860 | params_.ss.selected_sl == params_.ss.num_spatial_layers - 1); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 861 | RTC_CHECK(params_.video.selected_tl == -1 || |
| 862 | params_.video.selected_tl == |
| 863 | params_.video.num_temporal_layers - 1); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it |
| 867 | // does in some parts of the code? |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 868 | RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps); |
| 869 | RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps); |
| 870 | RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 871 | RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size()); |
| 872 | for (const VideoStream& stream : params_.ss.streams) { |
| 873 | RTC_CHECK_GE(stream.min_bitrate_bps, 0); |
| 874 | RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps); |
| 875 | RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps); |
| 876 | RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()), |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 877 | params_.video.num_temporal_layers - 1); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 878 | } |
| 879 | // TODO(ivica): Should we check if the sum of all streams/layers is equal to |
| 880 | // the total bitrate? We anyway have to update them in the case bitrate |
| 881 | // estimator changes the total bitrates. |
| 882 | RTC_CHECK_GE(params_.ss.num_spatial_layers, 1); |
| 883 | RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers); |
| 884 | RTC_CHECK(params_.ss.spatial_layers.empty() || |
| 885 | params_.ss.spatial_layers.size() == |
| 886 | static_cast<size_t>(params_.ss.num_spatial_layers)); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 887 | if (params_.video.codec == "VP8") { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 888 | RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 889 | } else if (params_.video.codec == "VP9") { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 890 | RTC_CHECK_EQ(params_.ss.streams.size(), 1u); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | // Static. |
| 895 | std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) { |
| 896 | // Parse comma separated nonnegative integers, where some elements may be |
| 897 | // empty. The empty values are replaced with -1. |
| 898 | // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40} |
| 899 | // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1} |
| 900 | std::vector<int> result; |
| 901 | if (str.empty()) |
| 902 | return result; |
| 903 | |
| 904 | const char* p = str.c_str(); |
| 905 | int value = -1; |
| 906 | int pos; |
| 907 | while (*p) { |
| 908 | if (*p == ',') { |
| 909 | result.push_back(value); |
| 910 | value = -1; |
| 911 | ++p; |
| 912 | continue; |
| 913 | } |
| 914 | RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1) |
| 915 | << "Unexpected non-number value."; |
| 916 | p += pos; |
| 917 | } |
| 918 | result.push_back(value); |
| 919 | return result; |
| 920 | } |
| 921 | |
| 922 | // Static. |
| 923 | VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) { |
| 924 | VideoStream stream; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 925 | stream.width = params.video.width; |
| 926 | stream.height = params.video.height; |
| 927 | stream.max_framerate = params.video.fps; |
| 928 | stream.min_bitrate_bps = params.video.min_bitrate_bps; |
| 929 | stream.target_bitrate_bps = params.video.target_bitrate_bps; |
| 930 | stream.max_bitrate_bps = params.video.max_bitrate_bps; |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 931 | stream.max_qp = 52; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 932 | if (params.video.num_temporal_layers == 2) |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 933 | stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps); |
| 934 | return stream; |
| 935 | } |
| 936 | |
| 937 | // Static. |
| 938 | void VideoQualityTest::FillScalabilitySettings( |
| 939 | Params* params, |
| 940 | const std::vector<std::string>& stream_descriptors, |
| 941 | size_t selected_stream, |
| 942 | int num_spatial_layers, |
| 943 | int selected_sl, |
| 944 | const std::vector<std::string>& sl_descriptors) { |
| 945 | // Read VideoStream and SpatialLayer elements from a list of comma separated |
| 946 | // lists. To use a default value for an element, use -1 or leave empty. |
| 947 | // Validity checks performed in CheckParams. |
| 948 | |
| 949 | RTC_CHECK(params->ss.streams.empty()); |
| 950 | for (auto descriptor : stream_descriptors) { |
| 951 | if (descriptor.empty()) |
| 952 | continue; |
| 953 | VideoStream stream = VideoQualityTest::DefaultVideoStream(*params); |
| 954 | std::vector<int> v = VideoQualityTest::ParseCSV(descriptor); |
| 955 | if (v[0] != -1) |
| 956 | stream.width = static_cast<size_t>(v[0]); |
| 957 | if (v[1] != -1) |
| 958 | stream.height = static_cast<size_t>(v[1]); |
| 959 | if (v[2] != -1) |
| 960 | stream.max_framerate = v[2]; |
| 961 | if (v[3] != -1) |
| 962 | stream.min_bitrate_bps = v[3]; |
| 963 | if (v[4] != -1) |
| 964 | stream.target_bitrate_bps = v[4]; |
| 965 | if (v[5] != -1) |
| 966 | stream.max_bitrate_bps = v[5]; |
| 967 | if (v.size() > 6 && v[6] != -1) |
| 968 | stream.max_qp = v[6]; |
| 969 | if (v.size() > 7) { |
| 970 | stream.temporal_layer_thresholds_bps.clear(); |
| 971 | stream.temporal_layer_thresholds_bps.insert( |
| 972 | stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end()); |
| 973 | } else { |
| 974 | // Automatic TL thresholds for more than two layers not supported. |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 975 | RTC_CHECK_LE(params->video.num_temporal_layers, 2); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 976 | } |
| 977 | params->ss.streams.push_back(stream); |
| 978 | } |
| 979 | params->ss.selected_stream = selected_stream; |
| 980 | |
| 981 | params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1; |
| 982 | params->ss.selected_sl = selected_sl; |
| 983 | RTC_CHECK(params->ss.spatial_layers.empty()); |
| 984 | for (auto descriptor : sl_descriptors) { |
| 985 | if (descriptor.empty()) |
| 986 | continue; |
| 987 | std::vector<int> v = VideoQualityTest::ParseCSV(descriptor); |
| 988 | RTC_CHECK_GT(v[2], 0); |
| 989 | |
| 990 | SpatialLayer layer; |
| 991 | layer.scaling_factor_num = v[0] == -1 ? 1 : v[0]; |
| 992 | layer.scaling_factor_den = v[1] == -1 ? 1 : v[1]; |
| 993 | layer.target_bitrate_bps = v[2]; |
| 994 | params->ss.spatial_layers.push_back(layer); |
| 995 | } |
| 996 | } |
| 997 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 998 | void VideoQualityTest::SetupVideo(Transport* send_transport, |
| 999 | Transport* recv_transport) { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1000 | if (params_.logs) |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1001 | trace_to_stderr_.reset(new test::TraceToStderr); |
| 1002 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1003 | size_t num_streams = params_.ss.streams.size(); |
brandtr | 841de6a | 2016-11-15 15:10:52 | [diff] [blame] | 1004 | CreateSendConfig(num_streams, 0, 0, send_transport); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1005 | |
| 1006 | int payload_type; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1007 | if (params_.video.codec == "H264") { |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1008 | video_encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264)); |
hbos | bab934b | 2016-01-27 09:36:03 | [diff] [blame] | 1009 | payload_type = kPayloadTypeH264; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1010 | } else if (params_.video.codec == "VP8") { |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1011 | video_encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1012 | payload_type = kPayloadTypeVP8; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1013 | } else if (params_.video.codec == "VP9") { |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1014 | video_encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1015 | payload_type = kPayloadTypeVP9; |
| 1016 | } else { |
| 1017 | RTC_NOTREACHED() << "Codec not supported!"; |
| 1018 | return; |
| 1019 | } |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1020 | video_send_config_.encoder_settings.encoder = video_encoder_.get(); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1021 | video_send_config_.encoder_settings.payload_name = params_.video.codec; |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1022 | video_send_config_.encoder_settings.payload_type = payload_type; |
| 1023 | video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs; |
| 1024 | video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType; |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1025 | for (size_t i = 0; i < num_streams; ++i) |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1026 | video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1027 | |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1028 | video_send_config_.rtp.extensions.clear(); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1029 | if (params_.call.send_side_bwe) { |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1030 | video_send_config_.rtp.extensions.push_back( |
isheriff | 6f8d686 | 2016-05-26 18:24:55 | [diff] [blame] | 1031 | RtpExtension(RtpExtension::kTransportSequenceNumberUri, |
Stefan Holmer | 1295297 | 2015-10-29 14:13:24 | [diff] [blame] | 1032 | test::kTransportSequenceNumberExtensionId)); |
| 1033 | } else { |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1034 | video_send_config_.rtp.extensions.push_back(RtpExtension( |
isheriff | 6f8d686 | 2016-05-26 18:24:55 | [diff] [blame] | 1035 | RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId)); |
Erik SprĂ¥ng | 6b8d355 | 2015-09-24 13:06:57 | [diff] [blame] | 1036 | } |
| 1037 | |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1038 | video_encoder_config_.min_transmit_bitrate_bps = |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1039 | params_.video.min_transmit_bps; |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 1040 | |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1041 | video_send_config_.suspend_below_min_bitrate = |
| 1042 | params_.video.suspend_below_min_bitrate; |
| 1043 | |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 1044 | video_encoder_config_.number_of_streams = params_.ss.streams.size(); |
| 1045 | video_encoder_config_.max_bitrate_bps = 0; |
| 1046 | for (size_t i = 0; i < params_.ss.streams.size(); ++i) { |
| 1047 | video_encoder_config_.max_bitrate_bps += |
| 1048 | params_.ss.streams[i].max_bitrate_bps; |
| 1049 | } |
| 1050 | video_encoder_config_.video_stream_factory = |
| 1051 | new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams); |
| 1052 | |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1053 | video_encoder_config_.spatial_layers = params_.ss.spatial_layers; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1054 | |
| 1055 | CreateMatchingReceiveConfigs(recv_transport); |
| 1056 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1057 | for (size_t i = 0; i < num_streams; ++i) { |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1058 | video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; |
Stefan Holmer | 1088001 | 2016-02-03 12:29:59 | [diff] [blame] | 1059 | video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i]; |
| 1060 | video_receive_configs_[i].rtp.rtx[payload_type].payload_type = |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1061 | kSendRtxPayloadType; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1062 | video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe; |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1063 | } |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1064 | |
| 1065 | if (params_.video.flexfec) { |
| 1066 | video_send_config_.rtp.flexfec.flexfec_payload_type = kFlexfecPayloadType; |
| 1067 | video_send_config_.rtp.flexfec.flexfec_ssrc = kFlexfecSendSsrc; |
| 1068 | video_send_config_.rtp.flexfec.protected_media_ssrcs = { |
| 1069 | kVideoSendSsrcs[params_.ss.selected_stream]}; |
| 1070 | |
| 1071 | FlexfecReceiveStream::Config flexfec_receive_config; |
| 1072 | flexfec_receive_config.flexfec_payload_type = |
| 1073 | video_send_config_.rtp.flexfec.flexfec_payload_type; |
| 1074 | flexfec_receive_config.flexfec_ssrc = |
| 1075 | video_send_config_.rtp.flexfec.flexfec_ssrc; |
| 1076 | flexfec_receive_config.protected_media_ssrcs = |
| 1077 | video_send_config_.rtp.flexfec.protected_media_ssrcs; |
| 1078 | flexfec_receive_configs_.push_back(flexfec_receive_config); |
| 1079 | } |
| 1080 | |
| 1081 | if (params_.video.ulpfec) { |
| 1082 | video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType; |
| 1083 | video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType; |
| 1084 | video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType; |
| 1085 | |
| 1086 | video_receive_configs_[params_.ss.selected_stream] |
| 1087 | .rtp.ulpfec.red_payload_type = |
| 1088 | video_send_config_.rtp.ulpfec.red_payload_type; |
| 1089 | video_receive_configs_[params_.ss.selected_stream] |
| 1090 | .rtp.ulpfec.ulpfec_payload_type = |
| 1091 | video_send_config_.rtp.ulpfec.ulpfec_payload_type; |
| 1092 | video_receive_configs_[params_.ss.selected_stream] |
| 1093 | .rtp.ulpfec.red_rtx_payload_type = |
| 1094 | video_send_config_.rtp.ulpfec.red_rtx_payload_type; |
| 1095 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1096 | } |
| 1097 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1098 | void VideoQualityTest::SetupScreenshare() { |
| 1099 | RTC_CHECK(params_.screenshare.enabled); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1100 | |
| 1101 | // Fill out codec settings. |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1102 | video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1103 | if (params_.video.codec == "VP8") { |
kthelgason | 29a44e3 | 2016-09-27 10:52:02 | [diff] [blame] | 1104 | VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings(); |
| 1105 | vp8_settings.denoisingOn = false; |
| 1106 | vp8_settings.frameDroppingOn = false; |
| 1107 | vp8_settings.numberOfTemporalLayers = |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1108 | static_cast<unsigned char>(params_.video.num_temporal_layers); |
kthelgason | 29a44e3 | 2016-09-27 10:52:02 | [diff] [blame] | 1109 | video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject< |
| 1110 | VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1111 | } else if (params_.video.codec == "VP9") { |
kthelgason | 29a44e3 | 2016-09-27 10:52:02 | [diff] [blame] | 1112 | VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings(); |
| 1113 | vp9_settings.denoisingOn = false; |
| 1114 | vp9_settings.frameDroppingOn = false; |
| 1115 | vp9_settings.numberOfTemporalLayers = |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1116 | static_cast<unsigned char>(params_.video.num_temporal_layers); |
kthelgason | 29a44e3 | 2016-09-27 10:52:02 | [diff] [blame] | 1117 | vp9_settings.numberOfSpatialLayers = |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1118 | static_cast<unsigned char>(params_.ss.num_spatial_layers); |
kthelgason | 29a44e3 | 2016-09-27 10:52:02 | [diff] [blame] | 1119 | video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject< |
| 1120 | VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | // Setup frame generator. |
| 1124 | const size_t kWidth = 1850; |
| 1125 | const size_t kHeight = 1110; |
| 1126 | std::vector<std::string> slides; |
| 1127 | slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv")); |
| 1128 | slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv")); |
| 1129 | slides.push_back(test::ResourcePath("photo_1850_1110", "yuv")); |
| 1130 | slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv")); |
| 1131 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1132 | if (params_.screenshare.scroll_duration == 0) { |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1133 | // Cycle image every slide_change_interval seconds. |
| 1134 | frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile( |
| 1135 | slides, kWidth, kHeight, |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1136 | params_.screenshare.slide_change_interval * params_.video.fps)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1137 | } else { |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1138 | RTC_CHECK_LE(params_.video.width, kWidth); |
| 1139 | RTC_CHECK_LE(params_.video.height, kHeight); |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1140 | RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0); |
| 1141 | const int kPauseDurationMs = (params_.screenshare.slide_change_interval - |
| 1142 | params_.screenshare.scroll_duration) * |
| 1143 | 1000; |
| 1144 | RTC_CHECK_LE(params_.screenshare.scroll_duration, |
| 1145 | params_.screenshare.slide_change_interval); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1146 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1147 | frame_generator_.reset( |
| 1148 | test::FrameGenerator::CreateScrollingInputFromYuvFiles( |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1149 | clock_, slides, kWidth, kHeight, params_.video.width, |
| 1150 | params_.video.height, params_.screenshare.scroll_duration * 1000, |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1151 | kPauseDurationMs)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 1155 | void VideoQualityTest::CreateCapturer() { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1156 | if (params_.screenshare.enabled) { |
| 1157 | test::FrameGeneratorCapturer* frame_generator_capturer = |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 1158 | new test::FrameGeneratorCapturer(clock_, frame_generator_.release(), |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1159 | params_.video.fps); |
ivica | 2d4e6c5 | 2015-09-23 08:57:06 | [diff] [blame] | 1160 | EXPECT_TRUE(frame_generator_capturer->Init()); |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1161 | video_capturer_.reset(frame_generator_capturer); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1162 | } else { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1163 | if (params_.video.clip_name.empty()) { |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1164 | video_capturer_.reset(test::VcmCapturer::Create( |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1165 | params_.video.width, params_.video.height, params_.video.fps)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1166 | } else { |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1167 | video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile( |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 1168 | test::ResourcePath(params_.video.clip_name, "yuv"), |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1169 | params_.video.width, params_.video.height, params_.video.fps, |
ivica | 2d4e6c5 | 2015-09-23 08:57:06 | [diff] [blame] | 1170 | clock_)); |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1171 | ASSERT_TRUE(video_capturer_) << "Could not create capturer for " |
| 1172 | << params_.video.clip_name |
| 1173 | << ".yuv. Is this resource file present?"; |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
sprang | 7a975f7 | 2015-10-12 13:33:21 | [diff] [blame] | 1178 | void VideoQualityTest::RunWithAnalyzer(const Params& params) { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1179 | params_ = params; |
| 1180 | |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1181 | RTC_CHECK(!params_.audio.enabled); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1182 | // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to |
| 1183 | // differentiate between the analyzer and the renderer case. |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1184 | CheckParams(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1185 | |
| 1186 | FILE* graph_data_output_file = nullptr; |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1187 | if (!params_.analyzer.graph_data_output_filename.empty()) { |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1188 | graph_data_output_file = |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1189 | fopen(params_.analyzer.graph_data_output_filename.c_str(), "w"); |
Peter Boström | 74f6e9e | 2016-04-04 15:56:10 | [diff] [blame] | 1190 | RTC_CHECK(graph_data_output_file) |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1191 | << "Can't open the file " << params_.analyzer.graph_data_output_filename |
| 1192 | << "!"; |
ivica | 87f83a9 | 2015-10-08 12:13:32 | [diff] [blame] | 1193 | } |
sprang | 7a975f7 | 2015-10-12 13:33:21 | [diff] [blame] | 1194 | |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 1195 | webrtc::RtcEventLogNullImpl event_log; |
| 1196 | Call::Config call_config(&event_log_); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1197 | call_config.bitrate_config = params.call.call_bitrate_config; |
stefan | f116bd0 | 2015-10-27 15:29:42 | [diff] [blame] | 1198 | CreateCalls(call_config, call_config); |
| 1199 | |
ivica | 87f83a9 | 2015-10-08 12:13:32 | [diff] [blame] | 1200 | test::LayerFilteringTransport send_transport( |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1201 | params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9, |
| 1202 | params_.video.selected_tl, params_.ss.selected_sl); |
| 1203 | test::DirectTransport recv_transport(params_.pipe, receiver_call_.get()); |
stefan | f116bd0 | 2015-10-27 15:29:42 | [diff] [blame] | 1204 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1205 | std::string graph_title = params_.analyzer.graph_title; |
| 1206 | if (graph_title.empty()) |
| 1207 | graph_title = VideoQualityTest::GenerateGraphTitle(); |
| 1208 | |
| 1209 | // In the case of different resolutions, the functions calculating PSNR and |
| 1210 | // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer |
| 1211 | // aborts if the average psnr/ssim are below the given threshold, which is |
| 1212 | // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary |
| 1213 | // abort. |
| 1214 | VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream]; |
| 1215 | int selected_sl = params_.ss.selected_sl != -1 |
| 1216 | ? params_.ss.selected_sl |
| 1217 | : params_.ss.num_spatial_layers - 1; |
| 1218 | bool disable_quality_check = |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1219 | selected_stream.width != params_.video.width || |
| 1220 | selected_stream.height != params_.video.height || |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1221 | (!params_.ss.spatial_layers.empty() && |
| 1222 | params_.ss.spatial_layers[selected_sl].scaling_factor_num != |
| 1223 | params_.ss.spatial_layers[selected_sl].scaling_factor_den); |
| 1224 | if (disable_quality_check) { |
| 1225 | fprintf(stderr, |
| 1226 | "Warning: Calculating PSNR and SSIM for downsized resolution " |
| 1227 | "not implemented yet! Skipping PSNR and SSIM calculations!"); |
| 1228 | } |
| 1229 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1230 | VideoAnalyzer analyzer( |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1231 | &send_transport, params_.analyzer.test_label, |
| 1232 | disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold, |
| 1233 | disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold, |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1234 | params_.analyzer.test_durations_secs * params_.video.fps, |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1235 | graph_data_output_file, graph_title, |
Stefan Holmer | 9fea80f | 2016-01-07 16:43:18 | [diff] [blame] | 1236 | kVideoSendSsrcs[params_.ss.selected_stream]); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1237 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1238 | analyzer.SetReceiver(receiver_call_->Receiver()); |
| 1239 | send_transport.SetReceiver(&analyzer); |
| 1240 | recv_transport.SetReceiver(sender_call_->Receiver()); |
| 1241 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1242 | SetupVideo(&analyzer, &recv_transport); |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1243 | video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer; |
pbos | 14fe708 | 2016-04-20 13:35:56 | [diff] [blame] | 1244 | video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy(); |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1245 | for (auto& config : video_receive_configs_) |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1246 | config.pre_decode_callback = &analyzer; |
Peter Boström | e449915 | 2016-02-05 10:13:28 | [diff] [blame] | 1247 | RTC_DCHECK(!video_send_config_.post_encode_callback); |
| 1248 | video_send_config_.post_encode_callback = analyzer.encode_timing_proxy(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1249 | |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1250 | if (params_.screenshare.enabled) |
| 1251 | SetupScreenshare(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1252 | |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1253 | CreateFlexfecStreams(); |
Stefan Holmer | 9fea80f | 2016-01-07 16:43:18 | [diff] [blame] | 1254 | CreateVideoStreams(); |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 1255 | analyzer.SetSendStream(video_send_stream_); |
perkj | 803d97f | 2016-11-01 18:45:46 | [diff] [blame] | 1256 | video_send_stream_->SetSource( |
| 1257 | analyzer.OutputInterface(), |
| 1258 | VideoSendStream::DegradationPreference::kBalanced); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1259 | |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 1260 | CreateCapturer(); |
| 1261 | rtc::VideoSinkWants wants; |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1262 | video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants); |
ivica | c1cc854 | 2015-10-08 10:44:06 | [diff] [blame] | 1263 | |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1264 | StartEncodedFrameLogs(video_send_stream_); |
| 1265 | StartEncodedFrameLogs(video_receive_streams_[0]); |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1266 | video_send_stream_->Start(); |
| 1267 | for (VideoReceiveStream* receive_stream : video_receive_streams_) |
| 1268 | receive_stream->Start(); |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1269 | for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_) |
| 1270 | receive_stream->Start(); |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1271 | video_capturer_->Start(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1272 | |
| 1273 | analyzer.Wait(); |
| 1274 | |
| 1275 | send_transport.StopSending(); |
| 1276 | recv_transport.StopSending(); |
| 1277 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1278 | video_capturer_->Stop(); |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1279 | for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_) |
| 1280 | receive_stream->Stop(); |
stefan | ff48361 | 2015-12-21 11:14:00 | [diff] [blame] | 1281 | for (VideoReceiveStream* receive_stream : video_receive_streams_) |
| 1282 | receive_stream->Stop(); |
| 1283 | video_send_stream_->Stop(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1284 | |
| 1285 | DestroyStreams(); |
| 1286 | |
| 1287 | if (graph_data_output_file) |
| 1288 | fclose(graph_data_output_file); |
| 1289 | } |
| 1290 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1291 | void VideoQualityTest::SetupAudio(int send_channel_id, |
| 1292 | int receive_channel_id, |
| 1293 | Call* call, |
| 1294 | Transport* transport, |
| 1295 | AudioReceiveStream** audio_receive_stream) { |
| 1296 | audio_send_config_ = AudioSendStream::Config(transport); |
| 1297 | audio_send_config_.voe_channel_id = send_channel_id; |
| 1298 | audio_send_config_.rtp.ssrc = kAudioSendSsrc; |
| 1299 | |
| 1300 | // Add extension to enable audio send side BWE, and allow audio bit rate |
| 1301 | // adaptation. |
| 1302 | audio_send_config_.rtp.extensions.clear(); |
| 1303 | if (params_.call.send_side_bwe) { |
| 1304 | audio_send_config_.rtp.extensions.push_back( |
| 1305 | webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, |
| 1306 | test::kTransportSequenceNumberExtensionId)); |
minyue | 10cbb46 | 2016-11-07 17:29:22 | [diff] [blame] | 1307 | audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps; |
| 1308 | audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps; |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1309 | } |
| 1310 | audio_send_config_.send_codec_spec.codec_inst = |
| 1311 | CodecInst{120, "OPUS", 48000, 960, 2, 64000}; |
| 1312 | |
| 1313 | audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_); |
| 1314 | |
| 1315 | AudioReceiveStream::Config audio_config; |
| 1316 | audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc; |
| 1317 | audio_config.rtcp_send_transport = transport; |
| 1318 | audio_config.voe_channel_id = receive_channel_id; |
| 1319 | audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc; |
| 1320 | audio_config.rtp.transport_cc = params_.call.send_side_bwe; |
| 1321 | audio_config.rtp.extensions = audio_send_config_.rtp.extensions; |
| 1322 | audio_config.decoder_factory = decoder_factory_; |
| 1323 | if (params_.video.enabled && params_.audio.sync_video) |
| 1324 | audio_config.sync_group = kSyncGroup; |
| 1325 | |
| 1326 | *audio_receive_stream = call->CreateAudioReceiveStream(audio_config); |
| 1327 | } |
| 1328 | |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1329 | void VideoQualityTest::RunWithRenderers(const Params& params) { |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1330 | params_ = params; |
| 1331 | CheckParams(); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1332 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1333 | // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to |
| 1334 | // match the full stack tests. |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 1335 | webrtc::RtcEventLogNullImpl event_log; |
| 1336 | Call::Config call_config(&event_log_); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1337 | call_config.bitrate_config = params_.call.call_bitrate_config; |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1338 | |
| 1339 | ::VoiceEngineState voe; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1340 | if (params_.audio.enabled) { |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1341 | CreateVoiceEngine(&voe, decoder_factory_); |
| 1342 | AudioState::Config audio_state_config; |
| 1343 | audio_state_config.voice_engine = voe.voice_engine; |
| 1344 | call_config.audio_state = AudioState::Create(audio_state_config); |
| 1345 | } |
| 1346 | |
kwiberg | 27f982b | 2016-03-01 19:52:33 | [diff] [blame] | 1347 | std::unique_ptr<Call> call(Call::Create(call_config)); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1348 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1349 | // TODO(minyue): consider if this is a good transport even for audio only |
| 1350 | // calls. |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1351 | test::LayerFilteringTransport transport( |
stefan | f116bd0 | 2015-10-27 15:29:42 | [diff] [blame] | 1352 | params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9, |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1353 | params.video.selected_tl, params_.ss.selected_sl); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1354 | // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at |
| 1355 | // least share as much code as possible. That way this test would also match |
| 1356 | // the full stack tests better. |
| 1357 | transport.SetReceiver(call->Receiver()); |
| 1358 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1359 | VideoReceiveStream* video_receive_stream = nullptr; |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1360 | FlexfecReceiveStream* flexfec_receive_stream = nullptr; |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1361 | std::unique_ptr<test::VideoRenderer> local_preview; |
| 1362 | std::unique_ptr<test::VideoRenderer> loopback_video; |
| 1363 | if (params_.video.enabled) { |
| 1364 | // Create video renderers. |
| 1365 | local_preview.reset(test::VideoRenderer::Create( |
| 1366 | "Local Preview", params_.video.width, params_.video.height)); |
ivica | 87f83a9 | 2015-10-08 12:13:32 | [diff] [blame] | 1367 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1368 | size_t stream_id = params_.ss.selected_stream; |
| 1369 | std::string title = "Loopback Video"; |
| 1370 | if (params_.ss.streams.size() > 1) { |
| 1371 | std::ostringstream s; |
| 1372 | s << stream_id; |
| 1373 | title += " - Stream #" + s.str(); |
| 1374 | } |
sprang | ce4aef1 | 2015-11-02 15:23:20 | [diff] [blame] | 1375 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1376 | loopback_video.reset(test::VideoRenderer::Create( |
| 1377 | title.c_str(), params_.ss.streams[stream_id].width, |
| 1378 | params_.ss.streams[stream_id].height)); |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 1379 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1380 | SetupVideo(&transport, &transport); |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1381 | video_send_config_.pre_encode_callback = local_preview.get(); |
| 1382 | video_receive_configs_[stream_id].renderer = loopback_video.get(); |
| 1383 | if (params_.audio.enabled && params_.audio.sync_video) |
| 1384 | video_receive_configs_[stream_id].sync_group = kSyncGroup; |
| 1385 | |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1386 | if (params_.screenshare.enabled) |
| 1387 | SetupScreenshare(); |
| 1388 | |
| 1389 | video_send_stream_ = call->CreateVideoSendStream( |
| 1390 | video_send_config_.Copy(), video_encoder_config_.Copy()); |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1391 | if (params_.video.flexfec) { |
| 1392 | RTC_DCHECK(!flexfec_receive_configs_.empty()); |
| 1393 | flexfec_receive_stream = |
| 1394 | call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]); |
| 1395 | } |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1396 | video_receive_stream = call->CreateVideoReceiveStream( |
| 1397 | video_receive_configs_[stream_id].Copy()); |
| 1398 | CreateCapturer(); |
perkj | 803d97f | 2016-11-01 18:45:46 | [diff] [blame] | 1399 | video_send_stream_->SetSource( |
| 1400 | video_capturer_.get(), |
| 1401 | VideoSendStream::DegradationPreference::kBalanced); |
philipel | 274c1dc | 2016-05-04 13:21:01 | [diff] [blame] | 1402 | } |
| 1403 | |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1404 | AudioReceiveStream* audio_receive_stream = nullptr; |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1405 | if (params_.audio.enabled) { |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1406 | SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(), |
| 1407 | &transport, &audio_receive_stream); |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1408 | } |
| 1409 | |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1410 | StartEncodedFrameLogs(video_receive_stream); |
| 1411 | StartEncodedFrameLogs(video_send_stream_); |
| 1412 | |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1413 | // Start sending and receiving video. |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1414 | if (params_.video.enabled) { |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1415 | if (flexfec_receive_stream) |
| 1416 | flexfec_receive_stream->Start(); |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1417 | video_receive_stream->Start(); |
| 1418 | video_send_stream_->Start(); |
| 1419 | video_capturer_->Start(); |
| 1420 | } |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1421 | |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1422 | if (params_.audio.enabled) { |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1423 | // Start receiving audio. |
| 1424 | audio_receive_stream->Start(); |
| 1425 | EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id)); |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1426 | |
| 1427 | // Start sending audio. |
| 1428 | audio_send_stream_->Start(); |
| 1429 | EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id)); |
| 1430 | } |
| 1431 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1432 | test::PressEnterToContinue(); |
| 1433 | |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1434 | if (params_.audio.enabled) { |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1435 | // Stop sending audio. |
| 1436 | EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id)); |
| 1437 | audio_send_stream_->Stop(); |
| 1438 | |
| 1439 | // Stop receiving audio. |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1440 | EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id)); |
| 1441 | audio_receive_stream->Stop(); |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1442 | call->DestroyAudioSendStream(audio_send_stream_); |
| 1443 | call->DestroyAudioReceiveStream(audio_receive_stream); |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | // Stop receiving and sending video. |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1447 | if (params_.video.enabled) { |
| 1448 | video_capturer_->Stop(); |
| 1449 | video_send_stream_->Stop(); |
| 1450 | video_receive_stream->Stop(); |
brandtr | 1293aca | 2016-11-17 06:47:29 | [diff] [blame^] | 1451 | if (flexfec_receive_stream) { |
| 1452 | flexfec_receive_stream->Stop(); |
| 1453 | call->DestroyFlexfecReceiveStream(flexfec_receive_stream); |
| 1454 | } |
minyue | a27172d | 2016-11-01 12:59:29 | [diff] [blame] | 1455 | call->DestroyVideoReceiveStream(video_receive_stream); |
| 1456 | call->DestroyVideoSendStream(video_send_stream_); |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1457 | } |
| 1458 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1459 | transport.StopSending(); |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1460 | if (params_.audio.enabled) |
minyue | 7320866 | 2016-08-18 13:28:55 | [diff] [blame] | 1461 | DestroyVoiceEngine(&voe); |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1462 | } |
| 1463 | |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1464 | void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) { |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1465 | if (!params_.video.encoded_frame_base_path.empty()) { |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1466 | std::ostringstream str; |
| 1467 | str << send_logs_++; |
| 1468 | std::string prefix = |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1469 | params_.video.encoded_frame_base_path + "." + str.str() + ".send."; |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1470 | stream->EnableEncodedFrameRecording( |
| 1471 | std::vector<rtc::PlatformFile>( |
| 1472 | {rtc::CreatePlatformFile(prefix + "1.ivf"), |
| 1473 | rtc::CreatePlatformFile(prefix + "2.ivf"), |
| 1474 | rtc::CreatePlatformFile(prefix + "3.ivf")}), |
| 1475 | 10000000); |
| 1476 | } |
| 1477 | } |
| 1478 | void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) { |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1479 | if (!params_.video.encoded_frame_base_path.empty()) { |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1480 | std::ostringstream str; |
| 1481 | str << receive_logs_++; |
| 1482 | std::string path = |
minyue | 626bc95 | 2016-10-31 12:47:02 | [diff] [blame] | 1483 | params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf"; |
palmkvist | e75f204 | 2016-09-28 13:19:48 | [diff] [blame] | 1484 | stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path), |
| 1485 | 10000000); |
| 1486 | } |
| 1487 | } |
| 1488 | |
ivica | 5d6a06c | 2015-09-17 12:30:24 | [diff] [blame] | 1489 | } // namespace webrtc |