blob: 04ad7c4695b905ac4255bf8c79b07e910eb1a654 [file] [log] [blame]
Marina Ciocea48623202020-04-01 10:19:44 +02001/*
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"
Artem Titovd15a5752021-02-10 14:31:24 +010017#include "api/sequence_checker.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010018#include "rtc_base/system/no_unique_address.h"
Marina Ciocea48623202020-04-01 10:19:44 +020019#include "rtc_base/task_queue.h"
20#include "rtc_base/thread.h"
21
22namespace webrtc {
23
24// Delegates calls to FrameTransformerInterface to transform frames, and to
25// ChannelReceive to receive the transformed frames using the
Artem Titovb0ea6372021-07-26 11:47:07 +020026// `receive_frame_callback_` on the `channel_receive_thread_`.
Marina Ciocea48623202020-04-01 10:19:44 +020027class 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,
Tommie2e04642021-06-09 16:11:11 +020035 TaskQueueBase* channel_receive_thread);
Marina Ciocea48623202020-04-01 10:19:44 +020036
Artem Titovb0ea6372021-07-26 11:47:07 +020037 // Registers `this` as callback for `frame_transformer_`, to get the
Marina Ciocea48623202020-04-01 10:19:44 +020038 // transformed frames.
39 void Init();
40
Artem Titovb0ea6372021-07-26 11:47:07 +020041 // Unregisters and releases the `frame_transformer_` reference, and resets
42 // `receive_frame_callback_` on `channel_receive_thread_`. Called from
Marina Ciocea48623202020-04-01 10:19:44 +020043 // 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
Marina Cioceafc23cc02020-04-02 12:10:03 +020057 // Delegates the call to ChannelReceive::OnReceivedPayloadData on the
Artem Titovb0ea6372021-07-26 11:47:07 +020058 // `channel_receive_thread_`, by calling `receive_frame_callback_`.
Marina Ciocea48623202020-04-01 10:19:44 +020059 void ReceiveFrame(std::unique_ptr<TransformableFrameInterface> frame) const;
60
61 protected:
62 ~ChannelReceiveFrameTransformerDelegate() override = default;
63
64 private:
Mirko Bonadei20e4c802020-11-23 11:07:42 +010065 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Marina Ciocea48623202020-04-01 10:19:44 +020066 ReceiveFrameCallback receive_frame_callback_
67 RTC_GUARDED_BY(sequence_checker_);
68 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
69 RTC_GUARDED_BY(sequence_checker_);
Tommie2e04642021-06-09 16:11:11 +020070 TaskQueueBase* const channel_receive_thread_;
Marina Ciocea48623202020-04-01 10:19:44 +020071};
72
73} // namespace webrtc
74#endif // AUDIO_CHANNEL_RECEIVE_FRAME_TRANSFORMER_DELEGATE_H_