audio_channel.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_CHANNEL_H_
  11. #define AUDIO_VOIP_AUDIO_CHANNEL_H_
  12. #include <map>
  13. #include <memory>
  14. #include <queue>
  15. #include <utility>
  16. #include "api/task_queue/task_queue_factory.h"
  17. #include "api/voip/voip_base.h"
  18. #include "audio/voip/audio_egress.h"
  19. #include "audio/voip/audio_ingress.h"
  20. #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
  21. #include "modules/utility/include/process_thread.h"
  22. #include "rtc_base/ref_count.h"
  23. namespace webrtc {
  24. // AudioChannel represents a single media session and provides APIs over
  25. // AudioIngress and AudioEgress. Note that a single RTP stack is shared with
  26. // these two classes as it has both sending and receiving capabilities.
  27. class AudioChannel : public rtc::RefCountInterface {
  28. public:
  29. AudioChannel(Transport* transport,
  30. uint32_t local_ssrc,
  31. TaskQueueFactory* task_queue_factory,
  32. ProcessThread* process_thread,
  33. AudioMixer* audio_mixer,
  34. rtc::scoped_refptr<AudioDecoderFactory> decoder_factory);
  35. ~AudioChannel() override;
  36. // Set and get ChannelId that this audio channel belongs for debugging and
  37. // logging purpose.
  38. void SetId(ChannelId id) { id_ = id; }
  39. ChannelId GetId() const { return id_; }
  40. // APIs to start/stop audio channel on each direction.
  41. // StartSend/StartPlay returns false if encoder/decoders
  42. // have not been set, respectively.
  43. bool StartSend();
  44. void StopSend();
  45. bool StartPlay();
  46. void StopPlay();
  47. // APIs relayed to AudioEgress.
  48. bool IsSendingMedia() const { return egress_->IsSending(); }
  49. AudioSender* GetAudioSender() { return egress_.get(); }
  50. void SetEncoder(int payload_type,
  51. const SdpAudioFormat& encoder_format,
  52. std::unique_ptr<AudioEncoder> encoder) {
  53. egress_->SetEncoder(payload_type, encoder_format, std::move(encoder));
  54. }
  55. absl::optional<SdpAudioFormat> GetEncoderFormat() const {
  56. return egress_->GetEncoderFormat();
  57. }
  58. void RegisterTelephoneEventType(int rtp_payload_type, int sample_rate_hz) {
  59. egress_->RegisterTelephoneEventType(rtp_payload_type, sample_rate_hz);
  60. }
  61. bool SendTelephoneEvent(int dtmf_event, int duration_ms) {
  62. return egress_->SendTelephoneEvent(dtmf_event, duration_ms);
  63. }
  64. // APIs relayed to AudioIngress.
  65. bool IsPlaying() const { return ingress_->IsPlaying(); }
  66. void ReceivedRTPPacket(rtc::ArrayView<const uint8_t> rtp_packet) {
  67. ingress_->ReceivedRTPPacket(rtp_packet);
  68. }
  69. void ReceivedRTCPPacket(rtc::ArrayView<const uint8_t> rtcp_packet) {
  70. ingress_->ReceivedRTCPPacket(rtcp_packet);
  71. }
  72. void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
  73. ingress_->SetReceiveCodecs(codecs);
  74. }
  75. private:
  76. // ChannelId that this audio channel belongs for logging purpose.
  77. ChannelId id_;
  78. // Synchronization is handled internally by AudioMixer.
  79. AudioMixer* audio_mixer_;
  80. // Synchronization is handled internally by ProcessThread.
  81. ProcessThread* process_thread_;
  82. // Listed in order for safe destruction of AudioChannel object.
  83. // Synchronization for these are handled internally.
  84. std::unique_ptr<ReceiveStatistics> receive_statistics_;
  85. std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp_;
  86. std::unique_ptr<AudioIngress> ingress_;
  87. std::unique_ptr<AudioEgress> egress_;
  88. };
  89. } // namespace webrtc
  90. #endif // AUDIO_VOIP_AUDIO_CHANNEL_H_