Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [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 | #include "audio/channel_receive_frame_transformer_delegate.h" |
| 12 | |
Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | #include <memory> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
Philipp Hancke | d209893 | 2023-11-01 18:18:32 | [diff] [blame] | 16 | #include <string> |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Harald Alvestrand | 6431a64 | 2024-06-04 21:29:14 | [diff] [blame] | 19 | #include "api/array_view.h" |
| 20 | #include "api/frame_transformer_interface.h" |
| 21 | #include "api/rtp_headers.h" |
| 22 | #include "api/scoped_refptr.h" |
| 23 | #include "api/sequence_checker.h" |
| 24 | #include "api/task_queue/task_queue_base.h" |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 25 | #include "api/units/timestamp.h" |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 26 | #include "rtc_base/buffer.h" |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 27 | #include "rtc_base/string_encode.h" |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 30 | |
Tony Herre | 8fb41a3 | 2021-09-24 12:05:20 | [diff] [blame] | 31 | class TransformableIncomingAudioFrame |
| 32 | : public TransformableAudioFrameInterface { |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 33 | public: |
Tony Herre | 8fb41a3 | 2021-09-24 12:05:20 | [diff] [blame] | 34 | TransformableIncomingAudioFrame(rtc::ArrayView<const uint8_t> payload, |
| 35 | const RTPHeader& header, |
Philipp Hancke | d209893 | 2023-11-01 18:18:32 | [diff] [blame] | 36 | uint32_t ssrc, |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 37 | const std::string& codec_mime_type, |
| 38 | Timestamp receive_time) |
Tony Herre | a45c705 | 2024-05-16 11:38:25 | [diff] [blame] | 39 | : TransformableAudioFrameInterface(Passkey()), |
| 40 | payload_(payload.data(), payload.size()), |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 41 | header_(header), |
Philipp Hancke | d209893 | 2023-11-01 18:18:32 | [diff] [blame] | 42 | ssrc_(ssrc), |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 43 | codec_mime_type_(codec_mime_type), |
| 44 | receive_time_(receive_time) {} |
Tony Herre | 8fb41a3 | 2021-09-24 12:05:20 | [diff] [blame] | 45 | ~TransformableIncomingAudioFrame() override = default; |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 46 | rtc::ArrayView<const uint8_t> GetData() const override { return payload_; } |
| 47 | |
| 48 | void SetData(rtc::ArrayView<const uint8_t> data) override { |
| 49 | payload_.SetData(data.data(), data.size()); |
| 50 | } |
| 51 | |
Palak Agarwal | fc260a18 | 2023-06-07 14:17:49 | [diff] [blame] | 52 | void SetRTPTimestamp(uint32_t timestamp) override { |
| 53 | header_.timestamp = timestamp; |
| 54 | } |
| 55 | |
Philipp Hancke | 2ace42f | 2021-08-24 08:40:12 | [diff] [blame] | 56 | uint8_t GetPayloadType() const override { return header_.payloadType; } |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 57 | uint32_t GetSsrc() const override { return ssrc_; } |
Philipp Hancke | 2ace42f | 2021-08-24 08:40:12 | [diff] [blame] | 58 | uint32_t GetTimestamp() const override { return header_.timestamp; } |
Sergio Garcia Murillo | 15dfc5a | 2022-10-11 10:34:44 | [diff] [blame] | 59 | rtc::ArrayView<const uint32_t> GetContributingSources() const override { |
| 60 | return rtc::ArrayView<const uint32_t>(header_.arrOfCSRCs, header_.numCSRCs); |
| 61 | } |
Tony Herre | 8fb41a3 | 2021-09-24 12:05:20 | [diff] [blame] | 62 | Direction GetDirection() const override { return Direction::kReceiver; } |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 63 | |
Philipp Hancke | d209893 | 2023-11-01 18:18:32 | [diff] [blame] | 64 | std::string GetMimeType() const override { return codec_mime_type_; } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 65 | const std::optional<uint16_t> SequenceNumber() const override { |
Tony Herre | 097a4de | 2023-06-19 15:13:08 | [diff] [blame] | 66 | return header_.sequenceNumber; |
| 67 | } |
| 68 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 69 | std::optional<uint64_t> AbsoluteCaptureTimestamp() const override { |
Tony Herre | fc68f1f | 2023-06-22 13:32:24 | [diff] [blame] | 70 | // This could be extracted from received header extensions + extrapolation, |
| 71 | // if required in future, eg for being able to re-send received frames. |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 72 | return std::nullopt; |
Tony Herre | fc68f1f | 2023-06-22 13:32:24 | [diff] [blame] | 73 | } |
| 74 | const RTPHeader& Header() const { return header_; } |
| 75 | |
| 76 | FrameType Type() const override { |
Joachim Reiersen | 5075cb4 | 2024-03-22 01:08:54 | [diff] [blame] | 77 | if (!header_.extension.audio_level()) { |
| 78 | // Audio level extension not set. |
| 79 | return FrameType::kAudioFrameCN; |
| 80 | } |
| 81 | return header_.extension.audio_level()->voice_activity() |
| 82 | ? FrameType::kAudioFrameSpeech |
| 83 | : FrameType::kAudioFrameCN; |
Tony Herre | fc68f1f | 2023-06-22 13:32:24 | [diff] [blame] | 84 | } |
| 85 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 86 | std::optional<uint8_t> AudioLevel() const override { |
Tony Herre | 64437e8 | 2024-04-29 13:13:48 | [diff] [blame] | 87 | if (header_.extension.audio_level()) { |
| 88 | return header_.extension.audio_level()->level(); |
| 89 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 90 | return std::nullopt; |
Tony Herre | 64437e8 | 2024-04-29 13:13:48 | [diff] [blame] | 91 | } |
| 92 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 93 | std::optional<Timestamp> ReceiveTime() const override { |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 94 | return receive_time_ == Timestamp::MinusInfinity() |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 95 | ? std::nullopt |
| 96 | : std::optional<Timestamp>(receive_time_); |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 97 | } |
| 98 | |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 99 | private: |
| 100 | rtc::Buffer payload_; |
| 101 | RTPHeader header_; |
| 102 | uint32_t ssrc_; |
Philipp Hancke | d209893 | 2023-11-01 18:18:32 | [diff] [blame] | 103 | std::string codec_mime_type_; |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 104 | Timestamp receive_time_; |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 105 | }; |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 106 | |
| 107 | ChannelReceiveFrameTransformerDelegate::ChannelReceiveFrameTransformerDelegate( |
| 108 | ReceiveFrameCallback receive_frame_callback, |
| 109 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer, |
Tommi | e2e0464 | 2021-06-09 14:11:11 | [diff] [blame] | 110 | TaskQueueBase* channel_receive_thread) |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 111 | : receive_frame_callback_(receive_frame_callback), |
| 112 | frame_transformer_(std::move(frame_transformer)), |
| 113 | channel_receive_thread_(channel_receive_thread) {} |
| 114 | |
| 115 | void ChannelReceiveFrameTransformerDelegate::Init() { |
| 116 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 117 | frame_transformer_->RegisterTransformedFrameCallback( |
| 118 | rtc::scoped_refptr<TransformedFrameCallback>(this)); |
| 119 | } |
| 120 | |
| 121 | void ChannelReceiveFrameTransformerDelegate::Reset() { |
| 122 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 123 | frame_transformer_->UnregisterTransformedFrameCallback(); |
| 124 | frame_transformer_ = nullptr; |
| 125 | receive_frame_callback_ = ReceiveFrameCallback(); |
| 126 | } |
| 127 | |
| 128 | void ChannelReceiveFrameTransformerDelegate::Transform( |
| 129 | rtc::ArrayView<const uint8_t> packet, |
| 130 | const RTPHeader& header, |
Philipp Hancke | d209893 | 2023-11-01 18:18:32 | [diff] [blame] | 131 | uint32_t ssrc, |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 132 | const std::string& codec_mime_type, |
| 133 | Timestamp receive_time) { |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 134 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Tony Herre | 6e95605 | 2023-11-16 13:59:54 | [diff] [blame] | 135 | if (short_circuit_) { |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 136 | receive_frame_callback_(packet, header, receive_time); |
Tony Herre | 6e95605 | 2023-11-16 13:59:54 | [diff] [blame] | 137 | } else { |
| 138 | frame_transformer_->Transform( |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 139 | std::make_unique<TransformableIncomingAudioFrame>( |
| 140 | packet, header, ssrc, codec_mime_type, receive_time)); |
Tony Herre | 6e95605 | 2023-11-16 13:59:54 | [diff] [blame] | 141 | } |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void ChannelReceiveFrameTransformerDelegate::OnTransformedFrame( |
| 145 | std::unique_ptr<TransformableFrameInterface> frame) { |
Niels Möller | a3361ff | 2022-01-13 13:05:38 | [diff] [blame] | 146 | rtc::scoped_refptr<ChannelReceiveFrameTransformerDelegate> delegate(this); |
Danil Chapovalov | c05a1be | 2022-07-19 11:07:12 | [diff] [blame] | 147 | channel_receive_thread_->PostTask( |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 148 | [delegate = std::move(delegate), frame = std::move(frame)]() mutable { |
| 149 | delegate->ReceiveFrame(std::move(frame)); |
Danil Chapovalov | c05a1be | 2022-07-19 11:07:12 | [diff] [blame] | 150 | }); |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 151 | } |
| 152 | |
Tony Herre | 6e95605 | 2023-11-16 13:59:54 | [diff] [blame] | 153 | void ChannelReceiveFrameTransformerDelegate::StartShortCircuiting() { |
| 154 | rtc::scoped_refptr<ChannelReceiveFrameTransformerDelegate> delegate(this); |
| 155 | channel_receive_thread_->PostTask([delegate = std::move(delegate)]() mutable { |
| 156 | RTC_DCHECK_RUN_ON(&delegate->sequence_checker_); |
| 157 | delegate->short_circuit_ = true; |
| 158 | }); |
| 159 | } |
| 160 | |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 161 | void ChannelReceiveFrameTransformerDelegate::ReceiveFrame( |
| 162 | std::unique_ptr<TransformableFrameInterface> frame) const { |
| 163 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 164 | if (!receive_frame_callback_) |
| 165 | return; |
Palak Agarwal | f263e1e | 2023-08-18 12:09:53 | [diff] [blame] | 166 | |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 167 | auto* transformed_frame = |
| 168 | static_cast<TransformableAudioFrameInterface*>(frame.get()); |
| 169 | Timestamp receive_time = |
| 170 | transformed_frame->ReceiveTime().value_or(Timestamp::MinusInfinity()); |
Palak Agarwal | f263e1e | 2023-08-18 12:09:53 | [diff] [blame] | 171 | RTPHeader header; |
| 172 | if (frame->GetDirection() == |
| 173 | TransformableFrameInterface::Direction::kSender) { |
Palak Agarwal | f263e1e | 2023-08-18 12:09:53 | [diff] [blame] | 174 | header.payloadType = transformed_frame->GetPayloadType(); |
| 175 | header.timestamp = transformed_frame->GetTimestamp(); |
| 176 | header.ssrc = transformed_frame->GetSsrc(); |
| 177 | if (transformed_frame->AbsoluteCaptureTimestamp().has_value()) { |
| 178 | header.extension.absolute_capture_time = AbsoluteCaptureTime(); |
| 179 | header.extension.absolute_capture_time->absolute_capture_timestamp = |
| 180 | transformed_frame->AbsoluteCaptureTimestamp().value(); |
| 181 | } |
| 182 | } else { |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 183 | auto* transformed_incoming_frame = |
Palak Agarwal | f263e1e | 2023-08-18 12:09:53 | [diff] [blame] | 184 | static_cast<TransformableIncomingAudioFrame*>(frame.get()); |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 185 | header = transformed_incoming_frame->Header(); |
Palak Agarwal | f263e1e | 2023-08-18 12:09:53 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // TODO(crbug.com/1464860): Take an explicit struct with the required |
| 189 | // information rather than the RTPHeader to make it easier to |
| 190 | // construct the required information when injecting transformed frames not |
| 191 | // originally from this receiver. |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 192 | receive_frame_callback_(frame->GetData(), header, receive_time); |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 193 | } |
Tony Herre | f921d25 | 2023-11-27 09:37:06 | [diff] [blame] | 194 | |
| 195 | rtc::scoped_refptr<FrameTransformerInterface> |
| 196 | ChannelReceiveFrameTransformerDelegate::FrameTransformer() { |
| 197 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 198 | return frame_transformer_; |
| 199 | } |
| 200 | |
Lionel Koenig Gélas | b446251 | 2024-07-30 13:47:41 | [diff] [blame] | 201 | std::unique_ptr<TransformableAudioFrameInterface> CloneReceiverAudioFrame( |
| 202 | TransformableAudioFrameInterface* original) { |
| 203 | RTC_CHECK(original->GetDirection() == |
| 204 | TransformableFrameInterface::Direction::kReceiver); |
| 205 | |
| 206 | auto* original_incoming_frame = |
| 207 | static_cast<TransformableIncomingAudioFrame*>(original); |
| 208 | return std::make_unique<TransformableIncomingAudioFrame>( |
| 209 | original->GetData(), original_incoming_frame->Header(), |
| 210 | original->GetSsrc(), original->GetMimeType(), |
| 211 | original->ReceiveTime().value_or(Timestamp::MinusInfinity())); |
| 212 | } |
Marina Ciocea | 4862320 | 2020-04-01 08:19:44 | [diff] [blame] | 213 | } // namespace webrtc |