File: media_opt_test.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (557 lines) | stat: -rw-r--r-- 18,951 bytes parent folder | download
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
555
556
557
/*
 *  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.
 */

// Implementation of Media Optimization Test
// testing is done via the VCM module, no specific Media opt functionality.

#include "webrtc/modules/video_coding/main/test/media_opt_test.h"

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>

#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
#include "webrtc/modules/video_coding/main/interface/video_coding.h"
#include "webrtc/modules/video_coding/main/test/test_macros.h"
#include "webrtc/modules/video_coding/main/test/test_util.h"
#include "webrtc/test/testsupport/fileutils.h"
#include "webrtc/test/testsupport/metrics/video_metrics.h"

using namespace webrtc;

int MediaOptTest::RunTest(int testNum, CmdArgs& args)
{
    Trace::CreateTrace();
    Trace::SetTraceFile((test::OutputPath() + "mediaOptTestTrace.txt").c_str());
    Trace::set_level_filter(webrtc::kTraceAll);
    VideoCodingModule* vcm = VideoCodingModule::Create();
    Clock* clock = Clock::GetRealTimeClock();
    MediaOptTest* mot = new MediaOptTest(vcm, clock);
    if (testNum == 0)
    { // regular
         mot->Setup(0, args);
         mot->GeneralSetup();
         mot->Perform();
         mot->Print(1);// print to screen
         mot->TearDown();
    }
    if (testNum == 1)
    {   // release test
        mot->Setup(0, args);
        mot->RTTest();
    }
    if (testNum == 2)
    { // release test, running from script
         mot->Setup(1, args);
         mot->GeneralSetup();
         mot->Perform();
         mot->Print(1);// print to screen
         mot->TearDown();
    }

    VideoCodingModule::Destroy(vcm);
    delete mot;
    Trace::ReturnTrace();
    return 0;

}


MediaOptTest::MediaOptTest(VideoCodingModule* vcm, Clock* clock)
    : _vcm(vcm),
      _rtp(NULL),
      _outgoingTransport(NULL),
      _dataCallback(NULL),
      _clock(clock),
      _width(0),
      _height(0),
      _lengthSourceFrame(0),
      _timeStamp(0),
      _frameRate(30.0f),
      _nackEnabled(false),
      _fecEnabled(false),
      _rttMS(0),
      _bitRate(300.0f),
      _lossRate(0.0f),
      _renderDelayMs(0),
      _frameCnt(0),
      _sumEncBytes(0),
      _numFramesDropped(0),
      _numberOfCores(4) {
}

MediaOptTest::~MediaOptTest() {
  delete _rtp;
}

void MediaOptTest::Setup(int testType, CmdArgs& args) {
    /*TEST USER SETTINGS*/
    // test parameters
    _inname = args.inputFile;
    if (args.outputFile == "")
        _outname = test::OutputPath() + "MOTest_out.vp8";
    else
        _outname = args.outputFile;
    // actual source after frame dropping
    _actualSourcename = test::OutputPath() + "MOTestSource.yuv";
    _codecName = args.codecName;
    _sendCodecType = args.codecType;
    _width = args.width;
    _height = args.height;
    _frameRate = args.frameRate;
    _bitRate = args.bitRate;
    _numberOfCores = 4;

    // error resilience
    _nackEnabled = false;
    _fecEnabled = true;
    _nackFecEnabled = false;

    _rttMS = 100;
    _lossRate = 0.00*255; // no packet loss

    _testType = testType;

    //For multiple runs with script
    if (_testType == 1)
    {
        float rateTest,lossTest;
        int numRuns;
        _fpinp = fopen("dat_inp","rb");
        _fpout = fopen("test_runs/dat_out","ab");
        _fpout2 = fopen("test_runs/dat_out2","ab");
        TEST(fscanf(_fpinp,"%f %f %d \n",&rateTest,&lossTest,&numRuns) > 0);
        _bitRate = rateTest;
        _lossRate = lossTest;
        _testNum = 0;

        // for bit rates: 500, 1000, 2000, 3000,4000
        // for loss rates: 0, 1, 3, 5, 10%
        _numParRuns = 25;

        _testNum = numRuns + 1;
        if (rateTest == 0.0) _lossRate = 0.0;
        else
        {
            if (rateTest == 4000)  //final bit rate
            {
                if (lossTest == 0.1*255) _lossRate = 0.0;  //start at 1%
                else
                    if (lossTest == 0.05*255) _lossRate = 0.1*255;  //final loss rate
                    else
                        if (lossTest == 0.0) _lossRate = 0.01*255;
                        else _lossRate = lossTest + 0.02*255;
            }
        }

        if (rateTest == 0.0 || rateTest == 4000) _bitRate = 500; //starting bit rate
        else
            if (rateTest == 500) _bitRate = 1000;
                else _bitRate = rateTest +  1000;
    }
   //

    _renderDelayMs = 0;
    /* test settings end*/

   _lengthSourceFrame  = 3*_width*_height/2;
    _log.open((test::OutputPath() + "VCM_MediaOptLog.txt").c_str(),
              std::fstream::out | std::fstream::app);
}

