This source file includes following definitions.
- CompareHeight
- CompareWidth
- CompareFrameRate
- CompareColor
- Add
- GetBestMatchedFormat
#include "media/video/capture/win/capability_list_win.h"
#include <algorithm>
#include "base/logging.h"
namespace media {
namespace {
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;
}
}
CapabilityList::CapabilityList() {
DetachFromThread();
}
CapabilityList::~CapabilityList() {}
void CapabilityList::Add(const VideoCaptureCapabilityWin& capability) {
DCHECK(CalledOnValidThread());
capabilities_.push_back(capability);
}
const VideoCaptureCapabilityWin& CapabilityList::GetBestMatchedFormat(
int requested_width,
int requested_height,
int requested_frame_rate) const {
DCHECK(CalledOnValidThread());
DCHECK(!capabilities_.empty());
std::list<ResolutionDiff> diff_list;
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;
diff.diff_frame_rate =
1000 * ((static_cast<float>(it->frame_rate_numerator) /
it->frame_rate_denominator) -
requested_frame_rate);
diff_list.push_back(diff);
}
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) {
diff_list.erase(it, diff_list.end());
break;
}
}
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) {
diff_list.erase(it, diff_list.end());
break;
}
}
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;
}
}
diff_list.sort(&CompareColor);
return *diff_list.front().capability;
}
}