summaryrefslogtreecommitdiff
path: root/general/SimpleMediaSource/MediaSource/SimpleMediaStream.cpp
blob: 2f8b603bc9db73a5324bce16855139f1e84cb744 (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
#include "SimpleMediaStream.h"

#define NUM_IMAGE_ROWS 240
#define NUM_IMAGE_COLS 320
#define BYTES_PER_PIXEL 4
#define IMAGE_BUFFER_SIZE_BYTES (NUM_IMAGE_ROWS * NUM_IMAGE_COLS * BYTES_PER_PIXEL)
#define IMAGE_ROW_SIZE_BYTES (NUM_IMAGE_COLS * BYTES_PER_PIXEL)

#define CHECKHR_GOTO( val, label )  \
hr = (val); \
if( FAILED( hr ) ) { \
    goto label; \
}

HRESULT SimpleMediaStream::RuntimeClassInitialize(_In_ SimpleMediaSource *pSource)
{
    HRESULT hr = S_OK;
    ComPtr<IMFMediaTypeHandler> spTypeHandler;
    ComPtr<IMFAttributes> attrs;

    AsWeak(pSource, &_wpSource);

    // Initialize media type and set the video output media type.
    CHECKHR_GOTO(MFCreateMediaType(&_spMediaType), done);
    _spMediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
    _spMediaType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB32);
    _spMediaType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
    _spMediaType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);
    MFSetAttributeSize(_spMediaType.Get(), MF_MT_FRAME_SIZE, NUM_IMAGE_COLS, NUM_IMAGE_ROWS);
    MFSetAttributeRatio(_spMediaType.Get(), MF_MT_FRAME_RATE, 30, 1);
    MFSetAttributeRatio(_spMediaType.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1);

    CHECKHR_GOTO(MFCreateAttributes(&_spAttributes, 10), done);
    CHECKHR_GOTO(this->_SetStreamAttributes(_spAttributes.Get()), done);
    CHECKHR_GOTO(MFCreateEventQueue(&_spEventQueue), done);

    // Initialize stream descriptors
    CHECKHR_GOTO(MFCreateStreamDescriptor(0, 1, _spMediaType.GetAddressOf(), &_spStreamDesc), done);

    CHECKHR_GOTO(_spStreamDesc->GetMediaTypeHandler(&spTypeHandler), done);
    CHECKHR_GOTO(spTypeHandler->SetCurrentMediaType(_spMediaType.Get()), done);
    CHECKHR_GOTO(this->_SetStreamDescriptorAttributes(_spStreamDesc.Get()), done);

done:
    return hr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// IMFMediaEventGenerator
IFACEMETHODIMP SimpleMediaStream::BeginGetEvent(IMFAsyncCallback *pCallback, IUnknown *punkState)
{
    auto lock = _critSec.Lock();

    HRESULT hr = _CheckShutdownRequiresLock();

    if (SUCCEEDED(hr))
    {
        hr = _spEventQueue->BeginGetEvent(pCallback, punkState);
    }

    return hr;
}

IFACEMETHODIMP SimpleMediaStream::EndGetEvent(IMFAsyncResult *pResult, IMFMediaEvent **ppEvent)
{
    auto lock = _critSec.Lock();

    HRESULT hr = _CheckShutdownRequiresLock();

    if (SUCCEEDED(hr))
    {
        hr = _spEventQueue->EndGetEvent(pResult, ppEvent);
    }

    return hr;
}

IFACEMETHODIMP SimpleMediaStream::GetEvent(DWORD dwFlags, IMFMediaEvent **ppEvent)
{
    // NOTE:
    // GetEvent can block indefinitely, so we don't hold the lock.
    // This requires some juggling with the event queue pointer.

    HRESULT hr = S_OK;

    ComPtr<IMFMediaEventQueue> spQueue;

    {
        auto lock = _critSec.Lock();

        // Check shutdown
        hr = _CheckShutdownRequiresLock();

        if (SUCCEEDED(hr))
        {
            spQueue = _spEventQueue;
        }
    }

    // Now get the event.
    if (SUCCEEDED(hr))
    {
        hr = spQueue->GetEvent(dwFlags, ppEvent);
    }

    return hr;
}

