blob: 7660e75ec3100afad41c77204fd161d33b5ec82f [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"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
Niels Möllerd377f042018-02-13 15:03:43 +010020
21namespace webrtc {
22
henrika2a490652018-08-28 15:52:10 +020023/* This class holds up to 120 ms of super-wideband (32 kHz) stereo audio. It
Niels Möllerd377f042018-02-13 15:03:43 +010024 * 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 {
henrika2a490652018-08-28 15:52:10 +020043 // Stereo, 32 kHz, 120 ms (2 * 32 * 120)
44 // Stereo, 192 kHz, 20 ms (2 * 192 * 20)
45 kMaxDataSizeSamples = 7680,
Niels Möllerd377f042018-02-13 15:03:43 +010046 kMaxDataSizeBytes = kMaxDataSizeSamples * sizeof(int16_t),
47 };
48
Yves Gerey665174f2018-06-19 15:03:05 +020049 enum VADActivity { kVadActive = 0, kVadPassive = 1, kVadUnknown = 2 };
Niels Möllerd377f042018-02-13 15:03:43 +010050 enum SpeechType {
51 kNormalSpeech = 0,
52 kPLC = 1,
53 kCNG = 2,
54 kPLCCNG = 3,
55 kUndefined = 4
56 };
57
58 AudioFrame();
59
60 // Resets all members to their default state.
61 void Reset();
62 // Same as Reset(), but leaves mute state unchanged. Muting a frame requires
63 // the buffer to be zeroed on the next call to mutable_data(). Callers
64 // intending to write to the buffer immediately after Reset() can instead use
65 // ResetWithoutMuting() to skip this wasteful zeroing.
66 void ResetWithoutMuting();
67
Yves Gerey665174f2018-06-19 15:03:05 +020068 void UpdateFrame(uint32_t timestamp,
69 const int16_t* data,
70 size_t samples_per_channel,
71 int sample_rate_hz,
72 SpeechType speech_type,
73 VADActivity vad_activity,
Niels Möllerd377f042018-02-13 15:03:43 +010074 size_t num_channels = 1);
75
76 void CopyFrom(const AudioFrame& src);
77
78 // Sets a wall-time clock timestamp in milliseconds to be used for profiling
79 // of time between two points in the audio chain.
80 // Example:
81 // t0: UpdateProfileTimeStamp()
82 // t1: ElapsedProfileTimeMs() => t1 - t0 [msec]
83 void UpdateProfileTimeStamp();
84 // Returns the time difference between now and when UpdateProfileTimeStamp()
85 // was last called. Returns -1 if UpdateProfileTimeStamp() has not yet been
86 // called.
87 int64_t ElapsedProfileTimeMs() const;
88
89 // data() returns a zeroed static buffer if the frame is muted.
90 // mutable_frame() always returns a non-static buffer; the first call to
91 // mutable_frame() zeros the non-static buffer and marks the frame unmuted.
92 const int16_t* data() const;
93 int16_t* mutable_data();
94
95 // Prefer to mute frames using AudioFrameOperations::Mute.
96 void Mute();
97 // Frame is muted by default.
98 bool muted() const;
99
henrika2250b052019-07-04 11:27:52 +0200100 size_t max_16bit_samples() const { return kMaxDataSizeSamples; }
101 size_t samples_per_channel() const { return samples_per_channel_; }
102 size_t num_channels() const { return num_channels_; }
103 ChannelLayout channel_layout() const { return channel_layout_; }
104 int sample_rate_hz() const { return sample_rate_hz_; }
105
Niels Möllerd377f042018-02-13 15:03:43 +0100106 // RTP timestamp of the first sample in the AudioFrame.
107 uint32_t timestamp_ = 0;
108 // Time since the first frame in milliseconds.
109 // -1 represents an uninitialized value.
110 int64_t elapsed_time_ms_ = -1;
111 // NTP time of the estimated capture time in local timebase in milliseconds.
112 // -1 represents an uninitialized value.
113 int64_t ntp_time_ms_ = -1;
114 size_t samples_per_channel_ = 0;
115 int sample_rate_hz_ = 0;
116 size_t num_channels_ = 0;
henrika2250b052019-07-04 11:27:52 +0200117 ChannelLayout channel_layout_ = CHANNEL_LAYOUT_NONE;
Niels Möllerd377f042018-02-13 15:03:43 +0100118 SpeechType speech_type_ = kUndefined;
119 VADActivity vad_activity_ = kVadUnknown;
120 // Monotonically increasing timestamp intended for profiling of audio frames.
121 // Typically used for measuring elapsed time between two different points in
122 // the audio path. No lock is used to save resources and we are thread safe
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200123 // by design. Also, absl::optional is not used since it will cause a "complex
Niels Möllerd377f042018-02-13 15:03:43 +0100124 // class/struct needs an explicit out-of-line destructor" build error.
125 int64_t profile_timestamp_ms_ = 0;
126
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000127 // Information about packets used to assemble this audio frame. This is needed
128 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
129 // MediaStreamTrack, in order to implement getContributingSources(). See:
130 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
131 //
132 // TODO(bugs.webrtc.org/10757):
133 // Note that this information might not be fully accurate since we currently
134 // don't have a proper way to track it across the audio sync buffer. The
135 // sync buffer is the small sample-holding buffer located after the audio
136 // decoder and before where samples are assembled into output frames.
137 //
138 // |RtpPacketInfos| may also be empty if the audio samples did not come from
139 // RTP packets. E.g. if the audio were locally generated by packet loss
140 // concealment, comfort noise generation, etc.
141 RtpPacketInfos packet_infos_;
142
Niels Möllerd377f042018-02-13 15:03:43 +0100143 private:
henrika2250b052019-07-04 11:27:52 +0200144 // A permanently zeroed out buffer to represent muted frames. This is a
Niels Möllerd377f042018-02-13 15:03:43 +0100145 // header-only class, so the only way to avoid creating a separate empty
146 // buffer per translation unit is to wrap a static in an inline function.
147 static const int16_t* empty_data();
148
149 int16_t data_[kMaxDataSizeSamples];
150 bool muted_ = true;
151
152 RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame);
153};
154
155} // namespace webrtc
156
157#endif // API_AUDIO_AUDIO_FRAME_H_