summaryrefslogtreecommitdiffstats
path: root/plugins/pluginDirectShow/internals/DSUtils.cxx
blob: 913c081de3018c0aeb317dc2be60bc4f0d9aadad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* Copyright (C) 2011-2013 Doubango Telecom <http://www.doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*/
#include "internals/DSUtils.h"

#if defined (_WIN32_WCE)
#include <atlbase.h>
#include <atlstr.h>
#else
#include <atlconv.h>
#include <d3d9.h>
#endif

#include "tsk_debug.h"

HWND GetMainWindow()
{
	HWND hWnd;
	if (!(hWnd = GetActiveWindow())) {
		if (!(hWnd = GetForegroundWindow())) {
#if !defined(_WIN32_WCE)
			if (!(hWnd = GetConsoleWindow())) {
				return NULL;
			}
#endif
		}
	}
	return hWnd;
}

bool IsMainThread()
{	
	HWND hWnd = GetMainWindow();
	if (hWnd) {
		DWORD mainTid = GetWindowThreadProcessId(hWnd, NULL);
		DWORD currentTid = GetCurrentThreadId();
		return (mainTid == currentTid);
	}
	return false;
}

bool IsD3D9Supported()
{
#if defined(_WIN32_WCE)
	return false;
#else
	static bool g_bChecked = false;
	static bool g_bSupported = false;

	if (g_bChecked) {
		return g_bSupported;
	}
	g_bChecked = true;
	HRESULT hr = S_OK;
	IDirect3D9* pD3D = NULL;
	D3DDISPLAYMODE mode = { 0 };
	D3DPRESENT_PARAMETERS pp = {0};
	IDirect3DDevice9* pDevice = NULL;

	if (!(pD3D = Direct3DCreate9(D3D_SDK_VERSION))) {
        hr = E_OUTOFMEMORY;
		goto bail;
    }

    hr = pD3D->GetAdapterDisplayMode(
        D3DADAPTER_DEFAULT,
        &mode
        );
	if (FAILED(hr)) {
		goto bail;
	}

    hr = pD3D->CheckDeviceType(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        mode.Format,
        D3DFMT_X8R8G8B8,
        TRUE    // windowed
        );
	if (FAILED(hr)) {
		goto bail;
	}
    pp.BackBufferFormat = D3DFMT_X8R8G8B8;
    pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	pp.Windowed = TRUE;
	pp.hDeviceWindow = GetDesktopWindow();
    hr = pD3D->CreateDevice(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        pp.hDeviceWindow,
        D3DCREATE_HARDWARE_VERTEXPROCESSING,
        &pp,
        &pDevice
        );
	if (FAILED(hr)) {
		goto bail;
	}

	// Everythings is OK
	g_bSupported = true;
	TSK_DEBUG_INFO("D3D9 supported");

bail:
	if (!g_bSupported) {
		TSK_DEBUG_WARN("D3D9 not supported");
	}
	SAFE_RELEASE(pDevice);
	SAFE_RELEASE(pD3D);
	return g_bSupported;
#endif /* _WIN32_WCE */
}

IPin *GetPin(IBaseFilter *filter, PIN_DIRECTION direction)
{
	IEnumPins	*enumPins = NULL;
	IPin		*pin = NULL;

	HRESULT hr = filter->EnumPins(&enumPins);
	if(!enumPins){
		return NULL;
	}

	for(;;){
		ULONG fetched = 0;
		PIN_DIRECTION pinDir = PIN_DIRECTION(-1);
		pin = NULL;

		if (FAILED(enumPins->Next(1, &pin, &fetched))){
			enumPins->Release();
			return NULL;
		}

		if (fetched == 1 && pin){
			pin->QueryDirection(&pinDir);
			if(pinDir == direction){
				break;
			}
			pin->Release();
		}
	}

	enumPins->Release();
	return pin;
}

HRESULT ConnectFilters(IGraphBuilder *graphBuilder, IBaseFilter *source, IBaseFilter *destination, AM_MEDIA_TYPE *mediaType)
{
	HRESULT hr;

	IPin *outPin = GetPin(source, PINDIR_OUTPUT);
	IPin *inPin = GetPin(destination, PINDIR_INPUT);

	if (mediaType != NULL){
		hr = graphBuilder->ConnectDirect(outPin, inPin, mediaType);
	}
	else{
		hr = graphBuilder->Connect(outPin, inPin);
	}

	SAFE_RELEASE(outPin);
	SAFE_RELEASE(inPin);

	return hr;
}

HRESULT DisconnectFilters(IGraphBuilder *graphBuilder, IBaseFilter *source, IBaseFilter *destination)
{
	HRESULT hr;

	IPin *outPin = GetPin(source, PINDIR_OUTPUT);
	IPin *inPin = GetPin(destination, PINDIR_INPUT);

	if (inPin){
		hr = graphBuilder->Disconnect(inPin);
	}

	if (outPin){
		hr = graphBuilder->Disconnect(outPin);
	}

	SAFE_RELEASE(outPin);
	SAFE_RELEASE(inPin);

	return hr;
}

bool DisconnectAllFilters(IGraphBuilder *graphBuilder)
{
	IEnumFilters* filterEnum = NULL;
	IBaseFilter* currentFilter = NULL;
	ULONG fetched;
	HRESULT hr;

	hr = graphBuilder->EnumFilters(&filterEnum);
	if (FAILED(hr)) {
		SAFE_RELEASE(filterEnum);
		return false;
	}

	while(filterEnum->Next(1, &currentFilter, &fetched) == S_OK){
		hr = DisconnectFilters(graphBuilder, currentFilter, currentFilter);
		SAFE_RELEASE(currentFilter);
	}
	SAFE_RELEASE(filterEnum);
	SAFE_RELEASE(currentFilter);
	return true;
}

