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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
// Copyright 2014 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/media_stream_remote_video_source.h"
#include <stdint.h>
#include <utility>
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_frame.h"
#include "media/base/video_util.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-blink.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/webrtc/convert_to_webrtc_video_frame_buffer.h"
#include "third_party/blink/renderer/platform/webrtc/track_observer.h"
#include "third_party/blink/renderer/platform/webrtc/webrtc_video_frame_adapter.h"
#include "third_party/blink/renderer/platform/webrtc/webrtc_video_utils.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
#include "third_party/webrtc/api/video/i420_buffer.h"
#include "third_party/webrtc/api/video/recordable_encoded_frame.h"
#include "third_party/webrtc/rtc_base/time_utils.h"
#include "third_party/webrtc/system_wrappers/include/clock.h"
namespace blink {
namespace {
class WebRtcEncodedVideoFrame : public EncodedVideoFrame {
public:
explicit WebRtcEncodedVideoFrame(const webrtc::RecordableEncodedFrame& frame)
: buffer_(frame.encoded_buffer()),
codec_(WebRtcToMediaVideoCodec(frame.codec())),
is_key_frame_(frame.is_key_frame()),
resolution_(frame.resolution().width, frame.resolution().height) {
if (frame.color_space()) {
color_space_ = WebRtcToGfxColorSpace(*frame.color_space());
}
if (frame.video_rotation()) {
transformation_ = WebRtcToMediaVideoRotation(*frame.video_rotation());
}
}
base::span<const uint8_t> Data() const override { return *buffer_; }
media::VideoCodec Codec() const override { return codec_; }
bool IsKeyFrame() const override { return is_key_frame_; }
std::optional<gfx::ColorSpace> ColorSpace() const override {
return color_space_;
}
std::optional<media::VideoTransformation> Transformation() const override {
return transformation_;
}
gfx::Size Resolution() const override { return resolution_; }
private:
webrtc::scoped_refptr<const webrtc::EncodedImageBufferInterface> buffer_;
media::VideoCodec codec_;
bool is_key_frame_;
std::optional<gfx::ColorSpace> color_space_;
std::optional<media::VideoTransformation> transformation_;
gfx::Size resolution_;
};
} // namespace
// Internal class used for receiving frames from the webrtc track on a
// libjingle thread and forward it to the IO-thread.
class MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate
: public WTF::ThreadSafeRefCounted<RemoteVideoSourceDelegate>,
public webrtc::VideoSinkInterface<webrtc::VideoFrame>,
public webrtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> {
public:
RemoteVideoSourceDelegate(
scoped_refptr<base::SequencedTaskRunner> video_task_runner,
VideoCaptureDeliverFrameCB new_frame_callback,
EncodedVideoFrameCB encoded_frame_callback,
VideoCaptureSubCaptureTargetVersionCB
sub_capture_target_version_callback);
protected:
friend class WTF::ThreadSafeRefCounted<RemoteVideoSourceDelegate>;
~RemoteVideoSourceDelegate() override;
// Implements webrtc::VideoSinkInterface used for receiving video frames
// from the PeerConnection video track. May be called on a libjingle internal
// thread.
void OnFrame(const webrtc::VideoFrame& frame) override;
// VideoSinkInterface<webrtc::RecordableEncodedFrame>
void OnFrame(const webrtc::RecordableEncodedFrame& frame) override;
void DoRenderFrameOnIOThread(scoped_refptr<media::VideoFrame> video_frame,
base::TimeTicks estimated_capture_time);
private:
void OnEncodedVideoFrameOnIO(scoped_refptr<EncodedVideoFrame> frame,
base::TimeTicks estimated_capture_time);
scoped_refptr<base::SequencedTaskRunner> video_task_runner_;
// |frame_callback_| is accessed on the IO thread.
VideoCaptureDeliverFrameCB frame_callback_;
// |encoded_frame_callback_| is accessed on the IO thread.
EncodedVideoFrameCB encoded_frame_callback_;
// |sub_capture_target_version_callback| is accessed on the IO thread.
VideoCaptureSubCaptureTargetVersionCB sub_capture_target_version_callback_;
// Timestamp of the first received frame.
std::optional<base::TimeTicks> start_timestamp_;
// WebRTC real time clock, needed to determine NTP offset.
raw_ptr<webrtc::Clock> clock_;
// Offset between NTP clock and WebRTC clock.
const int64_t ntp_offset_;
// Determined from a feature flag; if set WebRTC won't forward an unspecified
// color space.
const bool ignore_unspecified_color_space_;
};
MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::
RemoteVideoSourceDelegate(
scoped_refptr<base::SequencedTaskRunner> video_task_runner,
VideoCaptureDeliverFrameCB new_frame_callback,
EncodedVideoFrameCB encoded_frame_callback,
VideoCaptureSubCaptureTargetVersionCB
sub_capture_target_version_callback)
: video_task_runner_(video_task_runner),
frame_callback_(std::move(new_frame_callback)),
encoded_frame_callback_(std::move(encoded_frame_callback)),
sub_capture_target_version_callback_(
std::move(sub_capture_target_version_callback)),
clock_(webrtc::Clock::GetRealTimeClock()),
ntp_offset_(clock_->TimeInMilliseconds() -
clock_->CurrentNtpInMilliseconds()),
ignore_unspecified_color_space_(base::FeatureList::IsEnabled(
features::kWebRtcIgnoreUnspecifiedColorSpace)) {}
MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::
~RemoteVideoSourceDelegate() = default;
void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::OnFrame(
const webrtc::VideoFrame& incoming_frame) {
const webrtc::VideoFrame::RenderParameters render_parameters =
incoming_frame.render_parameters();
const bool render_immediately = render_parameters.use_low_latency_rendering ||
incoming_frame.timestamp_us() == 0;
const base::TimeTicks current_time = base::TimeTicks::Now();
const base::TimeTicks render_time =
render_immediately
? current_time
: base::TimeTicks() +
base::Microseconds(incoming_frame.timestamp_us());
if (!start_timestamp_)
start_timestamp_ = render_time;
const base::TimeDelta elapsed_timestamp = render_time - *start_timestamp_;
TRACE_EVENT2("webrtc", "RemoteVideoSourceDelegate::RenderFrame",
"Ideal Render Instant", render_time.ToInternalValue(),
"Timestamp", elapsed_timestamp.InMicroseconds());
webrtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
incoming_frame.video_frame_buffer();
scoped_refptr<media::VideoFrame> video_frame;
if (buffer->type() == webrtc::VideoFrameBuffer::Type::kNative) {
video_frame = static_cast<WebRtcVideoFrameAdapterInterface*>(buffer.get())
->getMediaVideoFrame();
video_frame->set_timestamp(elapsed_timestamp);
} else {
video_frame =
ConvertFromMappedWebRtcVideoFrameBuffer(buffer, elapsed_timestamp);
}
if (!video_frame)
return;
// Rotation may be explicitly set sometimes.
if (incoming_frame.rotation() != webrtc::kVideoRotation_0) {
video_frame->metadata().transformation =
WebRtcToMediaVideoRotation(incoming_frame.rotation());
}
// The second clause of the condition is controlled by the feature flag
// WebRtcIgnoreUnspecifiedColorSpace. If the feature is enabled we won't try
// to guess a color space if the webrtc::ColorSpace is unspecified. If the
// feature is disabled (default), an unspecified color space will get
// converted into a gfx::ColorSpace set to BT709.
if (incoming_frame.color_space() &&
!(ignore_unspecified_color_space_ &&
incoming_frame.color_space()->primaries() ==
webrtc::ColorSpace::PrimaryID::kUnspecified &&
incoming_frame.color_space()->transfer() ==
webrtc::ColorSpace::TransferID::kUnspecified &&
incoming_frame.color_space()->matrix() ==
webrtc::ColorSpace::MatrixID::kUnspecified)) {
video_frame->set_color_space(
WebRtcToGfxColorSpace(*incoming_frame.color_space()));
}
// Run render smoothness algorithm only when we don't have to render
// immediately.
if (!render_immediately)
video_frame->metadata().reference_time = render_time;
if (render_parameters.max_composition_delay_in_frames) {
video_frame->metadata().maximum_composition_delay_in_frames =
render_parameters.max_composition_delay_in_frames;
}
video_frame->metadata().decode_end_time = current_time;
// RTP_TIMESTAMP, PROCESSING_TIME, and CAPTURE_BEGIN_TIME are all exposed
// through the JavaScript callback mechanism
// video.requestVideoFrameCallback().
video_frame->metadata().rtp_timestamp =
static_cast<double>(incoming_frame.rtp_timestamp());
if (incoming_frame.processing_time()) {
video_frame->metadata().processing_time =
base::Microseconds(incoming_frame.processing_time()->Elapsed().us());
}
// Set capture time to the NTP time, which is the estimated capture time
// converted to the local clock.
if (incoming_frame.ntp_time_ms() > 0) {
video_frame->metadata().capture_begin_time =
base::TimeTicks() +
base::Milliseconds(incoming_frame.ntp_time_ms() + ntp_offset_);
}
// Set receive time to arrival of last packet.
if (!incoming_frame.packet_infos().empty()) {
webrtc::Timestamp last_packet_arrival =
std::max_element(
incoming_frame.packet_infos().cbegin(),
incoming_frame.packet_infos().cend(),
[](const webrtc::RtpPacketInfo& a, const webrtc::RtpPacketInfo& b) {
return a.receive_time() < b.receive_time();
})
->receive_time();
video_frame->metadata().receive_time =
base::TimeTicks() + base::Microseconds(last_packet_arrival.us());
base::UmaHistogramTimes(
"WebRTC.Video.TotalReceiveDelay",
current_time - *video_frame->metadata().receive_time);
}
// Use our computed render time as estimated capture time. If timestamp_us()
// (which is actually the suggested render time) is set by WebRTC, it's based
// on the RTP timestamps in the frame's packets, so congruent with the
// received frame capture timestamps. If set by us, it's as congruent as we
// can get with the timestamp sequence of frames we received.
PostCrossThreadTask(
*video_task_runner_, FROM_HERE,
CrossThreadBindOnce(&RemoteVideoSourceDelegate::DoRenderFrameOnIOThread,
WrapRefCounted(this), video_frame, render_time));
}
void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::
DoRenderFrameOnIOThread(scoped_refptr<media::VideoFrame> video_frame,
base::TimeTicks estimated_capture_time) {
DCHECK(video_task_runner_->RunsTasksInCurrentSequence());
TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::DoRenderFrameOnIOThread");
frame_callback_.Run(std::move(video_frame), estimated_capture_time);
}
void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::OnFrame(
const webrtc::RecordableEncodedFrame& frame) {
const bool render_immediately = frame.render_time().us() == 0;
const base::TimeTicks current_time = base::TimeTicks::Now();
const base::TimeTicks render_time =
render_immediately
? current_time
: base::TimeTicks() + base::Microseconds(frame.render_time().us());
// Use our computed render time as estimated capture time. If render_time()
// is set by WebRTC, it's based on the RTP timestamps in the frame's packets,
// so congruent with the received frame capture timestamps. If set by us, it's
// as congruent as we can get with the timestamp sequence of frames we
// received.
PostCrossThreadTask(
*video_task_runner_, FROM_HERE,
CrossThreadBindOnce(&RemoteVideoSourceDelegate::OnEncodedVideoFrameOnIO,
WrapRefCounted(this),
base::MakeRefCounted<WebRtcEncodedVideoFrame>(frame),
render_time));
}
void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::
OnEncodedVideoFrameOnIO(scoped_refptr<EncodedVideoFrame> frame,
base::TimeTicks estimated_capture_time) {
DCHECK(video_task_runner_->RunsTasksInCurrentSequence());
encoded_frame_callback_.Run(std::move(frame), estimated_capture_time);
}
MediaStreamRemoteVideoSource::MediaStreamRemoteVideoSource(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
std::unique_ptr<TrackObserver> observer)
: MediaStreamVideoSource(std::move(task_runner)),
observer_(std::move(observer)) {
// The callback will be automatically cleared when 'observer_' goes out of
// scope and no further callbacks will occur.
observer_->SetCallback(WTF::BindRepeating(
&MediaStreamRemoteVideoSource::OnChanged, WTF::Unretained(this)));
}
MediaStreamRemoteVideoSource::~MediaStreamRemoteVideoSource() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!observer_);
}
void MediaStreamRemoteVideoSource::OnSourceTerminated() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
StopSourceImpl();
}
void MediaStreamRemoteVideoSource::StartSourceImpl(
MediaStreamVideoSourceCallbacks media_stream_callbacks) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!delegate_.get());
delegate_ = base::MakeRefCounted<RemoteVideoSourceDelegate>(
video_task_runner(), std::move(media_stream_callbacks.deliver_frame_cb),
std::move(media_stream_callbacks.encoded_frame_cb),
std::move(media_stream_callbacks.sub_capture_target_version_cb));
scoped_refptr<webrtc::VideoTrackInterface> video_track(
static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
video_track->AddOrUpdateSink(delegate_.get(), webrtc::VideoSinkWants());
OnStartDone(mojom::MediaStreamRequestResult::OK);
}
void MediaStreamRemoteVideoSource::StopSourceImpl() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// StopSourceImpl is called either when MediaStreamTrack.stop is called from
// JS or blink gc the MediaStreamSource object or when OnSourceTerminated()
// is called. Garbage collection will happen after the PeerConnection no
// longer receives the video track.
if (!observer_)
return;
DCHECK(state() != MediaStreamVideoSource::ENDED);
scoped_refptr<webrtc::VideoTrackInterface> video_track(
static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
video_track->RemoveSink(delegate_.get());
// This removes the references to the webrtc video track.
observer_.reset();
}
webrtc::VideoSinkInterface<webrtc::VideoFrame>*
MediaStreamRemoteVideoSource::SinkInterfaceForTesting() {
return delegate_.get();
}
webrtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>*
MediaStreamRemoteVideoSource::EncodedSinkInterfaceForTesting() {
return delegate_.get();
}
void MediaStreamRemoteVideoSource::OnChanged(
webrtc::MediaStreamTrackInterface::TrackState state) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
switch (state) {
case webrtc::MediaStreamTrackInterface::kLive:
SetReadyState(WebMediaStreamSource::kReadyStateLive);
break;
case webrtc::MediaStreamTrackInterface::kEnded:
SetReadyState(WebMediaStreamSource::kReadyStateEnded);
break;
default:
NOTREACHED();
}
}
bool MediaStreamRemoteVideoSource::SupportsEncodedOutput() const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!observer_ || !observer_->track()) {
return false;
}
scoped_refptr<webrtc::VideoTrackInterface> video_track(
static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
return video_track->GetSource()->SupportsEncodedOutput();
}
void MediaStreamRemoteVideoSource::RequestKeyFrame() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!observer_ || !observer_->track()) {
return;
}
scoped_refptr<webrtc::VideoTrackInterface> video_track(
static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
if (video_track->GetSource()) {
video_track->GetSource()->GenerateKeyFrame();
}
}
base::WeakPtr<MediaStreamVideoSource>
MediaStreamRemoteVideoSource::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void MediaStreamRemoteVideoSource::OnEncodedSinkEnabled() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!observer_ || !observer_->track()) {
return;
}
scoped_refptr<webrtc::VideoTrackInterface> video_track(
static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
video_track->GetSource()->AddEncodedSink(delegate_.get());
}
void MediaStreamRemoteVideoSource::OnEncodedSinkDisabled() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!observer_ || !observer_->track()) {
return;
}
scoped_refptr<webrtc::VideoTrackInterface> video_track(
static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
video_track->GetSource()->RemoveEncodedSink(delegate_.get());
}
} // namespace blink
|