audio_frame.h 6.8 KB

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