blob: 3cf105c693b19b1264bc95c019a61d82e4a11af4 [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"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000026#include "system_wrappers/include/clock.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070027
28namespace webrtc {
29namespace test {
30
31class NetEqTestErrorCallback {
32 public:
33 virtual ~NetEqTestErrorCallback() = default;
Henrik Lundinc417d9e2017-06-14 12:29:03 +020034 virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {}
35 virtual void OnGetAudioError() {}
henrik.lundine8a77e32016-06-22 06:34:03 -070036};
37
38class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020039 void OnInsertPacketError(const NetEqInput::PacketData& packet) override;
40 void OnGetAudioError() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070041};
42
henrik.lundin02739d92017-05-04 06:09:06 -070043class NetEqPostInsertPacket {
44 public:
45 virtual ~NetEqPostInsertPacket() = default;
46 virtual void AfterInsertPacket(const NetEqInput::PacketData& packet,
47 NetEq* neteq) = 0;
48};
49
50class NetEqGetAudioCallback {
51 public:
52 virtual ~NetEqGetAudioCallback() = default;
53 virtual void BeforeGetAudio(NetEq* neteq) = 0;
54 virtual void AfterGetAudio(int64_t time_now_ms,
55 const AudioFrame& audio_frame,
56 bool muted,
57 NetEq* neteq) = 0;
58};
59
Ivo Creusen55de08e2018-09-03 11:49:27 +020060class NetEqSimulationEndedCallback {
61 public:
62 virtual ~NetEqSimulationEndedCallback() = default;
63 virtual void SimulationEnded(int64_t simulation_time_ms) = 0;
64};
65
henrik.lundine8a77e32016-06-22 06:34:03 -070066// Class that provides an input--output test for NetEq. The input (both packets
67// and output events) is provided by a NetEqInput object, while the output is
68// directed to an AudioSink object.
Ivo Creusen55de08e2018-09-03 11:49:27 +020069class NetEqTest : public NetEqSimulator {
henrik.lundine8a77e32016-06-22 06:34:03 -070070 public:
Niels Möller05543682019-01-10 16:55:06 +010071 using DecoderMap = std::map<int, SdpAudioFormat>;
henrik.lundine8a77e32016-06-22 06:34:03 -070072
henrik.lundin02739d92017-05-04 06:09:06 -070073 struct Callbacks {
74 NetEqTestErrorCallback* error_callback = nullptr;
75 NetEqPostInsertPacket* post_insert_packet = nullptr;
76 NetEqGetAudioCallback* get_audio_callback = nullptr;
Ivo Creusen55de08e2018-09-03 11:49:27 +020077 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
henrik.lundin02739d92017-05-04 06:09:06 -070078 };
79
henrik.lundine8a77e32016-06-22 06:34:03 -070080 // Sets up the test with given configuration, codec mappings, input, ouput,
81 // and callback objects for error reporting.
82 NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010083 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070084 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010085 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070086 std::unique_ptr<NetEqInput> input,
87 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070088 Callbacks callbacks);
henrik.lundine8a77e32016-06-22 06:34:03 -070089
Ivo Creusen55de08e2018-09-03 11:49:27 +020090 ~NetEqTest() override;
henrik.lundine8a77e32016-06-22 06:34:03 -070091
92 // Runs the test. Returns the duration of the produced audio in ms.
93 int64_t Run();
Ivo Creusen55de08e2018-09-03 11:49:27 +020094 // Runs the simulation until we hit the next GetAudio event. If the simulation
95 // is finished, is_simulation_finished will be set to true in the returned
96 // SimulationStepResult.
97 SimulationStepResult RunToNextGetAudio() override;
98
99 void SetNextAction(Action next_operation) override;
100 NetEqState GetNetEqState() override;
henrik.lundine8a77e32016-06-22 06:34:03 -0700101
102 // Returns the statistics from NetEq.
103 NetEqNetworkStatistics SimulationStats();
Alex Narest7ff6ca52018-02-07 18:46:33 +0100104 NetEqLifetimeStatistics LifetimeStats() const;
henrik.lundine8a77e32016-06-22 06:34:03 -0700105
Henrik Lundin7687ad52018-07-02 10:14:46 +0200106 static DecoderMap StandardDecoderMap();
107
henrik.lundine8a77e32016-06-22 06:34:03 -0700108 private:
109 void RegisterDecoders(const DecoderMap& codecs);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000110 SimulatedClock clock_;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200111 absl::optional<Action> next_action_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200112 absl::optional<int> last_packet_time_ms_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700113 std::unique_ptr<NetEq> neteq_;
114 std::unique_ptr<NetEqInput> input_;
115 std::unique_ptr<AudioSink> output_;
henrik.lundin02739d92017-05-04 06:09:06 -0700116 Callbacks callbacks_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700117 int sample_rate_hz_;
Ivo Creusen4384f532018-09-07 17:19:56 +0200118 NetEqState current_state_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200119 NetEqOperationsAndState prev_ops_state_;
Ivo Creusen2db46b02018-12-14 16:49:12 +0100120 NetEqLifetimeStatistics prev_lifetime_stats_;
121 absl::optional<uint32_t> last_packet_timestamp_;
122 std::unique_ptr<std::ofstream> text_log_;
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_