bool RemoveAllFilters(IGraphBuilder *graphBuilder)
{
	IEnumFilters* filterEnum = NULL;
	IBaseFilter* currentFilter = NULL;
	ULONG fetched;
	HRESULT hr;

	hr = graphBuilder->EnumFilters(&filterEnum);
	if (FAILED(hr)) return false;

	while(filterEnum->Next(1, &currentFilter, &fetched) == S_OK){
		hr = graphBuilder->RemoveFilter(currentFilter);
		if (FAILED(hr)){
			SAFE_RELEASE(filterEnum);
			return false;
		}
		SAFE_RELEASE(currentFilter);
		filterEnum->Reset();
	}

	SAFE_RELEASE(filterEnum);
	SAFE_RELEASE(currentFilter);
	return true;
}


#include "internals/DSDisplay.h"
#include "internals/DSGrabber.h"

#define WM_CREATE_DISPLAY_ON_UI_THREAD				(WM_USER + 101)
#define WM_CREATE_GRABBER_ON_UI_THREAD				(WM_CREATE_DISPLAY_ON_UI_THREAD + 1)
#define WM_CREATE_ON_UI_THREAD_TIMEOUT		1000

// C Callback that dispatch event to create display on UI thread
static LRESULT CALLBACK __create__WndProcWindow(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	HANDLE* event = reinterpret_cast<HANDLE*>(wParam);
	BOOL* isScreenCast = reinterpret_cast<BOOL*>(GetProp(hWnd, TEXT("screnCast")));

	if(event && lParam){
		switch(uMsg){
			case WM_CREATE_DISPLAY_ON_UI_THREAD:
				{
					HRESULT hr;
					DSDisplay** ppDisplay = reinterpret_cast<DSDisplay**>(lParam);
					*ppDisplay = new DSDisplay(&hr);
					SetEvent(event);
					break;
				}
			case WM_CREATE_GRABBER_ON_UI_THREAD:
				{
					HRESULT hr;
					DSGrabber** ppGrabber = reinterpret_cast<DSGrabber**>(lParam);
					*ppGrabber = new DSGrabber(&hr, *isScreenCast);
					SetEvent(event);
					break;
				}
		}
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int createOnCurrentThead(HWND hWnd, void** ppRet, BOOL display, BOOL screnCast)
{
	HRESULT hr;
	if(display) *ppRet = new DSDisplay(&hr);
	else *ppRet = new DSGrabber(&hr, screnCast);
	if(FAILED(hr)){
		TSK_DEBUG_ERROR("Failed to created DirectShow %s", display ? "Display" : "Grabber");
		SAFE_DELETE_PTR(*ppRet);
		return -2;
	}
	return 0;
}

int createOnUIThead(HWND hWnd, void** ppRet, BOOL display, BOOL screnCast)
{
	static BOOL __isScreenCastFalse = FALSE;
	static BOOL __isScreenCastTrue = TRUE;
	if(!ppRet){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	if (IsMainThread()) {
		return createOnCurrentThead(hWnd, ppRet, display, screnCast);
	}
	else{
		TSK_DEBUG_INFO("Create DirectShow element on worker thread");
		HANDLE event = NULL;
		int ret = 0;
		DWORD retWait, retryCount = 3;		

		if(!hWnd){
			if (!(hWnd = FindWindow(NULL, TEXT("Boghe - IMS/RCS Client")))) {
				if(!(hWnd = GetMainWindow())){
					TSK_DEBUG_ERROR("No Window handle could be used");
					return -2;
				}
			}
		}
#if defined(_WIN32_WCE)
		WNDPROC wndProc = (WNDPROC) SetWindowLong(hWnd, GWL_WNDPROC, (LONG) __create__WndProcWindow);
#else
		WNDPROC wndProc = (WNDPROC) SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR) __create__WndProcWindow);
#endif
		if (!wndProc) {
			TSK_DEBUG_ERROR("SetWindowLongPtr() failed with errcode=%d", GetLastError());
			return createOnCurrentThead(hWnd, ppRet, display, screnCast);
		}

		if (!(event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
			TSK_DEBUG_ERROR("Failed to create new event");
			ret = -4; goto bail;
		}
		SetProp(hWnd, TEXT("screnCast"), screnCast ? &__isScreenCastTrue : &__isScreenCastFalse);
		if (!PostMessage(hWnd, display ? WM_CREATE_DISPLAY_ON_UI_THREAD : WM_CREATE_GRABBER_ON_UI_THREAD, reinterpret_cast<WPARAM>(event), reinterpret_cast<LPARAM>(ppRet))) {
			TSK_DEBUG_ERROR("PostMessageA() failed");
			ret = -5; goto bail;
		}
		
		do {
			retWait = WaitForSingleObject(event, WM_CREATE_ON_UI_THREAD_TIMEOUT);
		}
		while (retryCount-- > 0 && (retWait == WAIT_TIMEOUT));

	bail:
		// restore
		if (hWnd && wndProc) {
#if defined(_WIN32_WCE)
			SetWindowLong(hWnd, GWL_WNDPROC, (LONG)wndProc);
#else
			SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)wndProc);
#endif
		}
		if (event) {
			CloseHandle(event);
		}

		return ret;
	}
}
OpenPOWER on IntegriCloud