summaryrefslogtreecommitdiffstats
path: root/plugins/pluginDirectShow/internals/DSDisplayGraph.cxx
blob: 75b00f4a8516fe88e1cb592d0557f14762caa0cc (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* 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.
*/
#if defined(VMR9)
#define DIRECT3D_VERSION 0x0900
#endif

#include "internals/DSDisplayGraph.h"
#include "internals/DSUtils.h"
#include "internals/DSOutputFilter.h"

#include "tsk_debug.h"

#include <iostream>

using namespace std;

DSDisplayGraph::DSDisplayGraph(HRESULT *hr)
{
    this->running = FALSE;
    this->paused = FALSE;
    this->fps = 15;

    this->graphBuilder = NULL;

    this->sourceFilter = NULL;
    this->colorspaceConverterFilter = NULL;
    this->videoRendererFilter = NULL;

    this->mediaController = NULL;
    this->mediaEvent = NULL;
    this->videoWindow = NULL;

#if defined(VMR) ||defined(VMR9) || defined(VMR9_WINDOWLESS)
    this->mixerBitmap = NULL;
    this->filterConfig = NULL;
#endif

#if defined(VMR9_WINDOWLESS)
    this->windowlessControl = NULL;
#endif

    *hr = this->createDisplayGraph();
    if (FAILED(*hr)) {
        return;
    }

    *hr = this->connect();
    if (FAILED(*hr)) {
        return;
    }
}

DSDisplayGraph::~DSDisplayGraph()
{
    this->disconnect();

#if defined(VMR9_WINDOWLESS)
    SAFE_RELEASE(this->windowlessControl);
#endif

#if defined(VMR) ||defined(VMR9) || defined(VMR9_WINDOWLESS)
    SAFE_RELEASE(this->filterConfig);
    SAFE_RELEASE(this->mixerBitmap);
#endif

    SAFE_RELEASE(this->videoWindow);
    SAFE_RELEASE(this->mediaEvent);
    SAFE_RELEASE(this->mediaController);

    SAFE_RELEASE(this->colorspaceConverterFilter);
    SAFE_RELEASE(this->videoRendererFilter);
    //SAFE_RELEASE(this->sourceFilter);

    SAFE_RELEASE(this->graphBuilder);
}

void DSDisplayGraph::setDisplayFps(int fps_)
{
    this->fps = fps_;
    if(this->sourceFilter) {
        this->sourceFilter->setFps(fps_);
    }
}

bool DSDisplayGraph::getImageFormat(UINT &width, UINT &height)
{
    if(this->sourceFilter) {
        return this->sourceFilter->getImageFormat(width, height);
    }
    return false;
}

bool DSDisplayGraph::setImageFormat(UINT width, UINT height)
{
    bool ret = true;
    if(this->sourceFilter) {
        UINT w=width, h = height;
        if(this->sourceFilter->getImageFormat(w, h)) {
            if(w!= width || h!=height) { // Image format has changed
                bool reconnect = this->connected; // IMPORTANT: Must reconnect all elements
                HRESULT hr;
                if(reconnect) {
                    if((hr = this->disconnect()) != S_OK) {
                        return false;
                    }
                }
                ret = (this->sourceFilter->setImageFormat(width, height) == S_OK);
                if(reconnect) {
                    if((hr = this->connect())) {
                        return false;
                    }
                }
            }
        }
    }
    return ret;
}

HRESULT DSDisplayGraph::connect()
{
    HRESULT hr;

    if((hr = ConnectFilters(this->graphBuilder, this->sourceFilter, this->colorspaceConverterFilter)) != S_OK) {
        TSK_DEBUG_ERROR("Failed to connect sourcefilter with the colorspace");
        return hr;
    }
    if((hr = ConnectFilters(this->graphBuilder, this->colorspaceConverterFilter, this->videoRendererFilter)) != S_OK) {
        TSK_DEBUG_ERROR("Failed to connect colorspace with the videorenderer");
        return hr;
    }

    this->connected = true;
    return S_OK;
}

HRESULT DSDisplayGraph::disconnect()
{
    HRESULT hr;

    if((hr = DisconnectFilters(this->graphBuilder, this->sourceFilter, this->colorspaceConverterFilter)) != S_OK) {
        TSK_DEBUG_ERROR("Failed to disconnect sourcefilter with the colorspace");
        return hr;
    }
    if((hr = DisconnectFilters(this->graphBuilder, this->colorspaceConverterFilter, this->videoRendererFilter)) != S_OK) {
        TSK_DEBUG_ERROR("Failed to connect colorspace with the videorenderer");
        return hr;
    }

    this->connected = false;
    return S_OK;
}

HRESULT DSDisplayGraph::start()
{
    HRESULT hr;
    this->running = true;
    this->sourceFilter->reset();

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

HRESULT DSDisplayGraph::pause()
{
    HRESULT hr = S_OK;
    if(isRunning() && !isPaused()) {
        hr = this->mediaController->Pause();
        if(SUCCEEDED(hr)) {
            this->paused = true;
        }
    }
    return hr;
}

HRESULT DSDisplayGraph::stop()
{
    HRESULT hr;

    hr = this->mediaController->Pause();
    if (hr == S_FALSE) {
        TSK_DEBUG_ERROR("DSDisplayGraph::mediaController->Pause() has failed with %ld. Waiting for transition.", hr);
        FILTER_STATE pfs;
        hr = this->mediaController->GetState(2500, (OAFilterState*) &pfs);
    }

    hr = this->mediaController->Stop();
    if (!SUCCEEDED(hr)) {
        TSK_DEBUG_ERROR("DSDisplayGraph::mediaController->Stop() has failed with %ld", hr);
    }

    this->running = false;
    this->paused = false;

    return hr;
}

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

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

void DSDisplayGraph::handleFrame(const void* data, int w, int h)
{
    HRESULT hr;

    if(!this->sourceFilter) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return;
    }

    if(!data || !this->running) {
        this->sourceFilter->setBuffer(NULL, (w*h*3));
        return;
    }

    hr = this->sourceFilter->setImageFormat(w, h);
    if (hr == S_OK) {
        this->stop();

        this->disconnect();
        this->connect();

        this->start();
    }

    this->sourceFilter->setBuffer((void*)data, (w*h*3));
}

HRESULT DSDisplayGraph::createDisplayGraph()
{
    HRESULT hr;

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


    // Create my custom filter
    LPUNKNOWN pUnk = NULL;
    this->sourceFilter = new DSOutputFilter(pUnk, &hr /*, this*/);
    if(FAILED(hr) || this->sourceFilter == NULL) {
        return hr;
    }

    // Create the color space convertor filter
    hr = COCREATE(CLSID_Colour, IID_IBaseFilter, this->colorspaceConverterFilter);
    if(FAILED(hr)) {
        return hr;
    }

#if defined(VMR)
    // Create the video mixing renderer based on Direct X
    hr = COCREATE(CLSID_VideoMixingRenderer, IID_IBaseFilter, this->videoRendererFilter);
    if(FAILED(hr)) {
        return hr;
    }
#elif defined(VMR9) || defined(VMR9_WINDOWLESS)
    // Create the video mixing renderer based on Direct X 9.0
    hr = COCREATE(CLSID_VideoMixingRenderer9, IID_IBaseFilter, this->videoRendererFilter);
    if(FAILED(hr)) {
        return hr;
    }
#else
    // Create the video renderer
    hr = COCREATE(CLSID_VideoRenderer, IID_IBaseFilter, this->videoRendererFilter);
    if(FAILED(hr)) {
        return hr;
    }
#endif


    // Add dource filter to the graph
    hr = this->graphBuilder->AddFilter(this->sourceFilter, FILTER_OUTPUT);
    if(FAILED(hr)) {
        return hr;
    }

    // Add the color space convertor to the graph
    hr = this->graphBuilder->AddFilter(this->colorspaceConverterFilter, FILTER_COLORSPACE_CONVERTOR);
    if(FAILED(hr)) {
        return hr;
    }

    // Add video renderer to the graph
    hr = this->graphBuilder->AddFilter(this->videoRendererFilter, FILTER_VIDEO_RENDERER);
    if(FAILED(hr)) {
        return hr;
    }


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

    // Find media event
    hr = QUERY(this->graphBuilder, IID_IMediaEventEx, this->mediaEvent);
    if(FAILED(hr)) {
        return hr;
    }
    // hr = this->mediaEvent->SetNotifyFlags(AM_MEDIAEVENT_NONOTIFY);


#if defined(VMR)
    // Find the bitmap mixer (Direct X)
    hr = QUERY(this->videoRendererFilter, IID_IVMRMixerBitmap, this->mixerBitmap);
    if(FAILED(hr)) {
        return hr;
    }

    // Find the bitmap configurer (Direct X)
    hr = QUERY(this->videoRendererFilter, IID_IVMRFilterConfig, this->filterConfig);
    if(FAILED(hr)) {
        return hr;
    }

    // Set the number of streams (Direct X)
    hr = this->filterConfig->SetNumberOfStreams(1);
    if(FAILED(hr)) {
        return hr;
    }
#elif defined(VMR9) || defined(VMR9_WINDOWLESS)
    // Find the bitmap mixer (Direct X 9.0)
    hr = QUERY(this->videoRendererFilter, IID_IVMRMixerBitmap9, this->mixerBitmap);
    if(FAILED(hr)) {
        return hr;
    }

    // Find the bitmap configurer (Direct X 9.0)
    hr = QUERY(this->videoRendererFilter, IID_IVMRFilterConfig9, this->filterConfig);
    if(FAILED(hr)) {
        return hr;
    }

    // Set the number of streams (Direct X 9.0)
    hr = this->filterConfig->SetNumberOfStreams(1);
    if(FAILED(hr)) {
        return hr;
    }
#endif

#if defined(VMR9_WINDOWLESS)
    // Set the rendering mode (Direct X 9.0)
    hr = this->filterConfig->SetRenderingMode(VMR9Mode_Windowless);
    if(FAILED(hr)) {
        return hr;
    }

    // Find the windowless control (Direct X 9.0)
    hr = QUERY(this->videoRendererFilter, IID_IVMRWindowlessControl9, this->windowlessControl);
    if(FAILED(hr)) {
        return hr;
    }
#else
    // Find IVideoWindow interface
    hr = QUERY(this->graphBuilder, IID_IVideoWindow, this->videoWindow);
    if(FAILED(hr)) {
        return  hr;
    }
#endif

    return hr;
}
OpenPOWER on IntegriCloud