115 changes: 49 additions & 66 deletions mythtv/libs/libmyth/audio/audiooutputsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Licensed under the GPL v2 or a later version at your choosing.
*/

#include <array>
#include <algorithm>
#include <vector>

Expand All @@ -21,22 +22,27 @@ extern "C" {

#define LOC QString("AOS: ")

const rate_array AudioOutputSettings::kStdRates
{
5512, 8000, 11025, 16000, 22050, 32000, 44100,
48000, 88200, 96000, 176400, 192000
};

const format_array AudioOutputSettings::kStdFormats
{
FORMAT_U8, FORMAT_S16, FORMAT_S24LSB, FORMAT_S24, FORMAT_S32, FORMAT_FLT
};

AudioOutputSettings::AudioOutputSettings(bool invalid) :
m_invalid(invalid)
{
m_sr.assign(srs, srs +
sizeof(srs) / sizeof(int));
m_sf.assign(fmts, fmts +
sizeof(fmts) / sizeof(AudioFormat));
m_srIt = m_sr.begin();
m_sfIt = m_sf.begin();
m_srIt = kStdRates.begin();
m_sfIt = kStdFormats.begin();
}

AudioOutputSettings::~AudioOutputSettings()
{
m_sr.clear();
m_rates.clear();
m_sf.clear();
m_formats.clear();
m_channels.clear();
}
Expand All @@ -46,26 +52,24 @@ AudioOutputSettings& AudioOutputSettings::operator=(
{
if (this == &rhs)
return *this;
m_sr = rhs.m_sr;
m_rates = rhs.m_rates;
m_sf = rhs.m_sf;
m_formats = rhs.m_formats;
m_channels = rhs.m_channels;
m_passthrough = rhs.m_passthrough;
m_features = rhs.m_features;
m_invalid = rhs.m_invalid;
m_hasEld = rhs.m_hasEld;
m_eld = rhs.m_eld;
m_srIt = m_sr.begin() + (rhs.m_srIt - rhs.m_sr.begin());
m_sfIt = m_sf.begin() + (rhs.m_sfIt - rhs.m_sf.begin());
m_srIt = kStdRates.begin() + (rhs.m_srIt - kStdRates.begin());
m_sfIt = kStdFormats.begin() + (rhs.m_sfIt - kStdFormats.begin());
return *this;
}

int AudioOutputSettings::GetNextRate()
{
if (m_srIt == m_sr.end())
if (m_srIt == kStdRates.end())
{
m_srIt = m_sr.begin();
m_srIt = kStdRates.begin();
return 0;
}

Expand All @@ -84,13 +88,8 @@ bool AudioOutputSettings::IsSupportedRate(int rate)
if (m_rates.empty() && rate == 48000)
return true;

vector<int>::iterator it;

for (it = m_rates.begin(); it != m_rates.end(); ++it)
if (*it == rate)
return true;

return false;
auto result = std::find(m_rates.cbegin(), m_rates.cend(), rate);
return result != m_rates.end();
}

int AudioOutputSettings::BestSupportedRate()
Expand All @@ -105,23 +104,22 @@ int AudioOutputSettings::NearestSupportedRate(int rate)
if (m_rates.empty())
return 48000;

vector<int>::iterator it;

// Assume rates vector is sorted
for (it = m_rates.begin(); it != m_rates.end(); ++it)
for (const auto entry : m_rates)
{
if (*it >= rate)
return *it;
if (entry >= rate)
return entry;
}

// Not found, so return highest available rate
return m_rates.back();
}

AudioFormat AudioOutputSettings::GetNextFormat()
{
if (m_sfIt == m_sf.end())
if (m_sfIt == kStdFormats.end())
{
m_sfIt = m_sf.begin();
m_sfIt = kStdFormats.begin();
return FORMAT_NONE;
}

Expand All @@ -140,13 +138,8 @@ bool AudioOutputSettings::IsSupportedFormat(AudioFormat format)
if (m_formats.empty() && format == FORMAT_S16)
return true;

vector<AudioFormat>::iterator it;

for (it = m_formats.begin(); it != m_formats.end(); ++it)
if (*it == format)
return true;

return false;
auto result = std::find(m_formats.cbegin(), m_formats.cend(), format);
return result != m_formats.end();
}

AudioFormat AudioOutputSettings::BestSupportedFormat()
Expand Down Expand Up @@ -255,10 +248,8 @@ bool AudioOutputSettings::IsSupportedChannels(int channels)
if (m_channels.empty() && channels == 2)
return true;

vector<int>::iterator it;

for (it = m_channels.begin(); it != m_channels.end(); ++it)
if (*it == channels)
for (const auto entry : m_channels)
if (entry == channels)
return true;

return false;
Expand Down Expand Up @@ -442,38 +433,30 @@ int AudioOutputSettings::GetMaxHDRate() const
return 768000; // TrueHD or DTS-HD MA: 192k, 16 bits, 8 ch
}

