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/libdevice/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   rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override;
47 
48  private:
49   int width_;
50   int height_;
51   rtc::VideoBroadcaster broadcaster_;
52 };
53 
54 // Wraps a VideoTrackSourceImpl as an implementation of the VideoSink interface.
55 // This is needed as the VideoTrackSourceImpl is a reference counted object that
56 // should only be referenced by rtc::scoped_refptr pointers, but the
57 // VideoSink interface is not a reference counted object and therefore not
58 // compatible with that kind of pointers. This class can be referenced by a
59 // shared pointer and it in turn holds a scoped_refptr to the wrapped object.
60 class VideoTrackSourceImplSinkWrapper : public VideoSink {
61  public:
62   virtual ~VideoTrackSourceImplSinkWrapper() = default;
63 
VideoTrackSourceImplSinkWrapper(rtc::scoped_refptr<VideoTrackSourceImpl> obj)64   VideoTrackSourceImplSinkWrapper(rtc::scoped_refptr<VideoTrackSourceImpl> obj)
65       : track_source_impl_(obj) {}
66 
OnFrame(std::shared_ptr<VideoFrameBuffer> frame,int64_t timestamp_us)67   void OnFrame(std::shared_ptr<VideoFrameBuffer> frame,
68                int64_t timestamp_us) override {
69     track_source_impl_->OnFrame(frame, timestamp_us);
70   }
71 
72  private:
73   rtc::scoped_refptr<VideoTrackSourceImpl> track_source_impl_;
74 };
75 
76 }  // namespace webrtc_streaming
77 }  // namespace cuttlefish
78