blob: 29bb0b81d8b6f12ab609c7943e040d0fc4d46585 [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#include "audio/channel_send_frame_transformer_delegate.h"
12
13#include <utility>
14
15namespace webrtc {
16namespace {
17
Tony Herre8fb41a32021-09-24 12:05:20 +000018class TransformableOutgoingAudioFrame : public TransformableFrameInterface {
Marina Ciocea65674d82020-03-31 22:41:30 +020019 public:
Tony Herre8fb41a32021-09-24 12:05:20 +000020 TransformableOutgoingAudioFrame(AudioFrameType frame_type,
21 uint8_t payload_type,
22 uint32_t rtp_timestamp,
23 uint32_t rtp_start_timestamp,
24 const uint8_t* payload_data,
25 size_t payload_size,
26 int64_t absolute_capture_timestamp_ms,
27 uint32_t ssrc)
Marina Ciocea65674d82020-03-31 22:41:30 +020028 : frame_type_(frame_type),
29 payload_type_(payload_type),
30 rtp_timestamp_(rtp_timestamp),
Philipp Hanckeb9d46852020-04-14 15:26:05 +020031 rtp_start_timestamp_(rtp_start_timestamp),
Marina Ciocea65674d82020-03-31 22:41:30 +020032 payload_(payload_data, payload_size),
33 absolute_capture_timestamp_ms_(absolute_capture_timestamp_ms),
34 ssrc_(ssrc) {}
Tony Herre8fb41a32021-09-24 12:05:20 +000035 ~TransformableOutgoingAudioFrame() override = default;
Marina Ciocea65674d82020-03-31 22:41:30 +020036 rtc::ArrayView<const uint8_t> GetData() const override { return payload_; }
37 void SetData(rtc::ArrayView<const uint8_t> data) override {
38 payload_.SetData(data.data(), data.size());
39 }
Philipp Hanckeb9d46852020-04-14 15:26:05 +020040 uint32_t GetTimestamp() const override {
41 return rtp_timestamp_ + rtp_start_timestamp_;
42 }
43 uint32_t GetStartTimestamp() const { return rtp_start_timestamp_; }
Marina Ciocea65674d82020-03-31 22:41:30 +020044 uint32_t GetSsrc() const override { return ssrc_; }
45
46 AudioFrameType GetFrameType() const { return frame_type_; }
Philipp Hancke2ace42f2021-08-24 10:40:12 +020047 uint8_t GetPayloadType() const override { return payload_type_; }
Marina Ciocea65674d82020-03-31 22:41:30 +020048 int64_t GetAbsoluteCaptureTimestampMs() const {
49 return absolute_capture_timestamp_ms_;
50 }
Tony Herre8fb41a32021-09-24 12:05:20 +000051 Direction GetDirection() const override { return Direction::kSender; }
Marina Ciocea65674d82020-03-31 22:41:30 +020052
53 private:
54 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 rtc::Buffer payload_;
59 int64_t absolute_capture_timestamp_ms_;
60 uint32_t ssrc_;
61};
62} // namespace
63
64ChannelSendFrameTransformerDelegate::ChannelSendFrameTransformerDelegate(
65 SendFrameCallback send_frame_callback,
66 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
67 rtc::TaskQueue* encoder_queue)
68 : send_frame_callback_(send_frame_callback),
69 frame_transformer_(std::move(frame_transformer)),
70 encoder_queue_(encoder_queue) {}
71
72void ChannelSendFrameTransformerDelegate::Init() {
73 frame_transformer_->RegisterTransformedFrameCallback(
74 rtc::scoped_refptr<TransformedFrameCallback>(this));
75}
76
77void ChannelSendFrameTransformerDelegate::Reset() {
78 frame_transformer_->UnregisterTransformedFrameCallback();
79 frame_transformer_ = nullptr;
80
Markus Handell62872802020-07-06 15:15:07 +020081 MutexLock lock(&send_lock_);
Marina Ciocea65674d82020-03-31 22:41:30 +020082 send_frame_callback_ = SendFrameCallback();
83}
84
85void ChannelSendFrameTransformerDelegate::Transform(
86 AudioFrameType frame_type,
87 uint8_t payload_type,
88 uint32_t rtp_timestamp,
Philipp Hanckeb9d46852020-04-14 15:26:05 +020089 uint32_t rtp_start_timestamp,
Marina Ciocea65674d82020-03-31 22:41:30 +020090 const uint8_t* payload_data,
91 size_t payload_size,
92 int64_t absolute_capture_timestamp_ms,
93 uint32_t ssrc) {
Tony Herre8fb41a32021-09-24 12:05:20 +000094 frame_transformer_->Transform(
95 std::make_unique<TransformableOutgoingAudioFrame>(
96 frame_type, payload_type, rtp_timestamp, rtp_start_timestamp,
97 payload_data, payload_size, absolute_capture_timestamp_ms, ssrc));
Marina Ciocea65674d82020-03-31 22:41:30 +020098}
99
100void ChannelSendFrameTransformerDelegate::OnTransformedFrame(
101 std::unique_ptr<TransformableFrameInterface> frame) {
Markus Handell62872802020-07-06 15:15:07 +0200102 MutexLock lock(&send_lock_);
Marina Ciocea65674d82020-03-31 22:41:30 +0200103 if (!send_frame_callback_)
104 return;
Niels Möllera3361ff2022-01-13 14:05:38 +0100105 rtc::scoped_refptr<ChannelSendFrameTransformerDelegate> delegate(this);
Marina Ciocea65674d82020-03-31 22:41:30 +0200106 encoder_queue_->PostTask(
107 [delegate = std::move(delegate), frame = std::move(frame)]() mutable {
108 delegate->SendFrame(std::move(frame));
109 });
110}
111
112void ChannelSendFrameTransformerDelegate::SendFrame(
113 std::unique_ptr<TransformableFrameInterface> frame) const {
Markus Handell62872802020-07-06 15:15:07 +0200114 MutexLock lock(&send_lock_);
Marina Ciocea65674d82020-03-31 22:41:30 +0200115 RTC_DCHECK_RUN_ON(encoder_queue_);
Tony Herre8fb41a32021-09-24 12:05:20 +0000116 RTC_CHECK_EQ(frame->GetDirection(),
117 TransformableFrameInterface::Direction::kSender);
Marina Ciocea65674d82020-03-31 22:41:30 +0200118 if (!send_frame_callback_)
119 return;
Tony Herre8fb41a32021-09-24 12:05:20 +0000120 auto* transformed_frame =
121 static_cast<TransformableOutgoingAudioFrame*>(frame.get());
Philipp Hanckeb9d46852020-04-14 15:26:05 +0200122 send_frame_callback_(transformed_frame->GetFrameType(),
123 transformed_frame->GetPayloadType(),
124 transformed_frame->GetTimestamp() -
125 transformed_frame->GetStartTimestamp(),
126 transformed_frame->GetData(),
127 transformed_frame->GetAbsoluteCaptureTimestampMs());
Marina Ciocea65674d82020-03-31 22:41:30 +0200128}
129
130} // namespace webrtc