blob: eb0027e4c83c46dbb5b2a507b647479738e1a480 [file] [log] [blame]
Marina Ciocea65674d82020-03-31 20:41:301/*
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 <memory>
15
16#include "api/frame_transformer_interface.h"
Artem Titovd15a5752021-02-10 13:31:2417#include "api/sequence_checker.h"
Marina Ciocea65674d82020-03-31 20:41:3018#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
19#include "rtc_base/buffer.h"
Markus Handell62872802020-07-06 13:15:0720#include "rtc_base/synchronization/mutex.h"
Marina Ciocea65674d82020-03-31 20:41:3021#include "rtc_base/task_queue.h"
22
23namespace webrtc {
24
25// Delegates calls to FrameTransformerInterface to transform frames, and to
Artem Titovb0ea6372021-07-26 09:47:0726// ChannelSend to send the transformed frames using `send_frame_callback_` on
27// the `encoder_queue_`.
Marina Ciocea65674d82020-03-31 20:41:3028// OnTransformedFrame() can be called from any thread, the delegate ensures
29// thread-safe access to the ChannelSend callback.
30class ChannelSendFrameTransformerDelegate : public TransformedFrameCallback {
31 public:
32 using SendFrameCallback =
33 std::function<int32_t(AudioFrameType frameType,
34 uint8_t payloadType,
Tony Herre36500ab2023-08-29 10:01:3235 uint32_t rtp_timestamp_with_offset,
Marina Ciocea65674d82020-03-31 20:41:3036 rtc::ArrayView<const uint8_t> payload,
37 int64_t absolute_capture_timestamp_ms)>;
38 ChannelSendFrameTransformerDelegate(
39 SendFrameCallback send_frame_callback,
40 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
41 rtc::TaskQueue* encoder_queue);
42
Artem Titovb0ea6372021-07-26 09:47:0743 // Registers `this` as callback for `frame_transformer_`, to get the
Marina Ciocea65674d82020-03-31 20:41:3044 // transformed frames.
45 void Init();
46
Artem Titovb0ea6372021-07-26 09:47:0747 // Unregisters and releases the `frame_transformer_` reference, and resets
48 // `send_frame_callback_` under lock. Called from ChannelSend destructor to
Marina Ciocea65674d82020-03-31 20:41:3049 // prevent running the callback on a dangling channel.
50 void Reset();
51
52 // Delegates the call to FrameTransformerInterface::TransformFrame, to
53 // transform the frame asynchronously.
54 void Transform(AudioFrameType frame_type,
55 uint8_t payload_type,
56 uint32_t rtp_timestamp,
Marina Ciocea65674d82020-03-31 20:41:3057 const uint8_t* payload_data,
58 size_t payload_size,
59 int64_t absolute_capture_timestamp_ms,
60 uint32_t ssrc);
61
62 // Implements TransformedFrameCallback. Can be called on any thread.
63 void OnTransformedFrame(
64 std::unique_ptr<TransformableFrameInterface> frame) override;
65
Artem Titovb0ea6372021-07-26 09:47:0766 // Delegates the call to ChannelSend::SendRtpAudio on the `encoder_queue_`,
67 // by calling `send_audio_callback_`.
Marina Ciocea65674d82020-03-31 20:41:3068 void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const;
69
70 protected:
71 ~ChannelSendFrameTransformerDelegate() override = default;
72
73 private:
Markus Handell62872802020-07-06 13:15:0774 mutable Mutex send_lock_;
Marina Ciocea65674d82020-03-31 20:41:3075 SendFrameCallback send_frame_callback_ RTC_GUARDED_BY(send_lock_);
76 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
77 rtc::TaskQueue* encoder_queue_ RTC_GUARDED_BY(send_lock_);
78};
Tove Petersson1e2d9512023-03-05 11:03:0679
Tony Herre097a4de2023-06-19 15:13:0880std::unique_ptr<TransformableAudioFrameInterface> CloneSenderAudioFrame(
Tove Petersson1e2d9512023-03-05 11:03:0681 TransformableAudioFrameInterface* original);
82
Marina Ciocea65674d82020-03-31 20:41:3083} // namespace webrtc
84#endif // AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_