Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 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_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 Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 17 | #include "api/sequence_checker.h" |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 18 | #include "modules/audio_coding/include/audio_coding_module_typedefs.h" |
| 19 | #include "rtc_base/buffer.h" |
Markus Handell | 6287280 | 2020-07-06 13:15:07 | [diff] [blame] | 20 | #include "rtc_base/synchronization/mutex.h" |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 21 | #include "rtc_base/task_queue.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // Delegates calls to FrameTransformerInterface to transform frames, and to |
Artem Titov | b0ea637 | 2021-07-26 09:47:07 | [diff] [blame] | 26 | // ChannelSend to send the transformed frames using `send_frame_callback_` on |
| 27 | // the `encoder_queue_`. |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 28 | // OnTransformedFrame() can be called from any thread, the delegate ensures |
| 29 | // thread-safe access to the ChannelSend callback. |
| 30 | class ChannelSendFrameTransformerDelegate : public TransformedFrameCallback { |
| 31 | public: |
| 32 | using SendFrameCallback = |
| 33 | std::function<int32_t(AudioFrameType frameType, |
| 34 | uint8_t payloadType, |
Tony Herre | 36500ab | 2023-08-29 10:01:32 | [diff] [blame] | 35 | uint32_t rtp_timestamp_with_offset, |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 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 Titov | b0ea637 | 2021-07-26 09:47:07 | [diff] [blame] | 43 | // Registers `this` as callback for `frame_transformer_`, to get the |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 44 | // transformed frames. |
| 45 | void Init(); |
| 46 | |
Artem Titov | b0ea637 | 2021-07-26 09:47:07 | [diff] [blame] | 47 | // Unregisters and releases the `frame_transformer_` reference, and resets |
| 48 | // `send_frame_callback_` under lock. Called from ChannelSend destructor to |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 49 | // 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 Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 57 | 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 Titov | b0ea637 | 2021-07-26 09:47:07 | [diff] [blame] | 66 | // Delegates the call to ChannelSend::SendRtpAudio on the `encoder_queue_`, |
| 67 | // by calling `send_audio_callback_`. |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 68 | void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const; |
| 69 | |
| 70 | protected: |
| 71 | ~ChannelSendFrameTransformerDelegate() override = default; |
| 72 | |
| 73 | private: |
Markus Handell | 6287280 | 2020-07-06 13:15:07 | [diff] [blame] | 74 | mutable Mutex send_lock_; |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 75 | 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 Petersson | 1e2d951 | 2023-03-05 11:03:06 | [diff] [blame] | 79 | |
Tony Herre | 097a4de | 2023-06-19 15:13:08 | [diff] [blame] | 80 | std::unique_ptr<TransformableAudioFrameInterface> CloneSenderAudioFrame( |
Tove Petersson | 1e2d951 | 2023-03-05 11:03:06 | [diff] [blame] | 81 | TransformableAudioFrameInterface* original); |
| 82 | |
Marina Ciocea | 65674d8 | 2020-03-31 20:41:30 | [diff] [blame] | 83 | } // namespace webrtc |
| 84 | #endif // AUDIO_CHANNEL_SEND_FRAME_TRANSFORMER_DELEGATE_H_ |