#include <QComboBox>
#include <QDebug>
#include <QMutex>
#include <QDateTime>
#include <QFile>
#include <QString>
#include "ffmpegDemo.h"
#include <windows.h>
#include <vector>
#include <dshow.h>
#pragma comment(lib, "Strmiids.lib")
#define MAX_FRIENDLY_NAME_LENGTH 128
#define MAX_MONIKER_NAME_LENGTH 256
typedef struct _TDeviceName
{
WCHAR FriendlyName[MAX_FRIENDLY_NAME_LENGTH];// 设备友好名
WCHAR MonikerName[MAX_MONIKER_NAME_LENGTH];// 设备Moniker名
} TDeviceName;
//WCHAR 转换为 Char
char* WCharToChar(WCHAR* s)
{
int w_nlen = WideCharToMultiByte(CP_ACP, 0, s, -1, NULL, 0, NULL, false);
char* ret = new char[w_nlen];
memset(ret, 0, w_nlen);
WideCharToMultiByte(CP_ACP, 0, s, -1, ret, w_nlen, NULL, false);
return ret;
}
//REFGUID guidValue : CLSID_AudioInputDeviceCategory(麦克风); CLSID_AudioRendererCategory(扬声器); //CLSID_VideoInputDeviceCategory(摄像头)
HRESULT GetAudioVideoInputDevices(std::vector<TDeviceName>& vectorDevices, REFGUID guidValue)
{
TDeviceName name;
HRESULT hr;
// 初始化
vectorDevices.clear();
// 初始化COM
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
return hr;
}
// 创建系统设备枚举器实例
ICreateDevEnum* pSysDevEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pSysDevEnum);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
// 获取设备类枚举器
IEnumMoniker* pEnumCat = NULL;
hr = pSysDevEnum->CreateClassEnumerator(guidValue, &pEnumCat, 0);
if (hr == S_OK)
{
// 枚举设备名称
IMoniker* pMoniker = NULL;
ULONG cFetched;
while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag* pPropBag;
hr = pMoniker->BindToStorage(NULL, NULL, IID_IPropertyBag, (void**)&pPropBag);
if (SUCCEEDED(hr))
{
// 获取设备友好名
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName", &varName, NULL);
if (SUCCEEDED(hr))
{
StringCchCopy(name.FriendlyName, MAX_FRIENDLY_NAME_LENGTH, varName.bstrVal);
// 获取设备Moniker名
LPOLESTR pOleDisplayName = reinterpret_cast<LPOLESTR>(CoTaskMemAlloc(MAX_MONIKER_NAME_LENGTH * 2));
if (pOleDisplayName != NULL)
{
hr = pMoniker->GetDisplayName(NULL, NULL, &pOleDisplayName);
if (SUCCEEDED(hr))
{
StringCchCopy(name.MonikerName, MAX_MONIKER_NAME_LENGTH, pOleDisplayName);
vectorDevices.push_back(name);
}
CoTaskMemFree(pOleDisplayName);
}
}
VariantClear(&varName);
pPropBag->Release();
}
pMoniker->Release();
} // End for While
pEnumCat->Release();
}
pSysDevEnum->Release();
CoUninitialize();
return hr;
}
ffmpegDemo::ffmpegDemo(QWidget *parent)
: QMainWindow(parent)
{
qInstallMessageHandler(logMessage);
ui.setupUi(this);
qInfo() << "Start";
av_register_all();
avcodec_register_all();
avdevice_register_all();
AVDeviceInfoList* device_list = NULL;
AVInputFormat* iformat = av_find_input_format("dshow");
avdevice_list_input_sources(av_find_input_format("dshow"), NULL , NULL, &device_list);
if (NULL != device_list) {
int nb_device = device_list->nb_devices;
qInfo() << "number of devices " << nb_device;
if (nb_device > 0) {
for (int i = 0; i < nb_device; i++) {
AVDeviceInfo* info = device_list->devices[i];
if (NULL != info) {
qInfo() << "device " << QString::number(i);
QString dvName = QString::fromUtf8(info->device_name);
qInfo() << "device name : " << dvName;
ui.comboBox->addItem(dvName);
}
else {
}
}
}
}
else {
qInfo() << "null device";
}
//
std::vector<TDeviceName> vectorDevices;
vectorDevices.clear();
//获取摄像头信息
GetAudioVideoInputDevices(vectorDevices, CLSID_VideoInputDeviceCategory);
for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)
{
qInfo() << ("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));
ui.comboBox->addItem(QString::fromStdWString(std::wstring(itor->FriendlyName)));
ui.comboBox->addItem(QString::fromStdWString(std::wstring(itor->MonikerName)));
}
vectorDevices.clear();
//获取麦克风设备信息
GetAudioVideoInputDevices(vectorDevices, CLSID_AudioInputDeviceCategory);
for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)
{
ui.comboBoxAudio->addItem(QString::fromStdWString(std::wstring(itor->FriendlyName)));
ui.comboBoxAudio->addItem(QString::fromStdWString(std::wstring(itor->MonikerName)));
qInfo() << ("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));
}
vectorDevices.clear();
//获取扬声器设备信息
GetAudioVideoInputDevices(vectorDevices, CLSID_AudioRendererCategory);
for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)
{
ui.comboBoxSpeeker->addItem(QString::fromStdWString(std::wstring(itor->FriendlyName)));
ui.comboBoxSpeeker->addItem(QString::fromStdWString(std::wstring(itor->MonikerName)));
//wprintf(L"FriendlyName = %ws, MonikerName = %ws\n", itor->FriendlyName, itor->MonikerName);
qInfo() << ("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));
}
}
void ffmpegDemo::logMessage(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
#if defined(QT_NO_DEBUG) && defined(DISABLE_QDEBUG_RELEASE)
if (type == QtDebugMsg) {
return;
}
#endif
static QMutex mutex;
mutex.lock();
// 设置输出信息格式
#if defined(Q_WS_WIN) || defined(Q_OS_WIN)
#define gettid() (uint32_t)GetCurrentThreadId()
#elif defined(Q_OS_MAC)
#define gettid() (uint32_t)pthread_mach_thread_np(pthread_self())
#else
#define gettid() (uint32_t)syscall(224)
#endif
// 设置输出信息格式
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
QString message = QString("[%1][%2:%3]%4").arg(current_date_time)
.arg(QString(context.file)).arg(context.line).arg(msg);
// 输出信息至文件中(读写、追加形式)
QFile file(qApp->applicationDirPath() + "/log.txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream << message << "\r\n";
file.flush();
file.close();
// 解锁
mutex.unlock();
}