voip_core.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_VOIP_CORE_H_
  11. #define AUDIO_VOIP_VOIP_CORE_H_
  12. #include <map>
  13. #include <memory>
  14. #include <queue>
  15. #include <unordered_map>
  16. #include <vector>
  17. #include "api/audio_codecs/audio_decoder_factory.h"
  18. #include "api/audio_codecs/audio_encoder_factory.h"
  19. #include "api/scoped_refptr.h"
  20. #include "api/task_queue/task_queue_factory.h"
  21. #include "api/voip/voip_base.h"
  22. #include "api/voip/voip_codec.h"
  23. #include "api/voip/voip_dtmf.h"
  24. #include "api/voip/voip_engine.h"
  25. #include "api/voip/voip_network.h"
  26. #include "audio/audio_transport_impl.h"
  27. #include "audio/voip/audio_channel.h"
  28. #include "modules/audio_device/include/audio_device.h"
  29. #include "modules/audio_mixer/audio_mixer_impl.h"
  30. #include "modules/audio_processing/include/audio_processing.h"
  31. #include "modules/utility/include/process_thread.h"
  32. #include "rtc_base/synchronization/mutex.h"
  33. namespace webrtc {
  34. // VoipCore is the implementatino of VoIP APIs listed in api/voip directory.
  35. // It manages a vector of AudioChannel objects where each is mapped with a
  36. // ChannelId (int) type. ChannelId is the primary key to locate a specific
  37. // AudioChannel object to operate requested VoIP API from the caller.
  38. //
  39. // This class receives required audio components from caller at construction and
  40. // owns the life cycle of them to orchestrate the proper destruction sequence.
  41. class VoipCore : public VoipEngine,
  42. public VoipBase,
  43. public VoipNetwork,
  44. public VoipCodec,
  45. public VoipDtmf {
  46. public:
  47. ~VoipCore() override = default;
  48. // Initialize VoipCore components with provided arguments.
  49. // Returns false only when |audio_device_module| fails to initialize which
  50. // would presumably render further processing useless.
  51. // TODO(natim@webrtc.org): Need to report audio device errors to user layer.
  52. bool Init(rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,
  53. rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
  54. std::unique_ptr<TaskQueueFactory> task_queue_factory,
  55. rtc::scoped_refptr<AudioDeviceModule> audio_device_module,
  56. rtc::scoped_refptr<AudioProcessing> audio_processing);
  57. // Implements VoipEngine interfaces.
  58. VoipBase& Base() override { return *this; }
  59. VoipNetwork& Network() override { return *this; }
  60. VoipCodec& Codec() override { return *this; }
  61. VoipDtmf& Dtmf() override { return *this; }
  62. // Implements VoipBase interfaces.
  63. absl::optional<ChannelId> CreateChannel(
  64. Transport* transport,
  65. absl::optional<uint32_t> local_ssrc) override;
  66. void ReleaseChannel(ChannelId channel) override;
  67. bool StartSend(ChannelId channel) override;
  68. bool StopSend(ChannelId channel) override;
  69. bool StartPlayout(ChannelId channel) override;
  70. bool StopPlayout(ChannelId channel) override;
  71. // Implements VoipNetwork interfaces.
  72. void ReceivedRTPPacket(ChannelId channel,
  73. rtc::ArrayView<const uint8_t> rtp_packet) override;
  74. void ReceivedRTCPPacket(ChannelId channel,
  75. rtc::ArrayView<const uint8_t> rtcp_packet) override;
  76. // Implements VoipCodec interfaces.
  77. void SetSendCodec(ChannelId channel,
  78. int payload_type,
  79. const SdpAudioFormat& encoder_format) override;
  80. void SetReceiveCodecs(
  81. ChannelId channel,
  82. const std::map<int, SdpAudioFormat>& decoder_specs) override;
  83. // Implements VoipDtmf interfaces.
  84. void RegisterTelephoneEventType(ChannelId channel,
  85. int rtp_payload_type,
  86. int sample_rate_hz) override;
  87. bool SendDtmfEvent(ChannelId channel,
  88. DtmfEvent dtmf_event,
  89. int duration_ms) override;
  90. private:
  91. // Fetches the corresponding AudioChannel assigned with given |channel|.
  92. // Returns nullptr if not found.
  93. rtc::scoped_refptr<AudioChannel> GetChannel(ChannelId channel);
  94. // Updates AudioTransportImpl with a new set of actively sending AudioSender
  95. // (AudioEgress). This needs to be invoked whenever StartSend/StopSend is
  96. // involved by caller. Returns false when the selected audio device fails to
  97. // initialize where it can't expect to deliver any audio input sample.
  98. bool UpdateAudioTransportWithSenders();
  99. // Synchronization for these are handled internally.
  100. rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_;
  101. rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
  102. std::unique_ptr<TaskQueueFactory> task_queue_factory_;
  103. // Synchronization is handled internally by AudioProessing.
  104. // Must be placed before |audio_device_module_| for proper destruction.
  105. rtc::scoped_refptr<AudioProcessing> audio_processing_;
  106. // Synchronization is handled internally by AudioMixer.
  107. // Must be placed before |audio_device_module_| for proper destruction.
  108. rtc::scoped_refptr<AudioMixer> audio_mixer_;
  109. // Synchronization is handled internally by AudioTransportImpl.
  110. // Must be placed before |audio_device_module_| for proper destruction.
  111. std::unique_ptr<AudioTransportImpl> audio_transport_;
  112. // Synchronization is handled internally by AudioDeviceModule.
  113. rtc::scoped_refptr<AudioDeviceModule> audio_device_module_;
  114. // Synchronization is handled internally by ProcessThread.
  115. // Must be placed before |channels_| for proper destruction.
  116. std::unique_ptr<ProcessThread> process_thread_;
  117. Mutex lock_;
  118. // Member to track a next ChannelId for new AudioChannel.
  119. int next_channel_id_ RTC_GUARDED_BY(lock_) = 0;
  120. // Container to track currently active AudioChannel objects mapped by
  121. // ChannelId.
  122. std::unordered_map<ChannelId, rtc::scoped_refptr<AudioChannel>> channels_
  123. RTC_GUARDED_BY(lock_);
  124. };
  125. } // namespace webrtc
  126. #endif // AUDIO_VOIP_VOIP_CORE_H_