channel_receive_frame_transformer_delegate.h (3519B)
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_RECEIVE_FRAME_TRANSFORMER_DELEGATE_H_ 12 #define AUDIO_CHANNEL_RECEIVE_FRAME_TRANSFORMER_DELEGATE_H_ 13 14 #include <cstdint> 15 #include <functional> 16 #include <memory> 17 #include <string> 18 19 #include "api/array_view.h" 20 #include "api/frame_transformer_interface.h" 21 #include "api/rtp_headers.h" 22 #include "api/scoped_refptr.h" 23 #include "api/sequence_checker.h" 24 #include "api/task_queue/task_queue_base.h" 25 #include "api/units/timestamp.h" 26 #include "rtc_base/system/no_unique_address.h" 27 #include "rtc_base/thread_annotations.h" 28 29 namespace webrtc { 30 31 // Delegates calls to FrameTransformerInterface to transform frames, and to 32 // ChannelReceive to receive the transformed frames using the 33 // `receive_frame_callback_` on the `channel_receive_thread_`. 34 class ChannelReceiveFrameTransformerDelegate : public TransformedFrameCallback { 35 public: 36 using ReceiveFrameCallback = 37 std::function<void(webrtc::ArrayView<const uint8_t> packet, 38 const RTPHeader& header, 39 Timestamp receive_time)>; 40 ChannelReceiveFrameTransformerDelegate( 41 ReceiveFrameCallback receive_frame_callback, 42 scoped_refptr<FrameTransformerInterface> frame_transformer, 43 TaskQueueBase* channel_receive_thread); 44 45 // Registers `this` as callback for `frame_transformer_`, to get the 46 // transformed frames. 47 void Init(); 48 49 // Unregisters and releases the `frame_transformer_` reference, and resets 50 // `receive_frame_callback_` on `channel_receive_thread_`. Called from 51 // ChannelReceive destructor to prevent running the callback on a dangling 52 // channel. 53 void Reset(); 54 55 // Delegates the call to FrameTransformerInterface::Transform, to transform 56 // the frame asynchronously. 57 void Transform(ArrayView<const uint8_t> packet, 58 const RTPHeader& header, 59 uint32_t ssrc, 60 const std::string& codec_mime_type, 61 Timestamp receive_time); 62 63 // Implements TransformedFrameCallback. Can be called on any thread. 64 void OnTransformedFrame( 65 std::unique_ptr<TransformableFrameInterface> frame) override; 66 67 void StartShortCircuiting() override; 68 69 // Delegates the call to ChannelReceive::OnReceivedPayloadData on the 70 // `channel_receive_thread_`, by calling `receive_frame_callback_`. 71 void ReceiveFrame(std::unique_ptr<TransformableFrameInterface> frame) const; 72 73 scoped_refptr<FrameTransformerInterface> FrameTransformer(); 74 75 protected: 76 ~ChannelReceiveFrameTransformerDelegate() override = default; 77 78 private: 79 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; 80 ReceiveFrameCallback receive_frame_callback_ 81 RTC_GUARDED_BY(sequence_checker_); 82 scoped_refptr<FrameTransformerInterface> frame_transformer_ 83 RTC_GUARDED_BY(sequence_checker_); 84 TaskQueueBase* const channel_receive_thread_; 85 bool short_circuit_ RTC_GUARDED_BY(sequence_checker_) = false; 86 }; 87 88 std::unique_ptr<TransformableAudioFrameInterface> CloneReceiverAudioFrame( 89 TransformableAudioFrameInterface* original); 90 91 } // namespace webrtc 92 #endif // AUDIO_CHANNEL_RECEIVE_FRAME_TRANSFORMER_DELEGATE_H_