Use Windows built in MP3 decoder to play audio? - c++

How do I from C or C++ use the MP3 decoder supposedly built in with Windows since Windows Media Player 6.1?
I want to play an mp3 file without having to depend on any other third party library such as for instance LAME.DLL.
I updated the question to better fit the answers I got, since I liked them a lot. Related question.

Sure. Like lots of other things in the Windows API, there's more than one way to go about playing .mp3 files. The "easiest" way to do this programmatically is using DirectShow. The MSDN docs even include a minimal code example on a page aptly called "How To Play a File" to get you started:
// Visual C++ example
#include <dshow.h>
#include <cstdio>
// For IID_IGraphBuilder, IID_IMediaControl, IID_IMediaEvent
#pragma comment(lib, "strmiids.lib")
// Obviously change this to point to a valid mp3 file.
const wchar_t* filePath = L"C:/example.mp3";
int main()
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = ::CoInitialize(NULL);
if (FAILED(hr))
{
::printf("ERROR - Could not initialize COM library");
return 0;
}
// Create the filter graph manager and query for interfaces.
hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
::printf("ERROR - Could not create the Filter Graph Manager.");
return 0;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph.
hr = pGraph->RenderFile(filePath, NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
// Clean up in reverse order.
pEvent->Release();
pControl->Release();
pGraph->Release();
::CoUninitialize();
}
Make sure you read through the DirectShow documentation to get an idea of what's supposed to happen in a proper DirectShow application.
To "feed" media data into a graph, you need to implement a IAsyncReader. Fortunately, the Windows SDK includes a sample that implements an IAsyncReader called CAsyncReader. The sample reads a media file into a memory buffer then uses CAsyncReader to stream the data into the graph. This may be what you want. On my machine the sample is located in the folder C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\multimedia\directshow\filters\async.

You can control an audio channel (in order to make it play anything, including MP3) with mciSendString http://msdn.microsoft.com/en-us/library/ms709492%28VS.85%29.aspx
Here's an example (it's in C#, but it's basically the same principle):
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/152f0149-a62a-446d-a205-91256da7845d
Here is the same principle in C:
http://www.daniweb.com/software-development/c/code/268167

Related

QT Include Windows SDK Library

I am new to Qt and I want to use the AudioSessionManager2 from the Windows SDK. In Qt Creator using this example I've written appended code below.
When compiling I get the messages
undefined reference to `__imp_CoCreateInstance'
undefined reference to `IID_IAudioSessionManager2'
In the documentation for the core audio APIs, Microsoft states the APIs are implemented in Mmdevapi.dll and Audioses.dll. While I hoped to find two matching .lib files the SDK I downloaded with Visual Studio, I only found mmdevapi.lib. After copying it to my project and adding it to the qt project file like below I still had no success with the same error message.
How am I supposed to know which lib files to import for which functions?
How do I get these lib files?
Did I import the lib file correctly?
audiomanager.h:
#ifndef AUDIOMANAGER_H
#define AUDIOMANAGER_H
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include "utils/SafeRelease.h"
class AudioManager {
public:
AudioManager();
~AudioManager();
private:
IAudioSessionManager2 *pSessionManager = nullptr;
HRESULT init();
};
#endif // AUDIOMANAGER_H
audiomanager.cpp
#include "audiomanager.h"
AudioManager::AudioManager () {
this->init();
}
AudioManager::~AudioManager () {
SafeRelease(&pSessionManager);
}
HRESULT AudioManager::init () {
HRESULT hr = S_OK;
// initialize needed vars
IMMDeviceEnumerator *pDeviceEnumerator = nullptr;
IMMDevice *pDevice = nullptr;
// get device enumerator
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator),
nullptr,
CLSCTX_ALL,
IID_PPV_ARGS(&pDeviceEnumerator)
);
if (FAILED(hr))
goto done;
// get the default audio output (for consoles)
// !!! only for consoles? (games, system, etc.), no music, video
pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
if (FAILED(hr))
goto done;
// get the session manager
hr = pDevice->Activate(
IID_IAudioSessionManager2,
CLSCTX_ALL,
nullptr,
reinterpret_cast<void**>(&pSessionManager)
);
done:
SafeRelease(&pDeviceEnumerator);
SafeRelease(&pDevice);
return hr;
}
Line from Qt Project file
LIBS += "$$PWD\..\libs\mmdevapi.lib"
After hours of trying I figured out it would work with
// get the session manager
hr = pDevice->Activate(
__uuidof(IAudioSessionManager2),
CLSCTX_ALL,
nullptr,
reinterpret_cast<void**>(&pSessionManager)
);
instead of
// get the session manager
hr = pDevice->Activate(
IID_IAudioSessionManager2,
CLSCTX_ALL,
nullptr,
reinterpret_cast<void**>(&pSessionManager)
);
I don't know, why I cannot find proper libraries for the IID_IAudioSessionManager, nor why the Microsoft source code example lists this, but the other provided option seems to work.

