blob: 0b3a2b6d85c1f6ce1a94020ff28c550e09973e41 [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
Niels Möllerd377f042018-02-13 15:03:43 +010011#include "api/audio/audio_frame.h"
12
Raphael Kubo da Costa7ce30912018-04-16 11:17:10 +020013#include <string.h>
14
Niels Möllerd377f042018-02-13 15:03:43 +010015#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/time_utils.h"
Niels Möllerd377f042018-02-13 15:03:43 +010017
18namespace webrtc {
19
20AudioFrame::AudioFrame() {
21 // Visual Studio doesn't like this in the class definition.
22 static_assert(sizeof(data_) == kMaxDataSizeBytes, "kMaxDataSizeBytes");
23}
24
25void AudioFrame::Reset() {
26 ResetWithoutMuting();
27 muted_ = true;
28}
29
30void AudioFrame::ResetWithoutMuting() {
31 // TODO(wu): Zero is a valid value for |timestamp_|. We should initialize
32 // to an invalid value, or add a new member to indicate invalidity.
33 timestamp_ = 0;
34 elapsed_time_ms_ = -1;
35 ntp_time_ms_ = -1;
36 samples_per_channel_ = 0;
37 sample_rate_hz_ = 0;
38 num_channels_ = 0;
henrika2250b052019-07-04 11:27:52 +020039 channel_layout_ = CHANNEL_LAYOUT_NONE;
Niels Möllerd377f042018-02-13 15:03:43 +010040 speech_type_ = kUndefined;
41 vad_activity_ = kVadUnknown;
42 profile_timestamp_ms_ = 0;
43}
44
45void AudioFrame::UpdateFrame(uint32_t timestamp,
Fredrik Solenberg03bfc732018-04-11 13:00:18 +020046 const int16_t* data,
47 size_t samples_per_channel,
48 int sample_rate_hz,
49 SpeechType speech_type,
50 VADActivity vad_activity,
51 size_t num_channels) {
Niels Möllerd377f042018-02-13 15:03:43 +010052 timestamp_ = timestamp;
53 samples_per_channel_ = samples_per_channel;
54 sample_rate_hz_ = sample_rate_hz;
55 speech_type_ = speech_type;
56 vad_activity_ = vad_activity;
57 num_channels_ = num_channels;
henrika2250b052019-07-04 11:27:52 +020058 channel_layout_ = GuessChannelLayout(num_channels);
59 if (channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED) {
60 RTC_DCHECK_EQ(num_channels, ChannelLayoutToChannelCount(channel_layout_));
61 }
Niels Möllerd377f042018-02-13 15:03:43 +010062
63 const size_t length = samples_per_channel * num_channels;
64 RTC_CHECK_LE(length, kMaxDataSizeSamples);
65 if (data != nullptr) {
66 memcpy(data_, data, sizeof(int16_t) * length);
67 muted_ = false;
68 } else {
69 muted_ = true;
70 }
71}
72
73void AudioFrame::CopyFrom(const AudioFrame& src) {
Yves Gerey665174f2018-06-19 15:03:05 +020074 if (this == &src)
75 return;
Niels Möllerd377f042018-02-13 15:03:43 +010076
77 timestamp_ = src.timestamp_;
78 elapsed_time_ms_ = src.elapsed_time_ms_;
79 ntp_time_ms_ = src.ntp_time_ms_;
80 muted_ = src.muted();
81 samples_per_channel_ = src.samples_per_channel_;
82 sample_rate_hz_ = src.sample_rate_hz_;
83 speech_type_ = src.speech_type_;
84 vad_activity_ = src.vad_activity_;
85 num_channels_ = src.num_channels_;
henrika2250b052019-07-04 11:27:52 +020086 channel_layout_ = src.channel_layout_;
Niels Möllerd377f042018-02-13 15:03:43 +010087
88 const size_t length = samples_per_channel_ * num_channels_;
89 RTC_CHECK_LE(length, kMaxDataSizeSamples);
90 if (!src.muted()) {
91 memcpy(data_, src.data(), sizeof(int16_t) * length);
92 muted_ = false;
93 }
94}
95
96void AudioFrame::UpdateProfileTimeStamp() {
97 profile_timestamp_ms_ = rtc::TimeMillis();
98}
99
100int64_t AudioFrame::ElapsedProfileTimeMs() const {
101 if (profile_timestamp_ms_ == 0) {
102 // Profiling has not been activated.
103 return -1;
104 }
105 return rtc::TimeSince(profile_timestamp_ms_);
106}
107
108const int16_t* AudioFrame::data() const {
109 return muted_ ? empty_data() : data_;
110}
111
112// TODO(henrik.lundin) Can we skip zeroing the buffer?
113// See https://bugs.chromium.org/p/webrtc/issues/detail?id=5647.
114int16_t* AudioFrame::mutable_data() {
115 if (muted_) {
116 memset(data_, 0, kMaxDataSizeBytes);
117 muted_ = false;
118 }
119 return data_;
120}
121
122void AudioFrame::Mute() {
123 muted_ = true;
124}
125
Yves Gerey665174f2018-06-19 15:03:05 +0200126bool AudioFrame::muted() const {
127 return muted_;
128}
Niels Möllerd377f042018-02-13 15:03:43 +0100129
Niels Möllerd377f042018-02-13 15:03:43 +0100130// static
131const int16_t* AudioFrame::empty_data() {
Tommi8f659a02018-04-20 12:35:14 +0200132 static int16_t* null_data = new int16_t[kMaxDataSizeSamples]();
133 return &null_data[0];
Niels Möllerd377f042018-02-13 15:03:43 +0100134}
135
136} // namespace webrtc