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