#define ARG(x) ((tmp.isEmpty() ? "" : ",") + QString(x))
struct featureStruct
{
const DigitalFeature flag;
const std::string name;
};
static const std::array<featureStruct,7> feature {{
{ FEATURE_AC3, "AC3"},
{ FEATURE_DTS, "DTS"},
{ FEATURE_LPCM, "LPCM"},
{ FEATURE_EAC3, "EAC3"},
{ FEATURE_TRUEHD, "TRUEHD"},
{ FEATURE_DTSHD, "DTSHD"},
{ FEATURE_AAC, "AAC"},
}};

QString AudioOutputSettings::FeaturesToString(DigitalFeature arg)
{
QString tmp;
DigitalFeature feature[] = {
FEATURE_AC3,
FEATURE_DTS,
FEATURE_LPCM,
FEATURE_EAC3,
FEATURE_TRUEHD,
FEATURE_DTSHD,
FEATURE_AAC,
(DigitalFeature)-1
};
const char *feature_str[] = {
"AC3",
"DTS",
"LPCM",
"EAC3",
"TRUEHD",
"DTSHD",
"AAC",
nullptr
};

for (unsigned int i = 0; feature[i] != (DigitalFeature)-1; i++)
QStringList tmp;
for (const auto & [flag, name] : feature)
{
if (arg & feature[i])
tmp += ARG(feature_str[i]);
if (arg & flag)
tmp += QString::fromStdString(name);
}
return tmp;
return tmp.join(",");
}

QString AudioOutputSettings::GetPassthroughParams(int codec, int codec_profile,
Expand Down
19 changes: 10 additions & 9 deletions mythtv/libs/libmyth/audio/audiooutputsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ enum DigitalFeature {
FEATURE_AAC = 1 << 6,
};

static const int srs[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
48000, 88200, 96000, 176400, 192000 };

static const AudioFormat fmts[] = { FORMAT_U8, FORMAT_S16, FORMAT_S24LSB,
FORMAT_S24, FORMAT_S32, FORMAT_FLT };
using rate_array = std::array<const int,12>;
using format_array = std::array<const AudioFormat,6>;

class MPUBLIC AudioOutputSettings
{
Expand Down Expand Up @@ -208,10 +205,14 @@ class MPUBLIC AudioOutputSettings
bool m_hasEld {false};
ELD m_eld;

std::vector<int> m_sr, m_rates, m_channels;
std::vector<AudioFormat> m_sf, m_formats;
std::vector<int>::iterator m_srIt;
std::vector<AudioFormat>::iterator m_sfIt;
std::vector<int> m_rates;
std::vector<int> m_channels;
std::vector<AudioFormat> m_formats;
rate_array::iterator m_srIt { };
format_array::iterator m_sfIt { };

static const rate_array kStdRates;
static const format_array kStdFormats;
};

#endif // AUDIO_OUTPUT_SETTINGS_H
15 changes: 5 additions & 10 deletions mythtv/libs/libmyth/audio/eldutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,15 @@ int ELD::update_eld(const char *buf, int size)
m_e.manufacture_id = LE_SHORT(buf + 16);
m_e.product_id = LE_SHORT(buf + 18);

if (mnl > ELD_MAX_MNL)
{
VBAUDIO(QString("MNL is reserved value %1").arg(mnl));
goto out_fail;
}
else if (ELD_FIXED_BYTES + mnl > size)
if (ELD_FIXED_BYTES + mnl > size)
{
VBAUDIO(QString("out of range MNL %1").arg(mnl));
goto out_fail;
}
else
{
strncpy(m_e.monitor_name, (char *)buf + ELD_FIXED_BYTES, mnl + 1);
m_e.monitor_name[mnl] = '\0';
std::string tmp(buf + ELD_FIXED_BYTES, mnl);
m_e.monitor_name = QString::fromStdString(tmp);
}

for (int i = 0; i < m_e.sad_count; i++)
Expand Down Expand Up @@ -457,9 +452,9 @@ void ELD::show()
}
}

QString ELD::product_name()
QString ELD::product_name() const
{
return QString(m_e.monitor_name);
return m_e.monitor_name;
}

QString ELD::connection_name() const
Expand Down
9 changes: 4 additions & 5 deletions mythtv/libs/libmyth/audio/eldutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "mythexp.h"

#define ELD_FIXED_BYTES 20
#define ELD_MAX_SIZE 256
#define ELD_MAX_MNL 16
#define ELD_MAX_SAD 16

