summaryrefslogtreecommitdiffstats
path: root/plugins/pluginWinMF/internals/mf_codec_topology.cxx
blob: 1ee2a1669e3f48c64c38d832f671cd238d6263d3 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Copyright (C) 2013 Mamadou DIOP
* Copyright (C) 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 "mf_codec_topology.h"
#include "mf_utils.h"

#include "tsk_debug.h"

//
//	 MFCodecTopologySampleGrabberCB
//

class MFCodecTopologySampleGrabberCB : public IMFSampleGrabberSinkCallback 
{
    long m_cRef;
    MFCodecTopology *m_pCodecTopology;

    MFCodecTopologySampleGrabberCB(MFCodecTopology *pCodecTopology)
		: m_cRef(1)
	{
		m_pCodecTopology = pCodecTopology;
		m_pCodecTopology->AddRef();
	}
	virtual ~MFCodecTopologySampleGrabberCB()
	{
		SafeRelease(&m_pCodecTopology);
	}

public:
    // Create a new instance of the object.
	static HRESULT MFCodecTopologySampleGrabberCB::CreateInstance(MFCodecTopology *pCodecTopology, MFCodecTopologySampleGrabberCB **ppCB)
	{
		*ppCB = new (std::nothrow) MFCodecTopologySampleGrabberCB(pCodecTopology);

		if (ppCB == NULL)
		{
			return E_OUTOFMEMORY;
		}
		return S_OK;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::QueryInterface(REFIID riid, void** ppv)
	{
		static const QITAB qit[] = 
		{
			QITABENT(MFCodecTopologySampleGrabberCB, IMFSampleGrabberSinkCallback),
			QITABENT(MFCodecTopologySampleGrabberCB, IMFClockStateSink),
			{ 0 }
		};
		return QISearch(this, qit, riid, ppv);
	}

	STDMETHODIMP_(ULONG) MFCodecTopologySampleGrabberCB::AddRef()
	{
		return InterlockedIncrement(&m_cRef);
	}

	STDMETHODIMP_(ULONG) MFCodecTopologySampleGrabberCB::Release()
	{
		ULONG cRef = InterlockedDecrement(&m_cRef);
		if (cRef == 0)
		{
			delete this;
		}
		return cRef;

	}

	// IMFClockStateSink methods

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnClockStart(MFTIME hnsSystemTime, LONGLONG llClockStartOffset)
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnClockStart(%lld, %lld)", hnsSystemTime, llClockStartOffset);
		return S_OK;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnClockStop(MFTIME hnsSystemTime)
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnClockStop(%lld)", hnsSystemTime);
		return S_OK;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnClockPause(MFTIME hnsSystemTime)
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnClockPause(%lld)", hnsSystemTime);
		return S_OK;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnClockRestart(MFTIME hnsSystemTime)
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnClockRestart(%lld)", hnsSystemTime);
		return S_OK;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnClockSetRate(MFTIME hnsSystemTime, float flRate)
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnClockSetRate(%lld, %f)", hnsSystemTime, flRate);
		return S_OK;
	}

	// IMFSampleGrabberSink methods.

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnSetPresentationClock(IMFPresentationClock* pClock)
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnSetPresentationClock");
		return S_OK;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnProcessSample(
		REFGUID guidMajorMediaType, DWORD dwSampleFlags,
		LONGLONG llSampleTime, LONGLONG llSampleDuration, const BYTE * pSampleBuffer,
		DWORD dwSampleSize)
	{
		HRESULT hr = S_OK;
		IMFSample *pSample = NULL;
		IMFMediaBuffer* pMediaBuffer = NULL;
		BYTE* _pcBufferPtr = NULL;

		CHECK_HR(hr = MFUtils::CreateMediaSample(dwSampleSize, &pSample));
		CHECK_HR(hr = pSample->SetSampleTime(llSampleTime));
		CHECK_HR(hr = pSample->SetSampleDuration(llSampleDuration));
		CHECK_HR(hr = pSample->GetBufferByIndex(0, &pMediaBuffer));
		CHECK_HR(hr = pMediaBuffer->Lock(&_pcBufferPtr, NULL, NULL));
		memcpy(_pcBufferPtr, pSampleBuffer, dwSampleSize);
		CHECK_HR(hr = pMediaBuffer->SetCurrentLength(dwSampleSize));
		CHECK_HR(hr = pMediaBuffer->Unlock());

		m_pCodecTopology->m_SampleQueue.Queue(pSample); // thread-safe
		
bail:
		SafeRelease(&pSample);
		SafeRelease(&pMediaBuffer);
		return hr;
	}

	STDMETHODIMP MFCodecTopologySampleGrabberCB::OnShutdown()
	{
		TSK_DEBUG_INFO("MFCodecTopologySampleGrabberCB::OnShutdown");
		return S_OK;
	}
};

