blob: cc078771faeb76aca32e601eee101056cd9ab8c7 [file] [log] [blame]
Niels Möllerd377f042018-02-13 15:03:43 +01001/*
2 * Copyright (c) 2018 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 API_AUDIO_AUDIO_FRAME_H_
12#define API_AUDIO_AUDIO_FRAME_H_
13
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020014#include <stddef.h>
Niels Möllera12c42a2018-07-25 16:05:48 +020015#include <stdint.h>
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020016
Niels Möllerd377f042018-02-13 15:03:43 +010017#include "rtc_base/constructormagic.h"
Niels Möllerd377f042018-02-13 15:03:43 +010018
19namespace webrtc {
20
21/* This class holds up to 60 ms of super-wideband (32 kHz) stereo audio. It
22 * allows for adding and subtracting frames while keeping track of the resulting
23 * states.
24 *
25 * Notes
26 * - This is a de-facto api, not designed for external use. The AudioFrame class
27 * is in need of overhaul or even replacement, and anyone depending on it
28 * should be prepared for that.
29 * - The total number of samples is samples_per_channel_ * num_channels_.
30 * - Stereo data is interleaved starting with the left channel.
31 */
32class AudioFrame {
33 public:
34 // Using constexpr here causes linker errors unless the variable also has an
35 // out-of-class definition, which is impractical in this header-only class.
36 // (This makes no sense because it compiles as an enum value, which we most
37 // certainly cannot take the address of, just fine.) C++17 introduces inline
38 // variables which should allow us to switch to constexpr and keep this a
39 // header-only class.
40 enum : size_t {
41 // Stereo, 32 kHz, 60 ms (2 * 32 * 60)
42 kMaxDataSizeSamples = 3840,
43 kMaxDataSizeBytes = kMaxDataSizeSamples * sizeof(int16_t),
44 };
45
Yves Gerey665174f2018-06-19 15:03:05 +020046 enum VADActivity { kVadActive = 0, kVadPassive = 1, kVadUnknown = 2 };
Niels Möllerd377f042018-02-13 15:03:43 +010047 enum SpeechType {
48 kNormalSpeech = 0,
49 kPLC = 1,
50 kCNG = 2,
51 kPLCCNG = 3,
52 kUndefined = 4
53 };
54
55 AudioFrame();
56
57 // Resets all members to their default state.
58 void Reset();
59 // Same as Reset(), but leaves mute state unchanged. Muting a frame requires
60 // the buffer to be zeroed on the next call to mutable_data(). Callers
61 // intending to write to the buffer immediately after Reset() can instead use
62 // ResetWithoutMuting() to skip this wasteful zeroing.
63 void ResetWithoutMuting();
64
Yves Gerey665174f2018-06-19 15:03:05 +020065 void UpdateFrame(uint32_t timestamp,
66 const int16_t* data,
67 size_t samples_per_channel,
68 int sample_rate_hz,
69 SpeechType speech_type,
70 VADActivity vad_activity,
Niels Möllerd377f042018-02-13 15:03:43 +010071 size_t num_channels = 1);
72
73 void CopyFrom(const AudioFrame& src);
74
75 // Sets a wall-time clock timestamp in milliseconds to be used for profiling
76 // of time between two points in the audio chain.
77 // Example:
78 // t0: UpdateProfileTimeStamp()
79 // t1: ElapsedProfileTimeMs() => t1 - t0 [msec]
80 void UpdateProfileTimeStamp();
81 // Returns the time difference between now and when UpdateProfileTimeStamp()
82 // was last called. Returns -1 if UpdateProfileTimeStamp() has not yet been
83 // called.
84 int64_t ElapsedProfileTimeMs() const;
85
86 // data() returns a zeroed static buffer if the frame is muted.
87 // mutable_frame() always returns a non-static buffer; the first call to
88 // mutable_frame() zeros the non-static buffer and marks the frame unmuted.
89 const int16_t* data() const;
90 int16_t* mutable_data();
91
92 // Prefer to mute frames using AudioFrameOperations::Mute.
93 void Mute();
94 // Frame is muted by default.
95 bool muted() const;
96
Niels Möllerd377f042018-02-13 15:03:43 +010097 // RTP timestamp of the first sample in the AudioFrame.
98 uint32_t timestamp_ = 0;
99 // Time since the first frame in milliseconds.
100 // -1 represents an uninitialized value.
101 int64_t elapsed_time_ms_ = -1;
102 // NTP time of the estimated capture time in local timebase in milliseconds.
103 // -1 represents an uninitialized value.
104 int64_t ntp_time_ms_ = -1;
105 size_t samples_per_channel_ = 0;
106 int sample_rate_hz_ = 0;
107 size_t num_channels_ = 0;
108 SpeechType speech_type_ = kUndefined;
109 VADActivity vad_activity_ = kVadUnknown;
110 // Monotonically increasing timestamp intended for profiling of audio frames.
111 // Typically used for measuring elapsed time between two different points in
112 // the audio path. No lock is used to save resources and we are thread safe
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200113 // by design. Also, absl::optional is not used since it will cause a "complex
Niels Möllerd377f042018-02-13 15:03:43 +0100114 // class/struct needs an explicit out-of-line destructor" build error.
115 int64_t profile_timestamp_ms_ = 0;
116
117 private:
118 // A permamently zeroed out buffer to represent muted frames. This is a
119 // header-only class, so the only way to avoid creating a separate empty
120 // buffer per translation unit is to wrap a static in an inline function.
121 static const int16_t* empty_data();
122
123 int16_t data_[kMaxDataSizeSamples];
124 bool muted_ = true;
125
126 RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame);
127};
128
129} // namespace webrtc
130
131#endif // API_AUDIO_AUDIO_FRAME_H_