Sign in to follow this  
Aiwendil

DirectShow and TestCooperativeLevel

Recommended Posts

Hi everyone, I'm trying to modify the cutscene sample from MSDN (the problem is that it requires you to enter the file into the command prompt to play the video, and I just want to be able to play the video from in-game without using the command prompt), and I finally got it to the point where it will play a video in my game, but now once the video finishes playing, TestCooperativeLevel fails. Here is the cutscene code:
#include "Main.h"

Cutscene::Cutscene()
{
	mFile = L"";
	mGraphBuilder = NULL;
	mMediaControl = NULL;
	mVideoWindow = NULL;
	mMediaEvent = NULL;
	mContinue = true;
	mSleep = true;
}

Cutscene::~Cutscene()
{
	mContinue = false;

	if (mGraphBuilder)
	{
		if (FAILED(mGraphBuilder->Release()))
		{
			MessageBox(game.mWindowHandle, L"Error releasing GraphBuilder", L"GraphBuilder->Release", MB_OK);
		}
	}

	if (mMediaControl)
	{
		if (FAILED(mMediaControl->Release()))
		{
			MessageBox(game.mWindowHandle, L"Error releasing MediaControl", L"MediaControl->Release", MB_OK);
		}
	}

	if (mVideoWindow)
	{
		if (FAILED(mVideoWindow->Release()))
		{
			MessageBox(game.mWindowHandle, L"Error releasing VideoWindow", L"VideoWindow->Release", MB_OK);
		}
	}

	if (mMediaEvent)
	{
		if (FAILED(mMediaEvent->Release()))
		{
			MessageBox(game.mWindowHandle, L"Error releasing MediaEvent", L"MediaEvent->Release", MB_OK);
		}
	}
}

void Cutscene::create(LPCWSTR file)
{
	mFile = file;
}

HRESULT Cutscene::getInterfaces()
{
	HRESULT hr;

    // Instantiate filter graph interface
    if (FAILED(hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&mGraphBuilder)))
	{
		MessageBox(game.mWindowHandle, L"Error creating graph builder", L"Graph Builder Create", MB_OK);
		return hr;
	}

    // Get interfaces to control playback & screensize
    if (FAILED(hr = mGraphBuilder->QueryInterface(IID_IMediaControl,  (void **)&mMediaControl)))
	{
		MessageBox(game.mWindowHandle, L"Error Media Control", L"Media Control", MB_OK);
		return hr;
	}

    if (FAILED(hr = mGraphBuilder->QueryInterface(IID_IVideoWindow,   (void **)&mVideoWindow)))
	{
		MessageBox(game.mWindowHandle, L"Error Video Window", L"Video Window", MB_OK);
		return hr;
	}

    // Get interface to allow the app to wait for completion of playback
    if (FAILED(hr = mGraphBuilder->QueryInterface(IID_IMediaEventEx,  (void **)&mMediaEvent)))
	{
		MessageBox(game.mWindowHandle, L"Error Media Event", L"Media Event", MB_OK);
		return hr;
	}

	return S_OK;
}

