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
|
// 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_video_underlying_sink.h"
#include "base/unguessable_token.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_encoded_video_frame.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/modules/peerconnection/rtc_encoded_video_frame.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_encoded_video_stream_transformer.h"
#include "third_party/webrtc/api/frame_transformer_interface.h"
namespace blink {
using webrtc::TransformableFrameInterface;
RTCEncodedVideoUnderlyingSink::RTCEncodedVideoUnderlyingSink(
ScriptState* script_state,
scoped_refptr<blink::RTCEncodedVideoStreamTransformer::Broker>
transformer_broker,
bool detach_frame_data_on_write)
: blink::RTCEncodedVideoUnderlyingSink(script_state,
std::move(transformer_broker),
detach_frame_data_on_write,
/*enable_frame_restrictions=*/false,
base::UnguessableToken::Null()) {}
RTCEncodedVideoUnderlyingSink::RTCEncodedVideoUnderlyingSink(
ScriptState* script_state,
scoped_refptr<blink::RTCEncodedVideoStreamTransformer::Broker>
transformer_broker,
bool detach_frame_data_on_write,
bool enable_frame_restrictions,
base::UnguessableToken owner_id)
: transformer_broker_(std::move(transformer_broker)),
detach_frame_data_on_write_(detach_frame_data_on_write),
enable_frame_restrictions_(enable_frame_restrictions),
owner_id_(owner_id) {
DCHECK(transformer_broker_);
}
ScriptPromise<IDLUndefined> RTCEncodedVideoUnderlyingSink::start(
ScriptState* script_state,
WritableStreamDefaultController* controller,
ExceptionState&) {
// No extra setup needed.
return ToResolvedUndefinedPromise(script_state);
}
ScriptPromise<IDLUndefined> RTCEncodedVideoUnderlyingSink::write(
ScriptState* script_state,
ScriptValue chunk,
WritableStreamDefaultController* controller,
ExceptionState& exception_state) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
RTCEncodedVideoFrame* encoded_frame = V8RTCEncodedVideoFrame::ToWrappable(
script_state->GetIsolate(), chunk.V8Value());
if (!encoded_frame) {
exception_state.ThrowTypeError("Invalid frame");
return EmptyPromise();
}
if (enable_frame_restrictions_ &&
(encoded_frame->OwnerId() != owner_id_ ||
encoded_frame->Counter() <= last_received_frame_counter_)) {
return ToResolvedUndefinedPromise(script_state);
}
last_received_frame_counter_ = encoded_frame->Counter();
if (!transformer_broker_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Stream closed");
return EmptyPromise();
}
auto webrtc_frame = encoded_frame->PassWebRtcFrame(
script_state->GetIsolate(), detach_frame_data_on_write_);
if (!webrtc_frame) {
exception_state.ThrowDOMException(DOMExceptionCode::kOperationError,
"Empty frame");
return EmptyPromise();
}
transformer_broker_->SendFrameToSink(std::move(webrtc_frame));
return ToResolvedUndefinedPromise(script_state);
}
ScriptPromise<IDLUndefined> RTCEncodedVideoUnderlyingSink::close(
ScriptState* script_state,
ExceptionState&) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// Disconnect from the transformer if the sink is closed.
if (transformer_broker_)
transformer_broker_.reset();
return ToResolvedUndefinedPromise(script_state);
}
ScriptPromise<IDLUndefined> RTCEncodedVideoUnderlyingSink::abort(
ScriptState* script_state,
ScriptValue reason,
ExceptionState& exception_state) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// It is not possible to cancel any frames already sent to the WebRTC sink,
// thus abort() has the same effect as close().
return close(script_state, exception_state);
}
void RTCEncodedVideoUnderlyingSink::ResetTransformerCallback() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
transformer_broker_->ResetTransformerCallback();
}
void RTCEncodedVideoUnderlyingSink::Trace(Visitor* visitor) const {
UnderlyingSinkBase::Trace(visitor);
}
} // namespace blink
|