blob: d5dcb5f7888020a08f4fc4ccfeaf2673d4c90a9a [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
henrika2250b052019-07-04 11:27:52 +020017#include "api/audio/channel_layout.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000018#include "api/rtp_packet_infos.h"
Niels Möllerd377f042018-02-13 15:03:43 +010019
20namespace webrtc {
21
henrika2a490652018-08-28 15:52:10 +020022/* This class holds up to 120 ms of super-wideband (32 kHz) stereo audio. It
Niels Möllerd377f042018-02-13 15:03:43 +010023 * allows for adding and subtracting frames while keeping track of the resulting
24 * states.
25 *
26 * Notes
27 * - This is a de-facto api, not designed for external use. The AudioFrame class
28 * is in need of overhaul or even replacement, and anyone depending on it
29 * should be prepared for that.
30 * - The total number of samples is samples_per_channel_ * num_channels_.
31 * - Stereo data is interleaved starting with the left channel.
32 */
33class AudioFrame {
34 public:
35 // Using constexpr here causes linker errors unless the variable also has an
36 // out-of-class definition, which is impractical in this header-only class.
37 // (This makes no sense because it compiles as an enum value, which we most
38 // certainly cannot take the address of, just fine.) C++17 introduces inline
39 // variables which should allow us to switch to constexpr and keep this a
40 // header-only class.
41 enum : size_t {
henrika2a490652018-08-28 15:52:10 +020042 // Stereo, 32 kHz, 120 ms (2 * 32 * 120)
43 // Stereo, 192 kHz, 20 ms (2 * 192 * 20)
44 kMaxDataSizeSamples = 7680,
Niels Möllerd377f042018-02-13 15:03:43 +010045 kMaxDataSizeBytes = kMaxDataSizeSamples * sizeof(int16_t),
46 };
47
Yves Gerey665174f2018-06-19 15:03:05 +020048 enum VADActivity { kVadActive = 0, kVadPassive = 1, kVadUnknown = 2 };
Niels Möllerd377f042018-02-13 15:03:43 +010049 enum SpeechType {
50 kNormalSpeech = 0,
51 kPLC = 1,
52 kCNG = 2,
53 kPLCCNG = 3,
Alex Narest5b5d97c2019-08-07 18:15:08 +020054 kCodecPLC = 5,
Niels Möllerd377f042018-02-13 15:03:43 +010055 kUndefined = 4
56 };
57
58 AudioFrame();
59
Byoungchan Leec065e732022-01-18 09:35:48 +090060 AudioFrame(const AudioFrame&) = delete;
61 AudioFrame& operator=(const AudioFrame&) = delete;
62
Niels Möllerd377f042018-02-13 15:03:43 +010063 // 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
Yves Gerey665174f2018-06-19 15:03:05 +020071 void UpdateFrame(uint32_t timestamp,
72 const int16_t* data,
73 size_t samples_per_channel,
74 int sample_rate_hz,
75 SpeechType speech_type,
76 VADActivity vad_activity,
Niels Möllerd377f042018-02-13 15:03:43 +010077 size_t num_channels = 1);
78
79 void CopyFrom(const AudioFrame& src);
80
81 // Sets a wall-time clock timestamp in milliseconds to be used for profiling
82 // of time between two points in the audio chain.
83 // Example:
84 // t0: UpdateProfileTimeStamp()
85 // t1: ElapsedProfileTimeMs() => t1 - t0 [msec]
86 void UpdateProfileTimeStamp();
87 // Returns the time difference between now and when UpdateProfileTimeStamp()
88 // was last called. Returns -1 if UpdateProfileTimeStamp() has not yet been
89 // called.
90 int64_t ElapsedProfileTimeMs() const;
91
92 // data() returns a zeroed static buffer if the frame is muted.
93 // mutable_frame() always returns a non-static buffer; the first call to
94 // mutable_frame() zeros the non-static buffer and marks the frame unmuted.
95 const int16_t* data() const;
96 int16_t* mutable_data();
97
98 // Prefer to mute frames using AudioFrameOperations::Mute.
99 void Mute();
100 // Frame is muted by default.
101 bool muted() const;
102
henrika2250b052019-07-04 11:27:52 +0200103 size_t max_16bit_samples() const { return kMaxDataSizeSamples; }
104 size_t samples_per_channel() const { return samples_per_channel_; }
105 size_t num_channels() const { return num_channels_; }
106 ChannelLayout channel_layout() const { return channel_layout_; }
107 int sample_rate_hz() const { return sample_rate_hz_; }
108
Minyue Lidea73ee2020-02-18 15:45:41 +0100109 void set_absolute_capture_timestamp_ms(
110 int64_t absolute_capture_time_stamp_ms) {
111 absolute_capture_timestamp_ms_ = absolute_capture_time_stamp_ms;
112 }
113
114 absl::optional<int64_t> absolute_capture_timestamp_ms() const {
115 return absolute_capture_timestamp_ms_;
116 }
117
Niels Möllerd377f042018-02-13 15:03:43 +0100118 // 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;
henrika2250b052019-07-04 11:27:52 +0200129 ChannelLayout channel_layout_ = CHANNEL_LAYOUT_NONE;
Niels Möllerd377f042018-02-13 15:03:43 +0100130 SpeechType speech_type_ = kUndefined;
131 VADActivity vad_activity_ = kVadUnknown;
132 // Monotonically increasing timestamp intended for profiling of audio frames.
133 // Typically used for measuring elapsed time between two different points in
134 // the audio path. No lock is used to save resources and we are thread safe
Minyue Lidea73ee2020-02-18 15:45:41 +0100135 // by design.
136 // TODO(nisse@webrtc.org): consider using absl::optional.
Niels Möllerd377f042018-02-13 15:03:43 +0100137 int64_t profile_timestamp_ms_ = 0;
138
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000139 // Information about packets used to assemble this audio frame. This is needed
Artem Titov0e61fdd2021-07-25 21:50:14 +0200140 // by `SourceTracker` when the frame is delivered to the RTCRtpReceiver's
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000141 // MediaStreamTrack, in order to implement getContributingSources(). See:
142 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
143 //
144 // TODO(bugs.webrtc.org/10757):
145 // Note that this information might not be fully accurate since we currently
146 // don't have a proper way to track it across the audio sync buffer. The
147 // sync buffer is the small sample-holding buffer located after the audio
148 // decoder and before where samples are assembled into output frames.
149 //
Artem Titov0e61fdd2021-07-25 21:50:14 +0200150 // `RtpPacketInfos` may also be empty if the audio samples did not come from
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000151 // RTP packets. E.g. if the audio were locally generated by packet loss
152 // concealment, comfort noise generation, etc.
153 RtpPacketInfos packet_infos_;
154
Niels Möllerd377f042018-02-13 15:03:43 +0100155 private:
henrika2250b052019-07-04 11:27:52 +0200156 // A permanently zeroed out buffer to represent muted frames. This is a
Niels Möllerd377f042018-02-13 15:03:43 +0100157 // header-only class, so the only way to avoid creating a separate empty
158 // buffer per translation unit is to wrap a static in an inline function.
159 static const int16_t* empty_data();
160
161 int16_t data_[kMaxDataSizeSamples];
162 bool muted_ = true;
163
Minyue Lidea73ee2020-02-18 15:45:41 +0100164 // Absolute capture timestamp when this audio frame was originally captured.
165 // This is only valid for audio frames captured on this machine. The absolute
Artem Titov0e61fdd2021-07-25 21:50:14 +0200166 // capture timestamp of a received frame is found in `packet_infos_`.
Minyue Lidea73ee2020-02-18 15:45:41 +0100167 // This timestamp MUST be based on the same clock as rtc::TimeMillis().
168 absl::optional<int64_t> absolute_capture_timestamp_ms_;
Niels Möllerd377f042018-02-13 15:03:43 +0100169};
170
171} // namespace webrtc
172
173#endif // API_AUDIO_AUDIO_FRAME_H_