blob: edbb6208317493bcc26f988fc49dbdf9ae29031c [file] [log] [blame]
henrik.lundine8a77e32016-06-22 06:34:03 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_
12#define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_
henrik.lundine8a77e32016-06-22 06:34:03 -070013
Ivo Creusen2db46b02018-12-14 16:49:12 +010014#include <fstream>
henrik.lundine8a77e32016-06-22 06:34:03 -070015#include <map>
16#include <memory>
17#include <string>
18#include <utility>
19
Ivo Creusen55de08e2018-09-03 11:49:27 +020020#include "absl/types/optional.h"
21#include "api/test/neteq_simulator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/neteq/include/neteq.h"
23#include "modules/audio_coding/neteq/tools/audio_sink.h"
24#include "modules/audio_coding/neteq/tools/neteq_input.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070025
26namespace webrtc {
27namespace test {
28
29class NetEqTestErrorCallback {
30 public:
31 virtual ~NetEqTestErrorCallback() = default;
Henrik Lundinc417d9e2017-06-14 12:29:03 +020032 virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {}
33 virtual void OnGetAudioError() {}
henrik.lundine8a77e32016-06-22 06:34:03 -070034};
35
36class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020037 void OnInsertPacketError(const NetEqInput::PacketData& packet) override;
38 void OnGetAudioError() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070039};
40
henrik.lundin02739d92017-05-04 06:09:06 -070041class NetEqPostInsertPacket {
42 public:
43 virtual ~NetEqPostInsertPacket() = default;
44 virtual void AfterInsertPacket(const NetEqInput::PacketData& packet,
45 NetEq* neteq) = 0;
46};
47
48class NetEqGetAudioCallback {
49 public:
50 virtual ~NetEqGetAudioCallback() = default;
51 virtual void BeforeGetAudio(NetEq* neteq) = 0;
52 virtual void AfterGetAudio(int64_t time_now_ms,
53 const AudioFrame& audio_frame,
54 bool muted,
55 NetEq* neteq) = 0;
56};
57
Ivo Creusen55de08e2018-09-03 11:49:27 +020058class NetEqSimulationEndedCallback {
59 public:
60 virtual ~NetEqSimulationEndedCallback() = default;
61 virtual void SimulationEnded(int64_t simulation_time_ms) = 0;
62};
63
henrik.lundine8a77e32016-06-22 06:34:03 -070064// Class that provides an input--output test for NetEq. The input (both packets
65// and output events) is provided by a NetEqInput object, while the output is
66// directed to an AudioSink object.
Ivo Creusen55de08e2018-09-03 11:49:27 +020067class NetEqTest : public NetEqSimulator {
henrik.lundine8a77e32016-06-22 06:34:03 -070068 public:
69 using DecoderMap = std::map<int, std::pair<NetEqDecoder, std::string> >;
70
71 struct ExternalDecoderInfo {
72 AudioDecoder* decoder;
73 NetEqDecoder codec;
74 std::string codec_name;
75 };
76
77 using ExtDecoderMap = std::map<int, ExternalDecoderInfo>;
78
henrik.lundin02739d92017-05-04 06:09:06 -070079 struct Callbacks {
80 NetEqTestErrorCallback* error_callback = nullptr;
81 NetEqPostInsertPacket* post_insert_packet = nullptr;
82 NetEqGetAudioCallback* get_audio_callback = nullptr;
Ivo Creusen55de08e2018-09-03 11:49:27 +020083 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
henrik.lundin02739d92017-05-04 06:09:06 -070084 };
85
henrik.lundine8a77e32016-06-22 06:34:03 -070086 // Sets up the test with given configuration, codec mappings, input, ouput,
87 // and callback objects for error reporting.
88 NetEqTest(const NetEq::Config& config,
89 const DecoderMap& codecs,
90 const ExtDecoderMap& ext_codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010091 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070092 std::unique_ptr<NetEqInput> input,
93 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070094 Callbacks callbacks);
henrik.lundine8a77e32016-06-22 06:34:03 -070095
Ivo Creusen55de08e2018-09-03 11:49:27 +020096 ~NetEqTest() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070097
98 // Runs the test. Returns the duration of the produced audio in ms.
99 int64_t Run();
Ivo Creusen55de08e2018-09-03 11:49:27 +0200100 // Runs the simulation until we hit the next GetAudio event. If the simulation
101 // is finished, is_simulation_finished will be set to true in the returned
102 // SimulationStepResult.
103 SimulationStepResult RunToNextGetAudio() override;
104
105 void SetNextAction(Action next_operation) override;
106 NetEqState GetNetEqState() override;
henrik.lundine8a77e32016-06-22 06:34:03 -0700107
108 // Returns the statistics from NetEq.
109 NetEqNetworkStatistics SimulationStats();
Alex Narest7ff6ca52018-02-07 18:46:33 +0100110 NetEqLifetimeStatistics LifetimeStats() const;
henrik.lundine8a77e32016-06-22 06:34:03 -0700111
Henrik Lundin7687ad52018-07-02 10:14:46 +0200112 static DecoderMap StandardDecoderMap();
113
henrik.lundine8a77e32016-06-22 06:34:03 -0700114 private:
115 void RegisterDecoders(const DecoderMap& codecs);
116 void RegisterExternalDecoders(const ExtDecoderMap& codecs);
Ivo Creusen55de08e2018-09-03 11:49:27 +0200117 absl::optional<Action> next_action_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200118 absl::optional<int> last_packet_time_ms_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700119 std::unique_ptr<NetEq> neteq_;
120 std::unique_ptr<NetEqInput> input_;
121 std::unique_ptr<AudioSink> output_;
henrik.lundin02739d92017-05-04 06:09:06 -0700122 Callbacks callbacks_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700123 int sample_rate_hz_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200124 NetEqState current_state_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200125 NetEqOperationsAndState prev_ops_state_;
Ivo Creusen2db46b02018-12-14 16:49:12 +0100126 NetEqLifetimeStatistics prev_lifetime_stats_;
127 absl::optional<uint32_t> last_packet_timestamp_;
128 std::unique_ptr<std::ofstream> text_log_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700129};
130
131} // namespace test
132} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200133#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_