amstream.c 15.7 KB
Newer Older
1 2 3
/*
 * Implementation of IAMMultiMediaStream Interface
 *
4
 * Copyright 2004, 2012 Christian Costa
5
 * Copyright 2006 Ivan Leo Puoti
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23
 */

#include "wine/debug.h"

24 25
#define COBJMACROS

26 27 28 29 30 31 32 33
#include "winbase.h"
#include "wingdi.h"

#include "amstream_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(amstream);

typedef struct {
34
    IAMMultiMediaStream IAMMultiMediaStream_iface;
35
    LONG ref;
36
    IGraphBuilder* pFilterGraph;
37 38
    IMediaSeeking* media_seeking;
    IMediaControl* media_control;
39
    IMediaStreamFilter *media_stream_filter;
40
    IPin* ipin;
41
    ULONG nbStreams;
42
    IAMMediaStream **pStreams;
43
    STREAM_TYPE StreamType;
44
    OAEVENT event;
45 46
} IAMMultiMediaStreamImpl;

47 48 49 50 51
static inline IAMMultiMediaStreamImpl *impl_from_IAMMultiMediaStream(IAMMultiMediaStream *iface)
{
    return CONTAINING_RECORD(iface, IAMMultiMediaStreamImpl, IAMMultiMediaStream_iface);
}

52
static const struct IAMMultiMediaStreamVtbl AM_Vtbl;
53 54 55 56 57

HRESULT AM_create(IUnknown *pUnkOuter, LPVOID *ppObj)
{
    IAMMultiMediaStreamImpl* object; 

58
    TRACE("(%p,%p)\n", pUnkOuter, ppObj);
59 60

    if( pUnkOuter )
61 62
        return CLASS_E_NOAGGREGATION;

63
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAMMultiMediaStreamImpl));
Christian Costa's avatar
Christian Costa committed
64 65
    if (!object)
        return E_OUTOFMEMORY;
66

67
    object->IAMMultiMediaStream_iface.lpVtbl = &AM_Vtbl;
68 69
    object->ref = 1;

70
    *ppObj = &object->IAMMultiMediaStream_iface;
71

72 73 74 75 76 77
    return S_OK;
}

/*** IUnknown methods ***/
static HRESULT WINAPI IAMMultiMediaStreamImpl_QueryInterface(IAMMultiMediaStream* iface, REFIID riid, void** ppvObject)
{
78
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
79

Christian Costa's avatar
Christian Costa committed
80
    TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
81

82
    if (IsEqualGUID(riid, &IID_IUnknown) ||
83
        IsEqualGUID(riid, &IID_IMultiMediaStream) ||
84 85
        IsEqualGUID(riid, &IID_IAMMultiMediaStream))
    {
86 87
        IAMMultiMediaStream_AddRef(iface);
        *ppvObject = iface;
88 89
        return S_OK;
    }
90

91 92 93
    ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);

    return E_NOINTERFACE;
94 95 96 97
}

static ULONG WINAPI IAMMultiMediaStreamImpl_AddRef(IAMMultiMediaStream* iface)
{
98
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
99 100

    TRACE("(%p/%p)\n", iface, This);
101

102
    return InterlockedIncrement(&This->ref);
103 104 105 106
}

static ULONG WINAPI IAMMultiMediaStreamImpl_Release(IAMMultiMediaStream* iface)
{
107
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
108
    ULONG ref = InterlockedDecrement(&This->ref);
109
    ULONG i;
110

111
    TRACE("(%p/%p)\n", iface, This);
112

113
    if (!ref)
114 115
    {
        for(i = 0; i < This->nbStreams; i++)
116
            IAMMediaStream_Release(This->pStreams[i]);
117
        CoTaskMemFree(This->pStreams);
118 119
        if (This->ipin)
            IPin_Release(This->ipin);
120
        if (This->media_stream_filter)
121
            IMediaStreamFilter_Release(This->media_stream_filter);
122 123 124 125
        if (This->media_seeking)
            IMediaSeeking_Release(This->media_seeking);
        if (This->media_control)
            IMediaControl_Release(This->media_control);
126 127
        if (This->pFilterGraph)
            IGraphBuilder_Release(This->pFilterGraph);
128
        HeapFree(GetProcessHeap(), 0, This);
129
    }
130

131
    return ref;
132 133 134
}

/*** IMultiMediaStream methods ***/
135
static HRESULT WINAPI IAMMultiMediaStreamImpl_GetInformation(IAMMultiMediaStream* iface, DWORD* pdwFlags, STREAM_TYPE* pStreamType)
136
{
137
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
138

Christian Costa's avatar
Christian Costa committed
139
    FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pdwFlags, pStreamType);