void
MediaOptTest::GeneralSetup()
{
    uint32_t minPlayoutDelayMs = 0;

    if ((_sourceFile = fopen(_inname.c_str(), "rb")) == NULL)
    {
        printf("Cannot read file %s.\n", _inname.c_str());
        exit(1);
    }

    if ((_decodedFile = fopen(_outname.c_str(), "wb")) == NULL)
    {
        printf("Cannot read file %s.\n", _outname.c_str());
        exit(1);
    }

    if ((_actualSourceFile = fopen(_actualSourcename.c_str(), "wb")) == NULL)
    {
        printf("Cannot read file %s.\n", _actualSourcename.c_str());
        exit(1);
    }
    if (_vcm->InitializeReceiver() < 0)
    {
        exit(1);
    }
    if (_vcm->InitializeSender())
    {
        exit(1);
    }
    _outgoingTransport = new RTPSendCompleteCallback(_clock);
    _dataCallback = new RtpDataCallback(_vcm);

    RtpRtcp::Configuration configuration;
    configuration.id = 1;
    configuration.audio = false;
    configuration.outgoing_transport = _outgoingTransport;
    _rtp = RtpRtcp::CreateRtpRtcp(configuration);

    _outgoingTransport->SetRtpModule(_rtp);

    // Registering codecs for the RTP module

    // Register receive and send payload
    VideoCodec video_codec;
    strncpy(video_codec.plName, "VP8", 32);
    video_codec.plType = VCM_VP8_PAYLOAD_TYPE;
    rtp_receiver_->RegisterReceivePayload(video_codec.plName,
                                          video_codec.plType,
                                          90000,
                                          0,
                                          video_codec.maxBitrate);
    _rtp->RegisterSendPayload(video_codec);

    strncpy(video_codec.plName, "ULPFEC", 32);
    video_codec.plType = VCM_ULPFEC_PAYLOAD_TYPE;
    rtp_receiver_->RegisterReceivePayload(video_codec.plName,
                                          video_codec.plType,
                                          90000,
                                          0,
                                          video_codec.maxBitrate);
    _rtp->RegisterSendPayload(video_codec);

    strncpy(video_codec.plName, "RED", 32);
    video_codec.plType = VCM_RED_PAYLOAD_TYPE;
    rtp_receiver_->RegisterReceivePayload(video_codec.plName,
                                          video_codec.plType,
                                          90000,
                                          0,
                                          video_codec.maxBitrate);
    _rtp->RegisterSendPayload(video_codec);

    if (_nackFecEnabled == 1)
        _rtp->SetGenericFECStatus(_nackFecEnabled, VCM_RED_PAYLOAD_TYPE,
                VCM_ULPFEC_PAYLOAD_TYPE);
    else
        _rtp->SetGenericFECStatus(_fecEnabled, VCM_RED_PAYLOAD_TYPE,
                VCM_ULPFEC_PAYLOAD_TYPE);

    // VCM: Registering codecs
    VideoCodec sendCodec;
    _vcm->InitializeSender();
    _vcm->InitializeReceiver();
    int32_t numberOfCodecs = _vcm->NumberOfCodecs();
    if (numberOfCodecs < 1)
    {
        exit(1);
    }

    if (_vcm->Codec(_sendCodecType, &sendCodec) != 0)
    {
        printf("Unknown codec\n");
        exit(1);
    }
    // register codec
    sendCodec.startBitrate = (int) _bitRate;
    sendCodec.height = _height;
    sendCodec.width = _width;
    sendCodec.maxFramerate = (uint8_t)_frameRate;
    _vcm->RegisterSendCodec(&sendCodec, _numberOfCores, 1440);
    _vcm->RegisterReceiveCodec(&sendCodec, _numberOfCores); // same settings for encode and decode

    _vcm->SetRenderDelay(_renderDelayMs);
    _vcm->SetMinimumPlayoutDelay(minPlayoutDelayMs);
}
// The following test shall be conducted under release tests