IFACEMETHODIMP SimpleMediaStream::QueueEvent(MediaEventType met, REFGUID guidExtendedType, HRESULT hrStatus, const PROPVARIANT *pvValue)
{
    auto lock = _critSec.Lock();

    HRESULT hr = _CheckShutdownRequiresLock();
    if (SUCCEEDED(hr))
    {
        hr = _spEventQueue->QueueEventParamVar(met, guidExtendedType, hrStatus, pvValue);
    }

    return hr;
}

//////////////////////////////////////////////////////////////////////////////////////////
// IMFMediaStream
IFACEMETHODIMP SimpleMediaStream::GetMediaSource(IMFMediaSource **ppMediaSource)
{
    if (ppMediaSource == nullptr)
    {
        return E_POINTER;
    }
    *ppMediaSource = nullptr;

    auto lock = _critSec.Lock();
    HRESULT hr = _CheckShutdownRequiresLock();

    if (SUCCEEDED(hr))
    {
        ComPtr<SimpleMediaSource> spSource;
        _wpSource.As(&spSource);
        if (spSource != nullptr)
        {
            hr = spSource->QueryInterface(IID_PPV_ARGS(ppMediaSource));
        }
        else
        {
            hr = E_UNEXPECTED;
        }
    }

    return hr;
}

IFACEMETHODIMP SimpleMediaStream::GetStreamDescriptor(IMFStreamDescriptor **ppStreamDescriptor)
{
    if (ppStreamDescriptor == nullptr)
    {
        return E_POINTER;
    }

    *ppStreamDescriptor = nullptr;

    auto lock = _critSec.Lock();
    HRESULT hr = _CheckShutdownRequiresLock();

    if (SUCCEEDED(hr))
    {
        if (_spStreamDesc != nullptr)
        {
            *ppStreamDescriptor = _spStreamDesc.Get();
            (*ppStreamDescriptor)->AddRef();
        }
        else
        {
            return E_UNEXPECTED;
        }
    }

    return hr;
}

/*
   Writes to a buffer representing a 2D image.
   Writes a different constant to each line based on row number and current time.

   Assumes top down image, no negative stride and pBuf points to the begnning of the buffer of length len.

   Param:
   pBuf - pointer to beginning of buffer
   pitch - line length in bytes
   len - length of buffer in bytes
*/
HRESULT WriteSampleData(BYTE *pBuf, LONG pitch, DWORD len)
{
    if (pBuf == nullptr)
    {
        return E_POINTER;
    }

    const int NUM_ROWS = len / abs(pitch);

    LONGLONG curSysTimeInS = MFGetSystemTime() / (MFTIME)10000000;
    int offset = curSysTimeInS % NUM_ROWS;

    for (int r = 0; r < NUM_ROWS; r++)
    {
        int grayColor = r + offset;
        memset(pBuf + (pitch * r), (BYTE)(grayColor % NUM_ROWS), pitch);
    }

    return S_OK;
}

IFACEMETHODIMP SimpleMediaStream::RequestSample(IUnknown *pToken)
{
    auto lock = _critSec.Lock();

    HRESULT hr = _CheckShutdownRequiresLock();

    if (SUCCEEDED(hr))
    {
        ComPtr<IMFSample> spSample;

        ComPtr<IMFMediaBuffer> spOutputBuffer;
        hr = MFCreate2DMediaBuffer(NUM_IMAGE_COLS, NUM_IMAGE_ROWS, D3DFMT_X8R8G8B8, false, &spOutputBuffer);

        LONG pitch = IMAGE_ROW_SIZE_BYTES;
        BYTE *bufferStart = nullptr; // not used, since in this example we know the image will be top down, so bufferStart == pBuf
        DWORD bufferLength = IMAGE_BUFFER_SIZE_BYTES;

        BYTE *pBuf = nullptr;
        ComPtr<IMF2DBuffer2> spBuffer2D;
        if (SUCCEEDED(hr))
        {
            hr = spOutputBuffer.As(&spBuffer2D);
        }

        if (SUCCEEDED(hr))
        {
            if (spBuffer2D != nullptr)
            {
                hr = spBuffer2D->Lock2DSize(MF2DBuffer_LockFlags_Write, &pBuf, &pitch, &bufferStart, &bufferLength);
            }
            else
            {
                hr = spOutputBuffer->Lock(&pBuf, nullptr, nullptr);
            }
        }

        if (SUCCEEDED(hr))
        {
            hr = WriteSampleData(pBuf, pitch, bufferLength);
        }

        if (SUCCEEDED(hr))
        {
            hr = spOutputBuffer->SetCurrentLength(bufferLength);
        }

        if (SUCCEEDED(hr))
        {
            hr = spBuffer2D->Unlock2D();
        }

        if (SUCCEEDED(hr))
        {
            hr = MFCreateSample(&spSample);
        }

        if (SUCCEEDED(hr))
        {
            hr = spSample->AddBuffer(spOutputBuffer.Get());
        }

        // set timestamp
        if (SUCCEEDED(hr))
        {
            hr = spSample->SetSampleTime(MFGetSystemTime());
        }
        if (SUCCEEDED(hr))
        {
            hr = spSample->SetSampleDuration(330000);
        }

        if (pToken != nullptr)
        {
            hr = spSample->SetUnknown(MFSampleExtension_Token, pToken);
        }

        if (SUCCEEDED(hr))
        {
            hr = _spEventQueue->QueueEventParamUnk(MEMediaSample, GUID_NULL, S_OK, spSample.Get());
        }
    }

    return hr;
}

