summaryrefslogtreecommitdiffstats
path: root/plugins/pluginDirectShow/internals/DSScreenCastGraph.cxx
blob: b425d652b6a28ba9b2a226b7a8de5bd556fb1f53 (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
#if !defined(_WIN32_WCE)
/* Copyright (C) 2014 Mamadou DIOP
*	
* 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 <streams.h>
#include "internals/DSScreenCastGraph.h"
#include "internals/DSPushSource.h"
#include "internals/DSUtils.h"
#include "internals/DSCaptureUtils.h"

#include "tsk_debug.h"

#include <iostream>

using namespace std;

DSScreenCastGraph::DSScreenCastGraph(ISampleGrabberCB* callback, HRESULT *hr)
: DSBaseCaptureGraph(callback, hr)
{
	this->grabberCallback = callback;

	this->captureFormat = NULL;
	this->captureGraphBuilder = NULL;
	this->graphBuilder = NULL;

	this->sourceFilter = NULL;
	this->sampleGrabberFilter = NULL;

	this->nullRendererFilter = NULL;
	this->grabberController = NULL;
	this->mediaController = NULL;
	this->mediaEventController = NULL;

	this->running = FALSE;
	this->paused = FALSE;

	*hr = this->createCaptureGraph();
}

DSScreenCastGraph::~DSScreenCastGraph()
{
	SAFE_RELEASE(this->mediaEventController);
	SAFE_RELEASE(this->mediaController);
	SAFE_RELEASE(this->grabberController);

	SAFE_RELEASE(this->nullRendererFilter);
	SAFE_RELEASE(this->sampleGrabberFilter);

	SAFE_RELEASE(this->graphBuilder);
	SAFE_RELEASE(this->captureGraphBuilder);

	SAFE_RELEASE(this->sourceFilter);
}

HRESULT DSScreenCastGraph::setParameters(DSCaptureFormat *format, int framerate)
{
	return S_OK;
}

#ifdef _WIN32_WCE
#	include <tinydshow/wce/DSInxbNullFilter.h>
#endif

HRESULT DSScreenCastGraph::connect()
{
	HRESULT hr;

	if (!this->sourceFilter){
		TSK_DEBUG_ERROR("Invalid source filter");
		return E_FAIL;
	}
#if 0
	if (!this->captureFormat){
		TSK_DEBUG_ERROR("Invalid capture format");
		return E_FAIL;
	}
#endif
	
	hr = ConnectFilters(this->graphBuilder, this->sourceFilter, this->sampleGrabberFilter); if(FAILED(hr)) { TSK_DEBUG_ERROR("ConnectFilters failed"); return hr; }
	hr = ConnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter); if(FAILED(hr)) { TSK_DEBUG_ERROR("ConnectFilters failed"); return hr; }

	return hr;
}

HRESULT DSScreenCastGraph::disconnect()
{
	HRESULT hr;

	if (!this->sourceFilter)
	{
		return E_FAIL;
	}
#if 0
	if (!this->captureFormat)
	{
		return E_FAIL;
	}
#endif

	hr = DisconnectFilters(this->graphBuilder, this->sourceFilter, this->sampleGrabberFilter);
	hr = DisconnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);

	return hr;
}

HRESULT DSScreenCastGraph::start()
{
	HRESULT hr;

	if (isRunning() && !isPaused())
	{
		return S_OK;
	}

	hr = this->mediaController->Run();
	if (!SUCCEEDED(hr))
	{
		TSK_DEBUG_ERROR("DSScreenCastGraph::mediaController->Run() has failed with %ld", hr);
		return hr;
	}
	this->running = true;
	return hr;
}

HRESULT DSScreenCastGraph::pause()
{
	HRESULT hr = S_OK;
	if (isRunning())
	{
		hr = this->mediaController->Pause();
		if (SUCCEEDED(hr))
		{
			this->paused = TRUE;
		}
	}
	return hr;
}

HRESULT DSScreenCastGraph::stop()
{
	if (!this->running)
	{
		return S_OK;
	}

	HRESULT hr;
	hr = this->mediaController->Stop();
	if (FAILED(hr))
	{
		TSK_DEBUG_ERROR("DSScreenCastGraph::mediaController->Stop() has failed with %ld", hr);
	}
	this->running = false;
	this->paused = false;
	return hr;
}

bool DSScreenCastGraph::isRunning()
{
	return this->running;
}

bool DSScreenCastGraph::isPaused()
{
	return this->paused;
}

HRESULT DSScreenCastGraph::getConnectedMediaType(AM_MEDIA_TYPE *mediaType)
{
	return this->grabberController->GetConnectedMediaType(mediaType);
}

HRESULT DSScreenCastGraph::createCaptureGraph()
{
	HRESULT hr;

	// Create capture graph builder
	hr = COCREATE(CLSID_CaptureGraphBuilder2, IID_ICaptureGraphBuilder2, this->captureGraphBuilder);
	if(FAILED(hr)) return hr;

	// Create the graph builder
	hr = COCREATE(CLSID_FilterGraph, IID_IGraphBuilder, this->graphBuilder);
	if(FAILED(hr)) return hr;

	// Initialize the Capture Graph Builder.
	hr = this->captureGraphBuilder->SetFiltergraph(this->graphBuilder);
	if(FAILED(hr)) return hr;

	// Create source filter
	LPUNKNOWN pUnk = NULL;
	this->sourceFilter = (CPushSourceDesktop*)CPushSourceDesktop::CreateInstance(pUnk, &hr);
	if(FAILED(hr)) return hr;
	this->sourceFilter->AddRef();

	// Create the sample grabber filter
	hr = COCREATE(CLSID_SampleGrabber, IID_IBaseFilter, this->sampleGrabberFilter);
	if(FAILED(hr)) return hr;

	// Create the NULL renderer
	hr = COCREATE(CLSID_NullRenderer, IID_IBaseFilter, this->nullRendererFilter);
	if(FAILED(hr)) return hr;

	// Add source filter to the graph
	hr = this->graphBuilder->AddFilter(this->sourceFilter, FILTER_SCREENCAST);
	if(FAILED(hr)) return hr;

	// Add sample grabber to the graph
	hr = this->graphBuilder->AddFilter(this->sampleGrabberFilter, FITLER_SAMPLE_GRABBER);
	if(FAILED(hr)) return hr;

	// Add null renderer to the graph
	hr = this->graphBuilder->AddFilter(this->nullRendererFilter, FILTER_NULL_RENDERER);
	if(FAILED(hr)) return hr;

	// Find media control
	hr = QUERY(this->graphBuilder, IID_IMediaControl, this->mediaController);
	if(FAILED(hr)) return hr;

	// Create the sample grabber controller
	hr = QUERY(this->sampleGrabberFilter, IID_ISampleGrabber, this->grabberController);
	if(FAILED(hr)) return hr;

	// Set the sample grabber media type (RGB24)
	// TODO : CHECK
	AM_MEDIA_TYPE mt;
	ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
	mt.majortype = MEDIATYPE_Video;
	mt.subtype = MEDIASUBTYPE_RGB24;
	mt.formattype = FORMAT_VideoInfo;

	hr = this->grabberController->SetMediaType(&mt);
	if(FAILED(hr)) return hr;

	// Set sample grabber media type
	this->grabberController->SetOneShot(FALSE);
	this->grabberController->SetBufferSamples(FALSE);

	hr = this->grabberController->SetCallback(this->grabberCallback, 1);
	if(FAILED(hr)) return hr;

	return hr;
}

#endif /* _WIN32_WCE */
OpenPOWER on IntegriCloud