int32_t
MediaOptTest::Perform()
{
    VCMDecodeCompleteCallback receiveCallback(_decodedFile);

    VCMRTPEncodeCompleteCallback* encodeCompleteCallback = new VCMRTPEncodeCompleteCallback(_rtp);
    _vcm->RegisterTransportCallback(encodeCompleteCallback);
    encodeCompleteCallback->SetCodecType(ConvertCodecType(_codecName.c_str()));
    encodeCompleteCallback->SetFrameDimensions(_width, _height);

    // callback settings
    VideoProtectionCallback  protectionCallback;
    protectionCallback.RegisterRtpModule(_rtp);
    _vcm->RegisterProtectionCallback(&protectionCallback);

    // set error resilience / test parameters:
    _outgoingTransport->SetLossPct(_lossRate);
    if (_nackFecEnabled == 1) {
        _vcm->SetVideoProtection(kProtectionNackFEC, _nackFecEnabled);
    } else {
        _vcm->SetVideoProtection(kProtectionNack, _nackEnabled);
        _vcm->SetVideoProtection(kProtectionFEC, _fecEnabled);
    }

    // START TEST
    I420VideoFrame sourceFrame;
    uint8_t* tmpBuffer = new uint8_t[_lengthSourceFrame];
    _vcm->SetChannelParameters(static_cast<uint32_t>(1000 * _bitRate),
                               (uint8_t)_lossRate, _rttMS);
    _vcm->RegisterReceiveCallback(&receiveCallback);

    _frameCnt  = 0;
    _sumEncBytes = 0;
    _numFramesDropped = 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;

    while (feof(_sourceFile)== 0)
    {
        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);
        TEST(_vcm->AddVideoFrame(sourceFrame) == VCM_OK);
        // inform RTP Module of error resilience features
        //_rtp->SetFECCodeRate(protectionCallback.FECKeyRate(),protectionCallback.FECDeltaRate());
        //_rtp->SetNACKStatus(protectionCallback.NACKMethod());

        int32_t ret = _vcm->Decode();
        if (ret < 0 )
        {
            TEST(ret == 0);
            printf ("Decode error in frame # %d",_frameCnt);
        }

        size_t encBytes = encodeCompleteCallback->EncodedBytes();
        if (encBytes == 0)
        {
            _numFramesDropped += 1;
            //printf("frame #%d dropped \n", _frameCnt );
        }
        else
        {
          // write frame to file
          if (PrintI420VideoFrame(sourceFrame, _actualSourceFile) < 0) {
            return -1;
          }
        }

        _sumEncBytes += encBytes;
    }

    //END TEST
    delete encodeCompleteCallback;
    delete tmpBuffer;

return 0;

}

