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