Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [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 14:31:24 +0100 | [diff] [blame] | 17 | #include "api/sequence_checker.h" |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [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 15:15:07 +0200 | [diff] [blame] | 20 | #include "rtc_base/synchronization/mutex.h" |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [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 11:47:07 +0200 | [diff] [blame] | 26 | // ChannelSend to send the transformed frames using `send_frame_callback_` on |
| 27 | // the `encoder_queue_`. |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [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, |
| 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 Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 43 | // Registers `this` as callback for `frame_transformer_`, to get the |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [diff] [blame] | 44 | // transformed frames. |
| 45 | void Init(); |
| 46 | |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [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 22:41:30 +0200 | [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, |
Philipp Hancke | b9d4685 | 2020-04-14 15:26:05 +0200 | [diff] [blame] | 57 | uint32_t rtp_start_timestamp, |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [diff] [blame] | 58 | 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 Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 67 | // Delegates the call to ChannelSend::SendRtpAudio on the `encoder_queue_`, |
| 68 | // by calling `send_audio_callback_`. |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [diff] [blame] | 69 | void SendFrame(std::unique_ptr<TransformableFrameInterface> frame) const; |
| 70 | |
| 71 | protected: |
| 72 | ~ChannelSendFrameTransformerDelegate() override = default; |
| 73 | |
| 74 | private: |
Markus Handell | 6287280 | 2020-07-06 15:15:07 +0200 | [diff] [blame] | 75 | mutable Mutex send_lock_; |
Marina Ciocea | 65674d8 | 2020-03-31 22:41:30 +0200 | [diff] [blame] | 76 | 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_ |