You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

544 lines
20 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // libjingle
  2. // Copyright 2010 Google Inc.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. // 2. Redistributions in binary form must reproduce the above copyright notice,
  10. // this list of conditions and the following disclaimer in the documentation
  11. // and/or other materials provided with the distribution.
  12. // 3. The name of the author may not be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  18. // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #include "talk/media/base/videoadapter.h"
  26. #include <limits.h>
  27. #include "talk/media/base/constants.h"
  28. #include "talk/base/logging.h"
  29. #include "talk/base/timeutils.h"
  30. #include "talk/media/base/videoframe.h"
  31. namespace cricket {
  32. // TODO(fbarchard): Make downgrades settable
  33. static const int kMaxCpuDowngrades = 2; // Downgrade at most 2 times for CPU.
  34. static const int kDefaultDowngradeWaitTimeMs = 2000;
  35. // TODO(fbarchard): Consider making scale factor table settable, to allow
  36. // application to select quality vs performance tradeoff.
  37. // TODO(fbarchard): Add framerate scaling to tables for 1/2 framerate.
  38. // List of scale factors that adapter will scale by.
  39. #if defined(IOS) || defined(ANDROID)
  40. // Mobile needs 1/4 scale for VGA (640 x 360) to QQVGA (160 x 90)
  41. // or 1/4 scale for HVGA (480 x 270) to QQHVGA (120 x 67)
  42. static const int kMinNumPixels = 120 * 67;
  43. static float kScaleFactors[] = {
  44. 1.f, // full size
  45. 3.f / 4.f, // 3/4 scale
  46. 1.f / 2.f, // 1/2 scale
  47. 3.f / 8.f, // 3/8 scale
  48. 1.f / 4.f, // 1/4 scale
  49. };
  50. #else
  51. // PC needs 1/8 scale for HD (1280 x 720) to QQVGA (160 x 90)
  52. static const int kMinNumPixels = 160 * 100;
  53. static float kScaleFactors[] = {
  54. 1.f, // full size
  55. 3.f / 4.f, // 3/4 scale
  56. 1.f / 2.f, // 1/2 scale
  57. 3.f / 8.f, // 3/8 scale
  58. 1.f / 4.f, // 1/4 scale
  59. 3.f / 16.f, // 3/16 scale
  60. 1.f / 8.f // 1/8 scale
  61. };
  62. #endif
  63. // Find scale factor that applied to width and height, is best match
  64. // to num_pixels.
  65. float VideoAdapter::FindClosestScale(int width, int height,
  66. int target_num_pixels) {
  67. if (!target_num_pixels) {
  68. return 0.f;
  69. }
  70. int best_distance = INT_MAX;
  71. int best_index = 0; // Default to unscaled.
  72. for (size_t i = 0u; i < ARRAY_SIZE(kScaleFactors); ++i) {
  73. int test_num_pixels = static_cast<int>(width * kScaleFactors[i] *
  74. height * kScaleFactors[i]);
  75. int diff = test_num_pixels - target_num_pixels;
  76. if (diff < 0) {
  77. diff = -diff;
  78. }
  79. if (diff < best_distance) {
  80. best_distance = diff;
  81. best_index = i;
  82. if (!best_distance) { // Found exact match
  83. break;
  84. }
  85. }
  86. }
  87. return kScaleFactors[best_index];
  88. }
  89. // Find scale factor that applied to width and height, produces less than
  90. // num_pixels.
  91. float VideoAdapter::FindLowerScale(int width, int height,
  92. int target_num_pixels) {
  93. if (!target_num_pixels) {
  94. return 0.f;
  95. }
  96. int best_distance = INT_MAX;
  97. int best_index = 0; // Default to unscaled.
  98. for (size_t i = 0u; i < ARRAY_SIZE(kScaleFactors); ++i) {
  99. int test_num_pixels = static_cast<int>(width * kScaleFactors[i] *
  100. height * kScaleFactors[i]);
  101. int diff = target_num_pixels - test_num_pixels;
  102. if (diff >= 0 && diff < best_distance) {
  103. best_distance = diff;
  104. best_index = i;
  105. if (!best_distance) { // Found exact match.
  106. break;
  107. }
  108. }
  109. }
  110. return kScaleFactors[best_index];
  111. }
  112. // There are several frame sizes used by Adapter. This explains them
  113. // input_format - set once by server to frame size expected from the camera.
  114. // output_format - size that output would like to be. Includes framerate.
  115. // output_num_pixels - size that output should be constrained to. Used to
  116. // compute output_format from in_frame.
  117. // in_frame - actual camera captured frame size, which is typically the same
  118. // as input_format. This can also be rotated or cropped for aspect ratio.
  119. // out_frame - actual frame output by adapter. Should be a direct scale of
  120. // in_frame maintaining rotation and aspect ratio.
  121. // OnOutputFormatRequest - server requests you send this resolution based on
  122. // view requests.
  123. // OnEncoderResolutionRequest - encoder requests you send this resolution based
  124. // on bandwidth
  125. // OnCpuLoadUpdated - cpu monitor requests you send this resolution based on
  126. // cpu load.
  127. ///////////////////////////////////////////////////////////////////////
  128. // Implementation of VideoAdapter
  129. VideoAdapter::VideoAdapter()
  130. : output_num_pixels_(0),
  131. black_output_(false),
  132. is_black_(false),
  133. interval_next_frame_(0) {
  134. }
  135. VideoAdapter::~VideoAdapter() {
  136. }
  137. // TODO(fbarchard): Consider SetInputFormat and SetOutputFormat without
  138. // VideoFormat.
  139. void VideoAdapter::SetInputFormat(const VideoFormat& format) {
  140. talk_base::CritScope cs(&critical_section_);
  141. input_format_ = format;
  142. output_format_.interval = talk_base::_max(
  143. output_format_.interval, input_format_.interval);
  144. }
  145. void VideoAdapter::SetOutputFormat(const VideoFormat& format) {
  146. talk_base::CritScope cs(&critical_section_);
  147. output_format_ = format;
  148. output_num_pixels_ = output_format_.width * output_format_.height;
  149. output_format_.interval = talk_base::_max(
  150. output_format_.interval, input_format_.interval);
  151. }
  152. const VideoFormat& VideoAdapter::input_format() {
  153. talk_base::CritScope cs(&critical_section_);
  154. return input_format_;
  155. }
  156. const VideoFormat& VideoAdapter::output_format() {
  157. talk_base::CritScope cs(&critical_section_);
  158. return output_format_;
  159. }
  160. void VideoAdapter::SetBlackOutput(bool black) {
  161. talk_base::CritScope cs(&critical_section_);
  162. black_output_ = black;
  163. }
  164. // Constrain output resolution to this many pixels overall
  165. void VideoAdapter::SetOutputNumPixels(int num_pixels) {
  166. output_num_pixels_ = num_pixels;
  167. }
  168. int VideoAdapter::GetOutputNumPixels() const {
  169. return output_num_pixels_;
  170. }
  171. // TODO(fbarchard): Add AdaptFrameRate function that only drops frames but
  172. // not resolution.
  173. bool VideoAdapter::AdaptFrame(const VideoFrame* in_frame,
  174. const VideoFrame** out_frame) {
  175. talk_base::CritScope cs(&critical_section_);
  176. if (!in_frame || !out_frame || input_format_.IsSize0x0()) {
  177. return false;
  178. }
  179. // Drop the input frame if necessary.
  180. bool should_drop = false;
  181. if (!output_num_pixels_) {
  182. // Drop all frames as the output format is 0x0.
  183. should_drop = true;
  184. } else {
  185. // Drop some frames based on input fps and output fps.
  186. // Normally output fps is less than input fps.
  187. // TODO(fbarchard): Consider adjusting interval to reflect the adjusted
  188. // interval between frames after dropping some frames.
  189. interval_next_frame_ += input_format_.interval;
  190. if (interval_next_frame_ >= output_format_.interval) {
  191. interval_next_frame_ %= output_format_.interval;
  192. } else {
  193. should_drop = true;
  194. }
  195. }
  196. if (should_drop) {
  197. *out_frame = NULL;
  198. return true;
  199. }
  200. if (output_num_pixels_) {
  201. float scale = VideoAdapter::FindClosestScale(in_frame->GetWidth(),
  202. in_frame->GetHeight(),
  203. output_num_pixels_);
  204. output_format_.width = static_cast<int>(in_frame->GetWidth() * scale);
  205. output_format_.height = static_cast<int>(in_frame->GetHeight() * scale);
  206. }
  207. if (!StretchToOutputFrame(in_frame)) {
  208. return false;
  209. }
  210. *out_frame = output_frame_.get();
  211. return true;
  212. }
  213. bool VideoAdapter::StretchToOutputFrame(const VideoFrame* in_frame) {
  214. int output_width = output_format_.width;
  215. int output_height = output_format_.height;
  216. // Create and stretch the output frame if it has not been created yet or its
  217. // size is not same as the expected.
  218. bool stretched = false;
  219. if (!output_frame_ ||
  220. output_frame_->GetWidth() != static_cast<size_t>(output_width) ||
  221. output_frame_->GetHeight() != static_cast<size_t>(output_height)) {
  222. output_frame_.reset(
  223. in_frame->Stretch(output_width, output_height, true, true));
  224. if (!output_frame_) {
  225. LOG(LS_WARNING) << "Adapter failed to stretch frame to "
  226. << output_width << "x" << output_height;
  227. return false;
  228. }
  229. stretched = true;
  230. is_black_ = false;
  231. }
  232. if (!black_output_) {
  233. if (!stretched) {
  234. // The output frame does not need to be blacken and has not been stretched
  235. // from the input frame yet, stretch the input frame. This is the most
  236. // common case.
  237. in_frame->StretchToFrame(output_frame_.get(), true, true);
  238. }
  239. is_black_ = false;
  240. } else {
  241. if (!is_black_) {
  242. output_frame_->SetToBlack();
  243. is_black_ = true;
  244. }
  245. output_frame_->SetElapsedTime(in_frame->GetElapsedTime());
  246. output_frame_->SetTimeStamp(in_frame->GetTimeStamp());
  247. }
  248. return true;
  249. }
  250. ///////////////////////////////////////////////////////////////////////
  251. // Implementation of CoordinatedVideoAdapter
  252. CoordinatedVideoAdapter::CoordinatedVideoAdapter()
  253. : cpu_adaptation_(false),
  254. gd_adaptation_(true),
  255. view_adaptation_(true),
  256. view_switch_(false),
  257. cpu_downgrade_count_(0),
  258. cpu_downgrade_wait_time_(0),
  259. high_system_threshold_(kHighSystemCpuThreshold),
  260. low_system_threshold_(kLowSystemCpuThreshold),
  261. process_threshold_(kProcessCpuThreshold),
  262. view_desired_num_pixels_(INT_MAX),
  263. view_desired_interval_(0),
  264. encoder_desired_num_pixels_(INT_MAX),
  265. cpu_desired_num_pixels_(INT_MAX) {
  266. }
  267. // Helper function to UPGRADE or DOWNGRADE a number of pixels
  268. void CoordinatedVideoAdapter::StepPixelCount(
  269. CoordinatedVideoAdapter::AdaptRequest request,
  270. int* num_pixels) {
  271. switch (request) {
  272. case CoordinatedVideoAdapter::DOWNGRADE:
  273. *num_pixels /= 2;
  274. break;
  275. case CoordinatedVideoAdapter::UPGRADE:
  276. *num_pixels *= 2;
  277. break;
  278. default: // No change in pixel count
  279. break;
  280. }
  281. return;
  282. }
  283. // Find the adaptation request of the cpu based on the load. Return UPGRADE if
  284. // the load is low, DOWNGRADE if the load is high, and KEEP otherwise.
  285. CoordinatedVideoAdapter::AdaptRequest CoordinatedVideoAdapter::FindCpuRequest(
  286. int current_cpus, int max_cpus,
  287. float process_load, float system_load) {
  288. // Downgrade if system is high and plugin is at least more than midrange.
  289. if (system_load >= high_system_threshold_ * max_cpus &&
  290. process_load >= process_threshold_ * current_cpus) {
  291. return CoordinatedVideoAdapter::DOWNGRADE;
  292. // Upgrade if system is low.
  293. } else if (system_load < low_system_threshold_ * max_cpus) {
  294. return CoordinatedVideoAdapter::UPGRADE;
  295. }
  296. return CoordinatedVideoAdapter::KEEP;
  297. }
  298. // A remote view request for a new resolution.
  299. void CoordinatedVideoAdapter::OnOutputFormatRequest(const VideoFormat& format) {
  300. talk_base::CritScope cs(&request_critical_section_);
  301. if (!view_adaptation_) {
  302. return;
  303. }
  304. // Set output for initial aspect ratio in mediachannel unittests.
  305. int old_num_pixels = GetOutputNumPixels();
  306. SetOutputFormat(format);
  307. SetOutputNumPixels(old_num_pixels);
  308. view_desired_num_pixels_ = format.width * format.height;
  309. view_desired_interval_ = format.interval;
  310. int new_width, new_height;
  311. bool changed = AdaptToMinimumFormat(&new_width, &new_height);
  312. LOG(LS_INFO) << "VAdapt View Request: "
  313. << format.width << "x" << format.height
  314. << " Pixels: " << view_desired_num_pixels_
  315. << " Changed: " << (changed ? "true" : "false")
  316. << " To: " << new_width << "x" << new_height;
  317. }
  318. // A Bandwidth GD request for new resolution
  319. void CoordinatedVideoAdapter::OnEncoderResolutionRequest(
  320. int width, int height, AdaptRequest request) {
  321. talk_base::CritScope cs(&request_critical_section_);
  322. if (!gd_adaptation_) {
  323. return;
  324. }
  325. int old_encoder_desired_num_pixels = encoder_desired_num_pixels_;
  326. if (KEEP != request) {
  327. int new_encoder_desired_num_pixels = width * height;
  328. int old_num_pixels = GetOutputNumPixels();
  329. if (new_encoder_desired_num_pixels != old_num_pixels) {
  330. LOG(LS_VERBOSE) << "VAdapt GD resolution stale. Ignored";
  331. } else {
  332. // Update the encoder desired format based on the request.
  333. encoder_desired_num_pixels_ = new_encoder_desired_num_pixels;
  334. StepPixelCount(request, &encoder_desired_num_pixels_);
  335. }
  336. }
  337. int new_width, new_height;
  338. bool changed = AdaptToMinimumFormat(&new_width, &new_height);
  339. // Ignore up or keep if no change.
  340. if (DOWNGRADE != request && view_switch_ && !changed) {
  341. encoder_desired_num_pixels_ = old_encoder_desired_num_pixels;
  342. LOG(LS_VERBOSE) << "VAdapt ignoring GD request.";
  343. }
  344. LOG(LS_INFO) << "VAdapt GD Request: "
  345. << (DOWNGRADE == request ? "down" :
  346. (UPGRADE == request ? "up" : "keep"))
  347. << " From: " << width << "x" << height
  348. << " Pixels: " << encoder_desired_num_pixels_
  349. << " Changed: " << (changed ? "true" : "false")
  350. << " To: " << new_width << "x" << new_height;
  351. }
  352. // A CPU request for new resolution
  353. void CoordinatedVideoAdapter::OnCpuLoadUpdated(
  354. int current_cpus, int max_cpus, float process_load, float system_load) {
  355. talk_base::CritScope cs(&request_critical_section_);
  356. if (!cpu_adaptation_) {
  357. return;
  358. }
  359. AdaptRequest request = FindCpuRequest(current_cpus, max_cpus,
  360. process_load, system_load);
  361. // Update how many times we have downgraded due to the cpu load.
  362. switch (request) {
  363. case DOWNGRADE:
  364. if (cpu_downgrade_count_ < kMaxCpuDowngrades) {
  365. // Ignore downgrades if we have downgraded the maximum times or we just
  366. // downgraded in a short time.
  367. if (cpu_downgrade_wait_time_ != 0 &&
  368. talk_base::TimeIsLater(talk_base::Time(),
  369. cpu_downgrade_wait_time_)) {
  370. LOG(LS_VERBOSE) << "VAdapt CPU load high but do not downgrade until "
  371. << talk_base::TimeUntil(cpu_downgrade_wait_time_)
  372. << " ms.";
  373. request = KEEP;
  374. } else {
  375. ++cpu_downgrade_count_;
  376. }
  377. } else {
  378. LOG(LS_VERBOSE) << "VAdapt CPU load high but do not downgrade "
  379. "because maximum downgrades reached";
  380. SignalCpuAdaptationUnable();
  381. }
  382. break;
  383. case UPGRADE:
  384. if (cpu_downgrade_count_ > 0) {
  385. bool is_min = IsMinimumFormat(cpu_desired_num_pixels_);
  386. if (is_min) {
  387. --cpu_downgrade_count_;
  388. } else {
  389. LOG(LS_VERBOSE) << "VAdapt CPU load low but do not upgrade "
  390. "because cpu is not limiting resolution";
  391. }
  392. } else {
  393. LOG(LS_VERBOSE) << "VAdapt CPU load low but do not upgrade "
  394. "because minimum downgrades reached";
  395. }
  396. break;
  397. case KEEP:
  398. default:
  399. break;
  400. }
  401. if (KEEP != request) {
  402. // TODO(fbarchard): compute stepping up/down from OutputNumPixels but
  403. // clamp to inputpixels / 4 (2 steps)
  404. cpu_desired_num_pixels_ = static_cast<int>(
  405. input_format().width * input_format().height >> cpu_downgrade_count_);
  406. }
  407. int new_width, new_height;
  408. bool changed = AdaptToMinimumFormat(&new_width, &new_height);
  409. LOG(LS_INFO) << "VAdapt CPU Request: "
  410. << (DOWNGRADE == request ? "down" :
  411. (UPGRADE == request ? "up" : "keep"))
  412. << " Process: " << process_load
  413. << " System: " << system_load
  414. << " Steps: " << cpu_downgrade_count_
  415. << " Changed: " << (changed ? "true" : "false")
  416. << " To: " << new_width << "x" << new_height;
  417. }
  418. // Called by cpu adapter on up requests.
  419. bool CoordinatedVideoAdapter::IsMinimumFormat(int pixels) {
  420. // Find closest scale factor that matches input resolution to min_num_pixels
  421. // and set that for output resolution. This is not needed for VideoAdapter,
  422. // but provides feedback to unittests and users on expected resolution.
  423. // Actual resolution is based on input frame.
  424. VideoFormat new_output = output_format();
  425. VideoFormat input = input_format();
  426. if (input_format().IsSize0x0()) {
  427. input = new_output;
  428. }
  429. float scale = 1.0f;
  430. if (!input.IsSize0x0()) {
  431. scale = FindClosestScale(input.width,
  432. input.height,
  433. pixels);
  434. }
  435. new_output.width = static_cast<int>(input.width * scale);
  436. new_output.height = static_cast<int>(input.height * scale);
  437. int new_pixels = new_output.width * new_output.height;
  438. int num_pixels = GetOutputNumPixels();
  439. return new_pixels <= num_pixels;
  440. }
  441. // Called by all coordinators when there is a change.
  442. bool CoordinatedVideoAdapter::AdaptToMinimumFormat(int* new_width,
  443. int* new_height) {
  444. VideoFormat new_output = output_format();
  445. VideoFormat input = input_format();
  446. if (input_format().IsSize0x0()) {
  447. input = new_output;
  448. }
  449. int old_num_pixels = GetOutputNumPixels();
  450. // Find resolution that respects ViewRequest or less pixels.
  451. int min_num_pixels = view_desired_num_pixels_;
  452. if (!input.IsSize0x0()) {
  453. float scale = FindLowerScale(input.width, input.height, min_num_pixels);
  454. min_num_pixels =
  455. static_cast<int>(input.width * input.height * scale * scale);
  456. }
  457. // Reduce resolution further, if necessary, based on encoder bandwidth (GD).
  458. if (encoder_desired_num_pixels_ &&
  459. (encoder_desired_num_pixels_ < min_num_pixels)) {
  460. min_num_pixels = encoder_desired_num_pixels_;
  461. }
  462. // Reduce resolution further, if necessary, based on CPU.
  463. if (cpu_adaptation_ && cpu_desired_num_pixels_ &&
  464. (cpu_desired_num_pixels_ < min_num_pixels)) {
  465. min_num_pixels = cpu_desired_num_pixels_;
  466. // Update the cpu_downgrade_wait_time_ if we are going to downgrade video.
  467. cpu_downgrade_wait_time_ =
  468. talk_base::TimeAfter(kDefaultDowngradeWaitTimeMs);
  469. }
  470. // prevent going below QQVGA
  471. if (min_num_pixels > 0 && min_num_pixels < kMinNumPixels) {
  472. min_num_pixels = kMinNumPixels;
  473. }
  474. SetOutputNumPixels(min_num_pixels);
  475. // Find closest scale factor that matches input resolution to min_num_pixels
  476. // and set that for output resolution. This is not needed for VideoAdapter,
  477. // but provides feedback to unittests and users on expected resolution.
  478. // Actual resolution is based on input frame.
  479. float scale = 1.0f;
  480. if (!input.IsSize0x0()) {
  481. scale = FindClosestScale(input.width, input.height, min_num_pixels);
  482. }
  483. *new_width = new_output.width = static_cast<int>(input.width * scale);
  484. *new_height = new_output.height = static_cast<int>(input.height * scale);
  485. new_output.interval = view_desired_interval_;
  486. SetOutputFormat(new_output);
  487. int new_num_pixels = GetOutputNumPixels();
  488. bool changed = new_num_pixels != old_num_pixels;
  489. LOG(LS_VERBOSE) << "VAdapt Status View: " << view_desired_num_pixels_
  490. << " GD: " << encoder_desired_num_pixels_
  491. << " CPU: " << cpu_desired_num_pixels_
  492. << " Pixels: " << min_num_pixels
  493. << " Scale: " << scale
  494. << " Resolution: " << new_output.width
  495. << "x" << new_output.height
  496. << " Changed: " << (changed ? "true" : "false");
  497. return changed;
  498. }
  499. } // namespace cricket