Skip to content

Commit b808cb5

Browse files
committed
Make sure InteropContext outlasts all player instances, track MediaPlayer lifetime and only startup/shutdown MediaFoundation when necessary
1 parent 5f8f782 commit b808cb5

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

samples/SimplePlayback/src/SimplePlaybackApp.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace ui = ImGui;
1919
class SimplePlaybackApp : public app::App
2020
{
2121
public:
22-
2322
void setup ( ) override;
2423
void update ( ) override;
2524
void draw ( ) override;
@@ -30,7 +29,7 @@ class SimplePlaybackApp : public app::App
3029
AX::Video::MediaPlayerRef _player;
3130
AX::Video::MediaPlayer::Error _error{ AX::Video::MediaPlayer::Error::NoError };
3231

33-
bool _hardwareAccelerated{ false };
32+
bool _hardwareAccelerated{ true };
3433
gl::TextureRef _texture;
3534
};
3635

src/Win32/AX-MediaPlayerWin32DXGIRenderPath.cxx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ namespace AX::Video
5050
bool _isValid{ false };
5151
};
5252

53+
// @note(andrew): Lazily initialize this but make sure
54+
// it hangs around for the remainer of the application
55+
// so that it outlives any of the players that depend on
56+
// it being alive and valid
57+
58+
static std::unique_ptr<InteropContext> kInteropContext{ nullptr };
5359
InteropContext & InteropContext::Get ( )
5460
{
55-
static InteropContext kInstance;
56-
return kInstance;
61+
if ( !kInteropContext ) kInteropContext.reset ( new InteropContext ( ) );
62+
return *kInteropContext;
5763
}
5864

5965
class DXGIRenderPath::SharedTexture
@@ -218,6 +224,7 @@ namespace AX::Video
218224
{
219225
if ( IsLocked() ) wglDXUnlockObjectsNV ( InteropContext::Get ( ).Handle ( ), 1, &_shareHandle );
220226
wglDXUnregisterObjectNV ( InteropContext::Get ( ).Handle ( ), _shareHandle );
227+
_shareHandle = nullptr;
221228
}
222229
}
223230

src/Win32/AX-MediaPlayerWin32Impl.cxx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ using namespace ci;
2525

2626
namespace
2727
{
28+
static std::atomic_int kNumMediaFoundationInstances = 0;
29+
30+
static void OnMediaPlayerCreated ( )
31+
{
32+
if ( kNumMediaFoundationInstances++ == 0 )
33+
{
34+
MFStartup ( MF_VERSION );
35+
}
36+
}
37+
38+
static void OnMediaPlayerDestroyed ( )
39+
{
40+
if ( --kNumMediaFoundationInstances == 0 )
41+
{
42+
MFShutdown ( );
43+
}
44+
}
45+
2846
class MFCallbackBase : public IMFAsyncCallback
2947
{
3048
public:
@@ -244,7 +262,7 @@ namespace AX::Video
244262
, _source ( source )
245263
, _flags ( flags )
246264
{
247-
MFStartup ( MF_VERSION );
265+
OnMediaPlayerCreated ( );
248266

249267
ComPtr<IMFMediaEngineClassFactory> factory;
250268
if ( SUCCEEDED ( CoCreateInstance ( CLSID_MFMediaEngineClassFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS ( &factory ) ) ) )
@@ -608,12 +626,15 @@ namespace AX::Video
608626

609627
MediaPlayer::Impl::~Impl ( )
610628
{
629+
_renderPath = nullptr;
630+
_hasNewFrame.store ( false );
631+
611632
if ( _mediaEngine )
612633
{
613634
_mediaEngine->Shutdown ( );
614635
_mediaEngine = nullptr;
615636
}
616637

617-
MFShutdown ( );
638+
OnMediaPlayerDestroyed ( );
618639
}
619640
}

0 commit comments

Comments
 (0)