123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- #include "mainform.h"
- #include "ui_mainform.h"
- #include <portaudio.h>
- #include <QMessageBox>
- #include <QCoreApplication>
- #include <QDir>
- #include <QSettings>
- #include "imediadeviceinfo.h"
- #ifndef MAX_STR_LEN
- #define MAX_STR_LEN 512
- #endif // !MAX_STR_LEN
- #ifndef MAX_PATH
- #define MAX_PATH 260
- #endif // !MAX_PATH
- typedef int (*lpfn_get_cameracountfun)();
- typedef int (*lpfn_get_videodevice_namefun)(int device_id, char* buf, int len);
- typedef int (*lpfn_get_videodevice_infofun)(int device_id, char* namebuf, int namelen, char* pathbuf, int pathlen);
- typedef int (*lpfn_get_videodeviceid)(const char* dev_name);
- static lpfn_get_cameracountfun get_cameracount = NULL;
- static lpfn_get_videodevice_namefun get_videodevice_name = NULL;
- static lpfn_get_videodevice_infofun get_videodevice_info = NULL;
- static lpfn_get_videodevice_namefun get_device_fullpathname = NULL;
- static lpfn_get_videodeviceid get_videodeviceid = NULL;
- QList<AudioDeviceInfo> availableDevices(DevMode mode);
- static int audio_translate_id(int in_direction, int idx)
- {
- int i, n, ii;
- //audio_log_set_func(NULL);
- n = Pa_GetDeviceCount();
- for (i = 0, ii = 0; i < n; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (in_direction) {
- if (info->maxInputChannels) {
- if (ii == idx) {
- //audio_log_set_func(__audio_log_func);
- return i;
- }
- ii++;
- }
- } else {
- if (info->maxOutputChannels) {
- if (ii == idx) {
- //audio_log_set_func(__audio_log_func);
- return i;
- }
- ii++;
- }
- }
- }
- //audio_log_set_func(__audio_log_func);
- return -1;
- }
- int audio_get_dev_count(int* in_cnt, int* out_cnt)
- {
- int i;
- int icnt = 0, ocnt = 0;
- int cnt = Pa_GetDeviceCount();
- qDebug() << "device count is " << cnt;
- for (i = 0; i < cnt; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (info->maxInputChannels)
- icnt++;
- if (info->maxOutputChannels)
- ocnt++;
- }
- if (in_cnt)
- *in_cnt = icnt;
- if (out_cnt)
- *out_cnt = ocnt;
- return 0;
- }
- static char* audio_get_dev_name(char* buf, bool in_direction, int idx)
- {
- int cnt = Pa_GetDeviceCount();
- int ii, i;
- for (i = 0, ii = 0; i < cnt; ++i) {
- const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
- if (in_direction) {
- if (info->maxInputChannels) {
- if (idx == ii) {
- strcpy(buf, info->name);
- return buf;
- }
- ii++;
- }
- } else {
- if (info->maxOutputChannels) {
- if (idx == ii) {
- strcpy(buf, info->name);
- return buf;
- }
- ii++;
- }
- }
- }
- return NULL;
- }
- static QList<AudioDeviceInfo> show_audio_dev(DevMode flag)
- {
- int icnt, ocnt;
- QList< AudioDeviceInfo> result;
- int rc = audio_get_dev_count(&icnt, &ocnt);
- if (rc == 0) {
- int i;
- char tmp[128];
- if (flag & AudioInput) {
- qDebug() << "audio input devices: " << icnt;
- for (i = 0; i < icnt; ++i) {
- audio_get_dev_name(tmp, true, i);
- qDebug() << i << "=" << tmp;
- AudioDeviceInfo info;
- info.name = tmp;
- info.mod = AudioInput;
- result << info;
- }
- }
- if (flag & AudioOutput) {
- qDebug() << "audio output devices: " << ocnt;
- for (i = 0; i < ocnt; ++i) {
- audio_get_dev_name(tmp, false, i);
- qDebug() << i << "=" << tmp;
- AudioDeviceInfo info;
- info.name = tmp;
- info.mod = AudioOutput;
- result << info;
- }
- }
- }
- return result;
- }
- static QList<AudioDeviceInfo> show_video_dev()
- {
- int icount = get_cameracount();
- qDebug() << "video devices:" << icount;
- QList< AudioDeviceInfo> result;
- int inumber = 0;
- for (int i = 0; i < 2 * icount; ++i) {
- char strcamera[2 * MAX_PATH] = { 0 };
- char strpath[MAX_PATH] = { 0 };
- if (0 == get_device_fullpathname(i, strcamera, 2 * MAX_PATH)) {
- qDebug() << inumber++ << " " << strcamera;
- AudioDeviceInfo info;
- info.name = strcamera;
- info.mod = VideoDevice;
- result << info;
- }
- }
- return result;
- }
- QList<AudioDeviceInfo> availableDevices(DevMode mode)
- {
- QList<AudioDeviceInfo> devList;
- if (mode & VideoDevice) {
- devList.append(show_video_dev());
- }
- if (mode & AudioInput || mode & AudioOutput) {
- devList.append(show_audio_dev(mode));
- }
- return devList;
- }
- MainForm::MainForm(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::MainForm), deviceInfoLib(nullptr), rootFilePath()
- {
- Pa_Initialize();
- ui->setupUi(this);
- ui->tabWidget->setVisible(false);
- connect(ui->modeBox, QOverload<int>::of(&QComboBox::activated), this, &MainForm::modeChanged);
- connect(ui->deviceBox, QOverload<int>::of(&QComboBox::activated), this, &MainForm::deviceChanged);
- ui->modeBox->setCurrentIndex(0);
- modeChanged(0);
- ui->deviceBox->setCurrentIndex(0);
- deviceChanged(0);
- }
- MainForm::~MainForm()
- {
- delete ui;
- if (deviceInfoLib != nullptr) {
- deviceInfoLib->unload();
- delete deviceInfoLib;
- }
- Pa_Terminate();
- }
- bool MainForm::loadExportFunctions()
- {
- bool result = false;
- if (deviceInfoLib != nullptr) {
- return true;
- }
- QString appDir = QCoreApplication::applicationDirPath();
- QString libAbsolutePath = appDir + "/libmediadeviceinfo.so";
- qDebug() << "Enter loadExportFunctions" << " " << libAbsolutePath << endl;
- deviceInfoLib = new QLibrary(libAbsolutePath);
- deviceInfoLib->load();
- do
- {
- if (!deviceInfoLib->isLoaded()) {
- qDebug() << "Load libmediadeviceinfo.so failed: " << deviceInfoLib->errorString() << endl;
- break;
- }
- get_cameracount = (lpfn_get_cameracountfun)deviceInfoLib->resolve("rvc_videocap_get_device_count");
- if (!get_cameracount) {
- qDebug() << "Load rvc_videocap_get_device_count failed: " << deviceInfoLib->errorString() << endl;
- break;
- }
- get_videodevice_name = (lpfn_get_videodevice_namefun)deviceInfoLib->resolve("rvc_videocap_get_device_name");
- if (!get_videodevice_name) {
- qDebug() << "Load rvc_videocap_get_device_name failed: " << deviceInfoLib->errorString() << endl;
- break;
- }
- get_videodevice_info = (lpfn_get_videodevice_infofun)deviceInfoLib->resolve("rvc_videocap_get_device_info");
- if (!get_videodevice_info) {
- qDebug() << "Load rvc_videocap_get_device_info failed: " << deviceInfoLib->errorString() << endl;
- break;
- }
- get_device_fullpathname = (lpfn_get_videodevice_namefun)deviceInfoLib->resolve("rvc_videocap_get_device_fullpathname");
- if (!get_device_fullpathname) {
- qDebug() << "Load rvc_videocap_get_device_fullpathname failed: " << deviceInfoLib->errorString() << endl;
- break;
- }
- get_videodeviceid = (lpfn_get_videodeviceid)deviceInfoLib->resolve("rvc_videocap_get_video_device_id");
- if (!get_videodeviceid) {
- qDebug() << "Load rvc_videocap_get_video_device_id failed: " << deviceInfoLib->errorString() << endl;
- break;
- }
- result = true;
- } while (false);
- if (!result) {
- if (deviceInfoLib != nullptr) {
- deviceInfoLib->unload();
- delete deviceInfoLib;
- deviceInfoLib = nullptr;
- }
- }
- return result;
- }
- DevMode MainForm::convertIdx2Mode(int idx)
- {
- DevMode result = DevMode::MediaALL;
- switch (idx) {
- case 0:
- result = DevMode::AudioInput;
- break;
- case 1:
- result = DevMode::AudioOutput;
- break;
- case 2:
- result = DevMode::VideoDevice;
- break;
- default:
- break;
- }
- return result;
- }
- void MainForm::modeChanged(int idx)
- {
- ui->deviceBox->clear();
- DevMode mode = convertIdx2Mode(idx);
- if (deviceInfoLib != nullptr || loadExportFunctions()) {
- for (auto& deviceInfo : availableDevices(mode))
- ui->deviceBox->addItem(deviceInfo.name, QVariant::fromValue(deviceInfo));
- ui->deviceBox->setCurrentIndex(0);
- }
- ui->deviceBox->setCurrentIndex(0);
- deviceChanged(0);
- }
- void MainForm::deviceChanged(int idx)
- {
- if (ui->deviceBox->count() == 0)
- return;
- if (idx == -1) {
- int newIdx = 0;
- const int selectIdx = ui->modeBox->currentIndex();
- DevMode mode = convertIdx2Mode(selectIdx);
- QVariant curName = getCurrentActiveDev(mode);
- for (int i = 0; i < ui->deviceBox->count() && curName.isValid(); ++i) {
- AudioDeviceInfo item = ui->deviceBox->itemData(i).value<AudioDeviceInfo>();
- qDebug() << "devicechange: " << item.name << " vs " << curName.toString();
- if (item.name == curName.toString()) {
- newIdx = i;
- break;
- }
- }
- ui->deviceBox->setCurrentIndex(newIdx);
- qDebug() << "pre deviceChanged -1 with " << newIdx;
- deviceChanged(newIdx);
- qDebug() << "return deviceChanged -1 with " << newIdx;
- return;
- }
- qDebug() << "pre pre deviceChanged normal with " << idx;
- ui->deviceBox->itemData(idx).value<AudioDeviceInfo>();
- // device has changed
- qDebug() << "pre deviceChanged normal with " << idx;
- m_deviceInfo = ui->deviceBox->itemData(idx).value<AudioDeviceInfo>();
- qDebug() << "return deviceChanged normal with " << idx;
- }
- void MainForm::on_loadBtn_clicked()
- {
- qDebug() << "Enter push button";
- const int selectIdx = ui->modeBox->currentIndex();
- modeChanged(selectIdx);
- }
- void MainForm::on_saveBtn_clicked()
- {
- const int selectIdx = ui->modeBox->currentIndex();
- DevMode mode = convertIdx2Mode(selectIdx);
- if (!setCurrentActiveDev(mode, m_deviceInfo.name)) {
- QMessageBox::critical(this, "错误", "写入失败!");
- } else {
- //QString content = QString("写入 %1 成功!").arg(m_deviceInfo.name);
- QMessageBox::information(this, "提示", "已应用!");
- }
- }
- QVariant MainForm::getCurrentActiveDev(DevMode mode)
- {
- QString configPath = getRootIniPath();
- QSettings settings(configPath, QSettings::IniFormat);
- settings.setIniCodec("UTF-8");
- QVariant result;
- switch (mode) {
- case AudioInput:
- settings.beginGroup("Audio");
- result = settings.value("handfree_in_dev");
- settings.endGroup();
- break;
- case AudioOutput:
- settings.beginGroup("Audio");
- result = settings.value("handfree_out_dev");
- settings.endGroup();
- break;
- case VideoDevice:
- settings.beginGroup("Video");
- result = settings.value("EnvCamera");
- settings.endGroup();
- break;
- case MediaALL:
- break;
- default:
- break;
- }
- qDebug() << "get " << mode << " " << result;
- return result;
- }
- bool MainForm::setCurrentActiveDev(DevMode mode, QVariant value)
- {
- QString configPath = getRootIniPath();
- QSettings settings(configPath, QSettings::IniFormat);
- settings.setIniCodec("UTF-8");
- QVariant persistentValue(value);
- bool result = false;
- switch (mode) {
- case AudioInput:
- settings.beginGroup("Audio");
- settings.setValue("handfree_in_dev", persistentValue);
- settings.endGroup();
- result = true;
- break;
- case AudioOutput:
- settings.beginGroup("Audio");
- settings.setValue("handfree_out_dev", persistentValue);
- settings.endGroup();
- result = true;
- break;
- case VideoDevice:
- settings.beginGroup("Video");
- settings.setValue("EnvCamera", persistentValue);
- settings.endGroup();
- result = true;
- break;
- case MediaALL:
- break;
- default:
- break;
- }
- if (result) {
- QVariant newValue = getCurrentActiveDev(mode);
- if (newValue != value) {
- qDebug() << "newValue: " << newValue << " vs " << value;
- result = false;
- }
- }
- return result;
- }
- QString MainForm::getRootIniPath()
- {
- if (rootFilePath.isEmpty()) {
- QString appDir = QCoreApplication::applicationDirPath();
- QDir dir(appDir); //bin
- dir.cdUp(); //1.2.3.4
- dir.cdUp(); //version
- dir.cdUp(); //Run
- dir.cd("hardwarecfg");
- QString rootIniPath = dir.absolutePath() + "/root.ini";
- //QFileInfoList rootInis = dir.entryInfoList(QStringList() << "root.ini", QDir::Files);
- qDebug() << "root path at media page: " << rootIniPath;
- rootFilePath = rootIniPath;
- }
- return rootFilePath;
- }
|