Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ImageCapturer.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 //
27 // This is a private header. It shall be hidden from Open3D's public API. Do not
28 // put this in Open3D.h.in.
29 
30 #pragma once
31 
32 #include <api/video/i420_buffer.h>
33 #include <libyuv/convert.h>
34 #include <libyuv/video_common.h>
35 #include <media/base/video_broadcaster.h>
36 #include <media/base/video_common.h>
37 
38 #include <memory>
39 
40 #include "open3d/core/Tensor.h"
41 #include "open3d/utility/Console.h"
43 
44 namespace open3d {
45 namespace visualization {
46 namespace webrtc_server {
47 
48 class ImageCapturer : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
49 public:
50  ImageCapturer(const std::string& url_,
51  const std::map<std::string, std::string>& opts);
52  virtual ~ImageCapturer();
53 
54  static ImageCapturer* Create(
55  const std::string& url,
56  const std::map<std::string, std::string>& opts);
57 
58  ImageCapturer(const std::map<std::string, std::string>& opts);
59 
60  virtual void AddOrUpdateSink(
61  rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
62  const rtc::VideoSinkWants& wants) override;
63 
64  virtual void RemoveSink(
65  rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
66 
67  void OnCaptureResult(const std::shared_ptr<core::Tensor>& frame);
68 
69 protected:
70  int width_;
71  int height_;
72  rtc::VideoBroadcaster broadcaster_;
73 };
74 
76 public:
77  static rtc::scoped_refptr<BitmapTrackSourceInterface> Create(
78  const std::string& window_uid,
79  const std::map<std::string, std::string>& opts) {
80  std::unique_ptr<ImageCapturer> capturer =
81  absl::WrapUnique(ImageCapturer::Create(window_uid, opts));
82  if (!capturer) {
83  return nullptr;
84  }
85  rtc::scoped_refptr<BitmapTrackSourceInterface> video_source =
86  new rtc::RefCountedObject<ImageTrackSource>(
87  std::move(capturer));
88  return video_source;
89  }
90 
91  void OnFrame(const std::shared_ptr<core::Tensor>& frame) final override {
92  capturer_->OnCaptureResult(frame);
93  }
94 
95 protected:
96  explicit ImageTrackSource(std::unique_ptr<ImageCapturer> capturer)
97  : BitmapTrackSource(/*remote=*/false), capturer_(std::move(capturer)) {}
98 
99 private:
100  rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override {
101  return capturer_.get();
102  }
103  std::unique_ptr<ImageCapturer> capturer_;
104 };
105 
106 } // namespace webrtc_server
107 } // namespace visualization
108 } // namespace open3d
ImageTrackSource(std::unique_ptr< ImageCapturer > capturer)
Definition: ImageCapturer.h:96
int height_
Definition: ImageCapturer.h:71
virtual ~ImageCapturer()
Definition: ImageCapturer.cpp:48
Definition: Optional.h:912
int width_
Definition: ImageCapturer.h:70
virtual void AddOrUpdateSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink, const rtc::VideoSinkWants &wants) override
Definition: ImageCapturer.cpp:116
rtc::VideoBroadcaster broadcaster_
Definition: ImageCapturer.h:72
ImageCapturer(const std::string &url_, const std::map< std::string, std::string > &opts)
Definition: ImageCapturer.cpp:44
void OnCaptureResult(const std::shared_ptr< core::Tensor > &frame)
Definition: ImageCapturer.cpp:67
void OnFrame(const std::shared_ptr< core::Tensor > &frame) final override
Definition: ImageCapturer.h:91
virtual void RemoveSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink) override
Definition: ImageCapturer.cpp:122
Definition: PinholeCameraIntrinsic.cpp:35
Rect frame
Definition: BitmapWindowSystem.cpp:48
static rtc::scoped_refptr< BitmapTrackSourceInterface > Create(const std::string &window_uid, const std::map< std::string, std::string > &opts)
Definition: ImageCapturer.h:77
static ImageCapturer * Create(const std::string &url, const std::map< std::string, std::string > &opts)
Definition: ImageCapturer.cpp:50