channel_send_frame_transformer_delegate.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_
  11. #define AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_
  12. #include <memory>
  13. #include "api/frame_transformer_interface.h"
  14. #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
  15. #include "rtc_base/buffer.h"
  16. #include "rtc_base/synchronization/mutex.h"
  17. #include "rtc_base/synchronization/sequence_checker.h"
  18. #include "rtc_base/task_queue.h"
  19. namespace webrtc {
  20. // Delegates calls to FrameTransformerInterface to transform frames, and to
  21. // ChannelSend to send the transformed frames using |send_frame_callback_| on
  22. // the |encoder_queue_|.
  23. // OnTransformedFrame() can be called from any thread, the delegate ensures
  24. // thread-safe access to the ChannelSend callback.
  25. class ChannelSendFrameTransformerDelegate : public TransformedFrameCallback {
  26. public:
  27. using SendFrameCallback =
  28. std::function<int32_t(AudioFrameType frameType,
  29. uint8_t payloadType,
  30. uint32_t rtp_timestamp,
  31. rtc::ArrayView<const uint8_t> payload,
  32. int64_t absolute_capture_timestamp_ms)>;
  33. ChannelSendFrameTransformerDelegate(
  34. SendFrameCallback send_frame_callback,
  35. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
  36. rtc::TaskQueue* encoder_queue);
  37. // Registers |this| as callback for |frame_transformer_|, to get the
  38. // transformed frames.
  39. void Init();
  40. // Unregisters and releases the |frame_transformer_| reference, and resets
  41. // |send_frame_callback_| under lock. Called from ChannelSend destructor to
  42. // prevent running the callback on a dangling channel.
  43. void Reset();
  44. // Delegates the call to FrameTransformerInterface::TransformFrame, to
  45. // transform the frame asynchronously.
  46. void Transform(AudioFrameType frame_type,
  47. uint8_t payload_type,
  48. uint32_t rtp_timestamp,
  49. uint32_t rtp_start_timestamp,
  50. const uint8_t* payload_data,
  51. size_t payload_size,
  52. int64_t absolute_capture_timestamp_ms,
  53. uint32_t ssrc);
  54. // Implements TransformedFrameCallback. Can be called on any thread.
  55. void OnTransformedFrame(
  56. std::unique_ptr<TransformableFrameInterface> frame) override;
  57. // Delegates the call to ChannelSend::SendRtpAudio on the |encoder_queue_|,
  58. // by calling |send_audio_callback_|.
  59. void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const;
  60. protected:
  61. ~ChannelSendFrameTransformerDelegate() override = default;
  62. private:
  63. mutable Mutex send_lock_;
  64. SendFrameCallback send_frame_callback_ RTC_GUARDED_BY(send_lock_);
  65. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
  66. rtc::TaskQueue* encoder_queue_ RTC_GUARDED_BY(send_lock_);
  67. };
  68. } // namespace webrtc
  69. #endif // AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_