summaryrefslogtreecommitdiffstats
path: root/plugins/pluginDirectShow/internals/DSOutputStream.cxx
blob: 1e13d78a26d57cf7d513a2b1549b77348d1b83f8 (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
/* 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.
*/
#include "internals/DSOutputStream.h"
#include "internals/DSOutputFilter.h"
#include "internals/DSUtils.h"

#include <iostream>

#include "tsk_memory.h"

using namespace std;

#define DEFAULT_FPS	15

#define MEMCPY_WORKAROUND 1

// Overlay
#define OVERLAY 0
#define OVERLAY_TEXT TEXT("Press ESC to exit full screen mode")
#define OVERLAY_DURATION 3 // in seconds

DSOutputStream::DSOutputStream(HRESULT *phr, DSOutputFilter *pParent, LPCWSTR pPinName)
    : CSourceStream(_T("DSOutputStream"), phr, pParent, pPinName)
{
#if !(defined(_WIN32_WCE) && defined(_DEBUG))
    CAutoLock cAutoLock(m_pFilter->pStateLock());
#endif

    this->buffer = NULL;
    this->buffer_size = NULL;

    this->frameNumber = 0;
    this->frameLength = (1000)/DEFAULT_FPS;
    this->fps = DEFAULT_FPS;

    this->width = 352;
    this->height = 288;

    this->overlay = false;

    this->paintBuffer = NULL;
    this->paintDC = NULL;
    this->hDibSection = NULL;
    this->hObject = NULL;

    this->mutex = tsk_mutex_create();
}

DSOutputStream::~DSOutputStream()
{
    TSK_FREE(this->buffer);
    tsk_mutex_destroy(&this->mutex);
    // TODO : Is there anything to free ???
}

void DSOutputStream::setFps(int fps_)
{
    this->fps = fps_;
    this->frameLength = (1000)/this->fps;
}

void DSOutputStream::showOverlay(int value)
{
    if (value == 0) {
        this->overlay = false;
    }
    this->overlay = (value > 0);
}

HRESULT DSOutputStream::setImageFormat(UINT width, UINT height)
{
    if ((this->width == width) && (this->height == height)) {
        return S_FALSE;
    }

    this->width = width;
    this->height = height;

    this->frameNumber = 0;

    return S_OK;
}

bool DSOutputStream::getImageFormat(UINT &width, UINT &height)
{
    width = this->width;
    height = this->height;
    return true;
}

HRESULT DSOutputStream::GetMediaType(CMediaType *pMediaType)
{
    HRESULT hr = S_OK;
#if !(defined(_WIN32_WCE) && defined(_DEBUG))
    CAutoLock lock(m_pFilter->pStateLock());
#endif

    ZeroMemory(pMediaType, sizeof(CMediaType));

    VIDEOINFO *pvi = (VIDEOINFO *)pMediaType->AllocFormatBuffer(sizeof(VIDEOINFO));
    if (NULL == pvi) {
        return E_OUTOFMEMORY;
    }

    ZeroMemory(pvi, sizeof(VIDEOINFO));

    pvi->bmiHeader.biCompression = BI_RGB;
    pvi->bmiHeader.biBitCount = 24;
    pvi->bmiHeader.biSize			= sizeof(BITMAPINFOHEADER);
    pvi->bmiHeader.biWidth			= this->width;
    pvi->bmiHeader.biHeight			= this->height;
    pvi->bmiHeader.biPlanes			= 1;
    pvi->bmiHeader.biSizeImage		= GetBitmapSize(&pvi->bmiHeader);
    pvi->bmiHeader.biClrImportant	= 0;

    // Frame rate
    pvi->AvgTimePerFrame			= DS_MILLIS_TO_100NS(1000/this->fps);

    SetRectEmpty(&(pvi->rcSource));	// we want the whole image area rendered.
    SetRectEmpty(&(pvi->rcTarget));	// no particular destination rectangle

    pMediaType->SetType(&MEDIATYPE_Video);
    pMediaType->SetFormatType(&FORMAT_VideoInfo);
    pMediaType->SetTemporalCompression(FALSE);

    pMediaType->SetSubtype(&MEDIASUBTYPE_RGB24);
    pMediaType->SetSampleSize(pvi->bmiHeader.biSizeImage);

    bitmapInfo.bmiHeader = pvi->bmiHeader;

    return hr;
}

HRESULT DSOutputStream::DecideBufferSize(IMemAllocator *pMemAlloc, ALLOCATOR_PROPERTIES *pProperties)
{
    CheckPointer(pMemAlloc, E_POINTER);
    CheckPointer(pProperties, E_POINTER);

#if !(defined(_WIN32_WCE) && defined(_DEBUG))
    CAutoLock cAutoLock(m_pFilter->pStateLock());
#endif

    HRESULT hr = NOERROR;

    VIDEOINFO *pvi = (VIDEOINFO *) m_mt.Format();
    pProperties->cBuffers = 1;
    pProperties->cbBuffer = pvi->bmiHeader.biSizeImage;

    // Ask the allocator to reserve us some sample memory. NOTE: the function
    // can succeed (return NOERROR) but still not have allocated the
    // memory that we requested, so we must check we got whatever we wanted.
    ALLOCATOR_PROPERTIES Actual;
    hr = pMemAlloc->SetProperties(pProperties,&Actual);
    if(FAILED(hr)) {
        return hr;
    }

    // Is this allocator unsuitable?
    if(Actual.cbBuffer < pProperties->cbBuffer) {
        return E_FAIL;
    }

    // Make sure that we have only 1 buffer (we erase the ball in the
    // old buffer to save having to zero a 200k+ buffer every time
    // we draw a frame)
    return NOERROR;
}

HRESULT DSOutputStream::OnThreadCreate()
{
#if OVERLAY
    hDibSection = CreateDIBSection(NULL, (BITMAPINFO *) &bitmapInfo, DIB_RGB_COLORS, &paintBuffer, NULL, 0);

    HDC hDC = GetDC(NULL);
    paintDC = CreateCompatibleDC(hDC);
    SetMapMode(paintDC, GetMapMode(hDC));
    SetBkMode(paintDC, TRANSPARENT);
    SetTextColor(paintDC, RGB(255,255,255));

    hObject = SelectObject(paintDC, hDibSection);
#endif

    return CSourceStream::OnThreadCreate();
}

HRESULT DSOutputStream::OnThreadDestroy()
{
#if OVERLAY
    if (paintDC) {
        DeleteDC(paintDC);
    }
    if (hObject) {
        DeleteObject(hObject);
    }

    if (paintBuffer) {
        //delete[] paintBuffer; // will be done
        //paintBuffer = NULL;
    }
#endif
    return CSourceStream::OnThreadDestroy();
}

inline HRESULT DSOutputStream::DrawOverLay(void *pBuffer, long lSize)
{
    // called only #if OVERLAY
    CopyMemory(paintBuffer, pBuffer, lSize);

    // Draw the current frame
#ifdef _WIN32_WCE

#else
    if( !TextOut( paintDC, 0, 0, OVERLAY_TEXT, (int)_tcslen( OVERLAY_TEXT ) ) ) {
        return E_FAIL;
    }
#endif

    CopyMemory(pBuffer, paintBuffer, lSize);

    return S_OK;
}

static __inline void TransfertBuffer(void* src, void* dest, long lSize)
{
    __try {
#if MEMCPY_WORKAROUND
        //#ifdef _WIN32_WCE
        memmove(dest, src, lSize);
        /*#else
        		unsigned char * pDst = (unsigned char *) dest;

        		if(src){
        			unsigned char const * pSrc = (unsigned char const *) src;
        			for( register int i=0; ((i< lSize) && src); i++) *pDst++ = *pSrc++;
        		}else{
        			for( register int i=0; i< lSize; i++) *pDst++ = 0;
        		}
        #endif*/
#else
        CopyMemory(dest, src, lSize); //BUGGY
#endif
    }
    __except(EXCEPTION_ACCESS_VIOLATION == GetExceptionCode()) {
        //ZeroMemory(dest, sizeof(void*));
    }
}

HRESULT DSOutputStream::FillBuffer(IMediaSample *pSample)
{
    CheckPointer(pSample, E_POINTER);
#if !(defined(_WIN32_WCE) && defined(_DEBUG))
    CAutoLock lock(m_pFilter->pStateLock());
#endif

    HRESULT hr;
    BYTE *pBuffer = NULL;
    long lSize, lDataSize;

    hr = pSample->GetPointer(&pBuffer);
    if (SUCCEEDED(hr)) {
        lDataSize = lSize = pSample->GetSize();

        // Check that we're still using video
        //ASSERT(m_mt.formattype == FORMAT_VideoInfo);

        if (this->buffer) {
#if OVERLAY
            if (this->overlay) {
                DrawOverLay(this->buffer, lSize);
            }
#endif
            // Why try do not work, see: http://msdn2.microsoft.com/en-us/library/xwtb73ad(vs.80).aspx
            this->lockBuffer();
            lDataSize = TSK_MIN(lSize, this->buffer_size);
            TransfertBuffer(this->buffer, (void*)pBuffer, lDataSize);
            this->unlockBuffer();
        }
        else {
            // Avoid caching last image
            memset((void*)pBuffer, NULL, lSize);
        }

        REFERENCE_TIME rtStart = DS_MILLIS_TO_100NS(this->frameNumber * this->frameLength);
        REFERENCE_TIME rtStop  = rtStart + DS_MILLIS_TO_100NS(this->frameLength);

        this->frameNumber++;

        pSample->SetTime(&rtStart, &rtStop);
        //pSample->SetMediaTime(&rtStart, &rtStop);
        pSample->SetActualDataLength(lDataSize);
        pSample->SetPreroll(FALSE);
        pSample->SetDiscontinuity(FALSE);
    }

    // Set TRUE on every sample for uncompressed frames (KEYFRAME)
    pSample->SetSyncPoint(TRUE);

    return S_OK;
}
OpenPOWER on IntegriCloud