blob: 6fb0ffaa0ce1811d12066da28a53bae7b3b5a216 [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
13#include "rtc_base/checks.h"
Niels Möllerd377f042018-02-13 15:03:43 +010014#include "rtc_base/timeutils.h"
15
16namespace webrtc {
17
18AudioFrame::AudioFrame() {
19 // Visual Studio doesn't like this in the class definition.
20 static_assert(sizeof(data_) == kMaxDataSizeBytes, "kMaxDataSizeBytes");
21}
22
23void AudioFrame::Reset() {
24 ResetWithoutMuting();
25 muted_ = true;
26}
27
28void AudioFrame::ResetWithoutMuting() {
29 // TODO(wu): Zero is a valid value for |timestamp_|. We should initialize
30 // to an invalid value, or add a new member to indicate invalidity.
31 timestamp_ = 0;
32 elapsed_time_ms_ = -1;
33 ntp_time_ms_ = -1;
34 samples_per_channel_ = 0;
35 sample_rate_hz_ = 0;
36 num_channels_ = 0;
37 speech_type_ = kUndefined;
38 vad_activity_ = kVadUnknown;
39 profile_timestamp_ms_ = 0;
40}
41
42void AudioFrame::UpdateFrame(uint32_t timestamp,
Fredrik Solenberg03bfc732018-04-11 13:00:18 +020043 const int16_t* data,
44 size_t samples_per_channel,
45 int sample_rate_hz,
46 SpeechType speech_type,
47 VADActivity vad_activity,
48 size_t num_channels) {
Niels Möllerd377f042018-02-13 15:03:43 +010049 timestamp_ = timestamp;
50 samples_per_channel_ = samples_per_channel;
51 sample_rate_hz_ = sample_rate_hz;
52 speech_type_ = speech_type;
53 vad_activity_ = vad_activity;
54 num_channels_ = num_channels;
55
56 const size_t length = samples_per_channel * num_channels;
57 RTC_CHECK_LE(length, kMaxDataSizeSamples);
58 if (data != nullptr) {
59 memcpy(data_, data, sizeof(int16_t) * length);
60 muted_ = false;
61 } else {
62 muted_ = true;
63 }
64}
65
66void AudioFrame::CopyFrom(const AudioFrame& src) {
67 if (this == &src) return;
68
69 timestamp_ = src.timestamp_;
70 elapsed_time_ms_ = src.elapsed_time_ms_;
71 ntp_time_ms_ = src.ntp_time_ms_;
72 muted_ = src.muted();
73 samples_per_channel_ = src.samples_per_channel_;
74 sample_rate_hz_ = src.sample_rate_hz_;
75 speech_type_ = src.speech_type_;
76 vad_activity_ = src.vad_activity_;
77 num_channels_ = src.num_channels_;
78
79 const size_t length = samples_per_channel_ * num_channels_;
80 RTC_CHECK_LE(length, kMaxDataSizeSamples);
81 if (!src.muted()) {
82 memcpy(data_, src.data(), sizeof(int16_t) * length);
83 muted_ = false;
84 }
85}
86
87void AudioFrame::UpdateProfileTimeStamp() {
88 profile_timestamp_ms_ = rtc::TimeMillis();
89}
90
91int64_t AudioFrame::ElapsedProfileTimeMs() const {
92 if (profile_timestamp_ms_ == 0) {
93 // Profiling has not been activated.
94 return -1;
95 }
96 return rtc::TimeSince(profile_timestamp_ms_);
97}
98
99const int16_t* AudioFrame::data() const {
100 return muted_ ? empty_data() : data_;
101}
102
103// TODO(henrik.lundin) Can we skip zeroing the buffer?
104// See https://bugs.chromium.org/p/webrtc/issues/detail?id=5647.
105int16_t* AudioFrame::mutable_data() {
106 if (muted_) {
107 memset(data_, 0, kMaxDataSizeBytes);
108 muted_ = false;
109 }
110 return data_;
111}
112
113void AudioFrame::Mute() {
114 muted_ = true;
115}
116
117bool AudioFrame::muted() const { return muted_; }
118
Niels Möllerd377f042018-02-13 15:03:43 +0100119// static
120const int16_t* AudioFrame::empty_data() {
121 static const int16_t kEmptyData[kMaxDataSizeSamples] = {0};
122 static_assert(sizeof(kEmptyData) == kMaxDataSizeBytes, "kMaxDataSizeBytes");
123 return kEmptyData;
124}
125
126} // namespace webrtc