blob: 787d507504512d62334e1af54d1f8e17176ffc37 [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
14#include <map>
15#include <memory>
16#include <string>
17#include <utility>
18
Ivo Creusen55de08e2018-09-03 11:49:27 +020019#include "absl/types/optional.h"
20#include "api/test/neteq_simulator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/neteq/include/neteq.h"
22#include "modules/audio_coding/neteq/tools/audio_sink.h"
23#include "modules/audio_coding/neteq/tools/neteq_input.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070024
25namespace webrtc {
26namespace test {
27
28class NetEqTestErrorCallback {
29 public:
30 virtual ~NetEqTestErrorCallback() = default;
Henrik Lundinc417d9e2017-06-14 12:29:03 +020031 virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {}
32 virtual void OnGetAudioError() {}
henrik.lundine8a77e32016-06-22 06:34:03 -070033};
34
35class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020036 void OnInsertPacketError(const NetEqInput::PacketData& packet) override;
37 void OnGetAudioError() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070038};
39
henrik.lundin02739d92017-05-04 06:09:06 -070040class NetEqPostInsertPacket {
41 public:
42 virtual ~NetEqPostInsertPacket() = default;
43 virtual void AfterInsertPacket(const NetEqInput::PacketData& packet,
44 NetEq* neteq) = 0;
45};
46
47class NetEqGetAudioCallback {
48 public:
49 virtual ~NetEqGetAudioCallback() = default;
50 virtual void BeforeGetAudio(NetEq* neteq) = 0;
51 virtual void AfterGetAudio(int64_t time_now_ms,
52 const AudioFrame& audio_frame,
53 bool muted,
54 NetEq* neteq) = 0;
55};
56
Ivo Creusen55de08e2018-09-03 11:49:27 +020057class NetEqSimulationEndedCallback {
58 public:
59 virtual ~NetEqSimulationEndedCallback() = default;
60 virtual void SimulationEnded(int64_t simulation_time_ms) = 0;
61};
62
henrik.lundine8a77e32016-06-22 06:34:03 -070063// Class that provides an input--output test for NetEq. The input (both packets
64// and output events) is provided by a NetEqInput object, while the output is
65// directed to an AudioSink object.
Ivo Creusen55de08e2018-09-03 11:49:27 +020066class NetEqTest : public NetEqSimulator {
henrik.lundine8a77e32016-06-22 06:34:03 -070067 public:
68 using DecoderMap = std::map<int, std::pair<NetEqDecoder, std::string> >;
69
70 struct ExternalDecoderInfo {
71 AudioDecoder* decoder;
72 NetEqDecoder codec;
73 std::string codec_name;
74 };
75
76 using ExtDecoderMap = std::map<int, ExternalDecoderInfo>;
77
henrik.lundin02739d92017-05-04 06:09:06 -070078 struct Callbacks {
79 NetEqTestErrorCallback* error_callback = nullptr;
80 NetEqPostInsertPacket* post_insert_packet = nullptr;
81 NetEqGetAudioCallback* get_audio_callback = nullptr;
Ivo Creusen55de08e2018-09-03 11:49:27 +020082 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
henrik.lundin02739d92017-05-04 06:09:06 -070083 };
84
henrik.lundine8a77e32016-06-22 06:34:03 -070085 // Sets up the test with given configuration, codec mappings, input, ouput,
86 // and callback objects for error reporting.
87 NetEqTest(const NetEq::Config& config,
88 const DecoderMap& codecs,
89 const ExtDecoderMap& ext_codecs,
90 std::unique_ptr<NetEqInput> input,
91 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070092 Callbacks callbacks);
henrik.lundine8a77e32016-06-22 06:34:03 -070093
Ivo Creusen55de08e2018-09-03 11:49:27 +020094 ~NetEqTest() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070095
96 // Runs the test. Returns the duration of the produced audio in ms.
97 int64_t Run();
Ivo Creusen55de08e2018-09-03 11:49:27 +020098 // Runs the simulation until we hit the next GetAudio event. If the simulation
99 // is finished, is_simulation_finished will be set to true in the returned
100 // SimulationStepResult.
101 SimulationStepResult RunToNextGetAudio() override;
102
103 void SetNextAction(Action next_operation) override;
104 NetEqState GetNetEqState() override;
henrik.lundine8a77e32016-06-22 06:34:03 -0700105
106 // Returns the statistics from NetEq.
107 NetEqNetworkStatistics SimulationStats();
Alex Narest7ff6ca52018-02-07 18:46:33 +0100108 NetEqLifetimeStatistics LifetimeStats() const;
henrik.lundine8a77e32016-06-22 06:34:03 -0700109
Henrik Lundin7687ad52018-07-02 10:14:46 +0200110 static DecoderMap StandardDecoderMap();
111
henrik.lundine8a77e32016-06-22 06:34:03 -0700112 private:
113 void RegisterDecoders(const DecoderMap& codecs);
114 void RegisterExternalDecoders(const ExtDecoderMap& codecs);
Ivo Creusen55de08e2018-09-03 11:49:27 +0200115 absl::optional<Action> next_action_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200116 absl::optional<int> last_packet_time_ms_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700117 std::unique_ptr<NetEq> neteq_;
118 std::unique_ptr<NetEqInput> input_;
119 std::unique_ptr<AudioSink> output_;
henrik.lundin02739d92017-05-04 06:09:06 -0700120 Callbacks callbacks_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700121 int sample_rate_hz_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200122 NetEqState current_state_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700123};
124
125} // namespace test
126} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200127#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_