2011-10-06 06:44:54 +00:00
|
|
|
/*
|
2012-02-09 09:01:51 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-10-06 06:44:54 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/video_coding/codecs/test/packet_manipulator.h"
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/format_macros.h"
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
PacketManipulatorImpl::PacketManipulatorImpl(PacketReader* packet_reader,
|
2011-12-08 07:42:18 +00:00
|
|
|
const NetworkingConfig& config,
|
|
|
|
|
bool verbose)
|
2011-10-06 06:44:54 +00:00
|
|
|
: packet_reader_(packet_reader),
|
|
|
|
|
config_(config),
|
2011-12-08 07:42:18 +00:00
|
|
|
active_burst_packets_(0),
|
2012-02-09 09:01:51 +00:00
|
|
|
random_seed_(1),
|
2011-12-08 07:42:18 +00:00
|
|
|
verbose_(verbose) {
|
2011-10-06 06:44:54 +00:00
|
|
|
assert(packet_reader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PacketManipulatorImpl::ManipulatePackets(
|
|
|
|
|
webrtc::EncodedImage* encoded_image) {
|
|
|
|
|
int nbr_packets_dropped = 0;
|
|
|
|
|
// There's no need to build a copy of the image data since viewing an
|
|
|
|
|
// EncodedImage object, setting the length to a new lower value represents
|
|
|
|
|
// that everything is dropped after that position in the byte array.
|
|
|
|
|
// EncodedImage._size is the allocated bytes.
|
|
|
|
|
// EncodedImage._length is how many that are filled with data.
|
|
|
|
|
int new_length = 0;
|
|
|
|
|
packet_reader_->InitializeReading(encoded_image->_buffer,
|
|
|
|
|
encoded_image->_length,
|
|
|
|
|
config_.packet_size_in_bytes);
|
2013-04-08 11:13:29 +00:00
|
|
|
uint8_t* packet = NULL;
|
2011-10-06 06:44:54 +00:00
|
|
|
int nbr_bytes_to_read;
|
|
|
|
|
// keep track of if we've lost any packets, since then we shall loose
|
|
|
|
|
// the remains of the current frame:
|
|
|
|
|
bool packet_loss_has_occurred = false;
|
|
|
|
|
while ((nbr_bytes_to_read = packet_reader_->NextPacket(&packet)) > 0) {
|
|
|
|
|
// Check if we're currently in a packet loss burst that is not completed:
|
|
|
|
|
if (active_burst_packets_ > 0) {
|
|
|
|
|
active_burst_packets_--;
|
|
|
|
|
nbr_packets_dropped++;
|
|
|
|
|
} else if (RandomUniform() < config_.packet_loss_probability ||
|
2015-12-21 03:04:49 -08:00
|
|
|
packet_loss_has_occurred) {
|
2011-10-06 06:44:54 +00:00
|
|
|
packet_loss_has_occurred = true;
|
|
|
|
|
nbr_packets_dropped++;
|
|
|
|
|
if (config_.packet_loss_mode == kBurst) {
|
|
|
|
|
// Initiate a new burst
|
|
|
|
|
active_burst_packets_ = config_.packet_loss_burst_length - 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
new_length += nbr_bytes_to_read;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
encoded_image->_length = new_length;
|
|
|
|
|
if (nbr_packets_dropped > 0) {
|
|
|
|
|
// Must set completeFrame to false to inform the decoder about this:
|
|
|
|
|
encoded_image->_completeFrame = false;
|
2011-12-08 07:42:18 +00:00
|
|
|
if (verbose_) {
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
printf("Dropped %d packets for frame %d (frame length: %" PRIuS ")\n",
|
2011-12-08 07:42:18 +00:00
|
|
|
nbr_packets_dropped, encoded_image->_timeStamp,
|
|
|
|
|
encoded_image->_length);
|
|
|
|
|
}
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
return nbr_packets_dropped;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-09 09:01:51 +00:00
|
|
|
void PacketManipulatorImpl::InitializeRandomSeed(unsigned int seed) {
|
|
|
|
|
random_seed_ = seed;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-02 16:34:52 +00:00
|
|
|
inline double PacketManipulatorImpl::RandomUniform() {
|
2012-02-09 09:01:51 +00:00
|
|
|
// Use the previous result as new seed before each rand() call. Doing this
|
|
|
|
|
// it doesn't matter if other threads are calling rand() since we'll always
|
|
|
|
|
// get the same behavior as long as we're using a fixed initial seed.
|
2017-03-31 02:03:55 -07:00
|
|
|
critsect_.Enter();
|
2012-02-09 09:01:51 +00:00
|
|
|
srand(random_seed_);
|
2015-12-21 03:04:49 -08:00
|
|
|
random_seed_ = rand(); // NOLINT (rand_r instead of rand)
|
2017-03-31 02:03:55 -07:00
|
|
|
critsect_.Leave();
|
2015-12-21 03:04:49 -08:00
|
|
|
return (random_seed_ + 1.0) / (RAND_MAX + 1.0);
|
2011-11-02 16:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:50:22 +00:00
|
|
|
const char* PacketLossModeToStr(PacketLossMode e) {
|
|
|
|
|
switch (e) {
|
|
|
|
|
case kUniform:
|
|
|
|
|
return "Uniform";
|
|
|
|
|
case kBurst:
|
|
|
|
|
return "Burst";
|
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
2011-11-02 16:34:52 +00:00
|
|
|
return "Unknown";
|
2011-10-07 06:50:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
} // namespace test
|
2015-12-21 03:04:49 -08:00
|
|
|
} // namespace webrtc
|