summaryrefslogtreecommitdiffstats
path: root/branches/1.0/tinyDSHOW/src/DSUtils.cxx
blob: 736984bac813eb36187b8b8a3fce9a1e2ca0be78 (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
/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)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 <tinydshow/DSUtils.h>

#include <atlbase.h>
#include <atlstr.h>

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

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)
{
	CComPtr<IEnumFilters> filterEnum = NULL;
	CComPtr<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 = DisconnectFilters(graphBuilder, currentFilter, currentFilter);
	}

	filterEnum.Release();
	return true;
}

bool RemoveAllFilters(IGraphBuilder *graphBuilder)
{
	CComPtr<IEnumFilters> filterEnum = NULL;
	CComPtr<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)){
			filterEnum.Release();
			return false;
		}
		currentFilter.Release();
		filterEnum->Reset();
	}

	filterEnum.Release();
	return true;
}
OpenPOWER on IntegriCloud