void Cutscene::play()
{
	if (FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
	{
		MessageBox(game.mWindowHandle, L"Error initializing COM", L"COM Initialize", MB_OK);
		return;
	}

	if (FAILED(getInterfaces()))
    {
        CoUninitialize();
		MessageBox(game.mWindowHandle, L"GetInterfaces Failed", L"GetInterfaces Failed", MB_OK);
        return;
    }

	if (FAILED(mGraphBuilder->RenderFile(mFile, NULL)))
	{
		MessageBox(game.mWindowHandle, L"GraphBuilder->RenderFile Failed", L"GraphBuilder->RenderFile Failed", MB_OK);
        return;
	}

	if (FAILED(mVideoWindow->put_MessageDrain((OAHWND)game.mWindowHandle)))
	{
		MessageBox(game.mWindowHandle, L"VideoWindow->put_MessageDrain Failed", L"VideoWindow->put_MessageDrain Failed", MB_OK);
        return;
	}

	if (FAILED(setFullscreen()))
	{
		MessageBox(game.mWindowHandle, L"setFullscreen() Failed", L"setFullscreen()", MB_OK);
		return;
	}

	if (FAILED(mMediaControl->Pause()))
	{
		MessageBox(game.mWindowHandle, L"MediaControl->Pause Failed", L"MediaControl->Pause", MB_OK);
		return;
	}

	if (FAILED(mMediaControl->Run()))
	{
		MessageBox(game.mWindowHandle, L"MediaControl->Run Failed", L"MediaControl->Run", MB_OK);
		return;
	}

	while (mContinue)
	{
		MSG msg;
        long eventCode;
        LONG_PTR param1, param2;

        // Reset sleep flag
        mSleep = true;

        // Has there been a media event?  Look for end of stream condition.
        if(E_ABORT != mMediaEvent->GetEvent(&eventCode, &param1, &param2, 0))
        {
            // Is this the end of the movie?
            if (eventCode == EC_COMPLETE)
            {
               mContinue = false;
               mSleep = false;
            }

            // Free the media event resources
            if (FAILED(mMediaEvent->FreeEventParams(eventCode, param1, param2)))
			{
				MessageBox(game.mWindowHandle, L"MediaEvent->FreeEventParams Failed", L"MediaEvent->FreeEventParams", MB_OK);
				return;
			}
        }

        // Give system threads time to run (and don't sample user input madly)
        if (mSleep)
            Sleep(KEYBOARD_SAMPLE_FREQ);

        // Check and process window messages (like our keystrokes)
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
	}

	mMediaControl->Stop();

	CoUninitialize();
}

HRESULT Cutscene::setFullscreen()
{
	HRESULT hr = S_OK;
	LONG mode;
	static HWND drain = 0;

	if (!mVideoWindow)
		return S_FALSE;

	if (FAILED(hr = mVideoWindow->get_FullScreenMode(&mode)))
	{
		MessageBox(game.mWindowHandle, L"VideoWindow->get_FullScreenMode Error", L"VideoWindow->get_FullScreenMode Error", MB_OK);
		return hr;
	}

	if (mode == 0)
	{
		if (FAILED(hr = mVideoWindow->get_MessageDrain((OAHWND*) &drain)))
		{
			MessageBox(game.mWindowHandle, L"VideoWindow->get_MessageDrain Error", L"VideoWindow->get_MessageDraine Error", MB_OK);
			return hr;
		}

        // Set message drain to application main window
        if (FAILED(hr = mVideoWindow->put_MessageDrain((OAHWND) game.mWindowHandle)))
		{
			MessageBox(game.mWindowHandle, L"VideoWindow->put_MessageDrain Error", L"VideoWindow->put_MessageDrain Error", MB_OK);
			return hr;
		}

        // Switch to full-screen mode
        mode = -1;
        if (FAILED(hr = mVideoWindow->put_FullScreenMode(mode)))
		{
			MessageBox(game.mWindowHandle, L"VideoWindow->put_FullScreenMode Error", L"VideoWindow->put_FullScreenMode Error", MB_OK);
			return hr;
		}
	}

	return hr;
}

And the game code:
Cutscene intro;

void Game::advanceFrame()
{
	if (FAILED(mDevice->TestCooperativeLevel()))
	{
		MessageBox(mWindowHandle, L"TestCooperativeLevel() Failed", L"TestCooperativeLevel() Failed", MB_OK);
		return;
	}

	mDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	mDevice->BeginScene();

	GetCursorPos(&mMousePos);
	ScreenToClient(mWindowHandle, &mMousePos);

	if (mGameState == LoadMainMenu)
	{
		loadMainMenu();
	}
	else if (mGameState == DrawMainMenu)
	{
		drawMainMenu();
	}
	else if (mGameState == LoadGame)
	{
		drawLoadingScreen();
		loadGame();
	}
	else if (mGameState == PlayIntro)
	{
		playIntro();
	}
	else if (mGameState == Playing)
	{
	}
	else if (mGameState == Paused)
	{
	}
	else if (mGameState == ViewCredits)
	{
		drawCredits();
	}
	else if (mGameState == ViewLinks)
	{
		drawLinks();
	}
	else if (mGameState == ViewHowToPlay)
	{
		drawHowToPlay();
	}

	mDevice->EndScene();

	mDevice->Present(NULL, NULL, NULL, NULL);
}

void Game::playIntro()
{
	intro.play();

	mGameState = Playing;
}

The debug runtimes are:
'GAME.exe': Loaded 'C:\Users\...\Documents\Visual Studio 2008\Projects\GAME\Release\GAME.exe', Symbols loaded.
'GAME.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\user32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\secur32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll'
'GAME.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_e163563597edeada\msvcp90.dll'
'GAME.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_e163563597edeada\msvcr90.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\d3d9.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\version.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\d3d8thk.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\d3dx9_35.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\d3d9d.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\oleacc.dll'
Direct3D9: :====> ENTER: DLLMAIN(726fa170): Process Attach: 0000086c, tid=0000120c
Direct3D9: :====> EXIT: DLLMAIN(726fa170): Process Attach: 0000086c
Direct3D9: (INFO) :Direct3D9 Debug Runtime selected.
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atiumdag.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atiumdva.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
Direct3D9: (INFO) :======================= Hal HWVP device selected

Direct3D9: (INFO) :HalDevice Driver Style b

Direct3D9: :Subclassing window 0008047e
Direct3D9: :StartExclusiveMode
Direct3D9: :Window 000100be is on top of us!!
Direct3D9: :Window 000100b6 is on top of us!!
Direct3D9: :Window 000100be is on top of us!!
Direct3D9: :Window 000100b6 is on top of us!!
Direct3D9: :WM_ACTIVATEAPP: BEGIN Activating app pid=0000086c, tid=0000120c
Direct3D9: :*** Active state changing
Direct3D9: :WM_ACTIVATEAPP: DONE Activating app pid=0000086c, tid=0000120c
Direct3D9: (INFO) :Using FF to VS converter

Direct3D9: (INFO) :Using FF to PS converter

'GAME.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\quartz.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll'
'GAME.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.6002.18005_none_5cb72f96088b0de0\comctl32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\dxva2.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\devenum.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\VSFilter.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\comdlg32.dll'
'GAME.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.6001.18000_none_886786f450a74a05\comctl32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\winspool.drv'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\wininet.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\normaliz.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\iertutil.dll'
'GAME.exe': Loaded 'C:\Program Files (x86)\VistaCodecPack\filters\MpaSplitter.ax'
'GAME.exe': Loaded 'C:\Program Files (x86)\VistaCodecPack\filters\OggSplitter.ax'
'GAME.exe': Loaded 'C:\Program Files (x86)\VistaCodecPack\filters\ffdshow.ax', Binary was not built with debug information.
'GAME.exe': Loaded 'C:\Windows\SysWOW64\dinput.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\ddraw.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\dciman32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\ntmarta.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\Wldap32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\psapi.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\samlib.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\d3dim700.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\qdv.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\msvfw32.dll'
'GAME.exe': Loaded 'C:\Windows\SysWOW64\atipdlxx.dll'
'GAME.exe': Unloaded 'C:\Windows\SysWOW64\atipdlxx.dll'
The thread 'Win32 Thread' (0xe24) has exited with code 0 (0x0).
Direct3D9: :WM_ACTIVATEAPP: BEGIN Deactivating app pid=0000086c, tid=0000120c
Direct3D9: :DoneExclusiveMode
Direct3D9: :INACTIVE: 0000086c: Restoring original mode (800x600x22x75) at adapter index 0
Direct3D9: :*** Active state changing
Direct3D9: :WM_ACTIVATEAPP: DONE Deactivating app pid=0000086c, tid=0000120c
The thread 'Win32 Thread' (0x10f0) has exited with code 0 (0x0).
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (WARN) :Window does not have focus. TestCooperativeLevel fails
Direct3D9: :WM_ACTIVATEAPP: Ignoring while minimized
The thread 'Win32 Thread' (0x4b8) has exited with code 0 (0x0).
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (WARN) :Window does not have focus. TestCooperativeLevel fails
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (WARN) :Window does not have focus. TestCooperativeLevel fails
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (WARN) :Window does not have focus. TestCooperativeLevel fails
Direct3D9: (ERROR) :Lost due to display uniqueness change
Direct3D9: (WARN) :Window does not have focus. TestCooperativeLevel fails
Direct3D9: :WM_ACTIVATEAPP: BEGIN Deactivating app pid=0000086c, tid=0000120c
Direct3D9: :*** Already deactivated
Direct3D9: :WM_ACTIVATEAPP: DONE Deactivating app pid=0000086c, tid=0000120c
The program '[2156] GAME.exe: Native' has exited with code 0 (0x0).

The game is currently 1920x1080, and the video file is 800x600. The game works fine up until the player presses play from the main menu. Then the video plays (correctly) through to the end. It is only after the video ends that TestCooperativeLevel fails. I've tried everything I can think of. I've tried using a video that is the same resolution as the game. I feel like I'm missing something obvious here. Do you have any ideas? I'm sorry to post so much code and stuff, but I really need help here. Thanks

Share this post


Link to post
Share on other sites
Does this have something to do with the device getting lost, maybe when focus switches to the video window (if I understand it correctly)?

Also, how would I switch focus back to the game window?

Is there an alternative to DirectShow and video for windows that can play video files?

Please, I really, really need your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this