xref: /aosp_15_r20/external/webrtc/audio/channel_receive_frame_transformer_delegate.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <memory>
15 
16 #include "api/frame_transformer_interface.h"
17 #include "api/sequence_checker.h"
18 #include "rtc_base/system/no_unique_address.h"
19 #include "rtc_base/task_queue.h"
20 #include "rtc_base/thread.h"
21 
22 namespace webrtc {
23 
24 // Delegates calls to FrameTransformerInterface to transform frames, and to
25 // ChannelReceive to receive the transformed frames using the
26 // `receive_frame_callback_` on the `channel_receive_thread_`.
27 class ChannelReceiveFrameTransformerDelegate : public TransformedFrameCallback {
28  public:
29   using ReceiveFrameCallback =
30       std::function<void(rtc::ArrayView<const uint8_t> packet,
31                          const RTPHeader& header)>;
32   ChannelReceiveFrameTransformerDelegate(
33       ReceiveFrameCallback receive_frame_callback,
34       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
35       TaskQueueBase* channel_receive_thread);
36 
37   // Registers `this` as callback for `frame_transformer_`, to get the
38   // transformed frames.
39   void Init();
40 
41   // Unregisters and releases the `frame_transformer_` reference, and resets
42   // `receive_frame_callback_` on `channel_receive_thread_`. Called from
43   // ChannelReceive destructor to prevent running the callback on a dangling
44   // channel.
45   void Reset();
46 
47   // Delegates the call to FrameTransformerInterface::Transform, to transform
48   // the frame asynchronously.
49   void Transform(rtc::ArrayView<const uint8_t> packet,
50                  const RTPHeader& header,
51                  uint32_t ssrc);
52 
53   // Implements TransformedFrameCallback. Can be called on any thread.
54   void OnTransformedFrame(
55       std::unique_ptr<TransformableFrameInterface> frame) override;
56 
57   // Delegates the call to ChannelReceive::OnReceivedPayloadData on the
58   // `channel_receive_thread_`, by calling `receive_frame_callback_`.
59   void ReceiveFrame(std::unique_ptr<TransformableFrameInterface> frame) const;
60 
61  protected:
62   ~ChannelReceiveFrameTransformerDelegate() override = default;
63 
64  private:
65   RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
66   ReceiveFrameCallback receive_frame_callback_
67       RTC_GUARDED_BY(sequence_checker_);
68   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
69       RTC_GUARDED_BY(sequence_checker_);
70   TaskQueueBase* const channel_receive_thread_;
71 };
72 
73 }  // namespace webrtc
74 #endif  // AUDIO_CHANNEL_RECEIVE_FRAME_TRANSFORMER_DELEGATE_H_
75