How to show Video capture Filter dialog (camera settings like brightness, exposure)?

I am doing a windows based app where I use a camera. I want to allow the user to change camera settings (VfwCaptureDialog_Source) as it is common in skype or amcap application. For now, I found out that the dialog is from vfw and it can be activated from dshow api. When I do it from AmCap, it is working. Can you help me to get this code working for given webcam (multiple camera support is required)? I use Qt and OpenCV while working with camera. In opencv just camera index is used to select proper camera. The index is from Qt where can I get nice list of camera names. The error I get from this code is 1170 : "The property set specified does not exist on the object"
//libs -lDxva2 -lstrmiids -lvfw32 -lole32 -loleaut32
#include <Windows.h>
#include "strmif.h"
#include "dshow.h"
#include "Vfw.h"
...
HRESULT hr ;
IGraphBuilder* graph= nullptr;
hr = CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,IID_IGraphBuilder, (void **)&graph );
IMediaControl* ctrl = nullptr;
hr = graph->QueryInterface( IID_IMediaControl, (void **)&ctrl );
IMediaEventEx* mediaEvent=nullptr;
hr = graph->QueryInterface(IID_IMediaEvent, (LPVOID *) &mediaEvent);
ICreateDevEnum* devs = nullptr;
hr = CoCreateInstance (CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &devs);
IEnumMoniker* cams = nullptr;
hr = devs?devs->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &cams, 0):0;
IMoniker* mon = nullptr;
hr = cams?cams->Next (1, &mon, nullptr):0;
IBaseFilter* cam = nullptr;
hr = mon?mon->BindToObject(nullptr,nullptr,IID_IBaseFilter, (void**)&cam):0;
IEnumPins* pins = nullptr;
hr = cam?cam->EnumPins(&pins):0;
IPin* cap = nullptr;
hr = pins?pins->Next(1,&cap, nullptr):0;
IAMVfwCaptureDialogs *pVfw = nullptr;
hr = cap->QueryInterface(IID_IAMVfwCaptureDialogs, (void**)&pVfw);
if (SUCCEEDED(hr))
{
// Check if the device supports this dialog box.
if (S_OK == pVfw->HasDialog(VfwCaptureDialog_Source))
{
// Show the dialog box.
hr = pVfw->ShowDialog(VfwCaptureDialog_Source, HWND(this->winId()));
}
}
else
{
error("cap->QueryInterface");
}
First of all, you skipped a really important part. In your Qt/OpenCV application what is the API used and what exactly you have for given web camera. If it is Video for Windows, then you should look into VFW API on dialog interface. If it is DirectShow then you are basically not interested in VFW dialogs.
Presumably you interact with cameras via DirectShow (well, it does not make much sense to use VFW, esp. for multiple cameras). Then I doubt that AMCap uses exactly the code path you mentioned in the question. Note AMCap source comment:
// we use this interface to bring up the 3 dialogs
// NOTE: Only the VfW capture filter supports this. This app only brings
// up dialogs for legacy VfW capture drivers, since only those have dialogs
hr = gcap.pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video, gcap.pVCap,
IID_IAMVfwCaptureDialogs, (void **)&gcap.pDlg);
With DirectShow you would typically pop up configuration interface with ISpecifyPropertyPages and OleCreatePropertyFrame. AMCap has relevant code for this as well:
else if(id - MENU_DIALOG0 == gcap.iVCapDialogPos)
{
ISpecifyPropertyPages *pSpec;
CAUUID cauuid;
hr = gcap.pVCap->QueryInterface(IID_ISpecifyPropertyPages,
(void **)&pSpec);
if(hr == S_OK)
{
hr = pSpec->GetPages(&cauuid);
hr = OleCreatePropertyFrame(ghwndApp, 30, 30, NULL, 1,
(IUnknown **)&gcap.pVCap, cauuid.cElems,
(GUID *)cauuid.pElems, 0, 0, NULL);
CoTaskMemFree(cauuid.pElems);
pSpec->Release();
}
}
Displaying a Filter's Property Pages on MSDN should be even of more help for you (you already have IBaseFilter interface pointer in your code snippet in the question).

