blob: 70eb701d6bf2c5cc93d46b51efa7ead9cd89af66 [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
Chen Xing3e8ef942019-07-01 17:16:32 +020017#include "api/rtp_packet_infos.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.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,
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
Yves Gerey665174f2018-06-19 15:03:05 +020067 void UpdateFrame(uint32_t timestamp,
68 const int16_t* data,
69 size_t samples_per_channel,
70 int sample_rate_hz,
71 SpeechType speech_type,
72 VADActivity vad_activity,
Niels Möllerd377f042018-02-13 15:03:43 +010073 size_t num_channels = 1);
74
75 void CopyFrom(const AudioFrame& src);
76
77 // Sets a wall-time clock timestamp in milliseconds to be used for profiling
78 // of time between two points in the audio chain.
79 // Example:
80 // t0: UpdateProfileTimeStamp()
81 // t1: ElapsedProfileTimeMs() => t1 - t0 [msec]
82 void UpdateProfileTimeStamp();
83 // Returns the time difference between now and when UpdateProfileTimeStamp()
84 // was last called. Returns -1 if UpdateProfileTimeStamp() has not yet been
85 // called.
86 int64_t ElapsedProfileTimeMs() const;
87
88 // data() returns a zeroed static buffer if the frame is muted.
89 // mutable_frame() always returns a non-static buffer; the first call to
90 // mutable_frame() zeros the non-static buffer and marks the frame unmuted.
91 const int16_t* data() const;
92 int16_t* mutable_data();
93
94 // Prefer to mute frames using AudioFrameOperations::Mute.
95 void Mute();
96 // Frame is muted by default.
97 bool muted() const;
98
Niels Möllerd377f042018-02-13 15:03:43 +010099 // RTP timestamp of the first sample in the AudioFrame.
100 uint32_t timestamp_ = 0;
101 // Time since the first frame in milliseconds.
102 // -1 represents an uninitialized value.
103 int64_t elapsed_time_ms_ = -1;
104 // NTP time of the estimated capture time in local timebase in milliseconds.
105 // -1 represents an uninitialized value.
106 int64_t ntp_time_ms_ = -1;
107 size_t samples_per_channel_ = 0;
108 int sample_rate_hz_ = 0;
109 size_t num_channels_ = 0;
110 SpeechType speech_type_ = kUndefined;
111 VADActivity vad_activity_ = kVadUnknown;
112 // Monotonically increasing timestamp intended for profiling of audio frames.
113 // Typically used for measuring elapsed time between two different points in
114 // the audio path. No lock is used to save resources and we are thread safe
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200115 // by design. Also, absl::optional is not used since it will cause a "complex
Niels Möllerd377f042018-02-13 15:03:43 +0100116 // class/struct needs an explicit out-of-line destructor" build error.
117 int64_t profile_timestamp_ms_ = 0;
118
Chen Xing3e8ef942019-07-01 17:16:32 +0200119 // Information about packets used to assemble this audio frame. This is needed
120 // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
121 // MediaStreamTrack, in order to implement getContributingSources(). See:
122 // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
123 //
124 // TODO(bugs.webrtc.org/10757):
125 // Note that this information might not be fully accurate since we currently
126 // don't have a proper way to track it across the audio sync buffer. The
127 // sync buffer is the small sample-holding buffer located after the audio
128 // decoder and before where samples are assembled into output frames.
129 //
130 // |RtpPacketInfos| may also be empty if the audio samples did not come from
131 // RTP packets. E.g. if the audio were locally generated by packet loss
132 // concealment, comfort noise generation, etc.
133 RtpPacketInfos packet_infos_;
134
Niels Möllerd377f042018-02-13 15:03:43 +0100135 private:
136 // A permamently zeroed out buffer to represent muted frames. This is a
137 // header-only class, so the only way to avoid creating a separate empty
138 // buffer per translation unit is to wrap a static in an inline function.
139 static const int16_t* empty_data();
140
141 int16_t data_[kMaxDataSizeSamples];
142 bool muted_ = true;
143
144 RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame);
145};
146
147} // namespace webrtc
148
149#endif // API_AUDIO_AUDIO_FRAME_H_