34 VideoScaler(webrtc::scoped_refptr<BitmapTrackSourceInterface> video_source,
35 const std::map<std::string, std::string> &opts)
36 : video_source_(video_source),
39 rotation_(
webrtc::kVideoRotation_0),
44 if (opts.find(
"width") != opts.end()) {
45 width_ = std::stoi(opts.at(
"width"));
47 if (opts.find(
"height") != opts.end()) {
48 height_ = std::stoi(opts.at(
"height"));
50 if (opts.find(
"rotation") != opts.end()) {
51 int rotation = std::stoi(opts.at(
"rotation"));
54 rotation_ = webrtc::kVideoRotation_90;
57 rotation_ = webrtc::kVideoRotation_180;
60 rotation_ = webrtc::kVideoRotation_270;
64 if (opts.find(
"roi_x") != opts.end()) {
65 roi_x_ = std::stoi(opts.at(
"roi_x"));
67 utility::LogWarning(
"Ignore roi_x={}, it must be >=0", roi_x_);
71 if (opts.find(
"roi_y") != opts.end()) {
72 roi_y_ = std::stoi(opts.at(
"roi_y"));
74 utility::LogWarning(
"Ignore roi_y_={}, it must be >=0", roi_y_);
78 if (opts.find(
"roi_width") != opts.end()) {
79 roi_width_ = std::stoi(opts.at(
"roi_width"));
80 if (roi_width_ <= 0) {
81 utility::LogWarning(
"Ignore roi_width={}, it must be >0",
86 if (opts.find(
"roi_height") != opts.end()) {
87 roi_height_ = std::stoi(opts.at(
"roi_height"));
88 if (roi_height_ <= 0) {
89 utility::LogWarning(
"Ignore roi_height={}, it must be >0",
99 if (roi_x_ >=
frame.width()) {
101 "The ROI position protrudes beyond the right edge of the "
102 "image. Ignore roi_x.");
105 if (roi_y_ >=
frame.height()) {
107 "The ROI position protrudes beyond the right edge of the "
108 "image. Ignore roi_y_.");
111 if (roi_width_ != 0 && (roi_width_ + roi_x_) >
frame.width()) {
113 "The ROI position protrudes beyond the right edge of the "
114 "image. Ignore roi_width_.");
117 if (roi_height_ != 0 && (roi_height_ + roi_y_) >
frame.height()) {
119 "The ROI position protrudes beyond the right edge of the "
120 "image. Ignore roi_height_.");
124 if (roi_width_ == 0) {
125 roi_width_ =
frame.width() - roi_x_;
127 if (roi_height_ == 0) {
128 roi_height_ =
frame.height() - roi_y_;
132 if ((roi_width_ !=
frame.width() || roi_height_ !=
frame.height()) &&
133 (height_ == 0 && width_ == 0)) {
134 height_ = roi_height_;
138 if ((height_ == 0) && (width_ == 0) &&
139 (rotation_ == webrtc::kVideoRotation_0)) {
140 broadcaster_.OnFrame(
frame);
149 }
else if (
width == 0) {
152 webrtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer =
154 if (roi_width_ !=
frame.width() || roi_height_ !=
frame.height()) {
155 scaled_buffer->CropAndScaleFrom(
156 *
frame.video_frame_buffer()->ToI420(), roi_x_, roi_y_,
157 roi_width_, roi_height_);
159 scaled_buffer->ScaleFrom(*
frame.video_frame_buffer()->ToI420());
161 webrtc::VideoFrame scaledFrame =
162 webrtc::VideoFrame(scaled_buffer,
frame.timestamp_us(),
163 frame.render_time_ms(), rotation_);
165 broadcaster_.OnFrame(scaledFrame);