blob: 5261dd7db07a5c31d244dbcef28c6156b8fc6c89 [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 Creusen55de08e2018-09-03 11:49:27 +020022#include "api/test/neteq_simulator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/neteq/include/neteq.h"
24#include "modules/audio_coding/neteq/tools/audio_sink.h"
25#include "modules/audio_coding/neteq/tools/neteq_input.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070026
27namespace webrtc {
28namespace test {
29
30class NetEqTestErrorCallback {
31 public:
32 virtual ~NetEqTestErrorCallback() = default;
Henrik Lundinc417d9e2017-06-14 12:29:03 +020033 virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {}
34 virtual void OnGetAudioError() {}
henrik.lundine8a77e32016-06-22 06:34:03 -070035};
36
37class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020038 void OnInsertPacketError(const NetEqInput::PacketData& packet) override;
39 void OnGetAudioError() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070040};
41
henrik.lundin02739d92017-05-04 06:09:06 -070042class NetEqPostInsertPacket {
43 public:
44 virtual ~NetEqPostInsertPacket() = default;
45 virtual void AfterInsertPacket(const NetEqInput::PacketData& packet,
46 NetEq* neteq) = 0;
47};
48
49class NetEqGetAudioCallback {
50 public:
51 virtual ~NetEqGetAudioCallback() = default;
52 virtual void BeforeGetAudio(NetEq* neteq) = 0;
53 virtual void AfterGetAudio(int64_t time_now_ms,
54 const AudioFrame& audio_frame,
55 bool muted,
56 NetEq* neteq) = 0;
57};
58
Ivo Creusen55de08e2018-09-03 11:49:27 +020059class NetEqSimulationEndedCallback {
60 public:
61 virtual ~NetEqSimulationEndedCallback() = default;
62 virtual void SimulationEnded(int64_t simulation_time_ms) = 0;
63};
64
henrik.lundine8a77e32016-06-22 06:34:03 -070065// Class that provides an input--output test for NetEq. The input (both packets
66// and output events) is provided by a NetEqInput object, while the output is
67// directed to an AudioSink object.
Ivo Creusen55de08e2018-09-03 11:49:27 +020068class NetEqTest : public NetEqSimulator {
henrik.lundine8a77e32016-06-22 06:34:03 -070069 public:
Niels Möller05543682019-01-10 16:55:06 +010070 using DecoderMap = std::map<int, SdpAudioFormat>;
henrik.lundine8a77e32016-06-22 06:34:03 -070071
henrik.lundin02739d92017-05-04 06:09:06 -070072 struct Callbacks {
73 NetEqTestErrorCallback* error_callback = nullptr;
74 NetEqPostInsertPacket* post_insert_packet = nullptr;
75 NetEqGetAudioCallback* get_audio_callback = nullptr;
Ivo Creusen55de08e2018-09-03 11:49:27 +020076 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
henrik.lundin02739d92017-05-04 06:09:06 -070077 };
78
henrik.lundine8a77e32016-06-22 06:34:03 -070079 // Sets up the test with given configuration, codec mappings, input, ouput,
80 // and callback objects for error reporting.
81 NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010082 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070083 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010084 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070085 std::unique_ptr<NetEqInput> input,
86 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070087 Callbacks callbacks);
henrik.lundine8a77e32016-06-22 06:34:03 -070088
Ivo Creusen55de08e2018-09-03 11:49:27 +020089 ~NetEqTest() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070090
91 // Runs the test. Returns the duration of the produced audio in ms.
92 int64_t Run();
Ivo Creusen55de08e2018-09-03 11:49:27 +020093 // Runs the simulation until we hit the next GetAudio event. If the simulation
94 // is finished, is_simulation_finished will be set to true in the returned
95 // SimulationStepResult.
96 SimulationStepResult RunToNextGetAudio() override;
97
98 void SetNextAction(Action next_operation) override;
99 NetEqState GetNetEqState() override;
henrik.lundine8a77e32016-06-22 06:34:03 -0700100
101 // Returns the statistics from NetEq.
102 NetEqNetworkStatistics SimulationStats();
Alex Narest7ff6ca52018-02-07 18:46:33 +0100103 NetEqLifetimeStatistics LifetimeStats() const;
henrik.lundine8a77e32016-06-22 06:34:03 -0700104
Henrik Lundin7687ad52018-07-02 10:14:46 +0200105 static DecoderMap StandardDecoderMap();
106
henrik.lundine8a77e32016-06-22 06:34:03 -0700107 private:
108 void RegisterDecoders(const DecoderMap& codecs);
Ivo Creusen55de08e2018-09-03 11:49:27 +0200109 absl::optional<Action> next_action_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200110 absl::optional<int> last_packet_time_ms_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700111 std::unique_ptr<NetEq> neteq_;
112 std::unique_ptr<NetEqInput> input_;
113 std::unique_ptr<AudioSink> output_;
henrik.lundin02739d92017-05-04 06:09:06 -0700114 Callbacks callbacks_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700115 int sample_rate_hz_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200116 NetEqState current_state_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200117 NetEqOperationsAndState prev_ops_state_;
Ivo Creusen2db46b02018-12-14 16:49:12 +0100118 NetEqLifetimeStatistics prev_lifetime_stats_;
119 absl::optional<uint32_t> last_packet_timestamp_;
120 std::unique_ptr<std::ofstream> text_log_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700121};
122
123} // namespace test
124} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200125#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_