DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/*
/*
 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
 */
#include "media/engine/internalencoderfactory.h"
#include "media/engine/internalencoderfactory.h"
#include "media/engine/simulcast_encoder_adapter.h"
#include "modules/rtp_rtcp/source/rtp_format.h"
#include "rtc_base/numerics/sequence_number_util.h"
#include "test/call_test.h"
#include "test/field_trial.h"
#include "test/field_trial.h"

namespace webrtc {
namespace {
const int kFrameMaxWidth = 1280;
const int kFrameMaxHeight = 720;
const int kFrameMaxHeight = 720;
const int kFrameRate = 30;
const int kMaxSecondsLost = 5;
const int kMaxFramesLost = kFrameRate * kMaxSecondsLost;
const int kMinPacketsToObserve = 10;
const int kEncoderBitrateBps = 100000;
const int kEncoderBitrateBps = 100000;
const uint32_t kPictureIdWraparound = (1 << 15);

const char kVp8ForcedFallbackEncoderEnabled[] =
const char kVp8ForcedFallbackEncoderEnabled[] =
    "WebRTC-VP8-Forced-Fallback-Encoder-v2/Enabled/";
}  // namespace

class PictureIdObserver : public test::RtpRtcpObserver {
 public:
 public:
  PictureIdObserver()
      : test::RtpRtcpObserver(test::CallTest::kDefaultTimeoutMs),
        max_expected_picture_id_gap_(0),
        num_ssrcs_to_observe_(1) {}


  void SetExpectedSsrcs(size_t num_expected_ssrcs) {
    rtc::CritScope lock(&crit_);
    num_ssrcs_to_observe_ = num_expected_ssrcs;
  }


  void ResetObservedSsrcs() {
    rtc::CritScope lock(&crit_);
    // Do not clear the timestamp and picture_id, to ensure that we check
    // consistency between reinits and recreations.
    num_packets_sent_.clear();
    observed_ssrcs_.clear();
  }