How to play ogg Vorbis files using IGraphBuilder

I need to write a program that can play .ogg Vorbis file with the help of IGraphBuilder or any other windows API directly (in C++/win32 API)?
I tried with IGraphBuilder but that is not working for me.
Sample code:
IMediaControl *pControl = NULL;
IGraphBuilder *pGraph= NULL;
IMediaEventEx *pEvent= NULL;
IMediaPosition *pMediaPosition= NULL;
hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr)) {
return false;
}
hr = pGraph->AddFilter(pFilter, L"Out");
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
hr = pGraph->QueryInterface(IID_IMediaPosition, (void**)&pMediaPosition);
// Build the graph.
hr = pGraph->RenderFile(mFilePath.c_str()/*"C:\\sample.ogg file"*/, NULL);
/* here hr = 0x80040265 so SUCCEEDED(hr) didnt allow it to enter in if condition*/
if (SUCCEEDED(hr)) {
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr)) {
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
}
}
// Clean up in reverse order.
SAFE_RELEASE(pEvent);
SAFE_RELEASE(pControl);
SAFE_RELEASE(pGraph);
pGraph = NULL;
::CoUninitialize();
NOTE:
Above statement hr = pGraph->RenderFile() returned the hr = 0x80040265 and condition if (SUCCEEDED(hr)) doesnt allow to play it.
If I dont use this condition then pControl->Run() gets executed with return ID_OK. But nothing played with speaker.
Please suggest the solution/method.
Pay attention to HRESULT error codes; they mean something. MSDN is often helpful with function-specific error codes like the one you got. (With enough COM programming you'll be able to recognize common ones like E_INVALIDARG by sight.) If not, you can use your header files to pinpoint potential error codes. HRESULTs have a specific format; learn it!
In the case of IGraphBuilder::RenderFile(), that HRESULT maps to VFW_E_UNSUPPORTED_STREAM, which basically means your setup does not support playing Ogg Vorbis files. You will need to install a filter that allows DirectShow to play Ogg Vorbis files, such as the official one from Xiph.Org.

How to play a sound (mp3 / wav) on windows using native API on selected output device

I simply want to be able to create options for my program, so that user can pick which output device will be used to play sounds, like this one in MS Lync:
I originally created my program in Qt and I asked similar (but not identical) question here Qt5+ How to set default audio device for QMediaPlayer
I figured out that Qt is too much bugged for this and this is impossible, so I lowered my requirements and I will use native windows API as these are probably only solution here. This unfortunately requires rewrite of some parts of my program, and now I am following this guide on msdn: https://msdn.microsoft.com/en-us/library/windows/desktop/dd371455%28v=vs.85%29.aspx
I basically want to be able to do following:
List all available output devices and display them on preferences form - I already have a working code for that using IMMDeviceEnumerator
Let user pick a device they want to use for output of my program - I already have that part
Create a function, let's call it PlaySound(string path) that if called with path of .wav or .mp3 file would use the preferred IMMDevice and play a file through it - this is what I need help with
Because I was using Qt so far and I have pretty much no idea of MS windows internals, I have no idea how could one take a file stored somewhere on disk and play it using windows API's especially using that selected IMMDevice which user set in their preferences. I was googling and searching through documentation, but I could only work extremely complex and weird solutions, such as https://msdn.microsoft.com/en-us/library/windows/desktop/dd316756%28v=vs.85%29.aspx
I could even find some examples where you can play mp3 file using MCI device, but that didn't really explain how to alter preferred output device, so it isn't very useful for my use.
I understand that low-level API is probably not going to offer some simple "playmyfile" function, but it would be nice to have at least some example of super-simple solution or some tutorial that would play media files using selected output device on windows so that I could use that as a starting reference. I have a working active IMMDevice, now I just need to make it possible to play mp3 / wav files through it.
NOTE: This is not some generic "how to play a sound on windows" question. I need to be able to play that sound on selected audio output device. For my program only (just like MS Lync, VLC media player or any other advanced audio program can). I don't want to change system global preferences (default device etc).
I managed to do that but surprisingly using windows native libraries called "DirectShow" which are primarily designed for video rendering, but can handle audio as well.
How to:
Enumerate output devices
This functions iterates over all audio devices detected by OS and store them in a list.
void Options::Initialize()
{
#ifdef WIN
HRESULT hr;
ICreateDevEnum *pSysDevEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum);
if (FAILED(hr))
return;
IEnumMoniker *pEnumCat = NULL;
hr = pSysDevEnum->CreateClassEnumerator(CLSID_AudioRendererCategory, &pEnumCat, 0);
if (hr == S_OK)
{
// Enumerate the monikers.
IMoniker *pMoniker = NULL;
ULONG cFetched;
while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag);
if (SUCCEEDED(hr))
{
// To retrieve the filter's friendly name, do the following:
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
OutputDevice device;
device.Name = QString((QChar*)varName.bstrVal, wcslen(varName.bstrVal));
Options::devices.append(device);
}
VariantClear(&varName);
pPropBag->Release();
}
pMoniker->Release();
}
pEnumCat->Release();
}
pSysDevEnum->Release();
#endif
}
Create a filter for device that user selected
Iterate over all devices once more and make a filter for that which was selected by user
HRESULT hr;
ICreateDevEnum *pSysDevEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum);
if (FAILED(hr))
{
Error("Failed SystemDeviceEnum");
return;
}
IEnumMoniker *pEnumCat = NULL;
QSettings s;
hr = pSysDevEnum->CreateClassEnumerator(CLSID_AudioRendererCategory, &pEnumCat, 0);
IBaseFilter *pFilter = NULL;
if (hr == S_OK)
{
// Enumerate the monikers.
IMoniker *pMoniker = NULL;
ULONG cFetched;
int i = 0;
while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag);
if (SUCCEEDED(hr))
{
// retrieve the filter's friendly name now
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
QString name = QString((QChar*)varName.bstrVal, wcslen(varName.bstrVal));
if (s.value("d:" + name).toBool())
{
hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**)&pFilter);
// now we got the filter in pFilter so we can play sound using that filter
PlayWin(pFilter, path);
}
}
VariantClear(&varName);
pPropBag->Release();
}
pMoniker->Release();
}
pEnumCat->Release();
}
pSysDevEnum->Release();
Play the sound using the filter for our device
In this function device is pFilter from previous function
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, __uuidof(IGraphBuilder), (void **)&x->pGraph);
if (FAILED(hr))
{
Error("ERROR - Could not create the Filter Graph Manager.");
return;
}
hr = x->pGraph->QueryInterface(IID_IBasicAudio, (void**)&x->pOutput);
if (FAILED(hr))
{
Error("ERROR - Could not create the IBasicAudio.");
return;
}
x->pFlx = device;
if (device)
x->pGraph->AddFilter(device, L"fd");
hr = x->pGraph->QueryInterface(__uuidof(IMediaControl), (void **)&x->pControl);
hr = x->pGraph->QueryInterface(__uuidof(IMediaEvent), (void **)&x->pEvent);
// Build the graph.
hr = x->pGraph->RenderFile(path, NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = x->pControl->Run();
}
else
{
Error("Unable to play: " + QString::fromWCharArray(path));
}
This code on itself is of course not going to compile out of box, but it gives you a clue how to do this, in nutshell:
Retrieve list of all devices and store it somewhere, so that we can create dialog for user
Before we play a sound, we check which device user selected and create a filter for it
We apply the filter to DirectShow BasicAudio which is itself able to play any media file supported by system codecs.
Documentation on msdn: https://msdn.microsoft.com/en-us/library/windows/desktop/dd407292%28v=vs.85%29.aspx

