1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(avform): Replace bestModeInds on videoMode index and video height as quality name

This commit is contained in:
Diadlo 2016-06-13 20:19:32 +03:00
parent 6f3ef0cf59
commit 3f82396173
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 27 additions and 37 deletions

View File

@ -154,7 +154,7 @@ void AVForm::onVideoModesIndexChanged(int index)
camera.open(devName, mode); camera.open(devName, mode);
} }
std::map<int, int> AVForm::getBestModeInds(QVector<VideoMode> &allVideoModes) void AVForm::selectBestModes(QVector<VideoMode> &allVideoModes)
{ {
// Identify the best resolutions available for the supposed XXXXp resolutions. // Identify the best resolutions available for the supposed XXXXp resolutions.
std::map<int, VideoMode> idealModes; std::map<int, VideoMode> idealModes;
@ -169,7 +169,8 @@ std::map<int, int> AVForm::getBestModeInds(QVector<VideoMode> &allVideoModes)
for (int i = 0; i < allVideoModes.size(); ++i) for (int i = 0; i < allVideoModes.size(); ++i)
{ {
VideoMode mode = allVideoModes[i]; VideoMode mode = allVideoModes[i];
qDebug("width: %d, height: %d, FPS: %f, pixel format: %s", mode.width, mode.height, mode.FPS, CameraDevice::getPixelFormatString(mode.pixel_format).toStdString().c_str()); QString pixelFormat = CameraDevice::getPixelFormatString(mode.pixel_format);
qDebug("width: %d, height: %d, FPS: %f, pixel format: %s", mode.width, mode.height, mode.FPS, pixelFormat.toStdString().c_str());
// PS3-Cam protection, everything above 60fps makes no sense // PS3-Cam protection, everything above 60fps makes no sense
if(mode.FPS > 60) if(mode.FPS > 60)
@ -189,44 +190,43 @@ std::map<int, int> AVForm::getBestModeInds(QVector<VideoMode> &allVideoModes)
bestModeInds[res] = i; bestModeInds[res] = i;
continue; continue;
} }
int ind = bestModeInds[res];
if (mode.norm(idealMode) < allVideoModes[ind].norm(idealMode)) int index = bestModeInds[res];
VideoMode best = allVideoModes[index];
if (mode.norm(idealMode) < best.norm(idealMode))
{ {
bestModeInds[res] = i; bestModeInds[res] = i;
continue;
} }
else if (mode.norm(idealMode) == allVideoModes[ind].norm(idealMode))
if (mode.norm(idealMode) == best.norm(idealMode))
{ {
// prefer higher FPS and "better" pixel formats // prefer higher FPS and "better" pixel formats
if (mode.FPS > allVideoModes[ind].FPS) if (mode.FPS > best.FPS)
{ {
bestModeInds[res] = i; bestModeInds[res] = i;
continue;
} }
else if (mode.FPS == allVideoModes[ind].FPS &&
CameraDevice::betterPixelFormat(mode.pixel_format, allVideoModes[ind].pixel_format)) bool better = CameraDevice::betterPixelFormat(mode.pixel_format, best.pixel_format);
{ if (mode.FPS == best.FPS && better)
bestModeInds[res] = i; bestModeInds[res] = i;
} }
} }
} }
}
QVector<VideoMode> newVideoModes; QVector<VideoMode> newVideoModes;
int index = 0;
for (auto it = bestModeInds.rbegin(); it != bestModeInds.rend(); ++it) for (auto it = bestModeInds.rbegin(); it != bestModeInds.rend(); ++it)
{ {
int i = it->second; VideoMode mode = allVideoModes[it->second];
VideoMode mode = videoModes[i];
auto result = std::find(newVideoModes.begin(), newVideoModes.end(), mode); auto result = std::find(newVideoModes.begin(), newVideoModes.end(), mode);
if (result == newVideoModes.end()) if (result == newVideoModes.end())
newVideoModes.push_back(mode); newVideoModes.push_back(mode);
it->second = index++;
} }
videoModes = newVideoModes; allVideoModes = newVideoModes;
return bestModeInds;
} }
int AVForm::fillModesComboBox(std::map<int, int> bestModeInds) int AVForm::fillModesComboBox()
{ {
bool previouslyBlocked = bodyUI->videoModescomboBox->blockSignals(true); bool previouslyBlocked = bodyUI->videoModescomboBox->blockSignals(true);
bodyUI->videoModescomboBox->clear(); bodyUI->videoModescomboBox->clear();
@ -235,25 +235,22 @@ int AVForm::fillModesComboBox(std::map<int, int> bestModeInds)
QSize prefRes = Settings::getInstance().getCamVideoRes(); QSize prefRes = Settings::getInstance().getCamVideoRes();
unsigned short prefFPS = Settings::getInstance().getCamVideoFPS(); unsigned short prefFPS = Settings::getInstance().getCamVideoFPS();
// Iterate backwards to show higest resolution first. for(int i = 0; i < videoModes.size(); i++)
videoModes.clear();
for(auto iter = bestModeInds.rbegin(); iter != bestModeInds.rend(); ++iter)
{ {
int index = iter->second; VideoMode mode = videoModes[i];
VideoMode mode = videoModes[index];
if (mode.width == prefRes.width() if (mode.width == prefRes.width()
&& mode.height == prefRes.height() && mode.height == prefRes.height()
&& mode.FPS == prefFPS && mode.FPS == prefFPS
&& prefResIndex == -1) && prefResIndex == -1)
prefResIndex = index; prefResIndex = i;
QString str; QString str;
QString pixelFormat = CameraDevice::getPixelFormatString(mode.pixel_format); QString pixelFormat = CameraDevice::getPixelFormatString(mode.pixel_format);
qDebug("width: %d, height: %d, FPS: %f, pixel format: %s\n", mode.width, mode.height, mode.FPS, pixelFormat.toStdString().c_str()); qDebug("width: %d, height: %d, FPS: %f, pixel format: %s\n", mode.width, mode.height, mode.FPS, pixelFormat.toStdString().c_str());
if (mode.height && mode.width) if (mode.height && mode.width)
str += QString("%1p").arg(iter->first); str += QString("%1p").arg(mode.height);
else else
str += tr("Default resolution"); str += tr("Default resolution");
@ -269,7 +266,7 @@ int AVForm::fillModesComboBox(std::map<int, int> bestModeInds)
void AVForm::updateVideoModes(int curIndex) void AVForm::updateVideoModes(int curIndex)
{ {
if (curIndex<0 || curIndex>=videoDeviceList.size()) if (curIndex < 0 || curIndex >= videoDeviceList.size())
{ {
qWarning() << "Invalid index"; qWarning() << "Invalid index";
return; return;
@ -277,18 +274,12 @@ void AVForm::updateVideoModes(int curIndex)
QString devName = videoDeviceList[curIndex].first; QString devName = videoDeviceList[curIndex].first;
QVector<VideoMode> allVideoModes = CameraDevice::getVideoModes(devName); QVector<VideoMode> allVideoModes = CameraDevice::getVideoModes(devName);
std::sort(allVideoModes.begin(), allVideoModes.end(),
[](const VideoMode& a, const VideoMode& b)
{return a.width!=b.width ? a.width>b.width :
a.height!=b.height ? a.height>b.height :
a.FPS>b.FPS;});
qDebug("available Modes:"); qDebug("available Modes:");
std::map<int, int> bestModeInds = getBestModeInds(allVideoModes); selectBestModes(allVideoModes);
videoModes = allVideoModes; videoModes = allVideoModes;
qDebug("selected Modes:"); qDebug("selected Modes:");
int prefResIndex = fillModesComboBox(bestModeInds); int prefResIndex = fillModesComboBox();
if (prefResIndex != -1) if (prefResIndex != -1)
{ {
bodyUI->videoModescomboBox->setCurrentIndex(prefResIndex); bodyUI->videoModescomboBox->setCurrentIndex(prefResIndex);
@ -302,7 +293,6 @@ void AVForm::updateVideoModes(int curIndex)
return; return;
} }
// If the user hasn't set a preferred resolution yet, // If the user hasn't set a preferred resolution yet,
// we'll pick the resolution in the middle of the list, // we'll pick the resolution in the middle of the list,
// and the best FPS for that resolution. // and the best FPS for that resolution.

View File

@ -46,8 +46,8 @@ private:
void getAudioOutDevices(); void getAudioOutDevices();
void getVideoDevices(); void getVideoDevices();
std::map<int, int> getBestModeInds(QVector<VideoMode> &allVideoModes); void selectBestModes(QVector<VideoMode> &allVideoModes);
int fillModesComboBox(std::map<int, int> bestModeInds); int fillModesComboBox();
void createVideoSurface(); void createVideoSurface();
void killVideoSurface(); void killVideoSurface();