21 #include <api/media_stream_interface.h>
22 #include <media/base/video_broadcaster.h>
27 namespace visualization {
28 namespace webrtc_server {
30 class VideoScaler :
public rtc::VideoSinkInterface<webrtc::VideoFrame>,
31 public rtc::VideoSourceInterface<webrtc::VideoFrame> {
33 VideoScaler(rtc::scoped_refptr<BitmapTrackSourceInterface> video_source,
34 const std::map<std::string, std::string> &opts)
35 : video_source_(video_source),
38 rotation_(webrtc::kVideoRotation_0),
43 if (opts.find(
"width") != opts.end()) {
44 width_ = std::stoi(opts.at(
"width"));
46 if (opts.find(
"height") != opts.end()) {
47 height_ = std::stoi(opts.at(
"height"));
49 if (opts.find(
"rotation") != opts.end()) {
50 int rotation = std::stoi(opts.at(
"rotation"));
53 rotation_ = webrtc::kVideoRotation_90;
56 rotation_ = webrtc::kVideoRotation_180;
59 rotation_ = webrtc::kVideoRotation_270;
63 if (opts.find(
"roi_x") != opts.end()) {
64 roi_x_ = std::stoi(opts.at(
"roi_x"));
70 if (opts.find(
"roi_y") != opts.end()) {
71 roi_y_ = std::stoi(opts.at(
"roi_y"));
77 if (opts.find(
"roi_width") != opts.end()) {
78 roi_width_ = std::stoi(opts.at(
"roi_width"));
79 if (roi_width_ <= 0) {
85 if (opts.find(
"roi_height") != opts.end()) {
86 roi_height_ = std::stoi(opts.at(
"roi_height"));
87 if (roi_height_ <= 0) {
98 if (roi_x_ >=
frame.width()) {
100 "The ROI position protrudes beyond the right edge of the "
101 "image. Ignore roi_x.");
104 if (roi_y_ >=
frame.height()) {
106 "The ROI position protrudes beyond the right edge of the "
107 "image. Ignore roi_y_.");
110 if (roi_width_ != 0 && (roi_width_ + roi_x_) >
frame.width()) {
112 "The ROI position protrudes beyond the right edge of the "
113 "image. Ignore roi_width_.");
116 if (roi_height_ != 0 && (roi_height_ + roi_y_) >
frame.height()) {
118 "The ROI position protrudes beyond the right edge of the "
119 "image. Ignore roi_height_.");
123 if (roi_width_ == 0) {
124 roi_width_ =
frame.width() - roi_x_;
126 if (roi_height_ == 0) {
127 roi_height_ =
frame.height() - roi_y_;
131 if ((roi_width_ !=
frame.width() || roi_height_ !=
frame.height()) &&
132 (height_ == 0 && width_ == 0)) {
133 height_ = roi_height_;
137 if ((height_ == 0) && (width_ == 0) &&
138 (rotation_ == webrtc::kVideoRotation_0)) {
139 broadcaster_.OnFrame(
frame);
148 }
else if (
width == 0) {
151 rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer =
153 if (roi_width_ !=
frame.width() || roi_height_ !=
frame.height()) {
154 scaled_buffer->CropAndScaleFrom(
155 *
frame.video_frame_buffer()->ToI420(), roi_x_, roi_y_,
156 roi_width_, roi_height_);
158 scaled_buffer->ScaleFrom(*
frame.video_frame_buffer()->ToI420());
160 webrtc::VideoFrame scaledFrame =
161 webrtc::VideoFrame(scaled_buffer,
frame.timestamp(),
162 frame.render_time_ms(), rotation_);
164 broadcaster_.OnFrame(scaledFrame);
169 const rtc::VideoSinkWants &wants)
override {
170 video_source_->AddOrUpdateSink(
this, wants);
172 broadcaster_.AddOrUpdateSink(sink, wants);
176 rtc::VideoSinkInterface<webrtc::VideoFrame> *sink)
override {
177 video_source_->RemoveSink(
this);
179 broadcaster_.RemoveSink(sink);
186 rtc::scoped_refptr<BitmapTrackSourceInterface> video_source_;
187 rtc::VideoBroadcaster broadcaster_;
191 webrtc::VideoRotation rotation_;
Rect frame
Definition: BitmapWindowSystem.cpp:30
#define LogWarning(...)
Definition: Logging.h:60
Definition: VideoScaler.h:31
void RemoveSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink) override
Definition: VideoScaler.h:175
void OnFrame(const webrtc::VideoFrame &frame) override
Definition: VideoScaler.h:97
virtual ~VideoScaler()
Definition: VideoScaler.h:95
int width()
Definition: VideoScaler.h:182
void AddOrUpdateSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink, const rtc::VideoSinkWants &wants) override
Definition: VideoScaler.h:168
VideoScaler(rtc::scoped_refptr< BitmapTrackSourceInterface > video_source, const std::map< std::string, std::string > &opts)
Definition: VideoScaler.h:33
int height()
Definition: VideoScaler.h:183
Definition: PinholeCameraIntrinsic.cpp:16