140

141
    return E_NOTIMPL;
142 143 144 145
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetMediaStream(IAMMultiMediaStream* iface, REFMSPID idPurpose, IMediaStream** ppMediaStream)
{
146
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
147
    MSPID PurposeId;
148
    unsigned int i;
149

150
    TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(idPurpose), ppMediaStream);
151

152 153
    for (i = 0; i < This->nbStreams; i++)
    {
154
        IAMMediaStream_GetInformation(This->pStreams[i], &PurposeId, NULL);
155 156
        if (IsEqualIID(&PurposeId, idPurpose))
        {
157
            *ppMediaStream = (IMediaStream*)This->pStreams[i];
158 159 160 161 162 163
            IMediaStream_AddRef(*ppMediaStream);
            return S_OK;
        }
    }

    return MS_E_NOSTREAM;
164 165
}

166
static HRESULT WINAPI IAMMultiMediaStreamImpl_EnumMediaStreams(IAMMultiMediaStream* iface, LONG Index, IMediaStream** ppMediaStream)
167
{
168
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
169

170
    FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, Index, ppMediaStream);
171

172
    return E_NOTIMPL;
173 174 175 176
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetState(IAMMultiMediaStream* iface, STREAM_STATE* pCurrentState)
{
177
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
178

Christian Costa's avatar
Christian Costa committed
179
    FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentState);
180

181
    return E_NOTIMPL;
182 183
}

184
static HRESULT WINAPI IAMMultiMediaStreamImpl_SetState(IAMMultiMediaStream* iface, STREAM_STATE new_state)
185
{
186
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
187
    HRESULT hr = E_INVALIDARG;
188

189
    TRACE("(%p/%p)->(%u)\n", This, iface, new_state);
190

191
    if (new_state == STREAMSTATE_RUN)
192
        hr = IMediaControl_Run(This->media_control);
193
    else if (new_state == STREAMSTATE_STOP)
194 195 196
        hr = IMediaControl_Stop(This->media_control);

    return hr;
197 198 199 200
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetTime(IAMMultiMediaStream* iface, STREAM_TIME* pCurrentTime)
{
201
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
202

Christian Costa's avatar
Christian Costa committed
203
    FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentTime);
204

205
    return E_NOTIMPL;
206 207 208 209
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetDuration(IAMMultiMediaStream* iface, STREAM_TIME* pDuration)
{
210
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
211

Christian Costa's avatar
Christian Costa committed
212
    FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDuration);
213

214
    return E_NOTIMPL;
215 216
}

217
static HRESULT WINAPI IAMMultiMediaStreamImpl_Seek(IAMMultiMediaStream* iface, STREAM_TIME seek_time)
218
{
219
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
220

221
    TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(seek_time));
222

223
    return IMediaSeeking_SetPositions(This->media_seeking, &seek_time, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
224 225 226 227
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetEndOfStream(IAMMultiMediaStream* iface, HANDLE* phEOS)
{
228
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
229

Christian Costa's avatar
Christian Costa committed
230
    FIXME("(%p/%p)->(%p) stub!\n", This, iface, phEOS);
231

232
    return E_NOTIMPL;
233 234 235 236 237
}

/*** IAMMultiMediaStream methods ***/
static HRESULT WINAPI IAMMultiMediaStreamImpl_Initialize(IAMMultiMediaStream* iface, STREAM_TYPE StreamType, DWORD dwFlags, IGraphBuilder* pFilterGraph)
{
238
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
239
    HRESULT hr = S_OK;
240
    const WCHAR filternameW[] = {'M','e','d','i','a','S','t','r','e','a','m','F','i','l','t','e','r',0};
241

242
    TRACE("(%p/%p)->(%x,%x,%p)\n", This, iface, (DWORD)StreamType, dwFlags, pFilterGraph);
243

244 245 246 247 248 249 250 251 252 253 254 255 256
    if (pFilterGraph)
    {
        This->pFilterGraph = pFilterGraph;
        IGraphBuilder_AddRef(This->pFilterGraph);
    }
    else
    {
        hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&This->pFilterGraph);
    }

    if (SUCCEEDED(hr))
    {
        This->StreamType = StreamType;
257 258
        hr = IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IMediaSeeking, (void**)&This->media_seeking);
        if (SUCCEEDED(hr))
259
            hr = IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IMediaControl, (void**)&This->media_control);
260
        if (SUCCEEDED(hr))
261
            hr = CoCreateInstance(&CLSID_MediaStreamFilter, NULL, CLSCTX_INPROC_SERVER, &IID_IMediaStreamFilter, (void**)&This->media_stream_filter);