//
//	 MFCodecTopology
//


MFCodecTopology::MFCodecTopology(MFCodec* pCodec, HRESULT &hr)
: m_nRefCount(1)
, m_bInitialized(FALSE)
, m_bStarted(FALSE)
, m_pCodec(NULL)
, m_pSource(NULL)
, m_pSession(NULL)
, m_pTopologyFull(NULL)
, m_pTopologyPartial(NULL)
, m_pOutputType(NULL)
, m_pInputType(NULL)
, m_pGrabberCallback(NULL)
, m_pGrabberActivate(NULL)
, m_pTread(NULL)
{
	hr = S_OK;

	if(!pCodec)
	{
		CHECK_HR(hr = E_POINTER);
	}

	m_pCodec = pCodec;
	m_pCodec->AddRef();

bail: ;
}

MFCodecTopology::~MFCodecTopology()
{
	DeInitialize();
}

ULONG MFCodecTopology::AddRef()
{
    return InterlockedIncrement(&m_nRefCount);
}

ULONG  MFCodecTopology::Release()
{
    ULONG uCount = InterlockedDecrement(&m_nRefCount);
    if (uCount == 0)
    {
        delete this;
    }
    // For thread safety, return a temporary variable.
    return uCount;
}

HRESULT MFCodecTopology::QueryInterface(REFIID iid, void** ppv)
{
	return E_NOTIMPL;
}

HRESULT MFCodecTopology::Start()
{
	HRESULT hr = S_OK;

	if(m_bStarted)
	{
		return S_OK;
	}

	if(!m_bInitialized)
	{
		CHECK_HR(hr = E_FAIL);
	}

	CHECK_HR(hr = MFUtils::RunSession(m_pSession, m_pTopologyFull));

	// Start asynchronous watcher thread
	m_bStarted = TRUE;
	int ret = tsk_thread_create(&m_pTread, MFCodecTopology::RunSessionThread, this);
	if(ret != 0)
	{
		TSK_DEBUG_ERROR("Failed to create thread");
		m_bStarted = FALSE;
		if(m_pTread)
		{
			tsk_thread_join(&m_pTread);
		}
		MFUtils::ShutdownSession(m_pSession, m_pSource);
		CHECK_HR(hr = E_FAIL);
	}

	// FIXME
	Sleep(2000);

bail:
	return hr;
}

HRESULT MFCodecTopology::Stop()
{
	HRESULT hr = S_OK;

	if(!m_bStarted)
	{
		return S_OK;
	}

	m_bStarted = FALSE;
    hr = MFUtils::ShutdownSession(m_pSession, NULL); // stop session to wakeup the asynchronous thread
    if(m_pTread)
	{
        tsk_thread_join(&m_pTread);
    }
    hr = MFUtils::ShutdownSession(NULL, m_pSource);
	
	return hr;
}

HRESULT MFCodecTopology::Initialize()
{
	HRESULT hr = S_OK;
	IMFAttributes* pSessionAttributes = NULL;

	if(m_bInitialized)
	{
		CHECK_HR(hr = E_FAIL);
	}

	// Set session attributes
	CHECK_HR(hr = MFCreateAttributes(&pSessionAttributes, 1));
	CHECK_HR(hr = pSessionAttributes->SetUINT32(PLUGIN_MF_LOW_LATENCY, 1));

	// Get input and output type
	CHECK_HR(hr = m_pCodec->GetInputType(&m_pInputType));
	CHECK_HR(hr = m_pCodec->GetOutputType(&m_pOutputType));

	// Create custom source
	CHECK_HR(hr = CMFSource::CreateInstanceEx(IID_IMFMediaSource, (void**)&m_pSource, m_pInputType));

	// Create the sample grabber sink.
	CHECK_HR(hr = MFCodecTopologySampleGrabberCB::CreateInstance(this, &m_pGrabberCallback));
	CHECK_HR(hr = MFCreateSampleGrabberSinkActivate(m_pOutputType, m_pGrabberCallback, &m_pGrabberActivate));

	// To run as fast as possible, set this attribute (requires Windows 7 or later):
	CHECK_HR(hr = m_pGrabberActivate->SetUINT32(MF_SAMPLEGRABBERSINK_IGNORE_CLOCK, TRUE));

	// Create the Media Session.
	CHECK_HR(hr = MFCreateMediaSession(pSessionAttributes, &m_pSession));

	// Create the topology.
	CHECK_HR(hr = MFUtils::CreateTopology(
		m_pSource, 
		m_pCodec->GetMFT(), 
		m_pGrabberActivate, 
		NULL, // no preview
		m_pOutputType, 
		&m_pTopologyPartial));
	// Resolve topology (adds video processors if needed).
	CHECK_HR(hr = MFUtils::ResolveTopology(m_pTopologyPartial, &m_pTopologyFull));

	m_bInitialized = TRUE;

bail:
	SafeRelease(&pSessionAttributes);

	if(FAILED(hr))
	{
		DeInitialize();
	}

	return hr;
}

