blob: 726b9a98e34792705021c9726c1b62eda1c20bab [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
Henrik Lundinc49e9c22020-05-25 11:26:15 +020017#include <utility>
18
henrika2250b052019-07-04 11:27:52 +020019#include "api/audio/channel_layout.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000020#include "api/rtp_packet_infos.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/constructor_magic.h"
Niels Möllerd377f042018-02-13 15:03:43 +010022
23namespace webrtc {
24
henrika2a490652018-08-28 15:52:10 +020025/* This class holds up to 120 ms of super-wideband (32 kHz) stereo audio. It
Niels Möllerd377f042018-02-13 15:03:43 +010026 * allows for adding and subtracting frames while keeping track of the resulting
27 * states.
28 *
29 * Notes
30 * - This is a de-facto api, not designed for external use. The AudioFrame class
31 * is in need of overhaul or even replacement, and anyone depending on it
32 * should be prepared for that.
33 * - The total number of samples is samples_per_channel_ * num_channels_.
34 * - Stereo data is interleaved starting with the left channel.
35 */
36class AudioFrame {
37 public:
38 // Using constexpr here causes linker errors unless the variable also has an
39 // out-of-class definition, which is impractical in this header-only class.
40 // (This makes no sense because it compiles as an enum value, which we most
41 // certainly cannot take the address of, just fine.) C++17 introduces inline
42 // variables which should allow us to switch to constexpr and keep this a
43 // header-only class.
44 enum : size_t {
henrika2a490652018-08-28 15:52:10 +020045 // Stereo, 32 kHz, 120 ms (2 * 32 * 120)
46 // Stereo, 192 kHz, 20 ms (2 * 192 * 20)
47 kMaxDataSizeSamples = 7680,
Niels Möllerd377f042018-02-13 15:03:43 +010048 kMaxDataSizeBytes = kMaxDataSizeSamples * sizeof(int16_t),
49 };
50
Yves Gerey665174f2018-06-19 15:03:05 +020051 enum VADActivity { kVadActive = 0, kVadPassive = 1, kVadUnknown = 2 };
Niels Möllerd377f042018-02-13 15:03:43 +010052 enum SpeechType {
53 kNormalSpeech = 0,
54 kPLC = 1,
55 kCNG = 2,
56 kPLCCNG = 3,
Alex Narest5b5d97c2019-08-07 18:15:08 +020057 kCodecPLC = 5,
Niels Möllerd377f042018-02-13 15:03:43 +010058 kUndefined = 4
59 };
60
61 AudioFrame();
62
Henrik Lundinc49e9c22020-05-25 11:26:15 +020063 friend void swap(AudioFrame& a, AudioFrame& b);
64
Niels Möllerd377f042018-02-13 15:03:43 +010065 // Resets all members to their default state.
66 void Reset();
67 // Same as Reset(), but leaves mute state unchanged. Muting a frame requires
68 // the buffer to be zeroed on the next call to mutable_data(). Callers
69 // intending to write to the buffer immediately after Reset() can instead use
70 // ResetWithoutMuting() to skip this wasteful zeroing.
71 void ResetWithoutMuting();
72
Yves Gerey665174f2018-06-19 15:03:05 +020073 void UpdateFrame(uint32_t timestamp,
74 const int16_t* data,
75 size_t samples_per_channel,
76 int sample_rate_hz,
77 SpeechType speech_type,
78 VADActivity vad_activity,
Niels Möllerd377f042018-02-13 15:03:43 +010079 size_t num_channels = 1);
80
81 void CopyFrom(const AudioFrame& src);
82
83 // Sets a wall-time clock timestamp in milliseconds to be used for profiling
84 // of time between two points in the audio chain.
85 // Example:
86 // t0: UpdateProfileTimeStamp()
87 // t1: ElapsedProfileTimeMs() => t1 - t0 [msec]
88 void UpdateProfileTimeStamp();
89 // Returns the time difference between now and when UpdateProfileTimeStamp()
90 // was last called. Returns -1 if UpdateProfileTimeStamp() has not yet been
91 // called.
92 int64_t ElapsedProfileTimeMs() const;
93
94 // data() returns a zeroed static buffer if the frame is muted.
95 // mutable_frame() always returns a non-static buffer; the first call to
96 // mutable_frame() zeros the non-static buffer and marks the frame unmuted.
97 const int16_t* data() const;
98 int16_t* mutable_data();
99
100 // Prefer to mute frames using AudioFrameOperations::Mute.
101 void Mute();
102 // Frame is muted by default.
103 bool muted() const;
104
henrika2250b052019-07-04 11:27:52 +0200105 size_t max_16bit_samples() const { return kMaxDataSizeSamples; }
106 size_t samples_per_channel() const { return samples_per_channel_; }
107 size_t num_channels() const { return num_channels_; }
108 ChannelLayout channel_layout() const { return channel_layout_; }
109 int sample_rate_hz() const { return sample_rate_hz_; }
110
Minyue Lidea73ee2020-02-18 15:45:41 +0100111 void set_absolute_capture_timestamp_ms(
112 int64_t absolute_capture_time_stamp_ms) {
113 absolute_capture_timestamp_ms_ = absolute_capture_time_stamp_ms;
114 }
115
116 absl::optional<int64_t> absolute_capture_timestamp_ms() const {
117 return absolute_capture_timestamp_ms_;
118 }
119
Niels Möllerd377f042018-02-13 15:03:43 +0100120 // RTP timestamp of the first sample in the AudioFrame.
121 uint32_t timestamp_ = 0;
122 // Time since the first frame in milliseconds.
123 // -1 represents an uninitialized value.
124 int64_t elapsed_time_ms_ = -1;
125 // NTP time of the estimated capture time in local timebase in milliseconds.
126 // -1 represents an uninitialized value.
127 int64_t ntp_time_ms_ = -1;
128 size_t samples_per_channel_ = 0;
129 int sample_rate_hz_ = 0;
130 size_t num_channels_ = 0;
henrika2250b052019-07-04 11:27:52 +0200131 ChannelLayout channel_layout_ = CHANNEL_LAYOUT_NONE;
Niels Möllerd377f042018-02-13 15:03:43 +0100132 SpeechType speech_type_ = kUndefined;
133 VADActivity vad_activity_ = kVadUnknown;
134 // Monotonically increasing timestamp intended for profiling of audio frames.
135 // Typically used for measuring elapsed time between two different points in
136 // the audio path. No lock is used to save resources and we are thread safe
Minyue Lidea73ee2020-02-18 15:45:41 +0100137 // by design.
138 // TODO(nisse@webrtc.org): consider using absl::optional.
Niels Möllerd377f042018-02-13 15:03:43 +0100139 int64_t profile_timestamp_ms_ = 0;
140
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000141 // Information about packets used to assemble this audio frame. This is needed
Artem Titov0e61fdd2021-07-25 21:50:14 +0200142 // by `SourceTracker` when the frame is delivered to the RTCRtpReceiver's
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000143 // MediaStreamTrack, in order to implement getContributingSources(). See:
144 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
145 //
146 // TODO(bugs.webrtc.org/10757):
147 // Note that this information might not be fully accurate since we currently
148 // don't have a proper way to track it across the audio sync buffer. The
149 // sync buffer is the small sample-holding buffer located after the audio
150 // decoder and before where samples are assembled into output frames.
151 //
Artem Titov0e61fdd2021-07-25 21:50:14 +0200152 // `RtpPacketInfos` may also be empty if the audio samples did not come from
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000153 // RTP packets. E.g. if the audio were locally generated by packet loss
154 // concealment, comfort noise generation, etc.
155 RtpPacketInfos packet_infos_;
156
Niels Möllerd377f042018-02-13 15:03:43 +0100157 private:
henrika2250b052019-07-04 11:27:52 +0200158 // A permanently zeroed out buffer to represent muted frames. This is a
Niels Möllerd377f042018-02-13 15:03:43 +0100159 // header-only class, so the only way to avoid creating a separate empty
160 // buffer per translation unit is to wrap a static in an inline function.
161 static const int16_t* empty_data();
162
163 int16_t data_[kMaxDataSizeSamples];
164 bool muted_ = true;
165
Minyue Lidea73ee2020-02-18 15:45:41 +0100166 // Absolute capture timestamp when this audio frame was originally captured.
167 // This is only valid for audio frames captured on this machine. The absolute
Artem Titov0e61fdd2021-07-25 21:50:14 +0200168 // capture timestamp of a received frame is found in `packet_infos_`.
Minyue Lidea73ee2020-02-18 15:45:41 +0100169 // This timestamp MUST be based on the same clock as rtc::TimeMillis().
170 absl::optional<int64_t> absolute_capture_timestamp_ms_;
171
Niels Möllerd377f042018-02-13 15:03:43 +0100172 RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame);
173};
174
175} // namespace webrtc
176
177#endif // API_AUDIO_AUDIO_FRAME_H_