Index: media/video/capture/win/capability_list_win.cc |
diff --git a/media/video/capture/win/capability_list_win.cc b/media/video/capture/win/capability_list_win.cc |
index 9b4531b82820107c2908b61dd6fe0ddfdf366d4d..cefda435379923d706c75a09cd6a8710fb85a2f8 100644 |
--- a/media/video/capture/win/capability_list_win.cc |
+++ b/media/video/capture/win/capability_list_win.cc |
@@ -5,116 +5,49 @@ |
#include "media/video/capture/win/capability_list_win.h" |
#include <algorithm> |
+#include <functional> |
#include "base/logging.h" |
namespace media { |
-namespace { |
-// Help structure used for comparing video capture capabilities. |
-struct ResolutionDiff { |
- const VideoCaptureCapabilityWin* capability; |
- int diff_height; |
- int diff_width; |
- int diff_frame_rate; |
-}; |
- |
-bool CompareHeight(const ResolutionDiff& item1, const ResolutionDiff& item2) { |
- return abs(item1.diff_height) < abs(item2.diff_height); |
-} |
- |
-bool CompareWidth(const ResolutionDiff& item1, const ResolutionDiff& item2) { |
- return abs(item1.diff_width) < abs(item2.diff_width); |
-} |
- |
-bool CompareFrameRate(const ResolutionDiff& item1, |
- const ResolutionDiff& item2) { |
- return abs(item1.diff_frame_rate) < abs(item2.diff_frame_rate); |
-} |
- |
-bool CompareColor(const ResolutionDiff& item1, const ResolutionDiff& item2) { |
- return item1.capability->supported_format.pixel_format < |
- item2.capability->supported_format.pixel_format; |
-} |
- |
-} // namespace. |
- |
-CapabilityList::CapabilityList() { |
- DetachFromThread(); |
-} |
- |
-CapabilityList::~CapabilityList() {} |
- |
-// Appends an entry to the list. |
-void CapabilityList::Add(const VideoCaptureCapabilityWin& capability) { |
- DCHECK(CalledOnValidThread()); |
- capabilities_.push_back(capability); |
+static bool CompareCapability(const VideoCaptureFormat& requested, |
+ const CapabilityWin& capability_lhs, |
+ const CapabilityWin& capability_rhs) { |
+ const VideoCaptureFormat& lhs = capability_lhs.supported_format; |
+ const VideoCaptureFormat& rhs = capability_rhs.supported_format; |
+ |
+ const int diff_height_lhs = |
+ std::abs(lhs.frame_size.height() - requested.frame_size.height()); |
+ const int diff_height_rhs = |
+ std::abs(rhs.frame_size.height() - requested.frame_size.height()); |
+ if (diff_height_lhs != diff_height_rhs) |
+ return diff_height_lhs < diff_height_rhs; |
+ |
+ const int diff_width_lhs = |
+ std::abs(lhs.frame_size.width() - requested.frame_size.width()); |
+ const int diff_width_rhs = |
+ std::abs(rhs.frame_size.width() - requested.frame_size.width()); |
+ if (diff_width_lhs != diff_width_rhs) |
+ return diff_width_lhs < diff_width_rhs; |
+ |
+ const float diff_fps_lhs = std::fabs(lhs.frame_rate - requested.frame_rate); |
+ const float diff_fps_rhs = std::fabs(rhs.frame_rate - requested.frame_rate); |
+ if (diff_fps_lhs != diff_fps_rhs) |
+ return diff_fps_lhs < diff_fps_rhs; |
+ |
+ return lhs.pixel_format < rhs.pixel_format; |
} |
-const VideoCaptureCapabilityWin& CapabilityList::GetBestMatchedFormat( |
- int requested_width, |
- int requested_height, |
- float requested_frame_rate) const { |
- DCHECK(CalledOnValidThread()); |
- DCHECK(!capabilities_.empty()); |
- |
- std::list<ResolutionDiff> diff_list; |
- |
- // Loop through the candidates to create a list of differentials between the |
- // requested resolution and the camera capability. |
- for (Capabilities::const_iterator it = capabilities_.begin(); |
- it != capabilities_.end(); ++it) { |
- ResolutionDiff diff; |
- diff.capability = &(*it); |
- diff.diff_width = it->supported_format.frame_size.width() - requested_width; |
- diff.diff_height = |
- it->supported_format.frame_size.height() - requested_height; |
- // The 1000 allows using integer arithmetic for f.i. 29.971 fps. |
- diff.diff_frame_rate = |
- 1000 * ((static_cast<float>(it->frame_rate_numerator) / |
- it->frame_rate_denominator) - |
- requested_frame_rate); |
- diff_list.push_back(diff); |
- } |
- |
- // Sort the best height candidates. |
- diff_list.sort(&CompareHeight); |
- int best_diff = diff_list.front().diff_height; |
- for (std::list<ResolutionDiff>::iterator it = diff_list.begin(); |
- it != diff_list.end(); ++it) { |
- if (it->diff_height != best_diff) { |
- // Remove all candidates but the best. |
- diff_list.erase(it, diff_list.end()); |
- break; |
- } |
+CapabilityWin GetBestMatchedCapability(const VideoCaptureFormat& requested, |
+ const CapabilityList& capabilities) { |
+ DCHECK(!capabilities.empty()); |
+ CapabilityWin best_match = capabilities.front(); |
+ for (const CapabilityWin& capability : capabilities) { |
+ if (CompareCapability(requested, capability, best_match)) |
+ best_match = capability; |
} |
- |
- // Sort the best width candidates. |
- diff_list.sort(&CompareWidth); |
- best_diff = diff_list.front().diff_width; |
- for (std::list<ResolutionDiff>::iterator it = diff_list.begin(); |
- it != diff_list.end(); ++it) { |
- if (it->diff_width != best_diff) { |
- // Remove all candidates but the best. |
- diff_list.erase(it, diff_list.end()); |
- break; |
- } |
- } |
- |
- // Sort the best frame rate candidates. |
- diff_list.sort(&CompareFrameRate); |
- best_diff = diff_list.front().diff_frame_rate; |
- for (std::list<ResolutionDiff>::iterator it = diff_list.begin(); |
- it != diff_list.end(); ++it) { |
- if (it->diff_frame_rate != best_diff) { |
- diff_list.erase(it, diff_list.end()); |
- break; |
- } |
- } |
- |
- // Decide the best color format. |
- diff_list.sort(&CompareColor); |
- return *diff_list.front().capability; |
+ return best_match; |
} |
} // namespace media |