262
        if (SUCCEEDED(hr))
263
            hr = IGraphBuilder_AddFilter(This->pFilterGraph, (IBaseFilter*)This->media_stream_filter, filternameW);
264 265 266 267 268 269 270 271 272 273 274
        if (SUCCEEDED(hr))
        {
            IMediaEventEx* media_event = NULL;
            hr = IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IMediaEventEx, (void**)&media_event);
            if (SUCCEEDED(hr))
                hr = IMediaEventEx_GetEventHandle(media_event, &This->event);
            if (SUCCEEDED(hr))
                hr = IMediaEventEx_SetNotifyFlags(media_event, AM_MEDIAEVENT_NONOTIFY);
            if (media_event)
                IMediaEventEx_Release(media_event);
        }
275 276 277 278
    }

    if (FAILED(hr))
    {
279
        if (This->media_stream_filter)
280
            IMediaStreamFilter_Release(This->media_stream_filter);
281
        This->media_stream_filter = NULL;
282 283 284 285 286 287 288 289 290
        if (This->media_seeking)
            IMediaSeeking_Release(This->media_seeking);
        This->media_seeking = NULL;
        if (This->media_control)
            IMediaControl_Release(This->media_control);
        This->media_control = NULL;
        if (This->pFilterGraph)
            IGraphBuilder_Release(This->pFilterGraph);
        This->pFilterGraph = NULL;
291 292 293
    }

    return hr;
294 295 296 297
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilterGraph(IAMMultiMediaStream* iface, IGraphBuilder** ppGraphBuilder)
{
298
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
299

300
    TRACE("(%p/%p)->(%p)\n", This, iface, ppGraphBuilder);
301

302 303 304 305
    if (!ppGraphBuilder)
        return E_POINTER;

    if (This->pFilterGraph)
306
        return IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)ppGraphBuilder);
307 308 309 310
    else
        *ppGraphBuilder = NULL;

    return S_OK;
311 312 313 314
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilter(IAMMultiMediaStream* iface, IMediaStreamFilter** ppFilter)
{
315
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
316

317
    TRACE("(%p/%p)->(%p)\n", This, iface, ppFilter);
318

319 320 321
    if (!ppFilter)
        return E_POINTER;

322 323 324
    *ppFilter = This->media_stream_filter;
    if (*ppFilter)
        IMediaStreamFilter_AddRef(*ppFilter);
325

326
    return S_OK;
327 328
}

329
static HRESULT WINAPI IAMMultiMediaStreamImpl_AddMediaStream(IAMMultiMediaStream* iface, IUnknown* stream_object, const MSPID* PurposeId,
330 331
                                          DWORD dwFlags, IMediaStream** ppNewStream)
{
332
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
333
    HRESULT hr;
334 335
    IAMMediaStream* pStream;
    IAMMediaStream** pNewStreams;
336

337
    TRACE("(%p/%p)->(%p,%s,%x,%p)\n", This, iface, stream_object, debugstr_guid(PurposeId), dwFlags, ppNewStream);
338

339 340 341
    if (!IsEqualGUID(PurposeId, &MSPID_PrimaryVideo) && !IsEqualGUID(PurposeId, &MSPID_PrimaryAudio))
        return MS_E_PURPOSEID;

342 343 344
    if (stream_object)
        FIXME("Specifying a stream object in params is not yet supported\n");

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    if (dwFlags & AMMSF_ADDDEFAULTRENDERER)
    {
        if (IsEqualGUID(PurposeId, &MSPID_PrimaryVideo))
        {
            /* Default renderer not supported by video stream */
            return MS_E_PURPOSEID;
        }
        else
        {
            IBaseFilter* dsoundrender_filter;

            /* Create the default renderer for audio */
            hr = CoCreateInstance(&CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&dsoundrender_filter);
            if (SUCCEEDED(hr))
            {
                 hr = IGraphBuilder_AddFilter(This->pFilterGraph, dsoundrender_filter, NULL);
                 IBaseFilter_Release(dsoundrender_filter);
            }

            /* No media stream created when the default renderer is used */
            return hr;
        }
    }
368

369 370 371
    if (IsEqualGUID(PurposeId, &MSPID_PrimaryVideo))
        hr = ddrawmediastream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
    else
372
        hr = audiomediastream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
373 374
    if (SUCCEEDED(hr))
    {
375
        pNewStreams = CoTaskMemRealloc(This->pStreams, (This->nbStreams+1) * sizeof(IAMMediaStream*));
376 377
        if (!pNewStreams)
        {
378
            IAMMediaStream_Release(pStream);
379 380 381 382 383 384 385
            return E_OUTOFMEMORY;
        }
        This->pStreams = pNewStreams;
        This->pStreams[This->nbStreams] = pStream;
        This->nbStreams++;

        if (ppNewStream)
386
            *ppNewStream = (IMediaStream*)pStream;
387 388
    }

389 390 391
    if (SUCCEEDED(hr))
    {
        /* Add stream to the media stream filter */
392
        IMediaStreamFilter_AddMediaStream(This->media_stream_filter, pStream);
393 394
    }

395
    return hr;
396 397
}

