1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/peerconnection/rtc_encoded_audio_frame_delegate.h"
#include <optional>
#include <utility>
#include "base/time/time.h"
#include "base/types/expected.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"
#include "third_party/blink/renderer/modules/peerconnection/peer_connection_util.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/peerconnection/webrtc_util.h"
#include "third_party/webrtc/api/frame_transformer_factory.h"
#include "third_party/webrtc/api/frame_transformer_interface.h"
#include "third_party/webrtc/api/units/time_delta.h"
#include "third_party/webrtc/api/units/timestamp.h"
namespace blink {
static constexpr char kRTCEncodedAudioFrameDetachKey[] = "RTCEncodedAudioFrame";
static constexpr int kAcceptableCaptureTimeDeltaMs = 1;
const void* RTCEncodedAudioFramesAttachment::kAttachmentKey;
RTCEncodedAudioFrameDelegate::RTCEncodedAudioFrameDelegate(
std::unique_ptr<webrtc::TransformableAudioFrameInterface> webrtc_frame,
webrtc::ArrayView<const unsigned int> contributing_sources,
std::optional<uint16_t> sequence_number)
: webrtc_frame_(std::move(webrtc_frame)),
contributing_sources_(contributing_sources),
sequence_number_(sequence_number) {}
uint32_t RTCEncodedAudioFrameDelegate::RtpTimestamp() const {
base::AutoLock lock(lock_);
return webrtc_frame_ ? webrtc_frame_->GetTimestamp() : 0;
}
DOMArrayBuffer* RTCEncodedAudioFrameDelegate::CreateDataBuffer(
v8::Isolate* isolate) const {
ArrayBufferContents contents;
{
base::AutoLock lock(lock_);
if (!webrtc_frame_) {
// WebRTC frame already passed, return a detached ArrayBuffer.
DOMArrayBuffer* buffer = DOMArrayBuffer::Create(
/*num_elements=*/static_cast<size_t>(0), /*element_byte_size=*/1);
ArrayBufferContents contents_to_drop;
NonThrowableExceptionState exception_state;
buffer->Transfer(isolate,
V8AtomicString(isolate, kRTCEncodedAudioFrameDetachKey),
contents_to_drop, exception_state);
return buffer;
}
auto data = webrtc_frame_->GetData();
contents = ArrayBufferContents(
data.size(), 1, ArrayBufferContents::kNotShared,
ArrayBufferContents::kDontInitialize,
ArrayBufferContents::AllocationFailureBehavior::kCrash);
CHECK(contents.IsValid());
contents.ByteSpan().copy_from(data);
}
return DOMArrayBuffer::Create(std::move(contents));
}
void RTCEncodedAudioFrameDelegate::SetData(const DOMArrayBuffer* data) {
base::AutoLock lock(lock_);
if (webrtc_frame_ && data) {
webrtc_frame_->SetData(webrtc::ArrayView<const uint8_t>(
static_cast<const uint8_t*>(data->Data()), data->ByteLength()));
}
}
base::expected<void, String>
RTCEncodedAudioFrameDelegate::SetWebRtcFrameMetadata(
ExecutionContext* context,
const RTCEncodedAudioFrameMetadata* metadata) {
base::AutoLock lock(lock_);
if (!webrtc_frame_) {
return base::unexpected("Underlying webrtc frame doesn't exist.");
}
// Payload type always has a current value. The new metadata must match it if
// SetPayloadType is not supported.
if (metadata->hasPayloadType()) {
if (metadata->payloadType() != webrtc_frame_->GetPayloadType() &&
!webrtc_frame_->CanSetPayloadType()) {
return base::unexpected("payloadType cannot be modified");
}
// Payload types must be in the [0,127] range, but values in the [64,95]
// range are reserved for RCTP. For additional details, see
// https://tools.ietf.org/html/rfc5761#section-4
if ((metadata->payloadType() >= 64u && metadata->payloadType() <= 95u) ||
metadata->payloadType() > 127u) {
return base::unexpected("invalid payloadType value");
}
}
std::optional<uint8_t> audio_level_dbov;
if (metadata->hasAudioLevel()) {
audio_level_dbov = FromLinearAudioLevel(metadata->audioLevel());
}
if (audio_level_dbov != webrtc_frame_->AudioLevel() &&
!webrtc_frame_->CanSetAudioLevel()) {
return base::unexpected("audioLevel cannot be modified");
}
std::optional<webrtc::Timestamp> capture_time;
if (metadata->hasCaptureTime()) {
CaptureTimeInfo::ClockType clock_type;
switch (webrtc_frame_->GetDirection()) {
case webrtc::TransformableFrameInterface::Direction::kReceiver:
clock_type = CaptureTimeInfo::ClockType::kNtpRealClock;
break;
case webrtc::TransformableFrameInterface::Direction::kSender:
clock_type = CaptureTimeInfo::ClockType::kTimeTicks;
break;
case webrtc::TransformableFrameInterface::Direction::kUnknown:
return base::unexpected("captureTime not supported for this frame");
}
base::TimeDelta capture_time_delta = RTCEncodedFrameTimestampToCaptureTime(
context, metadata->captureTime(), clock_type);
capture_time =
webrtc::Timestamp::Micros(capture_time_delta.InMicroseconds());
}
bool capture_time_is_different = false;
if (!webrtc_frame_->CanSetCaptureTime() && capture_time.has_value()) {
if (!webrtc_frame_->CaptureTime().has_value()) {
capture_time_is_different = true;
} else {
// Ignore small differences in capture time.
webrtc::TimeDelta delta = *capture_time - *webrtc_frame_->CaptureTime();
if (delta.Abs() >
webrtc::TimeDelta::Millis(kAcceptableCaptureTimeDeltaMs)) {
capture_time_is_different = true;
}
}
}
if (capture_time_is_different && !webrtc_frame_->CanSetCaptureTime()) {
return base::unexpected("captureTime cannot be modified");
}
webrtc_frame_->SetRTPTimestamp(metadata->rtpTimestamp());
if (metadata->hasPayloadType() && webrtc_frame_->CanSetPayloadType()) {
webrtc_frame_->SetPayloadType(metadata->payloadType());
}
if (webrtc_frame_->CanSetCaptureTime()) {
webrtc_frame_->SetCaptureTime(capture_time);
}
if (webrtc_frame_->CanSetAudioLevel()) {
webrtc_frame_->SetAudioLevel(audio_level_dbov);
}
return base::ok();
}
std::optional<uint32_t> RTCEncodedAudioFrameDelegate::Ssrc() const {
base::AutoLock lock(lock_);
return webrtc_frame_ ? std::make_optional(webrtc_frame_->GetSsrc())
: std::nullopt;
}
std::optional<uint8_t> RTCEncodedAudioFrameDelegate::PayloadType() const {
base::AutoLock lock(lock_);
return webrtc_frame_ ? std::make_optional(webrtc_frame_->GetPayloadType())
: std::nullopt;
}
std::optional<std::string> RTCEncodedAudioFrameDelegate::MimeType() const {
base::AutoLock lock(lock_);
return webrtc_frame_ ? std::make_optional(webrtc_frame_->GetMimeType())
: std::nullopt;
}
std::optional<uint16_t> RTCEncodedAudioFrameDelegate::SequenceNumber() const {
return sequence_number_;
}
Vector<uint32_t> RTCEncodedAudioFrameDelegate::ContributingSources() const {
return contributing_sources_;
}
std::optional<base::TimeTicks> RTCEncodedAudioFrameDelegate::ReceiveTime()
const {
base::AutoLock lock(lock_);
if (!webrtc_frame_) {
return std::nullopt;
}
return ConvertToOptionalTimeTicks(webrtc_frame_->ReceiveTime());
}
std::optional<CaptureTimeInfo> RTCEncodedAudioFrameDelegate::CaptureTime()
const {
base::AutoLock lock(lock_);
if (!webrtc_frame_ || !webrtc_frame_->CaptureTime()) {
return std::nullopt;
}
CaptureTimeInfo::ClockType clock_type;
switch (webrtc_frame_->GetDirection()) {
case webrtc::TransformableFrameInterface::Direction::kReceiver:
clock_type = CaptureTimeInfo::ClockType::kNtpRealClock;
break;
case webrtc::TransformableFrameInterface::Direction::kSender:
clock_type = CaptureTimeInfo::ClockType::kTimeTicks;
break;
case webrtc::TransformableFrameInterface::Direction::kUnknown:
return std::nullopt;
}
return CaptureTimeInfo(
{.capture_time = base::Microseconds(webrtc_frame_->CaptureTime()->us()),
.clock_type = clock_type});
}
std::optional<base::TimeDelta>
RTCEncodedAudioFrameDelegate::SenderCaptureTimeOffset() const {
base::AutoLock lock(lock_);
if (!webrtc_frame_) {
return std::nullopt;
}
return ConvertToOptionalTimeDelta(webrtc_frame_->SenderCaptureTimeOffset());
}
std::optional<double> RTCEncodedAudioFrameDelegate::AudioLevel() const {
base::AutoLock lock(lock_);
return webrtc_frame_ && webrtc_frame_->AudioLevel()
? std::make_optional(
ToLinearAudioLevel(*webrtc_frame_->AudioLevel()))
: std::nullopt;
}
std::unique_ptr<webrtc::TransformableAudioFrameInterface>
RTCEncodedAudioFrameDelegate::PassWebRtcFrame() {
base::AutoLock lock(lock_);
return std::move(webrtc_frame_);
}
std::unique_ptr<webrtc::TransformableAudioFrameInterface>
RTCEncodedAudioFrameDelegate::CloneWebRtcFrame() {
base::AutoLock lock(lock_);
if (!webrtc_frame_) {
return nullptr;
}
return webrtc::CloneAudioFrame(webrtc_frame_.get());
}
} // namespace blink
|