#define PRINT_RATES_ADVISED_BUFSIZE 80
Expand All @@ -49,7 +47,7 @@ class MPUBLIC ELD
QString edid_version_name() const;
QString info_desc() const;
QString channel_allocation_desc() const;
QString product_name();
QString product_name() const;
QString connection_name() const;
bool isValid() const;
int maxLPCMChannels();
Expand Down Expand Up @@ -109,7 +107,7 @@ class MPUBLIC ELD
int baseline_len { 0 };
int eld_ver { 0 };
int cea_edid_ver { 0 };
char monitor_name[ELD_MAX_MNL + 1] {};
QString monitor_name {};
int manufacture_id { 0 };
int product_id { 0 };
uint64_t port_id { 0 };
Expand All @@ -120,8 +118,9 @@ class MPUBLIC ELD
int aud_synch_delay { 0 };
int spk_alloc { 0 };
int sad_count { 0 };
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
struct cea_sad sad[ELD_MAX_SAD] {};
eld_data() { memset(this, 0, sizeof(*this)); }
eld_data() = default;
};
eld_data m_e;
};
Expand Down
38 changes: 16 additions & 22 deletions mythtv/libs/libmyth/mediamonitor-unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extern "C" {
#ifndef MNTTYPE_SUPERMOUNT
#define MNTTYPE_SUPERMOUNT "supermount"
#endif
#define SUPER_OPT_DEV "dev="
static const std::string kSuperOptDev { "dev=" };

#if CONFIG_QTDBUS
// DBus UDisk service - http://hal.freedesktop.org/docs/udisks/
Expand Down Expand Up @@ -173,14 +173,14 @@ bool MediaMonitorUnix::CheckFileSystemTable(void)

#if CONFIG_QTDBUS
// Get a device property by name
static QVariant DeviceProperty(const QDBusObjectPath& o, const char kszProperty[])
static QVariant DeviceProperty(const QDBusObjectPath& o, const std::string& kszProperty)
{
QVariant v;

QDBusInterface iface(UDISKS_SVC, o.path(), UDISKS_IFACE".Device",
QDBusConnection::systemBus() );
if (iface.isValid())
v = iface.property(kszProperty);
v = iface.property(kszProperty.c_str());

return v;
}
Expand Down Expand Up @@ -684,27 +684,21 @@ bool MediaMonitorUnix::AddDevice(struct fstab * mep)
}
else
{
char *dev = nullptr;
int len = 0;
dev = strstr(mep->fs_mntops, SUPER_OPT_DEV);
if (dev == nullptr)
QString dev(mep->fs_mntops);
int pos = dev.indexOf(QString::fromStdString(kSuperOptDev));
if (pos == -1)
return false;

dev += sizeof(SUPER_OPT_DEV)-1;
while (dev[len] != ',' && dev[len] != ' ' && dev[len] != 0)
len++;

if (dev[len] != 0)
{
char devstr[256];
strncpy(devstr, dev, len);
devstr[len] = 0;
if (is_cdrom)
pDevice = MythCDROM::get(this, devstr,
is_supermount, m_allowEject);
}
else
dev = dev.mid(pos+kSuperOptDev.size());
#if QT_VERSION < QT_VERSION_CHECK(5,14,0)
QStringList parts = dev.split(QRegularExpression("[, ]"),
QString::SkipEmptyParts);
#else
QStringList parts = dev.split(QRegularExpression("[, ]"),
Qt::SkipEmptyParts);
#endif
if (parts[0].isEmpty())
return false;
pDevice = MythCDROM::get(this, qPrintable(dev), is_supermount, m_allowEject);
}

if (pDevice)
Expand Down
11 changes: 6 additions & 5 deletions mythtv/libs/libmyth/mythcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QHostInfo>
#include <QMutex>
#include <QDateTime>
#include <QTcpSocket>
Expand All @@ -11,6 +12,7 @@
#include <QtAndroidExtras>
#endif

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
Expand Down Expand Up @@ -579,21 +581,20 @@ bool MythContextPrivate::LoadDatabaseSettings(void)
if (hostname.isEmpty() ||
hostname == "my-unique-identifier-goes-here")
{
char localhostname[1024];
if (gethostname(localhostname, 1024))
QString localhostname = QHostInfo::localHostName();
if (localhostname.isEmpty())
{
LOG(VB_GENERAL, LOG_ALERT,
"MCP: Error, could not determine host name." + ENO);
localhostname[0] = '\0';
}
#ifdef Q_OS_ANDROID
#define ANDROID_EXCEPTION_CHECK \
if (env->ExceptionCheck()) { \
env->ExceptionClear(); \
exception=true; \
}
if (strcmp(localhostname, "localhost") == 0
|| localhostname[0] == '\0')

if ((localhostname == "localhost") || localhostname.isEmpty())
{
hostname = "android";
bool exception=false;
Expand Down
18 changes: 7 additions & 11 deletions mythtv/libs/libmyth/programinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,22 @@ static QString determineURLType(const QString& url)
return result;
}

static const std::array<const QString,ProgramInfo::kNumCatTypes> s_cattype
static const std::array<const QString,ProgramInfo::kNumCatTypes> kCatName
{ "", "movie", "series", "sports", "tvshow" };

QString myth_category_type_to_string(ProgramInfo::CategoryType category_type)
{
if ((category_type > ProgramInfo::kCategoryNone) &&
(category_type < s_cattype.size()))
return s_cattype[category_type];
(category_type < kCatName.size()))
return kCatName[category_type];

return "";
}

