audio_egress.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2020 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 AUDIO_VOIP_AUDIO_EGRESS_H_
  11. #define AUDIO_VOIP_AUDIO_EGRESS_H_
  12. #include <memory>
  13. #include <string>
  14. #include "api/audio_codecs/audio_format.h"
  15. #include "api/task_queue/task_queue_factory.h"
  16. #include "audio/utility/audio_frame_operations.h"
  17. #include "call/audio_sender.h"
  18. #include "modules/audio_coding/include/audio_coding_module.h"
  19. #include "modules/rtp_rtcp/include/report_block_data.h"
  20. #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
  21. #include "modules/rtp_rtcp/source/rtp_sender_audio.h"
  22. #include "rtc_base/synchronization/mutex.h"
  23. #include "rtc_base/task_queue.h"
  24. #include "rtc_base/thread_checker.h"
  25. #include "rtc_base/time_utils.h"
  26. namespace webrtc {
  27. // AudioEgress receives input samples from AudioDeviceModule via
  28. // AudioTransportImpl through AudioSender interface. Once it encodes the sample
  29. // via selected encoder through AudioPacketizationCallback interface, the
  30. // encoded payload will be packetized by the RTP stack, resulting in ready to
  31. // send RTP packet to remote endpoint.
  32. //
  33. // TaskQueue is used to encode and send RTP asynchrounously as some OS platform
  34. // uses the same thread for both audio input and output sample deliveries which
  35. // can affect audio quality.
  36. //
  37. // Note that this class is originally based on ChannelSend in
  38. // audio/channel_send.cc with non-audio related logic trimmed as aimed for
  39. // smaller footprint.
  40. class AudioEgress : public AudioSender, public AudioPacketizationCallback {
  41. public:
  42. AudioEgress(RtpRtcpInterface* rtp_rtcp,
  43. Clock* clock,
  44. TaskQueueFactory* task_queue_factory);
  45. ~AudioEgress() override;
  46. // Set the encoder format and payload type for AudioCodingModule.
  47. // It's possible to change the encoder type during its active usage.
  48. // |payload_type| must be the type that is negotiated with peer through
  49. // offer/answer.
  50. void SetEncoder(int payload_type,
  51. const SdpAudioFormat& encoder_format,
  52. std::unique_ptr<AudioEncoder> encoder);
  53. // Start or stop sending operation of AudioEgress. This will start/stop
  54. // the RTP stack also causes encoder queue thread to start/stop
  55. // processing input audio samples. StartSend will return false if
  56. // a send codec has not been set.
  57. bool StartSend();
  58. void StopSend();
  59. // Query the state of the RTP stack. This returns true if StartSend()
  60. // called and false if StopSend() is called.
  61. bool IsSending() const;
  62. // Enable or disable Mute state.
  63. void SetMute(bool mute);
  64. // Retrieve current encoder format info. This returns encoder format set
  65. // by SetEncoder() and if encoder is not set, this will return nullopt.
  66. absl::optional<SdpAudioFormat> GetEncoderFormat() const {
  67. MutexLock lock(&lock_);
  68. return encoder_format_;
  69. }
  70. // Register the payload type and sample rate for DTMF (RFC 4733) payload.
  71. void RegisterTelephoneEventType(int rtp_payload_type, int sample_rate_hz);
  72. // Send DTMF named event as specified by
  73. // https://tools.ietf.org/html/rfc4733#section-3.2
  74. // |duration_ms| specifies the duration of DTMF packets that will be emitted
  75. // in place of real RTP packets instead.
  76. // This will return true when requested dtmf event is successfully scheduled
  77. // otherwise false when the dtmf queue reached maximum of 20 events.
  78. bool SendTelephoneEvent(int dtmf_event, int duration_ms);
  79. // Implementation of AudioSender interface.
  80. void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
  81. // Implementation of AudioPacketizationCallback interface.
  82. int32_t SendData(AudioFrameType frame_type,
  83. uint8_t payload_type,
  84. uint32_t timestamp,
  85. const uint8_t* payload_data,
  86. size_t payload_size) override;
  87. private:
  88. void SetEncoderFormat(const SdpAudioFormat& encoder_format) {
  89. MutexLock lock(&lock_);
  90. encoder_format_ = encoder_format;
  91. }
  92. mutable Mutex lock_;
  93. // Current encoder format selected by caller.
  94. absl::optional<SdpAudioFormat> encoder_format_ RTC_GUARDED_BY(lock_);
  95. // Synchronization is handled internally by RtpRtcp.
  96. RtpRtcpInterface* const rtp_rtcp_;
  97. // Synchronization is handled internally by RTPSenderAudio.
  98. RTPSenderAudio rtp_sender_audio_;
  99. // Synchronization is handled internally by AudioCodingModule.
  100. const std::unique_ptr<AudioCodingModule> audio_coding_;
  101. // Struct that holds all variables used by encoder task queue.
  102. struct EncoderContext {
  103. // Offset used to mark rtp timestamp in sample rate unit in
  104. // newly received audio frame from AudioTransport.
  105. uint32_t frame_rtp_timestamp_ = 0;
  106. // Flag to track mute state from caller. |previously_muted_| is used to
  107. // track previous state as part of input to AudioFrameOperations::Mute
  108. // to implement fading effect when (un)mute is invoked.
  109. bool mute_ = false;
  110. bool previously_muted_ = false;
  111. };
  112. EncoderContext encoder_context_ RTC_GUARDED_BY(encoder_queue_);
  113. // Defined last to ensure that there are no running tasks when the other
  114. // members are destroyed.
  115. rtc::TaskQueue encoder_queue_;
  116. };
  117. } // namespace webrtc
  118. #endif // AUDIO_VOIP_AUDIO_EGRESS_H_