Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-22 14:04:28 +01:00
parent 81b91f4139
commit f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions

View file

@ -0,0 +1,72 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "video/render/incoming_video_stream.h"
#include <memory>
#include <utility>
#include "absl/types/optional.h"
#include "api/units/time_delta.h"
#include "rtc_base/checks.h"
#include "rtc_base/trace_event.h"
#include "video/render/video_render_frames.h"
namespace webrtc {
IncomingVideoStream::IncomingVideoStream(
TaskQueueFactory* task_queue_factory,
int32_t delay_ms,
rtc::VideoSinkInterface<VideoFrame>* callback)
: render_buffers_(delay_ms),
callback_(callback),
incoming_render_queue_(task_queue_factory->CreateTaskQueue(
"IncomingVideoStream",
TaskQueueFactory::Priority::HIGH)) {}
IncomingVideoStream::~IncomingVideoStream() {
RTC_DCHECK(main_thread_checker_.IsCurrent());
// The queue must be destroyed before its pointer is invalidated to avoid race
// between destructor and posting task to the task queue from itself.
// std::unique_ptr destructor does the same two operations in reverse order as
// it doesn't expect member would be used after its destruction has started.
incoming_render_queue_.get_deleter()(incoming_render_queue_.get());
incoming_render_queue_.release();
}
void IncomingVideoStream::OnFrame(const VideoFrame& video_frame) {
TRACE_EVENT0("webrtc", "IncomingVideoStream::OnFrame");
RTC_CHECK_RUNS_SERIALIZED(&decoder_race_checker_);
RTC_DCHECK(!incoming_render_queue_->IsCurrent());
// TODO(srte): Using video_frame = std::move(video_frame) would move the frame
// into the lambda instead of copying it, but it doesn't work unless we change
// OnFrame to take its frame argument by value instead of const reference.
incoming_render_queue_->PostTask([this, video_frame = video_frame]() mutable {
RTC_DCHECK_RUN_ON(incoming_render_queue_.get());
if (render_buffers_.AddFrame(std::move(video_frame)) == 1)
Dequeue();
});
}
void IncomingVideoStream::Dequeue() {
TRACE_EVENT0("webrtc", "IncomingVideoStream::Dequeue");
RTC_DCHECK_RUN_ON(incoming_render_queue_.get());
absl::optional<VideoFrame> frame_to_render = render_buffers_.FrameToRender();
if (frame_to_render)
callback_->OnFrame(*frame_to_render);
if (render_buffers_.HasPendingFrames()) {
uint32_t wait_time = render_buffers_.TimeToNextFrameRelease();
incoming_render_queue_->PostDelayedHighPrecisionTask(
[this]() { Dequeue(); }, TimeDelta::Millis(wait_time));
}
}
} // namespace webrtc

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef VIDEO_RENDER_INCOMING_VIDEO_STREAM_H_
#define VIDEO_RENDER_INCOMING_VIDEO_STREAM_H_
#include <stdint.h>
#include <memory>
#include "api/sequence_checker.h"
#include "api/task_queue/task_queue_base.h"
#include "api/task_queue/task_queue_factory.h"
#include "api/video/video_frame.h"
#include "api/video/video_sink_interface.h"
#include "rtc_base/race_checker.h"
#include "rtc_base/thread_annotations.h"
#include "video/render/video_render_frames.h"
namespace webrtc {
class IncomingVideoStream : public rtc::VideoSinkInterface<VideoFrame> {
public:
IncomingVideoStream(TaskQueueFactory* task_queue_factory,
int32_t delay_ms,
rtc::VideoSinkInterface<VideoFrame>* callback);
~IncomingVideoStream() override;
private:
void OnFrame(const VideoFrame& video_frame) override;
void Dequeue();
SequenceChecker main_thread_checker_;
rtc::RaceChecker decoder_race_checker_;
VideoRenderFrames render_buffers_ RTC_GUARDED_BY(incoming_render_queue_);
rtc::VideoSinkInterface<VideoFrame>* const callback_;
std::unique_ptr<TaskQueueBase, TaskQueueDeleter> incoming_render_queue_;
};
} // namespace webrtc
#endif // VIDEO_RENDER_INCOMING_VIDEO_STREAM_H_

View file

