blob: 6d9f0a86133dbb2ed2a0114287bcc6ab9e7e80d7 [file] [log] [blame]
Marina Ciocea65674d82020-03-31 22:41:30 +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_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 14:31:24 +010017#include "api/sequence_checker.h"
Marina Ciocea65674d82020-03-31 22:41:30 +020018#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
19#include "rtc_base/buffer.h"
Markus Handell62872802020-07-06 15:15:07 +020020#include "rtc_base/synchronization/mutex.h"
Marina Ciocea65674d82020-03-31 22:41:30 +020021#include "rtc_base/task_queue.h"
22
23namespace webrtc {
24
25// Delegates calls to FrameTransformerInterface to transform frames, and to
Artem Titovb0ea6372021-07-26 11:47:07 +020026// ChannelSend to send the transformed frames using `send_frame_callback_` on
27// the `encoder_queue_`.
Marina Ciocea65674d82020-03-31 22:41:30 +020028// 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,
35 uint32_t rtp_timestamp,
36 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 11:47:07 +020043 // Registers `this` as callback for `frame_transformer_`, to get the
Marina Ciocea65674d82020-03-31 22:41:30 +020044 // transformed frames.
45 void Init();
46
Artem Titovb0ea6372021-07-26 11:47:07 +020047 // Unregisters and releases the `frame_transformer_` reference, and resets
48 // `send_frame_callback_` under lock. Called from ChannelSend destructor to
Marina Ciocea65674d82020-03-31 22:41:30 +020049 // 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,
Philipp Hanckeb9d46852020-04-14 15:26:05 +020057 uint32_t rtp_start_timestamp,
Marina Ciocea65674d82020-03-31 22:41:30 +020058 const uint8_t* payload_data,
59 size_t payload_size,
60 int64_t absolute_capture_timestamp_ms,
61 uint32_t ssrc);
62
63 // Implements TransformedFrameCallback. Can be called on any thread.
64 void OnTransformedFrame(
65 std::unique_ptr<TransformableFrameInterface> frame) override;
66
Artem Titovb0ea6372021-07-26 11:47:07 +020067 // Delegates the call to ChannelSend::SendRtpAudio on the `encoder_queue_`,
68 // by calling `send_audio_callback_`.
Marina Ciocea65674d82020-03-31 22:41:30 +020069 void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const;
70
71 protected:
72 ~ChannelSendFrameTransformerDelegate() override = default;
73
74 private:
Markus Handell62872802020-07-06 15:15:07 +020075 mutable Mutex send_lock_;
Marina Ciocea65674d82020-03-31 22:41:30 +020076 SendFrameCallback send_frame_callback_ RTC_GUARDED_BY(send_lock_);
77 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
78 rtc::TaskQueue* encoder_queue_ RTC_GUARDED_BY(send_lock_);
79};
80} // namespace webrtc
81#endif // AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_