  void SetMaxExpectedPictureIdGap(int max_expected_picture_id_gap) {
  void SetMaxExpectedPictureIdGap(int max_expected_picture_id_gap) {
    rtc::CritScope lock(&crit_);
    max_expected_picture_id_gap_ = max_expected_picture_id_gap;
  }

 private:
 private:
  Action OnSendRtp(const uint8_t* packet, size_t length) override {
    rtc::CritScope lock(&crit_);

    // RTP header.
    RTPHeader header;
    RTPHeader header;
    EXPECT_TRUE(parser_->Parse(packet, length, &header));
    const uint32_t timestamp = header.timestamp;
    const uint32_t timestamp = header.timestamp;
    const uint32_t ssrc = header.ssrc;

    const bool known_ssrc = (ssrc == test::CallTest::kVideoSendSsrcs[0] ||
                             ssrc == test::CallTest::kVideoSendSsrcs[1] ||
                             ssrc == test::CallTest::kVideoSendSsrcs[2]);
                             ssrc == test::CallTest::kVideoSendSsrcs[2]);
    EXPECT_TRUE(known_ssrc) << "Unknown SSRC sent.";

    const bool is_padding =
        (length == header.headerLength + header.paddingLength);
    if (is_padding) {
    if (is_padding) {
      return SEND_PACKET;
    }

    // VP8 header.
    std::unique_ptr<RtpDepacketizer> depacketizer(
    std::unique_ptr<RtpDepacketizer> depacketizer(
        RtpDepacketizer::Create(kRtpVideoVp8));
    RtpDepacketizer::ParsedPayload parsed_payload;
    EXPECT_TRUE(depacketizer->Parse(
        &parsed_payload, &packet[header.headerLength],
        length - header.headerLength - header.paddingLength));
        length - header.headerLength - header.paddingLength));
    const uint16_t picture_id =
        parsed_payload.type.Video.codecHeader.VP8.pictureId;

    // If this is the first packet, we have nothing to compare to.
    if (last_observed_timestamp_.find(ssrc) == last_observed_timestamp_.end()) {
    if (last_observed_timestamp_.find(ssrc) == last_observed_timestamp_.end()) {
      last_observed_timestamp_[ssrc] = timestamp;
      last_observed_picture_id_[ssrc] = picture_id;
      ++num_packets_sent_[ssrc];

      return SEND_PACKET;
    }


    // Verify continuity and monotonicity of picture_id sequence.
    if (last_observed_timestamp_[ssrc] == timestamp) {
      // Packet belongs to same frame as before.
      EXPECT_EQ(last_observed_picture_id_[ssrc], picture_id);
    } else {
    } else {
      // Packet is a new frame.

      // Picture id should be increasing.
      const bool picture_id_is_increasing =
          AheadOf<uint16_t, kPictureIdWraparound>(
          AheadOf<uint16_t, kPictureIdWraparound>(
              picture_id, last_observed_picture_id_[ssrc]);
      EXPECT_TRUE(picture_id_is_increasing);

      // Picture id should not increase more than expected.
      const int picture_id_diff = ForwardDiff<uint16_t, kPictureIdWraparound>(
      const int picture_id_diff = ForwardDiff<uint16_t, kPictureIdWraparound>(
          last_observed_picture_id_[ssrc], picture_id);

      // For delta frames, expect continuously increasing picture id.
      if (parsed_payload.frame_type != kVideoFrameKey) {
        EXPECT_EQ(picture_id_diff, 1);
        EXPECT_EQ(picture_id_diff, 1);
      }
      // Any frames still in queue is lost when a VideoSendStream is destroyed.
      // The first frame after recreation should be a key frame.
      if (picture_id_diff > 1) {
        EXPECT_EQ(kVideoFrameKey, parsed_payload.frame_type);
        EXPECT_EQ(kVideoFrameKey, parsed_payload.frame_type);
        EXPECT_LE(picture_id_diff - 1, max_expected_picture_id_gap_);
      }
    }
    last_observed_timestamp_[ssrc] = timestamp;
    last_observed_picture_id_[ssrc] = picture_id;

    // Pass the test when enough media packets have been received
    // Pass the test when enough media packets have been received
    // on all streams.
    if (++num_packets_sent_[ssrc] >= kMinPacketsToObserve &&
        observed_ssrcs_.find(ssrc) == observed_ssrcs_.end()) {
      observed_ssrcs_.insert(ssrc);
      if (observed_ssrcs_.size() == num_ssrcs_to_observe_) {
      if (observed_ssrcs_.size() == num_ssrcs_to_observe_) {
        observation_complete_.Set();
      }
    }

    return SEND_PACKET;
  }
  }

  rtc::CriticalSection crit_;
  std::map<uint32_t, uint32_t> last_observed_timestamp_ RTC_GUARDED_BY(crit_);
  std::map<uint32_t, uint16_t> last_observed_picture_id_ RTC_GUARDED_BY(crit_);
  std::map<uint32_t, size_t> num_packets_sent_ RTC_GUARDED_BY(crit_);
  std::map<uint32_t, size_t> num_packets_sent_ RTC_GUARDED_BY(crit_);
  int max_expected_picture_id_gap_ RTC_GUARDED_BY(crit_);
  size_t num_ssrcs_to_observe_ RTC_GUARDED_BY(crit_);
  std::set<uint32_t> observed_ssrcs_ RTC_GUARDED_BY(crit_);
};


class PictureIdTest : public test::CallTest,
                      public ::testing::WithParamInterface<std::string> {
 public:
  PictureIdTest() : scoped_field_trial_(GetParam()) {}


  virtual ~PictureIdTest() {
    EXPECT_EQ(nullptr, video_send_stream_);
    EXPECT_TRUE(video_receive_streams_.empty());

    task_queue_.SendTask([this]() {
    task_queue_.SendTask([this]() {
      Stop();
      DestroyStreams();
      send_transport_.reset();
      receive_transport_.reset();
      receive_transport_.reset();
      DestroyCalls();
    });
  }

  void SetupEncoder(VideoEncoder* encoder);
  void SetupEncoder(VideoEncoder* encoder);
  void TestPictureIdContinuousAfterReconfigure(
      const std::vector<int>& ssrc_counts);
  void TestPictureIdIncreaseAfterRecreateStreams(
      const std::vector<int>& ssrc_counts);

 private:
 private:
  test::ScopedFieldTrials scoped_field_trial_;
  PictureIdObserver observer;
};

INSTANTIATE_TEST_CASE_P(TestWithForcedFallbackEncoderEnabled,
INSTANTIATE_TEST_CASE_P(TestWithForcedFallbackEncoderEnabled,
                        PictureIdTest,
                        ::testing::Values(kVp8ForcedFallbackEncoderEnabled,
                                          ""));

// Use a special stream factory to ensure that all simulcast streams are being
// Use a special stream factory to ensure that all simulcast streams are being
// sent.
class VideoStreamFactory
    : public VideoEncoderConfig::VideoStreamFactoryInterface {
 public:
  VideoStreamFactory() = default;
  VideoStreamFactory() = default;

 private:
  std::vector<VideoStream> CreateEncoderStreams(
      int width,
      int height,
      int height,
      const VideoEncoderConfig& encoder_config) override {
    std::vector<VideoStream> streams =
        test::CreateVideoStreams(width, height, encoder_config);


    if (encoder_config.number_of_streams > 1) {
      RTC_DCHECK_EQ(3, encoder_config.number_of_streams);

      for (size_t i = 0; i < encoder_config.number_of_streams; ++i) {
        streams[i].min_bitrate_bps = kEncoderBitrateBps;
        streams[i].min_bitrate_bps = kEncoderBitrateBps;
        streams[i].target_bitrate_bps = kEncoderBitrateBps;
        streams[i].max_bitrate_bps = kEncoderBitrateBps;
      }

      // test::CreateVideoStreams does not return frame sizes for the lower
      // test::CreateVideoStreams does not return frame sizes for the lower
      // streams that are accepted by VP8Impl::InitEncode.
      // streams that are accepted by VP8Impl::InitEncode.
      // TODO(brandtr): Fix the problem in test::CreateVideoStreams, rather
      // than overriding the values here.
      streams[1].width = streams[2].width / 2;
      streams[1].height = streams[2].height / 2;
      streams[0].width = streams[1].width / 2;
      streams[0].height = streams[1].height / 2;
    } else {
    } else {
      // Use the same total bitrates when sending a single stream to avoid
      // lowering the bitrate estimate and requiring a subsequent rampup.
      streams[0].min_bitrate_bps = 3 * kEncoderBitrateBps;
      streams[0].target_bitrate_bps = 3 * kEncoderBitrateBps;
      streams[0].max_bitrate_bps = 3 * kEncoderBitrateBps;
      streams[0].max_bitrate_bps = 3 * kEncoderBitrateBps;
    }

    return streams;
  }
};
};

void PictureIdTest::SetupEncoder(VideoEncoder* encoder) {
  task_queue_.SendTask([this, &encoder]() {
    Call::Config config(event_log_.get());
    CreateCalls(config, config);
    CreateCalls(config, config);

    send_transport_.reset(new test::PacketTransport(
        &task_queue_, sender_call_.get(), &observer,
        test::PacketTransport::kSender, payload_type_map_,
        FakeNetworkPipe::Config()));
        FakeNetworkPipe::Config()));

    CreateSendConfig(kNumSsrcs, 0, 0, send_transport_.get());
    video_send_config_.encoder_settings.encoder = encoder;
    video_send_config_.encoder_settings.payload_name = "VP8";
    video_encoder_config_.video_stream_factory =
        new rtc::RefCountedObject<VideoStreamFactory>();
    video_encoder_config_.number_of_streams = 1;
  });
}
}

void PictureIdTest::TestPictureIdContinuousAfterReconfigure(
    const std::vector<int>& ssrc_counts) {
  task_queue_.SendTask([this]() {
    CreateVideoStreams();
    CreateVideoStreams();
    CreateFrameGeneratorCapturer(kFrameRate, kFrameMaxWidth, kFrameMaxHeight);

    // Initial test with a single stream.
    Start();
  });
  });

