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 #include "host/frontend/webrtc/lib/video_track_source_impl.h"
18 
19 #include <api/video/video_frame_buffer.h>
20 
21 namespace cuttlefish {
22 namespace webrtc_streaming {
23 
24 namespace {
25 
26 class VideoFrameWrapper : public webrtc::I420BufferInterface {
27  public:
VideoFrameWrapper(std::shared_ptr<::cuttlefish::webrtc_streaming::VideoFrameBuffer> frame_buffer)28   VideoFrameWrapper(
29       std::shared_ptr<::cuttlefish::webrtc_streaming::VideoFrameBuffer>
30           frame_buffer)
31       : frame_buffer_(frame_buffer) {}
32   ~VideoFrameWrapper() override = default;
33   // From VideoFrameBuffer
width() const34   int width() const override { return frame_buffer_->width(); }
height() const35   int height() const override { return frame_buffer_->height(); }
36 
37   // From class PlanarYuvBuffer
StrideY() const38   int StrideY() const override { return frame_buffer_->StrideY(); }
StrideU() const39   int StrideU() const override { return frame_buffer_->StrideU(); }
StrideV() const40   int StrideV() const override { return frame_buffer_->StrideV(); }
41 
42   // From class PlanarYuv8Buffer
DataY() const43   const uint8_t *DataY() const override { return frame_buffer_->DataY(); }
DataU() const44   const uint8_t *DataU() const override { return frame_buffer_->DataU(); }
DataV() const45   const uint8_t *DataV() const override { return frame_buffer_->DataV(); }
46 
47  private:
48   std::shared_ptr<::cuttlefish::webrtc_streaming::VideoFrameBuffer>
49       frame_buffer_;
50 };
51 
52 }  // namespace
53 
VideoTrackSourceImpl(int width,int height)54 VideoTrackSourceImpl::VideoTrackSourceImpl(int width, int height)
55     : webrtc::VideoTrackSource(false), width_(width), height_(height) {}
56 
OnFrame(std::shared_ptr<VideoFrameBuffer> frame,int64_t timestamp_us)57 void VideoTrackSourceImpl::OnFrame(std::shared_ptr<VideoFrameBuffer> frame,
58                                    int64_t timestamp_us) {
59   auto video_frame =
60       webrtc::VideoFrame::Builder()
61           .set_video_frame_buffer(
62               new rtc::RefCountedObject<VideoFrameWrapper>(frame))
63           .set_timestamp_us(timestamp_us)
64           .build();
65   broadcaster_.OnFrame(video_frame);
66 }
67 
GetStats(Stats * stats)68 bool VideoTrackSourceImpl::GetStats(Stats *stats) {
69   stats->input_height = height_;
70   stats->input_width = width_;
71   return true;
72 }
73 
SupportsEncodedOutput() const74 bool VideoTrackSourceImpl::SupportsEncodedOutput() const { return false; }
source()75 rtc::VideoSourceInterface<webrtc::VideoFrame> *VideoTrackSourceImpl::source() {
76   return &broadcaster_;
77 }
78 
79 }  // namespace webrtc_streaming
80 }  // namespace cuttlefish
81