void
MediaOptTest::RTTest()
{
    // will only calculate PSNR - not create output files for all
    // SET UP
    // Set bit rates
    const float bitRateVec[] = {500, 1000, 2000,3000, 4000};
    //const float bitRateVec[] = {1000};
    // Set Packet loss values ([0,255])
    const double lossPctVec[]     = {0.0*255, 0.0*255, 0.01*255, 0.01*255, 0.03*255, 0.03*255, 0.05*255, 0.05*255, 0.1*255, 0.1*255};
    const bool  nackEnabledVec[] = {false  , false, false, false, false, false, false, false , false, false};
    const bool  fecEnabledVec[]  = {false  , true,  false, true , false, true , false, true , false, true};
    // fec and nack are set according to the packet loss values

    const float nBitrates = sizeof(bitRateVec)/sizeof(*bitRateVec);
    const float nlossPct = sizeof(lossPctVec)/sizeof(*lossPctVec);

    std::vector<const VideoSource*> sources;
    std::vector<const VideoSource*>::iterator it;

    sources.push_back(new const VideoSource(_inname, _width, _height));
    int numOfSrc = 1;

    // constant settings (valid for entire run time)
    _rttMS = 20;
    _renderDelayMs = 0;

    // same out name for all
    _outname = test::OutputPath() + "RTMOTest_out.yuv";
    // actual source after frame dropping
    _actualSourcename = test::OutputPath() + "RTMOTestSource.yuv";

    _codecName = "VP8";  // for now just this one - later iterate over all codec types
    _log.open((test::OutputPath() + "/VCM_RTMediaOptLog.txt").c_str(),
              std::fstream::out | std::fstream::app);
    _outputRes=fopen((test::OutputPath() + "VCM_MediaOptResults.txt").c_str(),
                     "ab");

    //char filename[128];
    /* test settings end*/

    // START TEST
    // iterate over test sequences
    printf("\n****START TEST OVER ALL RUNS ****\n");
    int runCnt = 0;
    for (it = sources.begin() ; it < sources.end(); it++)
    {

        // test set up
        _inname = (*it)->GetFileName();
        _width  = (*it)->GetWidth();
        _height = (*it)->GetHeight();
        _lengthSourceFrame  = 3*_width*_height/2;
        _frameRate = (*it)->GetFrameRate();
         //GeneralSetup();


        // iterate over all bit rates
        for (int i = 0; i < nBitrates; i++)
        {
           _bitRate = static_cast<float>(bitRateVec[i]);
            // iterate over all packet loss values
            for (int j = 0; j < nlossPct; j++)
            {
                 _lossRate = static_cast<float>(lossPctVec[j]);
                 _nackEnabled = static_cast<bool>(nackEnabledVec[j]);
                 _fecEnabled = static_cast<bool>(fecEnabledVec[j]);

                 runCnt++;
                 printf("run #%d out of %d \n", runCnt,(int)(nlossPct*nBitrates*numOfSrc));

                //printf("**FOR RUN: **%d %d %d %d \n",_nackEnabled,_fecEnabled,int(lossPctVec[j]),int(_bitRate));

                 /*
                 int ch = sprintf(filename,"../test_mediaOpt/RTMOTest_%d_%d_%d_%d.yuv",_nackEnabled,_fecEnabled,int(lossPctVec[j]),int(_bitRate));
                _outname = filename;

                printf("**FOR RUN: **%d %d %d %d \n",_nackEnabled,_fecEnabled,int(lossPctVec[j]),int(_bitRate));
               */
                 GeneralSetup();
                 Perform();
                 Print(1);
                 TearDown();

                 printf("\n");
                  //printf("**DONE WITH RUN: **%d %d %f %d \n",_nackEnabled,_fecEnabled,lossPctVec[j],int(_bitRate));
                 //

            }// end of packet loss loop
        }// end of bit rate loop
        delete *it;
    }// end of video sequence loop
    // at end of sequence
    fclose(_outputRes);
    printf("\nVCM Media Optimization Test: \n\n%i tests completed\n", vcmMacrosTests);
    if (vcmMacrosErrors > 0)
    {
        printf("%i FAILED\n\n", vcmMacrosErrors);
    }
    else
    {
        printf("ALL PASSED\n\n");
    }
}