  EXPECT_TRUE(observer.Wait()) << "Timed out waiting for packets.";

  // Reconfigure VideoEncoder and test picture id increase.
  // Expect continuously increasing picture id, equivalent to no gaps.
  // Expect continuously increasing picture id, equivalent to no gaps.
  observer.SetMaxExpectedPictureIdGap(0);
  for (int ssrc_count : ssrc_counts) {
    video_encoder_config_.number_of_streams = ssrc_count;
    observer.SetExpectedSsrcs(ssrc_count);
    observer.ResetObservedSsrcs();
    observer.ResetObservedSsrcs();
    // Make sure the picture_id sequence is continuous on reinit and recreate.
    task_queue_.SendTask([this]() {
      video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_.Copy());
    });
    });
    EXPECT_TRUE(observer.Wait()) << "Timed out waiting for packets.";
  }

  task_queue_.SendTask([this]() {
    Stop();
    Stop();
    DestroyStreams();
    send_transport_.reset();
    receive_transport_.reset();
    DestroyCalls();
  });
  });
}

void PictureIdTest::TestPictureIdIncreaseAfterRecreateStreams(
    const std::vector<int>& ssrc_counts) {
    const std::vector<int>& ssrc_counts) {
  task_queue_.SendTask([this]() {
    CreateVideoStreams();
    CreateFrameGeneratorCapturer(kFrameRate, kFrameMaxWidth, kFrameMaxHeight);

    // Initial test with a single stream.
    // Initial test with a single stream.
    Start();
  });

  EXPECT_TRUE(observer.Wait()) << "Timed out waiting for packets.";


  // Recreate VideoSendStream and test picture id increase.
  // When the VideoSendStream is destroyed, any frames still in queue is lost
  // with it, therefore it is expected that some frames might be lost.
  observer.SetMaxExpectedPictureIdGap(kMaxFramesLost);
  for (int ssrc_count : ssrc_counts) {
  for (int ssrc_count : ssrc_counts) {
    task_queue_.SendTask([this, &ssrc_count]() {
      video_encoder_config_.number_of_streams = ssrc_count;

      frame_generator_capturer_->Stop();
      sender_call_->DestroyVideoSendStream(video_send_stream_);
      sender_call_->DestroyVideoSendStream(video_send_stream_);

      observer.SetExpectedSsrcs(ssrc_count);
      observer.ResetObservedSsrcs();

      video_send_stream_ = sender_call_->CreateVideoSendStream(
      video_send_stream_ = sender_call_->CreateVideoSendStream(
          video_send_config_.Copy(), video_encoder_config_.Copy());
      video_send_stream_->Start();
      CreateFrameGeneratorCapturer(kFrameRate, kFrameMaxWidth, kFrameMaxHeight);
      frame_generator_capturer_->Start();
    });
    });

    EXPECT_TRUE(observer.Wait()) << "Timed out waiting for packets.";
  }

  task_queue_.SendTask([this]() {
    Stop();
    DestroyStreams();
    send_transport_.reset();
    receive_transport_.reset();
  });
}
}

