blob: 3b787a6cfbd4002bc4c185b8b1d98b27e4b97c57 [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"
Niels Möller3f651d82018-12-19 15:06:17 +010021#include "api/audio_codecs/audio_decoder_factory.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010022#include "api/neteq/neteq.h"
Ivo Creusencee751a2020-01-16 17:17:09 +010023#include "api/neteq/neteq_factory.h"
Ivo Creusen55de08e2018-09-03 11:49:27 +020024#include "api/test/neteq_simulator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/neteq/tools/audio_sink.h"
26#include "modules/audio_coding/neteq/tools/neteq_input.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000027#include "system_wrappers/include/clock.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070028
29namespace webrtc {
30namespace test {
31
32class NetEqTestErrorCallback {
33 public:
34 virtual ~NetEqTestErrorCallback() = default;
Henrik Lundinc417d9e2017-06-14 12:29:03 +020035 virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {}
36 virtual void OnGetAudioError() {}
henrik.lundine8a77e32016-06-22 06:34:03 -070037};
38
39class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020040 void OnInsertPacketError(const NetEqInput::PacketData& packet) override;
41 void OnGetAudioError() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070042};
43
henrik.lundin02739d92017-05-04 06:09:06 -070044class NetEqPostInsertPacket {
45 public:
46 virtual ~NetEqPostInsertPacket() = default;
47 virtual void AfterInsertPacket(const NetEqInput::PacketData& packet,
48 NetEq* neteq) = 0;
49};
50
51class NetEqGetAudioCallback {
52 public:
53 virtual ~NetEqGetAudioCallback() = default;
54 virtual void BeforeGetAudio(NetEq* neteq) = 0;
55 virtual void AfterGetAudio(int64_t time_now_ms,
56 const AudioFrame& audio_frame,
57 bool muted,
58 NetEq* neteq) = 0;
59};
60
Ivo Creusen55de08e2018-09-03 11:49:27 +020061class NetEqSimulationEndedCallback {
62 public:
63 virtual ~NetEqSimulationEndedCallback() = default;
Henrik Lundinc49e9c22020-05-25 11:26:15 +020064 virtual void SimulationEnded(int64_t simulation_time_ms, NetEq* neteq) = 0;
Ivo Creusen55de08e2018-09-03 11:49:27 +020065};
66
henrik.lundine8a77e32016-06-22 06:34:03 -070067// Class that provides an input--output test for NetEq. The input (both packets
68// and output events) is provided by a NetEqInput object, while the output is
69// directed to an AudioSink object.
Ivo Creusen55de08e2018-09-03 11:49:27 +020070class NetEqTest : public NetEqSimulator {
henrik.lundine8a77e32016-06-22 06:34:03 -070071 public:
Niels Möller05543682019-01-10 16:55:06 +010072 using DecoderMap = std::map<int, SdpAudioFormat>;
henrik.lundine8a77e32016-06-22 06:34:03 -070073
henrik.lundin02739d92017-05-04 06:09:06 -070074 struct Callbacks {
75 NetEqTestErrorCallback* error_callback = nullptr;
76 NetEqPostInsertPacket* post_insert_packet = nullptr;
77 NetEqGetAudioCallback* get_audio_callback = nullptr;
Ivo Creusen55de08e2018-09-03 11:49:27 +020078 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
henrik.lundin02739d92017-05-04 06:09:06 -070079 };
80
henrik.lundine8a77e32016-06-22 06:34:03 -070081 // Sets up the test with given configuration, codec mappings, input, ouput,
82 // and callback objects for error reporting.
83 NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010084 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070085 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010086 std::unique_ptr<std::ofstream> text_log,
Ivo Creusencee751a2020-01-16 17:17:09 +010087 NetEqFactory* neteq_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070088 std::unique_ptr<NetEqInput> input,
89 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070090 Callbacks callbacks);
henrik.lundine8a77e32016-06-22 06:34:03 -070091
Ivo Creusen55de08e2018-09-03 11:49:27 +020092 ~NetEqTest() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070093
94 // Runs the test. Returns the duration of the produced audio in ms.
Ivo Creusen182c2b82020-01-28 17:12:23 +010095 int64_t Run() override;
Ivo Creusen55de08e2018-09-03 11:49:27 +020096 // Runs the simulation until we hit the next GetAudio event. If the simulation
97 // is finished, is_simulation_finished will be set to true in the returned
98 // SimulationStepResult.
99 SimulationStepResult RunToNextGetAudio() override;
100
101 void SetNextAction(Action next_operation) override;
102 NetEqState GetNetEqState() override;
henrik.lundine8a77e32016-06-22 06:34:03 -0700103
104 // Returns the statistics from NetEq.
105 NetEqNetworkStatistics SimulationStats();
Alex Narest7ff6ca52018-02-07 18:46:33 +0100106 NetEqLifetimeStatistics LifetimeStats() const;
henrik.lundine8a77e32016-06-22 06:34:03 -0700107
Henrik Lundin7687ad52018-07-02 10:14:46 +0200108 static DecoderMap StandardDecoderMap();
109
henrik.lundine8a77e32016-06-22 06:34:03 -0700110 private:
111 void RegisterDecoders(const DecoderMap& codecs);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000112 SimulatedClock clock_;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200113 absl::optional<Action> next_action_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200114 absl::optional<int> last_packet_time_ms_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700115 std::unique_ptr<NetEq> neteq_;
116 std::unique_ptr<NetEqInput> input_;
117 std::unique_ptr<AudioSink> output_;
henrik.lundin02739d92017-05-04 06:09:06 -0700118 Callbacks callbacks_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700119 int sample_rate_hz_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200120 NetEqState current_state_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200121 NetEqOperationsAndState prev_ops_state_;
Ivo Creusen2db46b02018-12-14 16:49:12 +0100122 NetEqLifetimeStatistics prev_lifetime_stats_;
123 absl::optional<uint32_t> last_packet_timestamp_;
124 std::unique_ptr<std::ofstream> text_log_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700125};
126
127} // namespace test
128} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_