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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
/*
* Copyright (c) 2012 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
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/video_coding/main/test/generic_codec_test.h"
#include <math.h>
#include <stdio.h>
#include "webrtc/common_video/interface/i420_video_frame.h"
#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
#include "webrtc/modules/video_coding/main/interface/video_coding.h"
#include "webrtc/modules/video_coding/main/test/test_macros.h"
#include "webrtc/system_wrappers/interface/clock.h"
#include "webrtc/test/testsupport/fileutils.h"
using namespace webrtc;
enum { kMaxWaitEncTimeMs = 100 };
int GenericCodecTest::RunTest(CmdArgs& args)
{
SimulatedClock clock(0);
NullEventFactory event_factory;
VideoCodingModule* vcm = VideoCodingModule::Create(&clock, &event_factory);
GenericCodecTest* get = new GenericCodecTest(vcm, &clock);
Trace::CreateTrace();
Trace::SetTraceFile(
(test::OutputPath() + "genericCodecTestTrace.txt").c_str());
Trace::set_level_filter(webrtc::kTraceAll);
get->Perform(args);
Trace::ReturnTrace();
delete get;
VideoCodingModule::Destroy(vcm);
return 0;
}
GenericCodecTest::GenericCodecTest(VideoCodingModule* vcm,
SimulatedClock* clock):
_clock(clock),
_vcm(vcm),
_width(0),
_height(0),
_frameRate(0),
_lengthSourceFrame(0),
_timeStamp(0)
{
}
GenericCodecTest::~GenericCodecTest()
{
}
void
GenericCodecTest::Setup(CmdArgs& args)
{
_timeStamp = 0;
/* Test Sequence parameters */
_inname= args.inputFile;
if (args.outputFile.compare(""))
_outname = test::OutputPath() + "GCTest_decoded.yuv";
else
_outname = args.outputFile;
_encodedName = test::OutputPath() + "GCTest_encoded.vp8";
_width = args.width;
_height = args.height;
_frameRate = args.frameRate;
_lengthSourceFrame = 3*_width*_height/2;
/* File settings */
if ((_sourceFile = fopen(_inname.c_str(), "rb")) == NULL)
{
printf("Cannot read file %s.\n", _inname.c_str());
exit(1);
}
if ((_encodedFile = fopen(_encodedName.c_str(), "wb")) == NULL)
{
printf("Cannot write encoded file.\n");
exit(1);
}
if ((_decodedFile = fopen(_outname.c_str(), "wb")) == NULL)
{
printf("Cannot write file %s.\n", _outname.c_str());
exit(1);
}
return;
}
int32_t
GenericCodecTest::Perform(CmdArgs& args)
{
int32_t ret;
Setup(args);
/*
1. sanity checks
2. encode/decoder individuality
3. API testing
4. Target bitrate (within a specific timespan)
5. Pipeline Delay
*/
/*******************************/
/* sanity checks on inputs */
/*****************************/
VideoCodec sendCodec, receiveCodec;
sendCodec.maxBitrate = 8000;
TEST(_vcm->NumberOfCodecs() > 0); // This works since we now initialize the list in the constructor
TEST(_vcm->Codec(0, &sendCodec) == VCM_OK);
_vcm->InitializeSender();
_vcm->InitializeReceiver();
int32_t NumberOfCodecs = _vcm->NumberOfCodecs();
// registration of first codec in the list
int i = 0;
_vcm->Codec(0, &_sendCodec);
TEST(_vcm->RegisterSendCodec(&_sendCodec, 4, 1440) == VCM_OK);
// sanity on encoder registration
I420VideoFrame sourceFrame;
_vcm->InitializeSender();
TEST(_vcm->Codec(kVideoCodecVP8, &sendCodec) == 0);
sendCodec.maxBitrate = 8000;
_vcm->RegisterSendCodec(&sendCodec, 1, 1440);
_vcm->InitializeSender();
_vcm->Codec(kVideoCodecVP8, &sendCodec);
sendCodec.height = 0;
TEST(_vcm->RegisterSendCodec(&sendCodec, 1, 1440) < 0); // bad height
_vcm->Codec(kVideoCodecVP8, &sendCodec);
_vcm->Codec(kVideoCodecVP8, &sendCodec);
_vcm->InitializeSender();
// Setting rate when encoder uninitialized.
TEST(_vcm->SetChannelParameters(100000, 0, 0) < 0);
// register all availbale decoders -- need to have more for this test
for (i=0; i< NumberOfCodecs; i++)
{
_vcm->Codec(i, &receiveCodec);
_vcm->RegisterReceiveCodec(&receiveCodec, 1);
}
uint8_t* tmpBuffer = new uint8_t[_lengthSourceFrame];
TEST(fread(tmpBuffer, 1, _lengthSourceFrame, _sourceFile) > 0);
int half_width = (_width + 1) / 2;
int half_height = (_height + 1) / 2;
int size_y = _width * _height;
int size_uv = half_width * half_height;
sourceFrame.CreateFrame(size_y, tmpBuffer,
size_uv, tmpBuffer + size_y,
size_uv, tmpBuffer + size_y + size_uv,
_width, _height,
_width, half_width, half_width);
sourceFrame.set_timestamp(_timeStamp++);
TEST(_vcm->AddVideoFrame(sourceFrame) < 0 ); // encoder uninitialized
_vcm->InitializeReceiver();
// Setting rtt when receiver uninitialized.
TEST(_vcm->SetChannelParameters(100000, 0, 0) < 0);
/**************************************/
/* encoder/decoder individuality test */
/**************************************/
//Register both encoder and decoder, reset decoder - encode, set up decoder, reset encoder - decode.
rewind(_sourceFile);
_vcm->InitializeReceiver();
_vcm->InitializeSender();
NumberOfCodecs = _vcm->NumberOfCodecs();
// Register VP8
_vcm->Codec(kVideoCodecVP8, &_sendCodec);
_vcm->RegisterSendCodec(&_sendCodec, 4, 1440);
_vcm->SendCodec(&sendCodec);
sendCodec.startBitrate = 2000;
// Set target frame rate to half of the incoming frame rate
// to test the frame rate control in the VCM
sendCodec.maxFramerate = (uint8_t)(_frameRate / 2);
sendCodec.width = _width;
sendCodec.height = _height;
TEST(strncmp(_sendCodec.plName, "VP8", 3) == 0); // was VP8
_decodeCallback = new VCMDecodeCompleteCallback(_decodedFile);
_encodeCompleteCallback = new VCMEncodeCompleteCallback(_encodedFile);
_vcm->RegisterReceiveCallback(_decodeCallback);
_vcm->RegisterTransportCallback(_encodeCompleteCallback);
_encodeCompleteCallback->RegisterReceiverVCM(_vcm);
_vcm->RegisterSendCodec(&sendCodec, 4, 1440);
_encodeCompleteCallback->SetCodecType(ConvertCodecType(sendCodec.plName));
_vcm->InitializeReceiver();
_vcm->Process();
//encoding 1 second of video
for (i = 0; i < _frameRate; i++)
{
TEST(fread(tmpBuffer, 1, _lengthSourceFrame, _sourceFile) > 0);
sourceFrame.CreateFrame(size_y, tmpBuffer,
size_uv, tmpBuffer + size_y,
size_uv, tmpBuffer + size_y + size_uv,
_width, _height,
_width, half_width, half_width);
_timeStamp += (uint32_t)(9e4 / static_cast<float>(_frameRate));
sourceFrame.set_timestamp(_timeStamp);
TEST(_vcm->AddVideoFrame(sourceFrame) == VCM_OK);
IncrementDebugClock(_frameRate);
_vcm->Process();
}
sendCodec.maxFramerate = (uint8_t)_frameRate;
_vcm->InitializeSender();
TEST(_vcm->RegisterReceiveCodec(&sendCodec, 1) == VCM_OK); // same codec for encode and decode
ret = 0;
i = 0;
while ((i < 25) && (ret == 0) )
{
ret = _vcm->Decode();
TEST(ret == VCM_OK);
if (ret < 0)
{
printf("error in frame # %d \n", i);
}
IncrementDebugClock(_frameRate);
i++;
}
//TEST((ret == 0) && (i = 50));
if (ret == 0)
{
printf("Encoder/Decoder individuality test complete - View output files \n");
}
// last frame - not decoded
_vcm->InitializeReceiver();
TEST(_vcm->Decode() < 0); // frame to be encoded exists, decoder uninitialized
// Test key frame request on packet loss mode.
// This a frame as a key frame and fooling the receiver
// that the last packet was lost. The decoding will succeed,
// but the VCM will see a packet loss and request a new key frame.
VCMEncComplete_KeyReqTest keyReqTest_EncCompleteCallback(*_vcm);
KeyFrameReqTest frameTypeCallback;
_vcm->RegisterTransportCallback(&keyReqTest_EncCompleteCallback);
_encodeCompleteCallback->RegisterReceiverVCM(_vcm);
_vcm->RegisterSendCodec(&sendCodec, 4, 1440);
_encodeCompleteCallback->SetCodecType(ConvertCodecType(sendCodec.plName));
TEST(_vcm->SetVideoProtection(kProtectionKeyOnKeyLoss, true) == VCM_OK);
TEST(_vcm->RegisterFrameTypeCallback(&frameTypeCallback) == VCM_OK);
TEST(_vcm->RegisterReceiveCodec(&sendCodec, 1) == VCM_OK);
TEST(_vcm->AddVideoFrame(sourceFrame) == VCM_OK);
_timeStamp += (uint32_t)(9e4 / static_cast<float>(_frameRate));
sourceFrame.set_timestamp(_timeStamp);
// First packet of a subsequent frame required before the jitter buffer
// will allow decoding an incomplete frame.
TEST(_vcm->AddVideoFrame(sourceFrame) == VCM_OK);
TEST(_vcm->Decode() == VCM_OK);
printf("API tests complete \n");
/*******************/
/* Bit Rate Tests */
/*****************/
/* Requirements:
* 1. OneSecReq = 15 % above/below target over a time period of 1s (_frameRate number of frames)
* 3. FullReq = 10% for total seq. (for 300 frames/seq. coincides with #1)
* 4. Test will go over all registered codecs
//NOTE: time requirements are not part of the release tests
*/
double FullReq = 0.1;
//double OneSecReq = 0.15;
printf("\n RATE CONTROL TEST\n");
// initializing....
_vcm->InitializeSender();
_vcm->InitializeReceiver();
rewind(_sourceFile);
sourceFrame.CreateEmptyFrame(_width, _height, _width,
(_width + 1) / 2, (_width + 1) / 2);
const float bitRate[] = {100, 400, 600, 1000, 2000};
const float nBitrates = sizeof(bitRate)/sizeof(*bitRate);
float _bitRate = 0;
int _frameCnt = 0;
size_t totalBytesOneSec = 0;//, totalBytesTenSec;
size_t totalBytes;
float actualBitrate;
VCMFrameCount frameCount; // testing frame type counters
// start test
NumberOfCodecs = _vcm->NumberOfCodecs();
// going over all available codecs
_encodeCompleteCallback->SetFrameDimensions(_width, _height);
SendStatsTest sendStats;
for (int k = 0; k < NumberOfCodecs; k++)
//for (int k = NumberOfCodecs - 1; k >=0; k--)
{// static list starts from 0
//just checking
_vcm->InitializeSender();
_sendCodec.maxBitrate = 8000;
TEST(_vcm->Codec(k, &_sendCodec)== VCM_OK);
_vcm->RegisterSendCodec(&_sendCodec, 1, 1440);
_vcm->RegisterTransportCallback(_encodeCompleteCallback);
_encodeCompleteCallback->SetCodecType(ConvertCodecType(_sendCodec.plName));
printf (" \n\n Codec type = %s \n\n",_sendCodec.plName);
for (i = 0; i < nBitrates; i++)
{
_bitRate = static_cast<float>(bitRate[i]);
// just testing
_vcm->InitializeSender();
_sendCodec.startBitrate = (int)_bitRate;
_sendCodec.maxBitrate = 8000;
_sendCodec.maxFramerate = _frameRate;
_vcm->RegisterSendCodec(&_sendCodec, 1, 1440);
_vcm->RegisterTransportCallback(_encodeCompleteCallback);
// up to here
_vcm->SetChannelParameters(static_cast<uint32_t>(1000 * _bitRate),
0, 20);
_frameCnt = 0;
totalBytes = 0;
_encodeCompleteCallback->Initialize();
sendStats.set_framerate(static_cast<uint32_t>(_frameRate));
sendStats.set_bitrate(1000 * _bitRate);
_vcm->RegisterSendStatisticsCallback(&sendStats);
while (fread(tmpBuffer, 1, _lengthSourceFrame, _sourceFile) ==
_lengthSourceFrame)
{
_frameCnt++;
sourceFrame.CreateFrame(size_y, tmpBuffer,
size_uv, tmpBuffer + size_y,
size_uv, tmpBuffer + size_y + size_uv,
_width, _height,
_width, (_width + 1) / 2,
(_width + 1) / 2);
_timeStamp += (uint32_t)(9e4 / static_cast<float>(_frameRate));
sourceFrame.set_timestamp(_timeStamp);
ret = _vcm->AddVideoFrame(sourceFrame);
IncrementDebugClock(_frameRate);
// The following should be uncommneted for timing tests. Release tests only include
// compliance with full sequence bit rate.
if (_frameCnt == _frameRate)// @ 1sec
{
totalBytesOneSec = _encodeCompleteCallback->EncodedBytes();//totalBytes;
}
TEST(_vcm->TimeUntilNextProcess() >= 0);
} // video seq. encode done
TEST(_vcm->TimeUntilNextProcess() == 0);
_vcm->Process(); // Let the module calculate its send bit rate estimate
// estimating rates
// complete sequence
// bit rate assumes input frame rate is as specified
totalBytes = _encodeCompleteCallback->EncodedBytes();
actualBitrate = (float)(8.0/1000)*(totalBytes / (_frameCnt / _frameRate));
printf("Complete Seq.: target bitrate: %.0f kbps, actual bitrate: %.1f kbps\n", _bitRate, actualBitrate);
TEST((fabs(actualBitrate - _bitRate) < FullReq * _bitRate) ||
(strncmp(_sendCodec.plName, "I420", 4) == 0));
// 1 Sec.
actualBitrate = (float)(8.0/1000)*(totalBytesOneSec);
//actualBitrate = (float)(8.0*totalBytesOneSec)/(oneSecTime - startTime);
//printf("First 1Sec: target bitrate: %.0f kbps, actual bitrate: %.1f kbps\n", _bitRate, actualBitrate);
//TEST(fabs(actualBitrate - _bitRate) < OneSecReq * _bitRate);
rewind(_sourceFile);
//checking key/delta frame count
_vcm->SentFrameCount(frameCount);
printf("frame count: %d delta, %d key\n", frameCount.numDeltaFrames, frameCount.numKeyFrames);
}// end per codec
} // end rate control test
/********************************/
/* Encoder Pipeline Delay Test */
/******************************/
_vcm->InitializeSender();
NumberOfCodecs = _vcm->NumberOfCodecs();
bool encodeComplete = false;
// going over all available codecs
for (int k = 0; k < NumberOfCodecs; k++)
{
_vcm->Codec(k, &_sendCodec);
_vcm->InitializeSender();
_sendCodec.maxBitrate = 8000;
_vcm->RegisterSendCodec(&_sendCodec, 4, 1440);
_vcm->RegisterTransportCallback(_encodeCompleteCallback);
_frameCnt = 0;
encodeComplete = false;
while (encodeComplete == false)
{
TEST(fread(tmpBuffer, 1, _lengthSourceFrame, _sourceFile) > 0);
_frameCnt++;
sourceFrame.CreateFrame(size_y, tmpBuffer,
size_uv, tmpBuffer + size_y,
size_uv, tmpBuffer + size_y + size_uv,
_width, _height,
_width, half_width, half_width);
_timeStamp += (uint32_t)(9e4 / static_cast<float>(_frameRate));
sourceFrame.set_timestamp(_timeStamp);
_vcm->AddVideoFrame(sourceFrame);
encodeComplete = _encodeCompleteCallback->EncodeComplete();
} // first frame encoded
printf ("\n Codec type = %s \n", _sendCodec.plName);
printf(" Encoder pipeline delay = %d frames\n", _frameCnt - 1);
} // end for all codecs
/********************************/
/* Encoder Packet Size Test */
/********************************/
RTPSendCallback_SizeTest sendCallback;
RtpRtcp::Configuration configuration;
configuration.id = 1;
configuration.audio = false;
configuration.outgoing_transport = &sendCallback;
RtpRtcp& rtpModule = *RtpRtcp::CreateRtpRtcp(configuration);
VCMRTPEncodeCompleteCallback encCompleteCallback(&rtpModule);
_vcm->InitializeSender();
// Test temporal decimation settings
for (int k = 0; k < NumberOfCodecs; k++)
{
_vcm->Codec(k, &_sendCodec);
if (strncmp(_sendCodec.plName, "I420", 4) == 0)
{
// Only test with I420
break;
}
}
TEST(strncmp(_sendCodec.plName, "I420", 4) == 0);
_vcm->InitializeSender();
_sendCodec.maxFramerate = static_cast<uint8_t>(_frameRate / 2.0 + 0.5f);
_vcm->RegisterSendCodec(&_sendCodec, 4, 1440);
_vcm->SetChannelParameters(2000000, 0, 0);
_vcm->RegisterTransportCallback(_encodeCompleteCallback);
// up to here
_vcm->SetChannelParameters(static_cast<uint32_t>(1000 * _bitRate), 0, 20);
_encodeCompleteCallback->Initialize();
sendStats.set_framerate(static_cast<uint32_t>(_frameRate));
sendStats.set_bitrate(1000 * _bitRate);
_vcm->RegisterSendStatisticsCallback(&sendStats);
rewind(_sourceFile);
while (fread(tmpBuffer, 1, _lengthSourceFrame, _sourceFile) ==
_lengthSourceFrame) {
sourceFrame.CreateFrame(size_y, tmpBuffer,
size_uv, tmpBuffer + size_y,
size_uv, tmpBuffer + size_y + size_uv,
_width, _height,
_width, half_width, half_width);
_timeStamp += (uint32_t)(9e4 / static_cast<float>(_frameRate));
sourceFrame.set_timestamp(_timeStamp);
ret = _vcm->AddVideoFrame(sourceFrame);
if (_vcm->TimeUntilNextProcess() <= 0)
{
_vcm->Process();
}
IncrementDebugClock(_frameRate);
} // first frame encoded
delete &rtpModule;
Print();
delete tmpBuffer;
delete _decodeCallback;
delete _encodeCompleteCallback;
return 0;
}
void
GenericCodecTest::Print()
{
printf(" \n\n VCM Generic Encoder Test: \n\n%i tests completed\n", vcmMacrosTests);
if (vcmMacrosErrors > 0)
{
printf("%i FAILED\n\n", vcmMacrosErrors);
}
else
{
printf("ALL PASSED\n\n");
}
}
size_t
GenericCodecTest::WaitForEncodedFrame() const
{
int64_t startTime = _clock->TimeInMilliseconds();
while (_clock->TimeInMilliseconds() - startTime < kMaxWaitEncTimeMs*10)
{
if (_encodeCompleteCallback->EncodeComplete())
{
return _encodeCompleteCallback->EncodedBytes();
}
}
return 0;
}
void
GenericCodecTest::IncrementDebugClock(float frameRate)
{
_clock->AdvanceTimeMilliseconds(1000/frameRate);
}
int
RTPSendCallback_SizeTest::SendPacket(int channel, const void *data, size_t len)
{
_nPackets++;
_payloadSizeSum += len;
// Make sure no payloads (len - header size) are larger than maxPayloadSize
TEST(len > 0 && len - 12 <= _maxPayloadSize);
return 0;
}
void
RTPSendCallback_SizeTest::SetMaxPayloadSize(size_t maxPayloadSize)
{
_maxPayloadSize = maxPayloadSize;
}
void
RTPSendCallback_SizeTest::Reset()
{
_nPackets = 0;
_payloadSizeSum = 0;
}
float
RTPSendCallback_SizeTest::AveragePayloadSize() const
{
if (_nPackets > 0)
{
return _payloadSizeSum / static_cast<float>(_nPackets);
}
return 0;
}
int32_t VCMEncComplete_KeyReqTest::SendData(
uint8_t payloadType,
const webrtc::EncodedImage& encoded_image,
const RTPFragmentationHeader& /*fragmentationHeader*/,
const webrtc::RTPVideoHeader* /*videoHdr*/) {
WebRtcRTPHeader rtpInfo;
rtpInfo.header.markerBit = true; // end of frame
rtpInfo.type.Video.codecHeader.VP8.InitRTPVideoHeaderVP8();
rtpInfo.type.Video.codec = kRtpVideoVp8;
rtpInfo.header.payloadType = payloadType;
rtpInfo.header.sequenceNumber = _seqNo;
_seqNo += 2;
rtpInfo.header.ssrc = 0;
rtpInfo.header.timestamp = _timeStamp;
_timeStamp += 3000;
rtpInfo.type.Video.isFirstPacket = false;
rtpInfo.frameType = kVideoFrameKey;
return _vcm.IncomingPacket(encoded_image._buffer, encoded_image._length,
rtpInfo);
}
|