TEST_P(PictureIdTest, PictureIdContinuousAfterReconfigureVp8) {
  std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
  SetupEncoder(encoder.get());
  SetupEncoder(encoder.get());
  TestPictureIdContinuousAfterReconfigure({1, 3, 3, 1, 1});
}

TEST_P(PictureIdTest, PictureIdIncreasingAfterRecreateStreamVp8) {
  std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
  std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
  SetupEncoder(encoder.get());
  TestPictureIdIncreaseAfterRecreateStreams({1, 3, 3, 1, 1});
}

TEST_P(PictureIdTest, PictureIdIncreasingAfterStreamCountChangeVp8) {
TEST_P(PictureIdTest, PictureIdIncreasingAfterStreamCountChangeVp8) {
  std::unique_ptr<VideoEncoder> encoder(VP8Encoder::Create());
  // Make sure that that the picture id is not reset if the stream count goes
  // down and then up.
  std::vector<int> ssrc_counts = {3, 1, 3};
  SetupEncoder(encoder.get());
  SetupEncoder(encoder.get());
  TestPictureIdContinuousAfterReconfigure(ssrc_counts);
}

TEST_P(PictureIdTest,
       PictureIdContinuousAfterReconfigureSimulcastEncoderAdapter) {
       PictureIdContinuousAfterReconfigureSimulcastEncoderAdapter) {
  InternalEncoderFactory internal_encoder_factory;
  SimulcastEncoderAdapter simulcast_encoder_adapter(&internal_encoder_factory);
  SetupEncoder(&simulcast_encoder_adapter);
  TestPictureIdContinuousAfterReconfigure({1, 3, 3, 1, 1});
}
}

TEST_P(PictureIdTest,
       PictureIdIncreasingAfterRecreateStreamSimulcastEncoderAdapter) {
  InternalEncoderFactory internal_encoder_factory;
  SimulcastEncoderAdapter simulcast_encoder_adapter(&internal_encoder_factory);
  SimulcastEncoderAdapter simulcast_encoder_adapter(&internal_encoder_factory);
  SetupEncoder(&simulcast_encoder_adapter);
  TestPictureIdIncreaseAfterRecreateStreams({1, 3, 3, 1, 1});
}
}

// When using the simulcast encoder adapter, the picture id is randomly set
// when the ssrc count is reduced and then increased. This means that we are
// not spec compliant in that particular case.
TEST_P(PictureIdTest,
TEST_P(PictureIdTest,
       PictureIdIncreasingAfterStreamCountChangeSimulcastEncoderAdapter) {
  // If forced fallback is enabled, the picture id is set in the PayloadRouter
  // and the sequence should be continuous.
  if (GetParam() == kVp8ForcedFallbackEncoderEnabled) {
    InternalEncoderFactory internal_encoder_factory;
    InternalEncoderFactory internal_encoder_factory;
    SimulcastEncoderAdapter simulcast_encoder_adapter(
        &internal_encoder_factory);
    // Make sure that that the picture id is not reset if the stream count goes
    // Make sure that that the picture id is not reset if the stream count goes
    // down and then up.
    // down and then up.
    std::vector<int> ssrc_counts = {3, 1, 3};
    SetupEncoder(&simulcast_encoder_adapter);
    TestPictureIdContinuousAfterReconfigure(ssrc_counts);
    TestPictureIdContinuousAfterReconfigure(ssrc_counts);
  }
}

}  // namespace webrtc