//////////////////////////////////////////////////////////////////////////////////////////
// IMFMediaStream2
IFACEMETHODIMP SimpleMediaStream::SetStreamState(MF_STREAM_STATE state)
{
    bool runningState = false;
    auto lock = _critSec.Lock();

    HRESULT hr = S_OK;

    CHECKHR_GOTO(_CheckShutdownRequiresLock(), done);

    switch (state)
    {
    case MF_STREAM_STATE_PAUSED:
        goto done; // because not supported
    case MF_STREAM_STATE_RUNNING:
        runningState = true;
        break;
    case MF_STREAM_STATE_STOPPED:
        runningState = false;
        break;
    default:
        hr = MF_E_INVALID_STATE_TRANSITION;
        break;
    }

    _isSelected = runningState;

done:
    return hr;
}

IFACEMETHODIMP SimpleMediaStream::GetStreamState(_Out_ MF_STREAM_STATE *pState)
{
    auto lock = _critSec.Lock();
    BOOLEAN pauseState = false;

    HRESULT hr = _CheckShutdownRequiresLock();

    if (SUCCEEDED(hr))
    {
        *pState = (_isSelected ? MF_STREAM_STATE_RUNNING : MF_STREAM_STATE_STOPPED);
    }

    return hr;
}

HRESULT SimpleMediaStream::Shutdown()
{
    HRESULT hr = S_OK;
    auto lock = _critSec.Lock();

    _isShutdown = true;

    if (_spEventQueue != nullptr)
    {
        hr = _spEventQueue->Shutdown();
        _spEventQueue.Reset();
    }

    _spAttributes.Reset();
    _spMediaType.Reset();
    _spStreamDesc.Reset();

    _isSelected = false;

    _wpSource.Reset();

    return hr;
}

HRESULT SimpleMediaStream::_CheckShutdownRequiresLock()
{
    if (_isShutdown)
    {
        return MF_E_SHUTDOWN;
    }

    if (_spEventQueue == nullptr)
    {
        return E_UNEXPECTED;

    }
    return S_OK;
}

HRESULT SimpleMediaStream::_SetStreamAttributes(IMFAttributes *pAttributeStore)
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetGUID(MF_DEVICESTREAM_STREAM_CATEGORY, PINNAME_VIDEO_CAPTURE); }
    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetUINT32(MF_DEVICESTREAM_STREAM_ID, STREAMINDEX); }
    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetUINT32(MF_DEVICESTREAM_FRAMESERVER_SHARED, 1); }

    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetUINT32(MF_DEVICESTREAM_ATTRIBUTE_FRAMESOURCE_TYPES, _MFFrameSourceTypes::MFFrameSourceTypes_Color); }

    return hr;
}

HRESULT SimpleMediaStream::_SetStreamDescriptorAttributes(IMFAttributes *pAttributeStore)
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetGUID(MF_DEVICESTREAM_STREAM_CATEGORY, PINNAME_VIDEO_CAPTURE); }
    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetUINT32(MF_DEVICESTREAM_STREAM_ID, STREAMINDEX); }
    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetUINT32(MF_DEVICESTREAM_FRAMESERVER_SHARED, 1); }

    if (SUCCEEDED(hr)) { hr = pAttributeStore->SetUINT32(MF_DEVICESTREAM_ATTRIBUTE_FRAMESOURCE_TYPES, _MFFrameSourceTypes::MFFrameSourceTypes_Color); }

    return hr;
}