1 /*
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 
11 #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_FAKE_DECODE_FROM_FILE_H_
12 #define MODULES_AUDIO_CODING_NETEQ_TOOLS_FAKE_DECODE_FROM_FILE_H_
13 
14 #include <memory>
15 
16 #include "absl/types/optional.h"
17 #include "api/array_view.h"
18 #include "api/audio_codecs/audio_decoder.h"
19 #include "modules/audio_coding/neteq/tools/input_audio_file.h"
20 
21 namespace webrtc {
22 namespace test {
23 // Provides an AudioDecoder implementation that delivers audio data from a file.
24 // The "encoded" input should contain information about what RTP timestamp the
25 // encoding represents, and how many samples the decoder should produce for that
26 // encoding. A helper method PrepareEncoded is provided to prepare such
27 // encodings. If packets are missing, as determined from the timestamps, the
28 // file reading will skip forward to match the loss.
29 class FakeDecodeFromFile : public AudioDecoder {
30  public:
FakeDecodeFromFile(std::unique_ptr<InputAudioFile> input,int sample_rate_hz,bool stereo)31   FakeDecodeFromFile(std::unique_ptr<InputAudioFile> input,
32                      int sample_rate_hz,
33                      bool stereo)
34       : input_(std::move(input)),
35         sample_rate_hz_(sample_rate_hz),
36         stereo_(stereo) {}
37 
38   ~FakeDecodeFromFile() = default;
39 
40   std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
41                                         uint32_t timestamp) override;
42 
Reset()43   void Reset() override {}
44 
SampleRateHz()45   int SampleRateHz() const override { return sample_rate_hz_; }
46 
Channels()47   size_t Channels() const override { return stereo_ ? 2 : 1; }
48 
49   int DecodeInternal(const uint8_t* encoded,
50                      size_t encoded_len,
51                      int sample_rate_hz,
52                      int16_t* decoded,
53                      SpeechType* speech_type) override;
54 
55   int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;
56 
57   // Helper method. Writes |timestamp|, |samples| and
58   // |original_payload_size_bytes| to |encoded| in a format that the
59   // FakeDecodeFromFile decoder will understand. |encoded| must be at least 12
60   // bytes long.
61   static void PrepareEncoded(uint32_t timestamp,
62                              size_t samples,
63                              size_t original_payload_size_bytes,
64                              rtc::ArrayView<uint8_t> encoded);
65 
66  private:
67   std::unique_ptr<InputAudioFile> input_;
68   absl::optional<uint32_t> next_timestamp_from_input_;
69   const int sample_rate_hz_;
70   const bool stereo_;
71   size_t last_decoded_length_ = 0;
72   bool cng_mode_ = false;
73 };
74 
75 }  // namespace test
76 }  // namespace webrtc
77 #endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_FAKE_DECODE_FROM_FILE_H_
78