Help reqd with Media Foundation interfaces

Windows specific forum
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Help reqd with Media Foundation interfaces

Post by ozzie »

I want to use Windows Media Foundation in my program, but my knowledge of C++ is very limited so I'm having difficulty working out how to use the IMFMediaSession interface. I have coded the following:

Code: Select all

Interface IMFMediaSession
  ClearTopologies()
  Close()
  GetClock(*ppClock)
  GetFullTopology(dwGetFullTopologyFlags, TopoId, *ppFullTopo)
  GetSessionCapabilities(*pdwCaps)
  Pause()
  SetTopology(dwSetTopologyFlags.l, *pTopology)
  Shutdown()
  Start(*pguidTimeFormat.GUID, *pvarStartPosition)
  Stop()
EndInterface

Prototype.l PR_MFCreateMediaSession(*pConfiguration, *ppMediaSession)
Global MFCreateMediaSession.PR_MFCreateMediaSession
(plus OpenLibary(), GetFunction(), etc)

The documentation states "To obtain a pointer to this interface, call MFCreateMediaSession", so I have written:

Code: Select all

hResult = MFCreateMediaSession(#Null, @ppMS)
where ppMS is a pointer.

My question is, assuming what I've done so far is correct (and it may not be!), how do I then use functions within this object? For example, using the PB Interface structure I should be able issue something like:
\Shutdown()
on the object, but having just a pointer (ppMS) to the object I'm not sure how to do this.

Can anyone advise me?
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Help reqd with Media Foundation interfaces

Post by Thunder93 »

Code: Select all

ppMS.IMFMediaSession

ppMS\Shutdown()
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: Help reqd with Media Foundation interfaces

Post by ozzie »

Thanks, Thunder93 - that looked promising but unfortunately I get 'invalid memory access' on the ppMS\Shutdown(). I'm wondering if I need to do something different as ppMS is a pointer-to-a-pointer, because the C++ definition of MFCreateMediaSession is:

Code: Select all

HRESULT MFCreateMediaSession(
  IMFAttributes *pConfiguration,
  IMFMediaSession **ppMS
);
I've put together a basic trial program which just starts up MF (Media Foundation), creates a session, shuts down the session, and then shuts down MF. If I bypass the session creation and stop, the MF startup and shutdown are successful. If I include the session creation and stop, the session create is successful (but that doesn't use a PB interface function) but the session stop (which does use a PB interface function) crashes with an IMA. Here's the complete code of this trial program:

Code: Select all

EnableExplicit

Interface IMFMediaSession
  ClearTopologies()
  Close()
  GetClock(*ppClock)
  GetFullTopology(dwGetFullTopologyFlags, TopoId, *ppFullTopo)
  GetSessionCapabilities(*pdwCaps)
  Pause()
  SetTopology(dwSetTopologyFlags.l, *pTopology)
  Shutdown()
  Start(*pguidTimeFormat.GUID, *pvarStartPosition)
  Stop()
EndInterface
Global ppMS.IMFMediaSession

Prototype.l PR_MFStartup(version.l, dwFlags.l)
Prototype.l PR_MFShutdown()
Prototype.l PR_MFCreateMediaSession(*pConfiguration, *ppMediaSession)

Global MFStartup.PR_MFStartup
Global MFShutdown.PR_MFShutdown
Global MFCreateMediaSession.PR_MFCreateMediaSession

If OSVersion() >= #PB_OS_Windows_7
  Global MF_SDK_VERSION = $0002
Else ; Vista
  Global MF_SDK_VERSION = $0001
EndIf

#MF_API_VERSION = $0070 ;  This value is unused in the Win7 release and left at its Vista release value
Global MF_VERSION = MF_SDK_VERSION << 16 | #MF_API_VERSION

#MFSTARTUP_NOSOCKET = $1
#MFSTARTUP_LITE = #MFSTARTUP_NOSOCKET
#MFSTARTUP_FULL = 0

Procedure MFControl()
  Protected hResult.l
    
  hResult = MFStartup(MF_VERSION, #MFSTARTUP_FULL)
  Debug "MFStartup returned " + hResult
  
  If 1=1
    hResult = MFCreateMediaSession(#Null, @ppMS)
    Debug "MFCreateMediaSession(#Null, @ppMS) returned " + hResult
    If hResult = #S_OK
      ppMS\Shutdown()
    EndIf
  EndIf

  hResult = MFShutdown()
  Debug "MFShutdown returned " + hResult
    
EndProcedure

Procedure LibraryControl()
  Protected iMFLibrary.i, iMFPlatLibrary.i
  
  iMFPlatLibrary = OpenLibrary(#PB_Any, "mfplat.dll")
  If iMFPlatLibrary
    MFStartup.PR_MFStartup = GetFunction(iMFPlatLibrary, "MFStartup")
    Debug "MFStartup=" + MFStartup
    MFShutdown.PR_MFShutdown = GetFunction(iMFPlatLibrary, "MFShutdown")
    Debug "MFShutdown=" + MFShutdown

    iMFLibrary = OpenLibrary(#PB_Any, "mf.dll")
    If iMFLibrary
      MFCreateMediaSession.PR_MFCreateMediaSession = GetFunction(iMFLibrary, "MFCreateMediaSession")
      Debug "MFCreateMediaSession=" + MFCreateMediaSession
      
      MFControl()
      
      CloseLibrary(iMFLibrary)
    EndIf
    CloseLibrary(iMFPlatLibrary)
  EndIf
  
EndProcedure

LibraryControl()

Debug "End of run"
End
As I mentioned earlier in this posting, I suspect the ** is the cause of the problem. Any ideas how to fix this?
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Help reqd with Media Foundation interfaces

Post by Danilo »

Change

Code: Select all

Interface IMFMediaSession
to:

Code: Select all

Interface IMFMediaSession Extends IUnknown
IUnknown is the base COM interface. Every Windows COM object has this base interface.

You also need to call ppMS\Release() after ppMS\Shutdown():
- MFCreateMediaSession function
The caller must release the interface.
Before releasing the last reference to the IMFMediaSession pointer, the application must call the IMFMediaSession::Shutdown method.
AddRef() and Release() are from COM base interface 'IUnknown' and are used for reference counting
and releasing/freeing the object.
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: Help reqd with Media Foundation interfaces

Post by ozzie »

Thanks Danilo. I now have a clean run, and thanks for the explanation - I've never got my head around COM objects, but it looks like I'll need to!

I can now progress and start adding some useful code to this trial program.
User avatar
robertfern
User
User
Posts: 23
Joined: Fri Feb 02, 2018 10:33 pm
Location: New Jersey

Re: Help reqd with Media Foundation interfaces

Post by robertfern »

Hello Ozzie,

Did you ever get the Media Foundation working.

Some sample code would be greatly appreciated
examples for video and audio would be a great help.

Thanks
Mac OSX Ventura & Windows 10, PB 6.04
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: Help reqd with Media Foundation interfaces

Post by ozzie »

No, unfortunately I never got MF working. I've just revisited the test program I wrote and I get a memory error when I start it, so I must have something wrong in my code.

I ended up using TVideoGrabber (TVG) from Datastead Software. This works well, but I had to create an include file of PB structures, prototypes, etc, for the TVG functions, and have to update this where necessary when a new version of TVG is released. If anyone needs this include file then I'd be happy to share it. Although TVideoGrabber works well, it is not the ultimate video playback library - there are some things it doesn't do well, especially cross-fading, and it doesn't handle still images well. I use the PB 2D drawing library for still images. Regarding audio, I use the BASS audio library.
Post Reply