libavdevice/dshow_pin.c
Go to the documentation of this file.
00001 /*
00002  * DirectShow capture interface
00003  * Copyright (c) 2010 Ramiro Polla
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "dshow.h"
00023 
00024 #include <stddef.h>
00025 #define imemoffset offsetof(libAVPin, imemvtbl)
00026 
00027 DECLARE_QUERYINTERFACE(libAVPin,
00028     { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
00029 DECLARE_ADDREF(libAVPin)
00030 DECLARE_RELEASE(libAVPin)
00031 
00032 long WINAPI
00033 libAVPin_Connect(libAVPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
00034 {
00035     dshowdebug("libAVPin_Connect(%p, %p, %p)\n", this, pin, type);
00036     /* Input pins receive connections. */
00037     return S_FALSE;
00038 }
00039 long WINAPI
00040 libAVPin_ReceiveConnection(libAVPin *this, IPin *pin,
00041                            const AM_MEDIA_TYPE *type)
00042 {
00043     enum dshowDeviceType devtype = this->filter->type;
00044     dshowdebug("libAVPin_ReceiveConnection(%p)\n", this);
00045 
00046     if (!pin)
00047         return E_POINTER;
00048     if (this->connectedto)
00049         return VFW_E_ALREADY_CONNECTED;
00050 
00051     ff_print_AM_MEDIA_TYPE(type);
00052     if (devtype == VideoDevice) {
00053         if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video))
00054             return VFW_E_TYPE_NOT_ACCEPTED;
00055     } else {
00056         if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
00057             return VFW_E_TYPE_NOT_ACCEPTED;
00058     }
00059 
00060     IPin_AddRef(pin);
00061     this->connectedto = pin;
00062 
00063     ff_copy_dshow_media_type(&this->type, type);
00064 
00065     return S_OK;
00066 }
00067 long WINAPI
00068 libAVPin_Disconnect(libAVPin *this)
00069 {
00070     dshowdebug("libAVPin_Disconnect(%p)\n", this);
00071 
00072     if (this->filter->state != State_Stopped)
00073         return VFW_E_NOT_STOPPED;
00074     if (!this->connectedto)
00075         return S_FALSE;
00076     IPin_Release(this->connectedto);
00077     this->connectedto = NULL;
00078 
00079     return S_OK;
00080 }
00081 long WINAPI
00082 libAVPin_ConnectedTo(libAVPin *this, IPin **pin)
00083 {
00084     dshowdebug("libAVPin_ConnectedTo(%p)\n", this);
00085 
00086     if (!pin)
00087         return E_POINTER;
00088     if (!this->connectedto)
00089         return VFW_E_NOT_CONNECTED;
00090     IPin_AddRef(this->connectedto);
00091     *pin = this->connectedto;
00092 
00093     return S_OK;
00094 }
00095 long WINAPI
00096 libAVPin_ConnectionMediaType(libAVPin *this, AM_MEDIA_TYPE *type)
00097 {
00098     dshowdebug("libAVPin_ConnectionMediaType(%p)\n", this);
00099 
00100     if (!type)
00101         return E_POINTER;
00102     if (!this->connectedto)
00103         return VFW_E_NOT_CONNECTED;
00104 
00105     return ff_copy_dshow_media_type(type, &this->type);
00106 }
00107 long WINAPI
00108 libAVPin_QueryPinInfo(libAVPin *this, PIN_INFO *info)
00109 {
00110     dshowdebug("libAVPin_QueryPinInfo(%p)\n", this);
00111 
00112     if (!info)
00113         return E_POINTER;
00114 
00115     if (this->filter)
00116         libAVFilter_AddRef(this->filter);
00117 
00118     info->pFilter = (IBaseFilter *) this->filter;
00119     info->dir     = PINDIR_INPUT;
00120     wcscpy(info->achName, L"Capture");
00121 
00122     return S_OK;
00123 }
00124 long WINAPI
00125 libAVPin_QueryDirection(libAVPin *this, PIN_DIRECTION *dir)
00126 {
00127     dshowdebug("libAVPin_QueryDirection(%p)\n", this);
00128     if (!dir)
00129         return E_POINTER;
00130     *dir = PINDIR_INPUT;
00131     return S_OK;
00132 }
00133 long WINAPI
00134 libAVPin_QueryId(libAVPin *this, wchar_t **id)
00135 {
00136     dshowdebug("libAVPin_QueryId(%p)\n", this);
00137 
00138     if (!id)
00139         return E_POINTER;
00140 
00141     *id = wcsdup(L"libAV Pin");
00142 
00143     return S_OK;
00144 }
00145 long WINAPI
00146 libAVPin_QueryAccept(libAVPin *this, const AM_MEDIA_TYPE *type)
00147 {
00148     dshowdebug("libAVPin_QueryAccept(%p)\n", this);
00149     return S_FALSE;
00150 }
00151 long WINAPI
00152 libAVPin_EnumMediaTypes(libAVPin *this, IEnumMediaTypes **enumtypes)
00153 {
00154     const AM_MEDIA_TYPE *type = NULL;
00155     libAVEnumMediaTypes *new;
00156     dshowdebug("libAVPin_EnumMediaTypes(%p)\n", this);
00157 
00158     if (!enumtypes)
00159         return E_POINTER;
00160     new = libAVEnumMediaTypes_Create(type);
00161     if (!new)
00162         return E_OUTOFMEMORY;
00163 
00164     *enumtypes = (IEnumMediaTypes *) new;
00165     return S_OK;
00166 }
00167 long WINAPI
00168 libAVPin_QueryInternalConnections(libAVPin *this, IPin **pin,
00169                                   unsigned long *npin)
00170 {
00171     dshowdebug("libAVPin_QueryInternalConnections(%p)\n", this);
00172     return E_NOTIMPL;
00173 }
00174 long WINAPI
00175 libAVPin_EndOfStream(libAVPin *this)
00176 {
00177     dshowdebug("libAVPin_EndOfStream(%p)\n", this);
00178     /* I don't care. */
00179     return S_OK;
00180 }
00181 long WINAPI
00182 libAVPin_BeginFlush(libAVPin *this)
00183 {
00184     dshowdebug("libAVPin_BeginFlush(%p)\n", this);
00185     /* I don't care. */
00186     return S_OK;
00187 }
00188 long WINAPI
00189 libAVPin_EndFlush(libAVPin *this)
00190 {
00191     dshowdebug("libAVPin_EndFlush(%p)\n", this);
00192     /* I don't care. */
00193     return S_OK;
00194 }
00195 long WINAPI
00196 libAVPin_NewSegment(libAVPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
00197                     double rate)
00198 {
00199     dshowdebug("libAVPin_NewSegment(%p)\n", this);
00200     /* I don't care. */
00201     return S_OK;
00202 }
00203 
00204 static int
00205 libAVPin_Setup(libAVPin *this, libAVFilter *filter)
00206 {
00207     IPinVtbl *vtbl = this->vtbl;
00208     IMemInputPinVtbl *imemvtbl;
00209 
00210     if (!filter)
00211         return 0;
00212 
00213     imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
00214     if (!imemvtbl)
00215         return 0;
00216 
00217     SETVTBL(imemvtbl, libAVMemInputPin, QueryInterface);
00218     SETVTBL(imemvtbl, libAVMemInputPin, AddRef);
00219     SETVTBL(imemvtbl, libAVMemInputPin, Release);
00220     SETVTBL(imemvtbl, libAVMemInputPin, GetAllocator);
00221     SETVTBL(imemvtbl, libAVMemInputPin, NotifyAllocator);
00222     SETVTBL(imemvtbl, libAVMemInputPin, GetAllocatorRequirements);
00223     SETVTBL(imemvtbl, libAVMemInputPin, Receive);
00224     SETVTBL(imemvtbl, libAVMemInputPin, ReceiveMultiple);
00225     SETVTBL(imemvtbl, libAVMemInputPin, ReceiveCanBlock);
00226 
00227     this->imemvtbl = imemvtbl;
00228 
00229     SETVTBL(vtbl, libAVPin, QueryInterface);
00230     SETVTBL(vtbl, libAVPin, AddRef);
00231     SETVTBL(vtbl, libAVPin, Release);
00232     SETVTBL(vtbl, libAVPin, Connect);
00233     SETVTBL(vtbl, libAVPin, ReceiveConnection);
00234     SETVTBL(vtbl, libAVPin, Disconnect);
00235     SETVTBL(vtbl, libAVPin, ConnectedTo);
00236     SETVTBL(vtbl, libAVPin, ConnectionMediaType);
00237     SETVTBL(vtbl, libAVPin, QueryPinInfo);
00238     SETVTBL(vtbl, libAVPin, QueryDirection);
00239     SETVTBL(vtbl, libAVPin, QueryId);
00240     SETVTBL(vtbl, libAVPin, QueryAccept);
00241     SETVTBL(vtbl, libAVPin, EnumMediaTypes);
00242     SETVTBL(vtbl, libAVPin, QueryInternalConnections);
00243     SETVTBL(vtbl, libAVPin, EndOfStream);
00244     SETVTBL(vtbl, libAVPin, BeginFlush);
00245     SETVTBL(vtbl, libAVPin, EndFlush);
00246     SETVTBL(vtbl, libAVPin, NewSegment);
00247 
00248     this->filter = filter;
00249 
00250     return 1;
00251 }
00252 DECLARE_CREATE(libAVPin, libAVPin_Setup(this, filter), libAVFilter *filter)
00253 DECLARE_DESTROY(libAVPin, nothing)
00254 
00255 /*****************************************************************************
00256  * libAVMemInputPin
00257  ****************************************************************************/
00258 long WINAPI
00259 libAVMemInputPin_QueryInterface(libAVMemInputPin *this, const GUID *riid,
00260                                 void **ppvObject)
00261 {
00262     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
00263     dshowdebug("libAVMemInputPin_QueryInterface(%p)\n", this);
00264     return libAVPin_QueryInterface(pin, riid, ppvObject);
00265 }
00266 unsigned long WINAPI
00267 libAVMemInputPin_AddRef(libAVMemInputPin *this)
00268 {
00269     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
00270     dshowdebug("libAVMemInputPin_AddRef(%p)\n", this);
00271     return libAVPin_AddRef(pin);
00272 }
00273 unsigned long WINAPI
00274 libAVMemInputPin_Release(libAVMemInputPin *this)
00275 {
00276     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
00277     dshowdebug("libAVMemInputPin_Release(%p)\n", this);
00278     return libAVPin_Release(pin);
00279 }
00280 long WINAPI
00281 libAVMemInputPin_GetAllocator(libAVMemInputPin *this, IMemAllocator **alloc)
00282 {
00283     dshowdebug("libAVMemInputPin_GetAllocator(%p)\n", this);
00284     return VFW_E_NO_ALLOCATOR;
00285 }
00286 long WINAPI
00287 libAVMemInputPin_NotifyAllocator(libAVMemInputPin *this, IMemAllocator *alloc,
00288                                  WINBOOL rdwr)
00289 {
00290     dshowdebug("libAVMemInputPin_NotifyAllocator(%p)\n", this);
00291     return S_OK;
00292 }
00293 long WINAPI
00294 libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *this,
00295                                           ALLOCATOR_PROPERTIES *props)
00296 {
00297     dshowdebug("libAVMemInputPin_GetAllocatorRequirements(%p)\n", this);
00298     return E_NOTIMPL;
00299 }
00300 long WINAPI
00301 libAVMemInputPin_Receive(libAVMemInputPin *this, IMediaSample *sample)
00302 {
00303     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
00304     enum dshowDeviceType devtype = pin->filter->type;
00305     void *priv_data;
00306     uint8_t *buf;
00307     int buf_size;
00308     int index;
00309     int64_t curtime;
00310 
00311     dshowdebug("libAVMemInputPin_Receive(%p)\n", this);
00312 
00313     if (!sample)
00314         return E_POINTER;
00315 
00316     if (devtype == VideoDevice) {
00317         /* PTS from video devices is unreliable. */
00318         IReferenceClock *clock = pin->filter->clock;
00319         IReferenceClock_GetTime(clock, &curtime);
00320     } else {
00321         int64_t dummy;
00322         IMediaSample_GetTime(sample, &curtime, &dummy);
00323         curtime += pin->filter->start_time;
00324     }
00325 
00326     buf_size = IMediaSample_GetActualDataLength(sample);
00327     IMediaSample_GetPointer(sample, &buf);
00328     priv_data = pin->filter->priv_data;
00329     index = pin->filter->stream_index;
00330 
00331     pin->filter->callback(priv_data, index, buf, buf_size, curtime);
00332 
00333     return S_OK;
00334 }
00335 long WINAPI
00336 libAVMemInputPin_ReceiveMultiple(libAVMemInputPin *this,
00337                                  IMediaSample **samples, long n, long *nproc)
00338 {
00339     int i;
00340     dshowdebug("libAVMemInputPin_ReceiveMultiple(%p)\n", this);
00341 
00342     for (i = 0; i < n; i++)
00343         libAVMemInputPin_Receive(this, samples[i]);
00344 
00345     *nproc = n;
00346     return S_OK;
00347 }
00348 long WINAPI
00349 libAVMemInputPin_ReceiveCanBlock(libAVMemInputPin *this)
00350 {
00351     dshowdebug("libAVMemInputPin_ReceiveCanBlock(%p)\n", this);
00352     /* I swear I will not block. */
00353     return S_FALSE;
00354 }
00355 
00356 void
00357 libAVMemInputPin_Destroy(libAVMemInputPin *this)
00358 {
00359     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
00360     dshowdebug("libAVMemInputPin_Destroy(%p)\n", this);
00361     return libAVPin_Destroy(pin);
00362 }