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.

158 lines
6.2 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. #ifndef API_AUDIO_AUDIO_FRAME_H_
  11. #define API_AUDIO_AUDIO_FRAME_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include "api/audio/channel_layout.h"
  15. #include "api/rtp_packet_infos.h"
  16. #include "rtc_base/constructor_magic.h"
  17. namespace webrtc {
  18. /* This class holds up to 120 ms of super-wideband (32 kHz) stereo audio. It
  19. * allows for adding and subtracting frames while keeping track of the resulting
  20. * states.
  21. *
  22. * Notes
  23. * - This is a de-facto api, not designed for external use. The AudioFrame class
  24. * is in need of overhaul or even replacement, and anyone depending on it
  25. * should be prepared for that.
  26. * - The total number of samples is samples_per_channel_ * num_channels_.
  27. * - Stereo data is interleaved starting with the left channel.
  28. */
  29. class AudioFrame {
  30. public:
  31. // Using constexpr here causes linker errors unless the variable also has an
  32. // out-of-class definition, which is impractical in this header-only class.
  33. // (This makes no sense because it compiles as an enum value, which we most
  34. // certainly cannot take the address of, just fine.) C++17 introduces inline
  35. // variables which should allow us to switch to constexpr and keep this a
  36. // header-only class.
  37. enum : size_t {
  38. // Stereo, 32 kHz, 120 ms (2 * 32 * 120)
  39. // Stereo, 192 kHz, 20 ms (2 * 192 * 20)
  40. kMaxDataSizeSamples = 7680,
  41. kMaxDataSizeBytes = kMaxDataSizeSamples * sizeof(int16_t),
  42. };
  43. enum VADActivity { kVadActive = 0, kVadPassive = 1, kVadUnknown = 2 };
  44. enum SpeechType {
  45. kNormalSpeech = 0,
  46. kPLC = 1,
  47. kCNG = 2,
  48. kPLCCNG = 3,
  49. kCodecPLC = 5,
  50. kUndefined = 4
  51. };
  52. AudioFrame();
  53. // Resets all members to their default state.
  54. void Reset();
  55. // Same as Reset(), but leaves mute state unchanged. Muting a frame requires
  56. // the buffer to be zeroed on the next call to mutable_data(). Callers
  57. // intending to write to the buffer immediately after Reset() can instead use
  58. // ResetWithoutMuting() to skip this wasteful zeroing.
  59. void ResetWithoutMuting();
  60. void UpdateFrame(uint32_t timestamp,
  61. const int16_t* data,
  62. size_t samples_per_channel,
  63. int sample_rate_hz,
  64. SpeechType speech_type,
  65. VADActivity vad_activity,
  66. size_t num_channels = 1);
  67. void CopyFrom(const AudioFrame& src);
  68. // Sets a wall-time clock timestamp in milliseconds to be used for profiling
  69. // of time between two points in the audio chain.
  70. // Example:
  71. // t0: UpdateProfileTimeStamp()
  72. // t1: ElapsedProfileTimeMs() => t1 - t0 [msec]
  73. void UpdateProfileTimeStamp();
  74. // Returns the time difference between now and when UpdateProfileTimeStamp()
  75. // was last called. Returns -1 if UpdateProfileTimeStamp() has not yet been
  76. // called.
  77. int64_t ElapsedProfileTimeMs() const;
  78. // data() returns a zeroed static buffer if the frame is muted.
  79. // mutable_frame() always returns a non-static buffer; the first call to
  80. // mutable_frame() zeros the non-static buffer and marks the frame unmuted.
  81. const int16_t* data() const;
  82. int16_t* mutable_data();
  83. // Prefer to mute frames using AudioFrameOperations::Mute.
  84. void Mute();
  85. // Frame is muted by default.
  86. bool muted() const;
  87. size_t max_16bit_samples() const { return kMaxDataSizeSamples; }
  88. size_t samples_per_channel() const { return samples_per_channel_; }
  89. size_t num_channels() const { return num_channels_; }
  90. ChannelLayout channel_layout() const { return channel_layout_; }
  91. int sample_rate_hz() const { return sample_rate_hz_; }
  92. // RTP timestamp of the first sample in the AudioFrame.
  93. uint32_t timestamp_ = 0;
  94. // Time since the first frame in milliseconds.
  95. // -1 represents an uninitialized value.
  96. int64_t elapsed_time_ms_ = -1;
  97. // NTP time of the estimated capture time in local timebase in milliseconds.
  98. // -1 represents an uninitialized value.
  99. int64_t ntp_time_ms_ = -1;
  100. size_t samples_per_channel_ = 0;
  101. int sample_rate_hz_ = 0;
  102. size_t num_channels_ = 0;
  103. ChannelLayout channel_layout_ = CHANNEL_LAYOUT_NONE;
  104. SpeechType speech_type_ = kUndefined;
  105. VADActivity vad_activity_ = kVadUnknown;
  106. // Monotonically increasing timestamp intended for profiling of audio frames.
  107. // Typically used for measuring elapsed time between two different points in
  108. // the audio path. No lock is used to save resources and we are thread safe
  109. // by design. Also, absl::optional is not used since it will cause a "complex
  110. // class/struct needs an explicit out-of-line destructor" build error.
  111. int64_t profile_timestamp_ms_ = 0;
  112. // Information about packets used to assemble this audio frame. This is needed
  113. // by |SourceTracker| when the frame is delivered to the RTCRtpReceiver's
  114. // MediaStreamTrack, in order to implement getContributingSources(). See:
  115. // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
  116. //
  117. // TODO(bugs.webrtc.org/10757):
  118. // Note that this information might not be fully accurate since we currently
  119. // don't have a proper way to track it across the audio sync buffer. The
  120. // sync buffer is the small sample-holding buffer located after the audio
  121. // decoder and before where samples are assembled into output frames.
  122. //
  123. // |RtpPacketInfos| may also be empty if the audio samples did not come from
  124. // RTP packets. E.g. if the audio were locally generated by packet loss
  125. // concealment, comfort noise generation, etc.
  126. RtpPacketInfos packet_infos_;
  127. private:
  128. // A permanently zeroed out buffer to represent muted frames. This is a
  129. // header-only class, so the only way to avoid creating a separate empty
  130. // buffer per translation unit is to wrap a static in an inline function.
  131. static const int16_t* empty_data();
  132. int16_t data_[kMaxDataSizeSamples];
  133. bool muted_ = true;
  134. RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame);
  135. };
  136. } // namespace webrtc
  137. #endif // API_AUDIO_AUDIO_FRAME_H_