40 #include <api/media_stream_interface.h> 41 #include <media/base/video_broadcaster.h> 46 namespace visualization {
47 namespace webrtc_server {
49 class VideoScaler :
public rtc::VideoSinkInterface<webrtc::VideoFrame>,
50 public rtc::VideoSourceInterface<webrtc::VideoFrame> {
52 VideoScaler(rtc::scoped_refptr<BitmapTrackSourceInterface> video_source,
53 const std::map<std::string, std::string> &opts)
54 : video_source_(video_source),
57 rotation_(webrtc::kVideoRotation_0),
62 if (opts.find(
"width") != opts.end()) {
63 width_ = std::stoi(opts.at(
"width"));
65 if (opts.find(
"height") != opts.end()) {
66 height_ = std::stoi(opts.at(
"height"));
68 if (opts.find(
"rotation") != opts.end()) {
69 int rotation = std::stoi(opts.at(
"rotation"));
72 rotation_ = webrtc::kVideoRotation_90;
75 rotation_ = webrtc::kVideoRotation_180;
78 rotation_ = webrtc::kVideoRotation_270;
82 if (opts.find(
"roi_x") != opts.end()) {
83 roi_x_ = std::stoi(opts.at(
"roi_x"));
89 if (opts.find(
"roi_y") != opts.end()) {
90 roi_y_ = std::stoi(opts.at(
"roi_y"));
96 if (opts.find(
"roi_width") != opts.end()) {
97 roi_width_ = std::stoi(opts.at(
"roi_width"));
98 if (roi_width_ <= 0) {
104 if (opts.find(
"roi_height") != opts.end()) {
105 roi_height_ = std::stoi(opts.at(
"roi_height"));
106 if (roi_height_ <= 0) {
117 if (roi_x_ >= frame.width()) {
119 "The ROI position protrudes beyond the right edge of the " 120 "image. Ignore roi_x.");
123 if (roi_y_ >= frame.height()) {
125 "The ROI position protrudes beyond the right edge of the " 126 "image. Ignore roi_y_.");
129 if (roi_width_ != 0 && (roi_width_ + roi_x_) > frame.width()) {
131 "The ROI position protrudes beyond the right edge of the " 132 "image. Ignore roi_width_.");
135 if (roi_height_ != 0 && (roi_height_ + roi_y_) > frame.height()) {
137 "The ROI position protrudes beyond the right edge of the " 138 "image. Ignore roi_height_.");
142 if (roi_width_ == 0) {
143 roi_width_ = frame.width() - roi_x_;
145 if (roi_height_ == 0) {
146 roi_height_ = frame.height() - roi_y_;
150 if ((roi_width_ != frame.width() || roi_height_ != frame.height()) &&
151 (height_ == 0 && width_ == 0)) {
152 height_ = roi_height_;
156 if ((height_ == 0) && (width_ == 0) &&
157 (rotation_ == webrtc::kVideoRotation_0)) {
158 broadcaster_.OnFrame(frame);
162 if ((height == 0) && (width == 0)) {
163 height = frame.height();
164 width = frame.width();
165 }
else if (height == 0) {
166 height = (roi_height_ *
width) / roi_width_;
167 }
else if (width == 0) {
168 width = (roi_width_ *
height) / roi_height_;
170 rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer =
171 webrtc::I420Buffer::Create(width, height);
172 if (roi_width_ != frame.width() || roi_height_ != frame.height()) {
173 scaled_buffer->CropAndScaleFrom(
174 *frame.video_frame_buffer()->ToI420(), roi_x_, roi_y_,
175 roi_width_, roi_height_);
177 scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420());
179 webrtc::VideoFrame scaledFrame =
180 webrtc::VideoFrame(scaled_buffer, frame.timestamp(),
181 frame.render_time_ms(), rotation_);
183 broadcaster_.OnFrame(scaledFrame);
188 const rtc::VideoSinkWants &wants)
override {
189 video_source_->AddOrUpdateSink(
this, wants);
191 broadcaster_.AddOrUpdateSink(sink, wants);
195 rtc::VideoSinkInterface<webrtc::VideoFrame> *sink)
override {
196 video_source_->RemoveSink(
this);
198 broadcaster_.RemoveSink(sink);
205 rtc::scoped_refptr<BitmapTrackSourceInterface> video_source_;
206 rtc::VideoBroadcaster broadcaster_;
210 webrtc::VideoRotation rotation_;
void RemoveSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink) override
Definition: VideoScaler.h:194
Definition: VideoScaler.h:49
#define LogWarning(...)
Definition: Console.h:95
int height()
Definition: VideoScaler.h:202
void OnFrame(const webrtc::VideoFrame &frame) override
Definition: VideoScaler.h:116
virtual ~VideoScaler()
Definition: VideoScaler.h:114
void AddOrUpdateSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink, const rtc::VideoSinkWants &wants) override
Definition: VideoScaler.h:187
Definition: PinholeCameraIntrinsic.cpp:35
Rect frame
Definition: BitmapWindowSystem.cpp:48
VideoScaler(rtc::scoped_refptr< BitmapTrackSourceInterface > video_source, const std::map< std::string, std::string > &opts)
Definition: VideoScaler.h:52
int width()
Definition: VideoScaler.h:201