Play Audio Stream Using Windows APIs in C++ - c++

To play an .mp3 file in Windows using (in this case) DirectShow you only need:
#include <dshow.h>
#include <cstdio>
// For IID_IGraphBuilder, IID_IMediaControl, IID_IMediaEvent
#pragma comment(lib, "strmiids.lib")
const wchar_t* filePath = L"C:/Users/Public/Music/Sample Music/Sleep Away.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();
}
I can't find a way to have something like this, but to be able to play an .asx instead, like for example: http://listen.radiotunes.com/public5/solopiano.asx
In MSDN I can only find ways to do this in C# making a Forms application and inserting a WindowsMediaPlayer control in a form.
Any ideas?

An .asx file is actually a playlist. See here some information about the format.
.asx is not supported by DirectShow. See here for the supported formats.
You might parse the file, as it is XML, and find the actual URL of the stream, and then play it, or you could use the Windows Media Player SDK. You can see some sample code for WM SDK here.

OK, got it to work with this example taken from here and adding this extra line: hr = spPlayer->put_URL(L"http://listen.radiotunes.com/public5/solopiano.asx");:
#include "atlbase.h"
#include "atlwin.h"
#include "wmp.h"
#include <cstdio>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
HRESULT hr = S_OK;
CComBSTR bstrVersionInfo; // Contains the version string.
CComPtr<IWMPPlayer> spPlayer; // Smart pointer to IWMPPlayer interface.
hr = spPlayer.CoCreateInstance(__uuidof(WindowsMediaPlayer), 0, CLSCTX_INPROC_SERVER);
if (SUCCEEDED(hr))
{
hr = spPlayer->get_versionInfo(&bstrVersionInfo);
hr = spPlayer->put_URL(L"http://listen.radiotunes.com/public5/solopiano.asx");
}
if (SUCCEEDED(hr))
{
// Show the version in a message box.
COLE2T pStr(bstrVersionInfo);
MessageBox(NULL, (LPCSTR)pStr, _T("Windows Media Player Version"), MB_OK);
}
// Clean up.
spPlayer.Release();
CoUninitialize();
return 0;
}

Related

How to play mp4 movie with DirectShow

Directshow can't play mp4 default.
I found GDCL MPEG-4 and it seems to decode mp4 file.
But sample is not found.
Could anyone tell me how to play mp4 with directshow using GDCL MPEG-4.
I have simple directshow sample below.
#include <iostream>
#include <dshow.h>
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 1;
}
// 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 1;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void**)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"..\\BigBuckBunny360.wmv", 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.
}
else
{
std::cout << std::system_category().message(hr) << std::endl;
}
}
else
{
std::cout << std::system_category().message(hr) << std::endl;
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
return 0;
}
I want to know usage of GDCL MPEG-4 with upper sample.

Create borderless DirectShow Window

I am referring to this simple video player using DirectShow example provided here.
The code runs and builds however I need to make the video player window borderless.
I could not find a parameter to set in the CoCreateInstance function or the CoInitialize function which I can set to make the window borderless.
Here is my complete code for reference:
#include <dshow.h>
#pragma comment (lib, "strmiids.lib")
void main(void)
{
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;
}
// 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;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"D:\\dfs.avi", 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.
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
What additional piece of code do I need to add in order to achieve this?
I found this code here which does the job!
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "quartz.lib")
#include <string>
#include <dshow.h>
int PlayVideo(const std::string& pFile)
{
IGraphBuilder* pGraph = NULL;
IVideoWindow* pWin = NULL;
IMediaControl* pControl = NULL;
IMediaEvent* pEvent = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) return 1;
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr)) return 1;
// Get interfaces
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pWin);
// Build the graph (convert unicode filename)
size_t _size = mbstowcs(NULL,pFile.c_str(),2); // Add 2 for ZT
wchar_t* _wfile = new wchar_t[_size + 2];
_size = mbstowcs(_wfile, pFile.c_str(), pFile.length() + 1); // Add 1 for ZT
hr = pGraph->RenderFile(_wfile, NULL);
delete[] _wfile;
// Uncomment next line for borderless window display
pWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
// For fullscreen play, get Windows screen parameters and replace
pWin->SetWindowPosition(0, 0, 800, 600);
if (SUCCEEDED(hr))
{
hr = pControl->Run();
if (SUCCEEDED(hr))
{
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
}
}
pControl->Release();
pEvent->Release();
pWin->Release();
pGraph->Release();
CoUninitialize();
return 0;
}
int main()
{
PlayVideo("D:\\dfs.avi"); // play any format Windows can handle, avi/mpg etc.
return 0;
}

