Android-cuttlefish cvd tool
video_track_source_impl.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <media/base/video_broadcaster.h>
20#include <pc/video_track_source.h>
21
23
24namespace cuttlefish {
25namespace webrtc_streaming {
26
27// Video source implementation that bridges Cuttlefish display frames to WebRTC.
28//
29// Receives video frames from the display compositor (via VideoSink interface)
30// and broadcasts them to WebRTC video tracks. Handles both YUV (I420) and
31// native RGBA frame formats.
32//
33// Key features:
34// - Supports native ARGB/ABGR buffers for hardware encoder passthrough
35// - Enforces strictly monotonic timestamps to prevent frame drops
36// - Broadcasts frames to all registered WebRTC sinks
37//
38// Thread safety: OnFrame() may be called from any thread after construction.
39class VideoTrackSourceImpl : public webrtc::VideoTrackSource {
40 public:
41 VideoTrackSourceImpl(int width, int height);
42
43 void OnFrame(std::shared_ptr<VideoFrameBuffer> frame, int64_t timestamp_us);
44
45 // Returns false if no stats are available, e.g, for a remote source, or a
46 // source which has not seen its first frame yet.
47 //
48 // Implementation should avoid blocking.
49 bool GetStats(Stats* stats) override;
50
51 bool SupportsEncodedOutput() const override;
52 void GenerateKeyFrame() override {}
54 rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
56 rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
57
58 rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override;
59
60 private:
61 int width_;
63 int64_t last_timestamp_us_ = 0; // Ensure strictly monotonic timestamps
64 rtc::VideoBroadcaster broadcaster_;
65};
66
67// Wraps a VideoTrackSourceImpl as an implementation of the VideoSink interface.
68// This is needed as the VideoTrackSourceImpl is a reference counted object that
69// should only be referenced by rtc::scoped_refptr pointers, but the
70// VideoSink interface is not a reference counted object and therefore not
71// compatible with that kind of pointers. This class can be referenced by a
72// shared pointer and it in turn holds a scoped_refptr to the wrapped object.
74 public:
76
77 VideoTrackSourceImplSinkWrapper(rtc::scoped_refptr<VideoTrackSourceImpl> obj)
78 : track_source_impl_(obj) {}
79
80 void OnFrame(std::shared_ptr<VideoFrameBuffer> frame,
81 int64_t timestamp_us) override {
82 track_source_impl_->OnFrame(frame, timestamp_us);
83 }
84
85 private:
86 rtc::scoped_refptr<VideoTrackSourceImpl> track_source_impl_;
87};
88
89} // namespace webrtc_streaming
90} // namespace cuttlefish
Definition: video_sink.h:26
rtc::scoped_refptr< VideoTrackSourceImpl > track_source_impl_
Definition: video_track_source_impl.h:86
void OnFrame(std::shared_ptr< VideoFrameBuffer > frame, int64_t timestamp_us) override
Definition: video_track_source_impl.h:80
VideoTrackSourceImplSinkWrapper(rtc::scoped_refptr< VideoTrackSourceImpl > obj)
Definition: video_track_source_impl.h:77
Definition: video_track_source_impl.h:39
void GenerateKeyFrame() override
Definition: video_track_source_impl.h:52
void OnFrame(std::shared_ptr< VideoFrameBuffer > frame, int64_t timestamp_us)
Definition: video_track_source_impl.cpp:55
VideoTrackSourceImpl(int width, int height)
Definition: video_track_source_impl.cpp:52
int width_
Definition: video_track_source_impl.h:61
void RemoveEncodedSink(rtc::VideoSinkInterface< webrtc::RecordableEncodedFrame > *sink) override
Definition: video_track_source_impl.h:55
rtc::VideoSourceInterface< webrtc::VideoFrame > * source() override
Definition: video_track_source_impl.cpp:100
void AddEncodedSink(rtc::VideoSinkInterface< webrtc::RecordableEncodedFrame > *sink) override
Definition: video_track_source_impl.h:53
rtc::VideoBroadcaster broadcaster_
Definition: video_track_source_impl.h:64
bool GetStats(Stats *stats) override
Definition: video_track_source_impl.cpp:93
int height_
Definition: video_track_source_impl.h:62
int64_t last_timestamp_us_
Definition: video_track_source_impl.h:63
bool SupportsEncodedOutput() const override
Definition: video_track_source_impl.cpp:99
Definition: alloc_driver.h:20