blob: b477a17f22782750ceb0725f04687d5667748cda [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"
Niels Möllerd377f042018-02-13 15:03:43 +010016#include "rtc_base/timeutils.h"
17
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;
39 speech_type_ = kUndefined;
40 vad_activity_ = kVadUnknown;
41 profile_timestamp_ms_ = 0;
42}
43
44void AudioFrame::UpdateFrame(uint32_t timestamp,
Fredrik Solenberg03bfc732018-04-11 13:00:18 +020045 const int16_t* data,
46 size_t samples_per_channel,
47 int sample_rate_hz,
48 SpeechType speech_type,
49 VADActivity vad_activity,
50 size_t num_channels) {
Niels Möllerd377f042018-02-13 15:03:43 +010051 timestamp_ = timestamp;
52 samples_per_channel_ = samples_per_channel;
53 sample_rate_hz_ = sample_rate_hz;
54 speech_type_ = speech_type;
55 vad_activity_ = vad_activity;
56 num_channels_ = num_channels;
57
58 const size_t length = samples_per_channel * num_channels;
59 RTC_CHECK_LE(length, kMaxDataSizeSamples);
60 if (data != nullptr) {
61 memcpy(data_, data, sizeof(int16_t) * length);
62 muted_ = false;
63 } else {
64 muted_ = true;
65 }
66}
67
68void AudioFrame::CopyFrom(const AudioFrame& src) {
69 if (this == &src) return;
70
71 timestamp_ = src.timestamp_;
72 elapsed_time_ms_ = src.elapsed_time_ms_;
73 ntp_time_ms_ = src.ntp_time_ms_;
74 muted_ = src.muted();
75 samples_per_channel_ = src.samples_per_channel_;
76 sample_rate_hz_ = src.sample_rate_hz_;
77 speech_type_ = src.speech_type_;
78 vad_activity_ = src.vad_activity_;
79 num_channels_ = src.num_channels_;
80
81 const size_t length = samples_per_channel_ * num_channels_;
82 RTC_CHECK_LE(length, kMaxDataSizeSamples);
83 if (!src.muted()) {
84 memcpy(data_, src.data(), sizeof(int16_t) * length);
85 muted_ = false;
86 }
87}
88
89void AudioFrame::UpdateProfileTimeStamp() {
90 profile_timestamp_ms_ = rtc::TimeMillis();
91}
92
93int64_t AudioFrame::ElapsedProfileTimeMs() const {
94 if (profile_timestamp_ms_ == 0) {
95 // Profiling has not been activated.
96 return -1;
97 }
98 return rtc::TimeSince(profile_timestamp_ms_);
99}
100
101const int16_t* AudioFrame::data() const {
102 return muted_ ? empty_data() : data_;
103}
104
105// TODO(henrik.lundin) Can we skip zeroing the buffer?
106// See https://bugs.chromium.org/p/webrtc/issues/detail?id=5647.
107int16_t* AudioFrame::mutable_data() {
108 if (muted_) {
109 memset(data_, 0, kMaxDataSizeBytes);
110 muted_ = false;
111 }
112 return data_;
113}
114
115void AudioFrame::Mute() {
116 muted_ = true;
117}
118
119bool AudioFrame::muted() const { return muted_; }
120
Niels Möllerd377f042018-02-13 15:03:43 +0100121// static
122const int16_t* AudioFrame::empty_data() {
Tommi8f659a02018-04-20 12:35:14 +0200123 static int16_t* null_data = new int16_t[kMaxDataSizeSamples]();
124 return &null_data[0];
Niels Möllerd377f042018-02-13 15:03:43 +0100125}
126
127} // namespace webrtc