summaryrefslogtreecommitdiffstats
path: root/plugins/pluginWinMF/internals/mf_custom_src.h
blob: f9194c91e99b501d643071d6ef9f03c2ac2751d3 (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
/* 
* Copyright (C) Microsoft Corporation. All rights reserved.
* 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.
*/
#ifndef PLUGIN_WIN_MF_CUSTOM_SOURCE_H
#define PLUGIN_WIN_MF_CUSTOM_SOURCE_H

#include "../plugin_win_mf_config.h"

#include <new>
#include <windows.h>
#include <assert.h>

#include <mfapi.h>
#include <mfobjects.h>
#include <mfidl.h>
#include <mferror.h>
#include <shlwapi.h>

class CMFStreamSource;
class CMFSource;

LONGLONG AudioDurationFromBufferSize(const WAVEFORMATEX *pWav, DWORD cbAudioDataSize);


//////////////////////////////////////////////////////////////////////////
//  CMFSource
//  Description: Media source object.
//////////////////////////////////////////////////////////////////////////

class CMFSource : public IMFMediaSource
{
    friend class CMFStreamSource;

public:
    static HRESULT CreateInstance(REFIID iid, void **ppSource);
	static HRESULT CreateInstanceEx(REFIID iid, void **ppSource, IMFMediaType *pMediaType);

	// IMFCustomSource
	HRESULT CopyVideoBuffer(UINT32 nWidth, UINT32 nHeight, const void* pBufferPtr, UINT32 nBufferSize);

    // IUnknown
    STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

    // IMFMediaEventGenerator
    STDMETHODIMP BeginGetEvent(IMFAsyncCallback* pCallback,IUnknown* punkState);
    STDMETHODIMP EndGetEvent(IMFAsyncResult* pResult, IMFMediaEvent** ppEvent);
    STDMETHODIMP GetEvent(DWORD dwFlags, IMFMediaEvent** ppEvent);
    STDMETHODIMP QueueEvent(MediaEventType met, REFGUID guidExtendedType, HRESULT hrStatus, const PROPVARIANT* pvValue);

    // IMFMediaSource
    STDMETHODIMP CreatePresentationDescriptor(IMFPresentationDescriptor** ppPresentationDescriptor);
    STDMETHODIMP GetCharacteristics(DWORD* pdwCharacteristics);
    STDMETHODIMP Pause();
    STDMETHODIMP Shutdown();
    STDMETHODIMP Start(
        IMFPresentationDescriptor* pPresentationDescriptor,
        const GUID* pguidTimeFormat,
        const PROPVARIANT* pvarStartPosition
    );
    STDMETHODIMP Stop();

private:

    enum State
    {
        STATE_STOPPED,
        STATE_PAUSED,
        STATE_STARTED
    };


    // Constructor is private - client should use static CreateInstance method.
    CMFSource(HRESULT &hr, IMFMediaType *pMediaType);
    virtual ~CMFSource();

    HRESULT CheckShutdown() const
    {
        if (m_IsShutdown)
        {
            return MF_E_SHUTDOWN;
        }
        else
        {
            return S_OK;
        }
    }

    HRESULT     CreatePresentationDescriptor();
    HRESULT     QueueNewStreamEvent(IMFPresentationDescriptor *pPD);
    HRESULT     CreateCMFStreamSource(IMFStreamDescriptor *pSD);
    HRESULT     ValidatePresentationDescriptor(IMFPresentationDescriptor *pPD);

    LONGLONG    GetCurrentPosition() const;
    State       GetState() const { return m_state; }

    IMFMediaEventQueue          *m_pEventQueue;             // Event generator helper
    IMFPresentationDescriptor   *m_pPresentationDescriptor; // Default presentation

    CMFStreamSource				*m_pStream;                 // Media stream. Can be NULL is no stream is selected.

    long                        m_nRefCount;                // reference count
    CRITICAL_SECTION            m_critSec;
    BOOL                        m_IsShutdown;               // Flag to indicate if Shutdown() method was called.
    State                       m_state;                    // Current state (running, stopped, paused)

	IMFMediaType				*m_pMediaType;				// The supported mediaType
};


class SampleQueue
{
protected:

    // Nodes in the linked list
    struct Node
    {
        Node *prev;
        Node *next;
        IMFSample*  item;

        Node() : prev(NULL), next(NULL)
        {
        }

        Node(IMFSample* item) : prev(NULL), next(NULL)
        {
            this->item = item;
        }

        IMFSample* Item() const { return item; }
    };


protected:
    Node    m_anchor;  // Anchor node for the linked list.

public:

    SampleQueue()
    {
        m_anchor.next = &m_anchor;
        m_anchor.prev = &m_anchor;
    }

    virtual ~SampleQueue()
    {
        Clear();
    }

    HRESULT Queue(IMFSample* item)
    {
        if (item == NULL)
        {
            return E_POINTER;
        }

        Node *pNode = new (std::nothrow) Node(item);
        if (pNode == NULL)
        {
            return E_OUTOFMEMORY;
        }

        item->AddRef();

        Node *pBefore = m_anchor.prev;

        Node *pAfter = pBefore->next;

        pBefore->next = pNode;
        pAfter->prev = pNode;

        pNode->prev = pBefore;
        pNode->next = pAfter;

        return S_OK;

    }

    HRESULT Dequeue(IMFSample* *ppItem)
    {
        if (IsEmpty())
        {
            return E_FAIL;
        }
        if (ppItem == NULL)
        {
            return E_POINTER;
        }

        Node *pNode = m_anchor.next;

        // The next node's previous is this node's previous.
        pNode->next->prev = m_anchor.next->prev;

        // The previous node's next is this node's next.
        pNode->prev->next = pNode->next;

        *ppItem = pNode->item;
        delete pNode;

        return S_OK;
    }

    BOOL IsEmpty() const { return m_anchor.next == &m_anchor; }

    void Clear()
    {
        Node *n = m_anchor.next;

        // Delete the nodes
        while (n != &m_anchor)
        {
            if (n->item)
            {
                n->item->Release();
            }

            Node *tmp = n->next;
            delete n;
            n = tmp;
        }

        // Reset the anchor to point at itself
        m_anchor.next = &m_anchor;
        m_anchor.prev = &m_anchor;
    }

};



//////////////////////////////////////////////////////////////////////////
//  CMFStreamSource
//  Description: Media stream object.
//////////////////////////////////////////////////////////////////////////


class CMFStreamSource : public IMFMediaStream
{
    friend class CMFSource;

public:

	// IMFCustomSource
	HRESULT CopyVideoBuffer(UINT32 nWidth, UINT32 nHeight, const void* pBufferPtr, UINT32 nBufferSize);

    // IUnknown
    STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

    // IMFMediaEventGenerator
    STDMETHODIMP BeginGetEvent(IMFAsyncCallback* pCallback,IUnknown* punkState);
    STDMETHODIMP EndGetEvent(IMFAsyncResult* pResult, IMFMediaEvent** ppEvent);
    STDMETHODIMP GetEvent(DWORD dwFlags, IMFMediaEvent** ppEvent);
    STDMETHODIMP QueueEvent(MediaEventType met, REFGUID guidExtendedType, HRESULT hrStatus, const PROPVARIANT* pvValue);

    // IMFMediaStream
    STDMETHODIMP GetMediaSource(IMFMediaSource** ppMediaSource);
    STDMETHODIMP GetStreamDescriptor(IMFStreamDescriptor** ppStreamDescriptor);
    STDMETHODIMP RequestSample(IUnknown* pToken);

private:

    CMFStreamSource(CMFSource *pSource, IMFStreamDescriptor *pSD, HRESULT& hr);
    ~CMFStreamSource();


    HRESULT CheckShutdown() const
    {
        if (m_IsShutdown)
        {
            return MF_E_SHUTDOWN;
        }
        else
        {
            return S_OK;
        }
    }

	HRESULT		InitializeParams();
    HRESULT     Shutdown();
    HRESULT     CreateSample(IMFSample **pSample);
    HRESULT     DeliverSample(IMFSample *pSample);
    HRESULT     DeliverQueuedSamples();
    HRESULT     Flush();

    LONGLONG    GetCurrentPosition() const { return m_rtCurrentPosition; }
    HRESULT     SetPosition(LONGLONG rtNewPosition);
    HRESULT     CheckEndOfStream();


    long                        m_nRefCount;            // reference count
    CRITICAL_SECTION            m_critSec;
    BOOL                        m_IsShutdown;           // Flag to indicate if source's Shutdown() method was called.
    LONGLONG                    m_rtCurrentPosition;    // Current position in the stream, in 100-ns units
	UINT64						m_rtDuration;			// Sample duration, in 100-ns units
    BOOL                        m_discontinuity;        // Is the next sample a discontinuity?
    BOOL                        m_EOS;                  // Did we reach the end of the stream?

    IMFMediaEventQueue          *m_pEventQueue;         // Event generator helper.
    CMFSource                   *m_pSource;             // Parent media source
    IMFStreamDescriptor         *m_pStreamDescriptor;   // Stream descriptor for this stream.

    SampleQueue                  m_sampleQueue;          // Queue for samples while paused.
	GUID						m_guidMajorType;		// major media type (e.g. MFMediaType_Video or MFMediaType_Audio)
	GUID						m_guidSubType;			// Media subtype (e.g. MFVideoFormat_RGB32 or MFVideoFormat_H264)
	IMFMediaBuffer				*m_pMediaBuffer;			// Pointer to the data to deliver
	UINT32						m_nBufferSize;			// Size of the data to deliver 
	 
	struct
	{
		UINT32 nWidth;
		UINT32 nHeigh;
		UINT32 nFps;
	} 
	m_structVideoParams;
};


#endif /* PLUGIN_WIN_MF_CUSTOM_SOURCE_H */
OpenPOWER on IntegriCloud