void* TSK_STDCALL MFCodecTopology::RunSessionThread(void *pArg)
{
	MFCodecTopology *pSelf = (MFCodecTopology *)pArg;
	HRESULT hrStatus = S_OK;
	HRESULT hr = S_OK;
	IMFMediaEvent *pEvent = NULL;
	MediaEventType met;

	TSK_DEBUG_INFO("RunSessionThread (MFCodecTopology) - ENTER");

	while(pSelf->isStarted())
	{
		CHECK_HR(hr = pSelf->m_pSession->GetEvent(0, &pEvent));
		CHECK_HR(hr = pEvent->GetStatus(&hrStatus));
		CHECK_HR(hr = pEvent->GetType(&met));
		
		if (FAILED(hrStatus) /*&& hrStatus != MF_E_NO_SAMPLE_TIMESTAMP*/)
		{
			TSK_DEBUG_ERROR("Session error: 0x%x (event id: %d)\n", hrStatus, met);
			hr = hrStatus;
			goto bail;
		}
		if (met == MESessionEnded)
		{
			break;
		}
		SafeRelease(&pEvent);
	}

bail:
	TSK_DEBUG_INFO("RunSessionThread (MFCodecTopology) - EXIT");

	return NULL;
}

HRESULT MFCodecTopology::DeInitialize()
{
	Stop();

	SafeRelease(&m_pCodec);
	SafeRelease(&m_pSource);
	SafeRelease(&m_pCodec);
	SafeRelease(&m_pSession);
	SafeRelease(&m_pTopologyFull);
	SafeRelease(&m_pTopologyPartial);
	SafeRelease(&m_pOutputType);
	SafeRelease(&m_pInputType);
	SafeRelease(&m_pGrabberCallback);
	SafeRelease(&m_pGrabberActivate);

	if(m_pTread)
	{
		tsk_thread_join(&m_pTread);
	}

	m_SampleQueue.Clear();

	m_bInitialized = FALSE;

	return S_OK;
}

HRESULT MFCodecTopology::ProcessInput(IMFSample* pSample)
{
	HRESULT hr = S_OK;
	IMFMediaBuffer* pMediaBuffer = NULL;
	BYTE* _pcBufferPtr = NULL;

	if(!pSample)
	{
		CHECK_HR(hr = E_POINTER);
	}

	if(m_pCodec->GetMediaType() != MFCodecMediaType_Video)
	{
		CHECK_HR(hr = E_NOTIMPL);
	}

	if(!m_bStarted)
	{
		CHECK_HR(hr = Start());
	}

	CHECK_HR(hr = pSample->GetBufferByIndex(0, &pMediaBuffer));
	
	DWORD dwDataLength = 0;
	BOOL bLocked = FALSE;
	CHECK_HR(hr = pMediaBuffer->GetCurrentLength(&dwDataLength));
	bLocked = TRUE;
	if(dwDataLength > 0)
	{
		CHECK_HR(hr = pMediaBuffer->Lock(&_pcBufferPtr, NULL, NULL));
		CHECK_HR(hr = m_pSource->CopyVideoBuffer(
			dynamic_cast<MFCodecVideo*>(m_pCodec)->GetWidth(),
			dynamic_cast<MFCodecVideo*>(m_pCodec)->GetHeight(),
			_pcBufferPtr, dwDataLength));
	}

bail:
	if(bLocked)
	{
		pMediaBuffer->Unlock();
	}
	SafeRelease(&pMediaBuffer);
	return hr;
}

HRESULT MFCodecTopology::ProcessOutput(IMFSample **ppSample)
{
	HRESULT hr = S_OK;

	if(!ppSample)
	{
		CHECK_HR(hr = E_POINTER);
	}

	if(!m_SampleQueue.IsEmpty())
	{
		CHECK_HR(hr = m_SampleQueue.Dequeue(ppSample)); // thread-safe
	}

bail:
	return hr;
}

//
//	MFCodecVideoTopology
//


MFCodecVideoTopology::MFCodecVideoTopology(MFCodec* pCodec, HRESULT &hr)
: MFCodecTopology(pCodec, hr)
, m_nWidth(0)
, m_nHeight(0)
{
	assert(pCodec->GetMediaType() == MFCodecMediaType_Video);
}

MFCodecVideoTopology::~MFCodecVideoTopology()
{

}


OpenPOWER on IntegriCloud