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"));
66 utility::LogWarning(
"Ignore roi_x={}, it must be >=0", roi_x_);
70 if (opts.find(
"roi_y") != opts.end()) {
71 roi_y_ = std::stoi(opts.at(
"roi_y"));
73 utility::LogWarning(
"Ignore roi_y_={}, it must be >=0", roi_y_);
77 if (opts.find(
"roi_width") != opts.end()) {
78 roi_width_ = std::stoi(opts.at(
"roi_width"));
79 if (roi_width_ <= 0) {
80 utility::LogWarning(
"Ignore roi_width={}, it must be >0",
85 if (opts.find(
"roi_height") != opts.end()) {
86 roi_height_ = std::stoi(opts.at(
"roi_height"));
87 if (roi_height_ <= 0) {
88 utility::LogWarning(
"Ignore roi_height={}, it must be >0",
97 void OnFrame(
const webrtc::VideoFrame &frame)
override {
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);
145 width = frame.width();
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);