@ -0,0 +1,116 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "video/render/video_render_frames.h"
#include <type_traits>
#include <utility>
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/time_utils.h"
#include "system_wrappers/include/metrics.h"
namespace webrtc {
namespace {
// Don't render frames with timestamp older than 500ms from now.
const int kOldRenderTimestampMS = 500;
// Don't render frames with timestamp more than 10s into the future.
const int kFutureRenderTimestampMS = 10000;
const uint32_t kEventMaxWaitTimeMs = 200;
const uint32_t kMinRenderDelayMs = 10;
const uint32_t kMaxRenderDelayMs = 500;
const size_t kMaxIncomingFramesBeforeLogged = 100;
uint32_t EnsureValidRenderDelay(uint32_t render_delay) {
return (render_delay < kMinRenderDelayMs || render_delay > kMaxRenderDelayMs)
? kMinRenderDelayMs
: render_delay;
}
} // namespace
VideoRenderFrames::VideoRenderFrames(uint32_t render_delay_ms)
: render_delay_ms_(EnsureValidRenderDelay(render_delay_ms)) {}
VideoRenderFrames::~VideoRenderFrames() {
frames_dropped_ += incoming_frames_.size();
RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.RenderQueue",
frames_dropped_);
RTC_LOG(LS_INFO) << "WebRTC.Video.DroppedFrames.RenderQueue "
<< frames_dropped_;
}
int32_t VideoRenderFrames::AddFrame(VideoFrame&& new_frame) {
const int64_t time_now = rtc::TimeMillis();
// Drop old frames only when there are other frames in the queue, otherwise, a
// really slow system never renders any frames.
if (!incoming_frames_.empty() &&
new_frame.render_time_ms() + kOldRenderTimestampMS < time_now) {
RTC_LOG(LS_WARNING) << "Too old frame, timestamp=" << new_frame.timestamp();
++frames_dropped_;
return -1;
}
if (new_frame.render_time_ms() > time_now + kFutureRenderTimestampMS) {
RTC_LOG(LS_WARNING) << "Frame too long into the future, timestamp="
<< new_frame.timestamp();
++frames_dropped_;
return -1;
}
if (new_frame.render_time_ms() < last_render_time_ms_) {
RTC_LOG(LS_WARNING) << "Frame scheduled out of order, render_time="
<< new_frame.render_time_ms()
<< ", latest=" << last_render_time_ms_;
// For more details, see bug:
// https://bugs.chromium.org/p/webrtc/issues/detail?id=7253
++frames_dropped_;
return -1;
}
last_render_time_ms_ = new_frame.render_time_ms();
incoming_frames_.emplace_back(std::move(new_frame));
if (incoming_frames_.size() > kMaxIncomingFramesBeforeLogged) {
RTC_LOG(LS_WARNING) << "Stored incoming frames: "
<< incoming_frames_.size();
}
return static_cast<int32_t>(incoming_frames_.size());
}
absl::optional<VideoFrame> VideoRenderFrames::FrameToRender() {
absl::optional<VideoFrame> render_frame;
// Get the newest frame that can be released for rendering.
while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) {
if (render_frame) {
++frames_dropped_;
}
render_frame = std::move(incoming_frames_.front());
incoming_frames_.pop_front();
}
return render_frame;
}
uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
if (incoming_frames_.empty()) {
return kEventMaxWaitTimeMs;
}
const int64_t time_to_release = incoming_frames_.front().render_time_ms() -
render_delay_ms_ - rtc::TimeMillis();
return time_to_release < 0 ? 0u : static_cast<uint32_t>(time_to_release);
}
bool VideoRenderFrames::HasPendingFrames() const {
return !incoming_frames_.empty();
}
} // namespace webrtc

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef VIDEO_RENDER_VIDEO_RENDER_FRAMES_H_
#define VIDEO_RENDER_VIDEO_RENDER_FRAMES_H_
#include <stddef.h>
#include <stdint.h>
#include <list>
#include "absl/types/optional.h"
#include "api/video/video_frame.h"
namespace webrtc {
// Class definitions
class VideoRenderFrames {
public:
explicit VideoRenderFrames(uint32_t render_delay_ms);
VideoRenderFrames(const VideoRenderFrames&) = delete;
~VideoRenderFrames();
// Add a frame to the render queue
int32_t AddFrame(VideoFrame&& new_frame);
// Get a frame for rendering, or false if it's not time to render.
absl::optional<VideoFrame> FrameToRender();
// Returns the number of ms to next frame to render
uint32_t TimeToNextFrameRelease();
bool HasPendingFrames() const;
private:
// Sorted list with framed to be rendered, oldest first.
std::list<VideoFrame> incoming_frames_;
// Estimated delay from a frame is released until it's rendered.
const uint32_t render_delay_ms_;
int64_t last_render_time_ms_ = 0;
size_t frames_dropped_ = 0;
};
} // namespace webrtc
#endif // VIDEO_RENDER_VIDEO_RENDER_FRAMES_H_