ProgramInfo::CategoryType string_to_myth_category_type(const QString &category_type)
{
for (size_t i = 1; i < s_cattype.size(); i++)
if (category_type == s_cattype[i])
for (size_t i = 1; i < kCatName.size(); i++)
if (category_type == kCatName[i])
return (ProgramInfo::CategoryType) i;
return ProgramInfo::kCategoryNone;
}
Expand Down Expand Up @@ -2251,12 +2251,10 @@ bool ProgramInfo::IsSameChannel(const ProgramInfo& other) const
void ProgramInfo::CheckProgramIDAuthorities(void)
{
QMap<QString, int> authMap;
QString tables[] = { "program", "recorded", "oldrecorded", "" };
std::array<QString,3> tables { "program", "recorded", "oldrecorded" };
MSqlQuery query(MSqlQuery::InitCon());

int tableIndex = 0;
QString table = tables[tableIndex];
while (!table.isEmpty())
for (const QString& table : tables)
{
query.prepare(QString(
"SELECT DISTINCT LEFT(programid, LOCATE('/', programid)) "
Expand All @@ -2268,8 +2266,6 @@ void ProgramInfo::CheckProgramIDAuthorities(void)
while (query.next())
authMap[query.value(0).toString()] = 1;
}
++tableIndex;
table = tables[tableIndex];
}

int numAuths = authMap.count();
Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmyth/programinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class PMapDBReplacement;
class MPUBLIC ProgramInfo
{
friend int pginfo_init_statics(void);
private:
// Must match the number of items in CategoryType below
static const std::array<const QString,5> kCatName;
public:
static constexpr int kNumCatTypes = 5;
enum CategoryType { kCategoryNone, kCategoryMovie, kCategorySeries,
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmyth/remoteutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ vector<ProgramInfo *> *RemoteGetRecordedList(int sort)
return info;
}

bool RemoteGetLoad(double load[3])
bool RemoteGetLoad(system_load_array& load)
{
QStringList strlist(QString("QUERY_LOAD"));

Expand Down
4 changes: 3 additions & 1 deletion mythtv/libs/libmyth/remoteutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ using std::vector;
class ProgramInfo;
class MythEvent;

using system_load_array = std::array<double,3>;

MPUBLIC vector<ProgramInfo *> *RemoteGetRecordedList(int sort);
MPUBLIC bool RemoteGetLoad(double load[3]);
MPUBLIC bool RemoteGetLoad(system_load_array &load);
MPUBLIC bool RemoteGetUptime(time_t &uptime);
MPUBLIC
bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM);
Expand Down
4 changes: 2 additions & 2 deletions mythtv/programs/mythfrontend/statusbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,12 +1406,12 @@ void StatusBox::doMachineStatus()
}

// weighted average loads
double floads[3];
system_load_array floads;
if (RemoteGetLoad(floads))
{
auto UpdateRemoteLoad = [](StatusBoxItem* Item)
{
double loads[3] = { 0.0 };
system_load_array loads = { 0.0, 0.0, 0.0 };
RemoteGetLoad(loads);
Item->SetText(QString(" %1: %2 %3 %4").arg(tr("Load")).arg(loads[0], 1, 'f', 2)
.arg(loads[1], 1, 'f', 2).arg(loads[2], 1, 'f', 2));
Expand Down
3 changes: 2 additions & 1 deletion mythtv/programs/mythtranscode/audioreencodebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class AudioReencodeBuffer : public AudioOutput

// These are pure virtual in AudioOutput, but we don't need them here
void bufferOutputData(bool /*y*/) override { } // AudioOutput
int readOutputData(unsigned char */*read_buffer*/, int /*max_length*/) override { return 0; } // AudioOutput
int readOutputData(unsigned char */*read_buffer*/,
size_t /*max_length*/) override { return 0; } // AudioOutput

int m_channels {-1};
int m_bytes_per_frame {-1};
Expand Down