DirectShow DVD playback

I have created a custom allocator/presenter that works fine for playback of normal media files. However, when I use the following code to try to playback a DVD, it fails with a stack overflow exception.
vmr9_ap = new vmr9ap();
HMONITOR monitor = MonitorFromWindow(hwnd, NULL);
IGraphBuilder *graph;
IBaseFilter *filter;
IDvdGraphBuilder *builder;
CoCreateInstance(CLSID_DvdGraphBuilder, NULL, CLSCTX_INPROC_SERVER, IID_IDvdGraphBuilder, reinterpret_cast<void**>(&builder));
CoCreateInstance(::CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast<void**>(&filter));
builder->GetDvdInterface(IID_IVMRFilterConfig9, (void**)&vmr9_config);
vmr9_ap->Initialize(g_pd3dDevice, monitor, vmr9_config);
HRESULT hr = builder->RenderDvdVideoVolume(L"G:\\VIDEO_TS", AM_DVD_SWDEC_PREFER | AM_DVD_VMR9_ONLY, &status);
builder->GetFiltergraph(&graph);
IDvdControl2 *dvdControl;
builder->GetDvdInterface(::IID_IDvdControl2, (void**)&dvdControl);
graph->QueryInterface(::IID_IMediaControl, (void**)&control);
HRESULT h = control->Run();
The stack overflow happens immediately after the call to control->Run(). It's driving me nuts, as I'm sure I'm just forgetting something really really simple.
Thanks.
Your graph should look something like this. Make sure there aren't any buggy filters in your graph.
Because you are using a custom allocator, I would look there for the issue and set some breakpoints there. You code you pasted might be incomplete as I do not see you configure the VMR9 with the custom allocator, nor do I see it being added to the graph. I avoid using the DVDGraphBuilder as I had too difficult of a time getting it RenderVolume correctly with my VMR9+Allocator. I would build the graph a little more manually.
I have a custom allocator in my open source project, along w/ a DVD player. You can check that out for reference, though there is a lot of code noise in there due to me needing to hack a few things in there for WPF compatiblity. http://wpfmediakit.codeplex.com
What you are seeing should NOT be a DRM issue.
alt text http://img29.imageshack.us/img29/7798/capturelu.jpg
Could it be a form of DRM protection? Decoders in DVD graphs will typically try to prevent you building graphs that get access to the uncompressed data as you do here. Normally they do that by a cleaner method, such as refusing to connect to unauthorised renderers, but it's possible that this might be caused by something like that -- certainly there are mpeg-2 decoders which use deliberate crashes to prevent reverse engineering.
G
Thanks to the code Jeremiah Morrill pointed me to, I managed to get playback mostly working.
It works fine, as long as you don't try to resize the D3DImage that it's played in. Devil's in the details, I suppose.
Thanks to all the answers. DVD playback doesn't work with a debugger attached, which according to Google, is not DRM, but is an anti-reverse engineering measure. Could be particular to the DVD codec I'm using too.
extern "C" __declspec(dllexport) LPDIRECT3DSURFACE9 InitializeDvd(HWND hWnd)
{
CoInitialize(NULL);
IPin *dvdVideoOut;
IPin *vmr9VideoIn;
HRESULT hr = S_OK;
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&graph);
if(graph)
{
hr = CoCreateInstance(CLSID_DVDNavigator, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **)&dvdNavigator);
if(dvdNavigator)
{
hr = graph->AddFilter(dvdNavigator, L"DVD Navigator");
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **)&vmr9);
if(vmr9)
{
hr = vmr9->QueryInterface(IID_IVMRFilterConfig9, reinterpret_cast<void**>(&p_fConfig));
p_Ap = new VMR9AllocatorPresenter();
p_Dh = new DeviceHandler();
p_device = p_Dh->Initialize(hWnd);
p_fConfig->SetRenderingMode(VMR9Mode_Renderless);
p_fConfig->SetNumberOfStreams(1);
p_Ap->Initialize(hWnd, p_device, p_fConfig);
if(SUCCEEDED(hr))
{
hr = graph->AddFilter(vmr9, L"Video Mixing Renderer 9");
if(p_fConfig)
{
dvdNavigator->FindPin(L"Video", &dvdVideoOut);
if(dvdVideoOut)
{
hr = graph->Render(dvdVideoOut);
}
hr = graph->QueryInterface(IID_IMediaControl, reinterpret_cast<void**>(&control));
if(control)
{
control->Run();
}
}
}
}
}
}
}
return p_Dh->g_surface9;
}