Home | History | Annotate | Download | only in tools
      1 /*
      2  *  Copyright (c) 2014 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  */
     10 
     11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_QUALITY_TEST_H_
     12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_QUALITY_TEST_H_
     13 
     14 #include <gflags/gflags.h>
     15 #include <string>
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 #include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
     18 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
     19 #include "webrtc/modules/audio_coding/neteq/tools/rtp_generator.h"
     20 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     21 #include "webrtc/typedefs.h"
     22 
     23 using google::RegisterFlagValidator;
     24 
     25 namespace webrtc {
     26 namespace test {
     27 
     28 class LossModel {
     29  public:
     30   virtual ~LossModel() {};
     31   virtual bool Lost() = 0;
     32 };
     33 
     34 class NoLoss : public LossModel {
     35  public:
     36   virtual bool Lost() OVERRIDE;
     37 };
     38 
     39 class UniformLoss : public LossModel {
     40  public:
     41   UniformLoss(double loss_rate);
     42   virtual bool Lost() OVERRIDE;
     43   void set_loss_rate(double loss_rate) { loss_rate_ = loss_rate; }
     44 
     45  private:
     46   double loss_rate_;
     47 };
     48 
     49 class GilbertElliotLoss : public LossModel {
     50  public:
     51   GilbertElliotLoss(double prob_trans_11, double prob_trans_01);
     52   virtual bool Lost() OVERRIDE;
     53 
     54  private:
     55   // Prob. of losing current packet, when previous packet is lost.
     56   double prob_trans_11_;
     57   // Prob. of losing current packet, when previous packet is not lost.
     58   double prob_trans_01_;
     59   bool lost_last_;
     60   scoped_ptr<UniformLoss> uniform_loss_model_;
     61 };
     62 
     63 class NetEqQualityTest : public ::testing::Test {
     64  protected:
     65   NetEqQualityTest(int block_duration_ms,
     66                    int in_sampling_khz,
     67                    int out_sampling_khz,
     68                    enum NetEqDecoder decoder_type,
     69                    int channels,
     70                    std::string in_filename,
     71                    std::string out_filename);
     72   virtual void SetUp() OVERRIDE;
     73   virtual void TearDown() OVERRIDE;
     74 
     75   // EncodeBlock(...) does the following:
     76   // 1. encodes a block of audio, saved in |in_data| and has a length of
     77   // |block_size_samples| (samples per channel),
     78   // 2. save the bit stream to |payload| of |max_bytes| bytes in size,
     79   // 3. returns the length of the payload (in bytes),
     80   virtual int EncodeBlock(int16_t* in_data, int block_size_samples,
     81                           uint8_t* payload, int max_bytes) = 0;
     82 
     83   // PacketLost(...) determines weather a packet sent at an indicated time gets
     84   // lost or not.
     85   bool PacketLost();
     86 
     87   // DecodeBlock() decodes a block of audio using the payload stored in
     88   // |payload_| with the length of |payload_size_bytes_| (bytes). The decoded
     89   // audio is to be stored in |out_data_|.
     90   int DecodeBlock();
     91 
     92   // Transmit() uses |rtp_generator_| to generate a packet and passes it to
     93   // |neteq_|.
     94   int Transmit();
     95 
     96   // Simulate(...) runs encoding / transmitting / decoding up to |end_time_ms|
     97   // (miliseconds), the resulted audio is stored in the file with the name of
     98   // |out_filename_|.
     99   void Simulate(int end_time_ms);
    100 
    101  private:
    102   int decoded_time_ms_;
    103   int decodable_time_ms_;
    104   double drift_factor_;
    105   int packet_loss_rate_;
    106   const int block_duration_ms_;
    107   const int in_sampling_khz_;
    108   const int out_sampling_khz_;
    109   const enum NetEqDecoder decoder_type_;
    110   const int channels_;
    111   const std::string in_filename_;
    112   const std::string out_filename_;
    113   const std::string log_filename_;
    114 
    115   // Number of samples per channel in a frame.
    116   const int in_size_samples_;
    117 
    118   // Expected output number of samples per channel in a frame.
    119   const int out_size_samples_;
    120 
    121   int payload_size_bytes_;
    122   int max_payload_bytes_;
    123 
    124   scoped_ptr<InputAudioFile> in_file_;
    125   FILE* out_file_;
    126   FILE* log_file_;
    127 
    128   scoped_ptr<RtpGenerator> rtp_generator_;
    129   scoped_ptr<NetEq> neteq_;
    130   scoped_ptr<LossModel> loss_model_;
    131 
    132   scoped_ptr<int16_t[]> in_data_;
    133   scoped_ptr<uint8_t[]> payload_;
    134   scoped_ptr<int16_t[]> out_data_;
    135   WebRtcRTPHeader rtp_header_;
    136 
    137   long total_payload_size_bytes_;
    138 };
    139 
    140 }  // namespace test
    141 }  // namespace webrtc
    142 
    143 #endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_QUALITY_TEST_H_
    144