Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 1 | /* |
| 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öller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 11 | #include "api/audio/audio_frame.h" |
| 12 | |
Raphael Kubo da Costa | 7ce3091 | 2018-04-16 11:17:10 +0200 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | AudioFrame::AudioFrame() { |
| 21 | // Visual Studio doesn't like this in the class definition. |
| 22 | static_assert(sizeof(data_) == kMaxDataSizeBytes, "kMaxDataSizeBytes"); |
| 23 | } |
| 24 | |
| 25 | void AudioFrame::Reset() { |
| 26 | ResetWithoutMuting(); |
| 27 | muted_ = true; |
| 28 | } |
| 29 | |
| 30 | void 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; |
henrika | 2250b05 | 2019-07-04 11:27:52 +0200 | [diff] [blame] | 39 | channel_layout_ = CHANNEL_LAYOUT_NONE; |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 40 | speech_type_ = kUndefined; |
| 41 | vad_activity_ = kVadUnknown; |
| 42 | profile_timestamp_ms_ = 0; |
Alessio Bazzica | 8f319a3 | 2019-07-24 16:47:02 +0000 | [diff] [blame^] | 43 | packet_infos_ = RtpPacketInfos(); |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void AudioFrame::UpdateFrame(uint32_t timestamp, |
Fredrik Solenberg | 03bfc73 | 2018-04-11 13:00:18 +0200 | [diff] [blame] | 47 | const int16_t* data, |
| 48 | size_t samples_per_channel, |
| 49 | int sample_rate_hz, |
| 50 | SpeechType speech_type, |
| 51 | VADActivity vad_activity, |
| 52 | size_t num_channels) { |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 53 | timestamp_ = timestamp; |
| 54 | samples_per_channel_ = samples_per_channel; |
| 55 | sample_rate_hz_ = sample_rate_hz; |
| 56 | speech_type_ = speech_type; |
| 57 | vad_activity_ = vad_activity; |
| 58 | num_channels_ = num_channels; |
henrika | 2250b05 | 2019-07-04 11:27:52 +0200 | [diff] [blame] | 59 | channel_layout_ = GuessChannelLayout(num_channels); |
| 60 | if (channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED) { |
| 61 | RTC_DCHECK_EQ(num_channels, ChannelLayoutToChannelCount(channel_layout_)); |
| 62 | } |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 63 | |
| 64 | const size_t length = samples_per_channel * num_channels; |
| 65 | RTC_CHECK_LE(length, kMaxDataSizeSamples); |
| 66 | if (data != nullptr) { |
| 67 | memcpy(data_, data, sizeof(int16_t) * length); |
| 68 | muted_ = false; |
| 69 | } else { |
| 70 | muted_ = true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void AudioFrame::CopyFrom(const AudioFrame& src) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 75 | if (this == &src) |
| 76 | return; |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 77 | |
| 78 | timestamp_ = src.timestamp_; |
| 79 | elapsed_time_ms_ = src.elapsed_time_ms_; |
| 80 | ntp_time_ms_ = src.ntp_time_ms_; |
Alessio Bazzica | 8f319a3 | 2019-07-24 16:47:02 +0000 | [diff] [blame^] | 81 | packet_infos_ = src.packet_infos_; |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 82 | muted_ = src.muted(); |
| 83 | samples_per_channel_ = src.samples_per_channel_; |
| 84 | sample_rate_hz_ = src.sample_rate_hz_; |
| 85 | speech_type_ = src.speech_type_; |
| 86 | vad_activity_ = src.vad_activity_; |
| 87 | num_channels_ = src.num_channels_; |
henrika | 2250b05 | 2019-07-04 11:27:52 +0200 | [diff] [blame] | 88 | channel_layout_ = src.channel_layout_; |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 89 | |
| 90 | const size_t length = samples_per_channel_ * num_channels_; |
| 91 | RTC_CHECK_LE(length, kMaxDataSizeSamples); |
| 92 | if (!src.muted()) { |
| 93 | memcpy(data_, src.data(), sizeof(int16_t) * length); |
| 94 | muted_ = false; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void AudioFrame::UpdateProfileTimeStamp() { |
| 99 | profile_timestamp_ms_ = rtc::TimeMillis(); |
| 100 | } |
| 101 | |
| 102 | int64_t AudioFrame::ElapsedProfileTimeMs() const { |
| 103 | if (profile_timestamp_ms_ == 0) { |
| 104 | // Profiling has not been activated. |
| 105 | return -1; |
| 106 | } |
| 107 | return rtc::TimeSince(profile_timestamp_ms_); |
| 108 | } |
| 109 | |
| 110 | const int16_t* AudioFrame::data() const { |
| 111 | return muted_ ? empty_data() : data_; |
| 112 | } |
| 113 | |
| 114 | // TODO(henrik.lundin) Can we skip zeroing the buffer? |
| 115 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5647. |
| 116 | int16_t* AudioFrame::mutable_data() { |
| 117 | if (muted_) { |
| 118 | memset(data_, 0, kMaxDataSizeBytes); |
| 119 | muted_ = false; |
| 120 | } |
| 121 | return data_; |
| 122 | } |
| 123 | |
| 124 | void AudioFrame::Mute() { |
| 125 | muted_ = true; |
| 126 | } |
| 127 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | bool AudioFrame::muted() const { |
| 129 | return muted_; |
| 130 | } |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 131 | |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 132 | // static |
| 133 | const int16_t* AudioFrame::empty_data() { |
Tommi | 8f659a0 | 2018-04-20 12:35:14 +0200 | [diff] [blame] | 134 | static int16_t* null_data = new int16_t[kMaxDataSizeSamples](); |
| 135 | return &null_data[0]; |
Niels Möller | d377f04 | 2018-02-13 15:03:43 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace webrtc |