void
MediaOptTest::Print(int mode)
{
    double ActualBitRate =  8.0 *( _sumEncBytes / (_frameCnt / _frameRate));
    double actualBitRate = ActualBitRate / 1000.0;
    webrtc::test::QualityMetricsResult psnr;
    I420PSNRFromFiles(_actualSourcename.c_str(), _outname.c_str(), _width,
                      _height, &psnr);

    (_log) << "VCM: Media Optimization Test Cycle Completed!" << std::endl;
    (_log) << "Input file: " << _inname << std::endl;
    (_log) << "Output file:" << _outname << std::endl;
    ( _log) << "Actual bitrate: " << actualBitRate<< " kbps\tTarget: " << _bitRate << " kbps" << std::endl;
    (_log) << "Error Reslience: NACK:" << _nackEnabled << "; FEC: " << _fecEnabled << std::endl;
    (_log) << "Packet Loss applied= %f " << _lossRate << std::endl;
    (_log) << _numFramesDropped << " FRames were dropped" << std::endl;
     ( _log) << "PSNR: " << psnr.average << std::endl;
    (_log) << std::endl;

    if (_testType == 2)
    {
        fprintf(_outputRes,"************\n");
        fprintf(_outputRes,"\n\n\n");
        fprintf(_outputRes,"Actual bitrate: %f kbps\n", actualBitRate);
        fprintf(_outputRes,"Target bitrate: %f kbps\n", _bitRate);
        fprintf(_outputRes,"NACK: %s  ",(_nackEnabled)?"true":"false");
        fprintf(_outputRes,"FEC: %s \n ",(_fecEnabled)?"true":"false");
        fprintf(_outputRes,"Packet loss applied = %f\n", _lossRate);
        fprintf(_outputRes,"%d frames were dropped, and total number of frames processed %d  \n",_numFramesDropped,_frameCnt);
        fprintf(_outputRes,"PSNR: %f \n", psnr.average);
        fprintf(_outputRes,"************\n");
    }


    //
    if (_testType == 1)
    {
        fprintf(_fpout,"************\n");
        fprintf(_fpout,"\n\n\n");
        fprintf(_fpout,"Actual bitrate: %f kbps\n", actualBitRate);
        fprintf(_fpout,"Target bitrate: %f kbps\n", _bitRate);
        fprintf(_fpout,"NACK: %s  ",(_nackEnabled)?"true":"false");
        fprintf(_fpout,"FEC: %s \n ",(_fecEnabled)?"true":"false");
        fprintf(_fpout,"Packet loss applied = %f\n", _lossRate);
        fprintf(_fpout,"%d frames were dropped, and total number of frames processed %d  \n",_numFramesDropped,_frameCnt);
        fprintf(_fpout,"PSNR: %f \n", psnr.average);
        fprintf(_fpout,"************\n");

        int testNum1 = _testNum/(_numParRuns +1) + 1;
        int testNum2 = _testNum%_numParRuns;
        if (testNum2 == 0) testNum2 = _numParRuns;
        fprintf(_fpout2,"%d %d %f %f %f %f \n",testNum1,testNum2,_bitRate,actualBitRate,_lossRate,psnr.average);
        fclose(_fpinp);
        _fpinp = fopen("dat_inp","wb");
        fprintf(_fpinp,"%f %f %d \n",_bitRate,_lossRate,_testNum);
    }
    //


    if (mode == 1)
    {
        // print to screen
        printf("\n\n\n");
        printf("Actual bitrate: %f kbps\n", actualBitRate);
        printf("Target bitrate: %f kbps\n", _bitRate);
        printf("NACK: %s  ",(_nackEnabled)?"true":"false");
        printf("FEC: %s \n",(_fecEnabled)?"true":"false");
        printf("Packet loss applied = %f\n", _lossRate);
        printf("%d frames were dropped, and total number of frames processed %d  \n",_numFramesDropped,_frameCnt);
        printf("PSNR: %f \n", psnr.average);
    }
    TEST(psnr.average > 10); // low becuase of possible frame dropping (need to verify that OK for all packet loss values/ rates)
}

void MediaOptTest::TearDown() {
  delete _rtp;
  _rtp = NULL;
  delete _outgoingTransport;
  _outgoingTransport = NULL;
  delete _dataCallback;
  _dataCallback = NULL;
  _log.close();
  fclose(_sourceFile);
  fclose(_decodedFile);
  fclose(_actualSourceFile);
}