398
static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenFile(IAMMultiMediaStream* iface, LPCWSTR filename, DWORD flags)
399
{
400
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
401 402 403
    HRESULT ret = S_OK;
    IBaseFilter *BaseFilter = NULL;
    IEnumPins *EnumPins = NULL;
404 405
    IPin *ipin;
    PIN_DIRECTION pin_direction;
406
    const WCHAR sourceW[] = {'S','o','u','r','c','e',0};
407

408 409 410 411
    TRACE("(%p/%p)->(%s,%x)\n", This, iface, debugstr_w(filename), flags);

    if (!filename)
        return E_POINTER;
412

413 414 415 416
    /* If Initialize was not called before, we do it here */
    if (!This->pFilterGraph)
        ret = IAMMultiMediaStream_Initialize(iface, STREAMTYPE_READ, 0, NULL);

417
    if (SUCCEEDED(ret))
418
        ret = IGraphBuilder_AddSourceFilter(This->pFilterGraph, filename, sourceW, &BaseFilter);
419

420 421 422 423 424
    if (SUCCEEDED(ret))
        ret = IBaseFilter_EnumPins(BaseFilter, &EnumPins);

    if (SUCCEEDED(ret))
        ret = IEnumPins_Next(EnumPins, 1, &ipin, NULL);
425

426
    if (SUCCEEDED(ret))
427 428
    {
        ret = IPin_QueryDirection(ipin, &pin_direction);
429
        if (ret == S_OK && pin_direction == PINDIR_OUTPUT)
430 431 432
            This->ipin = ipin;
    }

433 434 435
    if (SUCCEEDED(ret) && !(flags & AMMSF_NORENDER))
        ret = IGraphBuilder_Render(This->pFilterGraph, This->ipin);

436 437 438 439
    if (EnumPins)
        IEnumPins_Release(EnumPins);
    if (BaseFilter)
        IBaseFilter_Release(BaseFilter);
440
    return ret;
441 442 443 444
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenMoniker(IAMMultiMediaStream* iface, IBindCtx* pCtx, IMoniker* pMoniker, DWORD dwFlags)
{
445
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
446

447
    FIXME("(%p/%p)->(%p,%p,%x) stub!\n", This, iface, pCtx, pMoniker, dwFlags);
448

449
    return E_NOTIMPL;
450 451 452 453
}

static HRESULT WINAPI IAMMultiMediaStreamImpl_Render(IAMMultiMediaStream* iface, DWORD dwFlags)
{
454
    IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
455

Christian Costa's avatar
Christian Costa committed
456
    FIXME("(%p/%p)->(%x) partial stub!\n", This, iface, dwFlags);
457

458 459 460
    if(dwFlags != AMMSF_NOCLOCK)
        return E_INVALIDARG;

461
    return IGraphBuilder_Render(This->pFilterGraph, This->ipin);
462 463
}

464
static const IAMMultiMediaStreamVtbl AM_Vtbl =
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
{
    IAMMultiMediaStreamImpl_QueryInterface,
    IAMMultiMediaStreamImpl_AddRef,
    IAMMultiMediaStreamImpl_Release,
    IAMMultiMediaStreamImpl_GetInformation,
    IAMMultiMediaStreamImpl_GetMediaStream,
    IAMMultiMediaStreamImpl_EnumMediaStreams,
    IAMMultiMediaStreamImpl_GetState,
    IAMMultiMediaStreamImpl_SetState,
    IAMMultiMediaStreamImpl_GetTime,
    IAMMultiMediaStreamImpl_GetDuration,
    IAMMultiMediaStreamImpl_Seek,
    IAMMultiMediaStreamImpl_GetEndOfStream,
    IAMMultiMediaStreamImpl_Initialize,
    IAMMultiMediaStreamImpl_GetFilterGraph,
    IAMMultiMediaStreamImpl_GetFilter,
    IAMMultiMediaStreamImpl_AddMediaStream,
    IAMMultiMediaStreamImpl_OpenFile,
    IAMMultiMediaStreamImpl_OpenMoniker,
    IAMMultiMediaStreamImpl_Render
};