DShow Sample Code for playing video does not play the video MSDN C++

I was referring to the example program provided here to run a video file, in my case mp4 format using DShow.
Refer to the complete code:
#include <dshow.h>
#pragma comment (lib, "strmiids.lib")
void main(void)
{
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;
}
// 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;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"C:\\Example.avi", 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.
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
However when I build the program, the build is sucessful but when I run it, the console window pops up and disappears within a second.
I referred to the comments on the same page and many others faced the same issue. However a few were able to successfully run the program.
What is it that I am doing wrong?
Is it the type of project I am selecting? I am selecting win32 console application. Should I select something else? Or is there something else that I am doing wrong?

How to use DirectShow and webcam to preview an image

I'm trying to use C++ and DirectShow to display my webcam... but I'm having some troubles.
The following code gives me a segmentation fault on:
m_pDF->EnumPins(&pinEnum);
my cpp code:
#include <tchar.h>
#include <strsafe.h>
#include <dshow.h>
#include <atlbase.h>
#include <d3d9.h>
#include <vmr9.h>
#pragma comment(lib,"Strmiids.lib")
#define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
{
// Create the System Device Enumerator.
ICreateDevEnum *pDevEnum; // Video and Audio interface object
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); // Creates the system device enumerator
if (SUCCEEDED(hr))
{
// Create an enumerator for the category.
hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0); // Enumeration of 'category' objects
if (hr == S_FALSE)
{
hr = VFW_E_NOT_FOUND; // The category is empty. Treat as an error.
}
pDevEnum->Release(); // Deletes Enumeration object
}
return hr;
}
void main(void)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL; // contains methods for stopping and starting the graph
IMediaEvent *pEvent = NULL; // methods for getting events from the Filter Graph Manager
IPin *m_pCamOutPin;
IBaseFilter *m_pDF=NULL;
IMoniker *pM;
IEnumMoniker *pEnum; // Enumerator object
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return;
}
// 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;
}
// Bind
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
pEnum->Next(1, &pM, NULL);
pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);
pM->Release();
hr=pGraph->AddFilter(m_pDF, L"Video Capture");
CComPtr<IEnumPins> pinEnum;
m_pDF->EnumPins(&pinEnum);
hr = pinEnum->Reset();
hr = pinEnum->Next(1, &m_pCamOutPin, NULL);
if (FAILED(hr))
return;
// control
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->Render(m_pCamOutPin);
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.
}
}
SAFE_RELEASE(pControl);
SAFE_RELEASE(pEvent);
SAFE_RELEASE(pGraph);
CoUninitialize();
}
Any ideas on what am I doing wrong and what do I have to do to get this to work?
m_pDF is a pointer to IBaseFilter:
IBaseFilter *m_pDF=NULL;
BindToObject last parameter is a pointer to a pointer. Which means this functioncall is wrong:
pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);
Instead you need to pass the address of m_pDF to get a pointer to a pointer:
pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&m_pDF);
It is not a problem to cast IBaseFilter** to void**, but you can't cast IBaseFilter* to void**.
The crash should be related m_pDF, it not be a valid pointer.
you should add check for pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF); to know that m_pDF is initialize sucess or not.
and you should also check hr=pGraph->AddFilter(m_pDF, L"Video Capture"); hr is ok or not.
if fail, you can get the error code know the reason.

Make media loop many times with direct show c++

I am using direct show to play media files within my program, and i have been able to play a media file a single time using the following code.
void main()
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
}
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
hr = pGraph->RenderFile(L"C:\\Example.mp3", NULL);
if (SUCCEEDED(hr))
{
hr = pControl->Run();
if (SUCCEEDED(hr))
{
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
return;
}
My question then becomes how would i make the file continue to play and repeat itself a given number of times, or infinitely if i so desired. IMediaControl has no member named loop.
There is no built in way to do this. Once way of implementing this is that instead of exiting you function when you get the completion event, you seek to the beginning of the file an start playback again.
The following link on event handling may help: Learning When an Event Occurs. EC_COMPLETE event/notification is issued when streaming is complete.