blob: 3ccb6fe3b44bfe00fb5fe0f1e2da12e5ba64bf4b [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:
70 using DecoderMap = std::map<int, std::pair<NetEqDecoder, std::string> >;
71
72 struct ExternalDecoderInfo {
73 AudioDecoder* decoder;
74 NetEqDecoder codec;
75 std::string codec_name;
76 };
77
78 using ExtDecoderMap = std::map<int, ExternalDecoderInfo>;
79
henrik.lundin02739d92017-05-04 06:09:06 -070080 struct Callbacks {
81 NetEqTestErrorCallback* error_callback = nullptr;
82 NetEqPostInsertPacket* post_insert_packet = nullptr;
83 NetEqGetAudioCallback* get_audio_callback = nullptr;
Ivo Creusen55de08e2018-09-03 11:49:27 +020084 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
henrik.lundin02739d92017-05-04 06:09:06 -070085 };
86
henrik.lundine8a77e32016-06-22 06:34:03 -070087 // Sets up the test with given configuration, codec mappings, input, ouput,
88 // and callback objects for error reporting.
89 NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010090 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070091 const DecoderMap& codecs,
92 const ExtDecoderMap& ext_codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010093 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070094 std::unique_ptr<NetEqInput> input,
95 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070096 Callbacks callbacks);
henrik.lundine8a77e32016-06-22 06:34:03 -070097
Ivo Creusen55de08e2018-09-03 11:49:27 +020098 ~NetEqTest() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070099
100 // Runs the test. Returns the duration of the produced audio in ms.
101 int64_t Run();
Ivo Creusen55de08e2018-09-03 11:49:27 +0200102 // Runs the simulation until we hit the next GetAudio event. If the simulation
103 // is finished, is_simulation_finished will be set to true in the returned
104 // SimulationStepResult.
105 SimulationStepResult RunToNextGetAudio() override;
106
107 void SetNextAction(Action next_operation) override;
108 NetEqState GetNetEqState() override;
henrik.lundine8a77e32016-06-22 06:34:03 -0700109
110 // Returns the statistics from NetEq.
111 NetEqNetworkStatistics SimulationStats();
Alex Narest7ff6ca52018-02-07 18:46:33 +0100112 NetEqLifetimeStatistics LifetimeStats() const;
henrik.lundine8a77e32016-06-22 06:34:03 -0700113
Henrik Lundin7687ad52018-07-02 10:14:46 +0200114 static DecoderMap StandardDecoderMap();
115
henrik.lundine8a77e32016-06-22 06:34:03 -0700116 private:
117 void RegisterDecoders(const DecoderMap& codecs);
118 void RegisterExternalDecoders(const ExtDecoderMap& codecs);
Ivo Creusen55de08e2018-09-03 11:49:27 +0200119 absl::optional<Action> next_action_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200120 absl::optional<int> last_packet_time_ms_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700121 std::unique_ptr<NetEq> neteq_;
122 std::unique_ptr<NetEqInput> input_;
123 std::unique_ptr<AudioSink> output_;
henrik.lundin02739d92017-05-04 06:09:06 -0700124 Callbacks callbacks_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700125 int sample_rate_hz_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200126 NetEqState current_state_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200127 NetEqOperationsAndState prev_ops_state_;
Ivo Creusen2db46b02018-12-14 16:49:12 +0100128 NetEqLifetimeStatistics prev_lifetime_stats_;
129 absl::optional<uint32_t> last_packet_timestamp_;
130 std::unique_ptr<std::ofstream> text_log_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700131};
132
133} // namespace test
134} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200135#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_