channel_send_frame_transformer_delegate.h (3918B)
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 11 #ifndef AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_ 12 #define AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <functional> 17 #include <memory> 18 #include <optional> 19 #include <string> 20 #include <vector> 21 22 #include "api/array_view.h" 23 #include "api/frame_transformer_interface.h" 24 #include "api/scoped_refptr.h" 25 #include "api/task_queue/task_queue_base.h" 26 #include "modules/audio_coding/include/audio_coding_module_typedefs.h" 27 #include "rtc_base/synchronization/mutex.h" 28 #include "rtc_base/thread_annotations.h" 29 30 namespace webrtc { 31 32 // Delegates calls to FrameTransformerInterface to transform frames, and to 33 // ChannelSend to send the transformed frames using `send_frame_callback_` on 34 // the `encoder_queue_`. 35 // OnTransformedFrame() can be called from any thread, the delegate ensures 36 // thread-safe access to the ChannelSend callback. 37 class ChannelSendFrameTransformerDelegate : public TransformedFrameCallback { 38 public: 39 using SendFrameCallback = 40 std::function<int32_t(AudioFrameType frameType, 41 uint8_t payloadType, 42 uint32_t rtp_timestamp_with_offset, 43 webrtc::ArrayView<const uint8_t> payload, 44 int64_t absolute_capture_timestamp_ms, 45 webrtc::ArrayView<const uint32_t> csrcs, 46 std::optional<uint8_t> audio_level_dbov)>; 47 ChannelSendFrameTransformerDelegate( 48 SendFrameCallback send_frame_callback, 49 scoped_refptr<FrameTransformerInterface> frame_transformer, 50 TaskQueueBase* encoder_queue); 51 52 // Registers `this` as callback for `frame_transformer_`, to get the 53 // transformed frames. 54 void Init(); 55 56 // Unregisters and releases the `frame_transformer_` reference, and resets 57 // `send_frame_callback_` under lock. Called from ChannelSend destructor to 58 // prevent running the callback on a dangling channel. 59 void Reset(); 60 61 // Delegates the call to FrameTransformerInterface::TransformFrame, to 62 // transform the frame asynchronously. 63 void Transform(AudioFrameType frame_type, 64 uint8_t payload_type, 65 uint32_t rtp_timestamp, 66 const uint8_t* payload_data, 67 size_t payload_size, 68 int64_t absolute_capture_timestamp_ms, 69 uint32_t ssrc, 70 const std::string& codec_mime_type, 71 std::optional<uint8_t> audio_level_dbov, 72 const std::vector<uint32_t>& csrcs = {}); 73 74 // Implements TransformedFrameCallback. Can be called on any thread. 75 void OnTransformedFrame( 76 std::unique_ptr<TransformableFrameInterface> frame) override; 77 78 void StartShortCircuiting() override; 79 80 // Delegates the call to ChannelSend::SendRtpAudio on the `encoder_queue_`, 81 // by calling `send_audio_callback_`. 82 void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const; 83 84 protected: 85 ~ChannelSendFrameTransformerDelegate() override = default; 86 87 private: 88 mutable Mutex send_lock_; 89 SendFrameCallback send_frame_callback_ RTC_GUARDED_BY(send_lock_); 90 scoped_refptr<FrameTransformerInterface> frame_transformer_; 91 TaskQueueBase* const encoder_queue_; 92 bool short_circuit_ RTC_GUARDED_BY(send_lock_) = false; 93 }; 94 95 std::unique_ptr<TransformableAudioFrameInterface> CloneSenderAudioFrame( 96 TransformableAudioFrameInterface* original); 97 98 } // namespace webrtc 99 #endif // AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_