You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.8 KiB

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