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 
22 #include "host/frontend/webrtc/lib/video_sink.h"
23 
24 namespace cuttlefish {
25 namespace webrtc_streaming {
26 
27 class VideoTrackSourceImpl : public webrtc::VideoTrackSource {
28  public:
29   VideoTrackSourceImpl(int width, int height);
30 
31   void OnFrame(std::shared_ptr<VideoFrameBuffer> frame, int64_t timestamp_us);
32 
33   // Returns false if no stats are available, e.g, for a remote source, or a
34   // source which has not seen its first frame yet.
35   //
36   // Implementation should avoid blocking.
37   bool GetStats(Stats* stats) override;
38 
39   bool SupportsEncodedOutput() const override;
GenerateKeyFrame()40   void GenerateKeyFrame() override {}
AddEncodedSink(rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> * sink)41   void AddEncodedSink(
42       rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
RemoveEncodedSink(rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> * sink)43   void RemoveEncodedSink(
44       rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
45 
46  protected:
47   rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override;
48 
49  private:
50   int width_;
51   int height_;
52   rtc::VideoBroadcaster broadcaster_;
53 };
54 
55 // Wraps a VideoTrackSourceImpl as an implementation of the VideoSink interface.
56 // This is needed as the VideoTrackSourceImpl is a reference counted object that
57 // should only be referenced by rtc::scoped_refptr pointers, but the
58 // VideoSink interface is not a reference counted object and therefore not
59 // compatible with that kind of pointers. This class can be referenced by a
60 // shared pointer and it in turn holds a scoped_refptr to the wrapped object.
61 class VideoTrackSourceImplSinkWrapper : public VideoSink {
62  public:
63   virtual ~VideoTrackSourceImplSinkWrapper() = default;
64 
VideoTrackSourceImplSinkWrapper(rtc::scoped_refptr<VideoTrackSourceImpl> obj)65   VideoTrackSourceImplSinkWrapper(rtc::scoped_refptr<VideoTrackSourceImpl> obj)
66       : track_source_impl_(obj) {}
67 
OnFrame(std::shared_ptr<VideoFrameBuffer> frame,int64_t timestamp_us)68   void OnFrame(std::shared_ptr<VideoFrameBuffer> frame,
69                int64_t timestamp_us) override {
70     track_source_impl_->OnFrame(frame, timestamp_us);
71   }
72 
73  private:
74   rtc::scoped_refptr<VideoTrackSourceImpl> track_source_impl_;
75 };
76 
77 }  // namespace webrtc_streaming
78 }  // namespace cuttlefish
79