I am new to COM and Windows programming. I am trying to play a mp3 file using media foundation.
Below is the code which I have tried to execute on windows 10. Here is the overview what I did in this code.
I first initialize the COM Then I start the Media Foundation. Then I created media source with the help of source resolver Then I created a MP3 Decoder MFT Then I created a Audio Media Activation object Then I created a Audio Media Sink Then I created a Topology Then I created 3 topology nodes and added Media source, Mp3 Decoder and Audio Media sink to it. Then I added all the topology nodes to the topology Then I created the Media Session and added the topology to media session Then I started the media session. Then I release all the COM objects. Then I shutdown the Media Foundation. Then I uninitialized the COM.
But unfortunately, the file does not start playing.
Please tell what is wrong with my code.
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <Wmcodecdsp.h>
#include <combaseapi.h>
#include <mferror.h>
#include <tchar.h>
#include <evr.h>
int main()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) {
std::cout << "cannot initilize COM";
}
// Initialize the Media Foundation platform.
hr= MFStartup(MF_VERSION);
if (FAILED(hr)) {
std::cout << "cannot initilize MF";
}
MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID;
IMFSourceResolver* pSourceResolver;
IUnknown* pSource = nullptr;
IMFMediaSource* mSource;
IMFPresentationDescriptor* ppPresentationDescriptor;
IMFTransform* mtransform = NULL;
IMFTopology* topology = NULL;
IMFStreamDescriptor* sdesip = NULL;
IMFTopologyNode* source = NULL;
IMFTopologyNode* transform;
IMFTopologyNode* output = NULL;
IMFMediaSink* mediasink = NULL;
IMFMediaTypeHandler* mediatypehandler;
IMFMediaType* mediatype;
IMFMediaType* decodermediatype;
IMFMediaSession* mediasession;
DWORD streamdesip;
hr = MFCreateSourceResolver(&pSourceResolver);
if (FAILED(hr)) {
std::cout << "cannot create source resolver";
}
hr = pSourceResolver->CreateObjectFromURL(
L"song.mp3", // URL of the source.
MF_RESOLUTION_MEDIASOURCE, // Create a source object.
NULL, // Optional property store.
&ObjectType, // Receives the created object type.
&pSource // Receives a pointer to the media source.
);
if (FAILED(hr)) {
std::cout << "media source";
}
hr = pSource->QueryInterface(IID_PPV_ARGS(&mSource));
if (FAILED(hr)) {
std::cout << "media source interface";
}
hr = mSource->CreatePresentationDescriptor(&ppPresentationDescriptor);
if (FAILED(hr)) {
std::cout << "presentation discriptor";
}
ppPresentationDescriptor->SelectStream(0);
hr = CoCreateInstance(__uuidof(CMP3DecMediaObject),
NULL,
CLSCTX_INPROC_SERVER,
IID_IMFTransform,
(void**)&mtransform);
if (FAILED(hr)) {
std::cout << "Decoder";
}
hr = MFCreateTopology(
&topology
);
if (FAILED(hr)) {
std::cout << "topology";
}
hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, &source);
if (FAILED(hr)) {
std::cout << "source topology node";
}
hr = source->SetUnknown(MF_TOPONODE_SOURCE, mSource);
if (FAILED(hr)) {
std::cout << "source set to topology node";
}
hr = source->SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, ppPresentationDescriptor);
if (FAILED(hr)) {
std::cout << "desciption set to topology node";
}
BOOL value;
hr = ppPresentationDescriptor->GetStreamDescriptorByIndex(0, &value, &sdesip);
if (FAILED(hr)) {
std::cout << "stream desciption get";
}
sdesip->GetStreamIdentifier(&streamdesip);
hr = sdesip->GetMediaTypeHandler(&mediatypehandler);
if (FAILED(hr)) {
std::cout << "media handler";
}
hr= mediatypehandler->GetCurrentMediaType(&mediatype);
if (FAILED(hr)) {
std::cout << "current media type";
}
hr = source->SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, sdesip);
if (FAILED(hr)) {
std::cout << "set stream desiptor";
}
hr = topology->AddNode(source);
if (FAILED(hr)) {
std::cout << "add source node";
}
hr = MFCreateTopologyNode(MF_TOPOLOGY_TRANSFORM_NODE, &transform);
if (FAILED(hr)) {
std::cout << "transform node";
}
hr = MFCreateMediaType(&decodermediatype);
if (FAILED(hr)) {
std::cout << "decoder media type";
}
mediatype->CopyAllItems(decodermediatype);
if (FAILED(hr)) {
std::cout << "set major type";
}
hr = decodermediatype->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
if (FAILED(hr)) {
std::cout << "set sub type";
}
hr = mtransform->SetInputType(0, mediatype, NULL);
if (SUCCEEDED(hr)) {
}
IMFMediaType* newmediatype;
hr = MFCreateMediaType(&newmediatype);
if (FAILED(hr)) {
std::cout << "unable to create";
}
hr = mtransform->GetOutputAvailableType(0, 1, &newmediatype);
if (FAILED(hr)) {
std::cout << "nodata";
}
hr = mtransform->SetOutputType(0, newmediatype, 0);
if (FAILED(hr)) {
std::cout << "output";
}
hr = transform->SetObject(mtransform);
if (FAILED(hr)) {
std::cout << "set transform node";
}
hr = topology->AddNode(transform);
if (FAILED(hr)) {
std::cout << "add trasnform node";
}
IMFActivate* active;
hr = MFCreateAudioRendererActivate(&active);
if (FAILED(hr)) {
std::cout << "active object";
}
hr = active->ActivateObject(__uuidof(IMFMediaSink), (void**)&mediasink);
if (FAILED(hr)) {
std::cout << "media sink";
}
hr = MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, &output);
if (FAILED(hr)) {
std::cout << "output node topology";
}
IMFStreamSink* streamsink;
hr = mediasink->GetStreamSinkByIndex(0, &streamsink);
if (FAILED(hr)) {
std::cout << "get stream sink";
}
hr = output->SetObject(streamsink);
if (FAILED(hr)) {
std::cout << "set out put node";
}
hr = output->SetUINT32(MF_TOPONODE_STREAMID, 0);
if (FAILED(hr)) {
std::cout << "uint set";
}
hr = output->SetUINT32(MF_TOPONODE_NOSHUTDOWN_ON_REMOVE, FALSE);
if (FAILED(hr)) {
std::cout << "uint set";
}
hr = topology->AddNode(output);
if (FAILED(hr)) {
std::cout << "add topo node output";
}
hr = source->ConnectOutput(0, transform, 0);
if (FAILED(hr)) {
std::cout << "connect source";
}
hr = transform->ConnectOutput(0, output, 0);
if (FAILED(hr)) {
std::cout << "connect output";
}
hr = MFCreateMediaSession(
NULL,
&mediasession
);
if (FAILED(hr)) {
std::cout << "create media session";
}
hr = mediasession->SetTopology(MFSESSION_SETTOPOLOGY_IMMEDIATE, topology);
if (FAILED(hr)) {
std::cout << "fail to set topology";
}
PROPVARIANT var;
PropVariantInit(&var);
hr = mediasession->Start(&GUID_NULL, &var);
if (FAILED(hr)) {
std::cout << "media session start";
}
pSourceResolver->Release();
pSource->Release();
mSource->Release();
ppPresentationDescriptor->Release();
mtransform->Release();
topology->Release();
sdesip->Release();
source->Release();
transform->Release();
output->Release();
mediatypehandler->Release();
mediatype->Release();
decodermediatype->Release();
mediasession->Release();
mediasink->Release();
MFShutdown();
CoUninitialize();
}