1 /*****************************************************************************
2 * crossbar.c : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2004, 2009 the VideoLAN team
7 * Author: Damien Fouilleul <damien dot fouilleul at laposte dot net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
35 /* Work-around a bug in w32api-2.5 */
36 # define QACONTAINERFLAGS QACONTAINERFLAGS_ANOTHERSOMETHINGELSE
43 // Helper function to associate a crossbar pin name with the type.
44 static const char * GetPhysicalPinName(long lType)
48 case PhysConn_Video_Tuner: return "Video Tuner";
49 case PhysConn_Video_Composite: return "Video Composite";
50 case PhysConn_Video_SVideo: return "S-Video";
51 case PhysConn_Video_RGB: return "Video RGB";
52 case PhysConn_Video_YRYBY: return "Video YRYBY";
53 case PhysConn_Video_SerialDigital: return "Video Serial Digital";
54 case PhysConn_Video_ParallelDigital: return "Video Parallel Digital";
55 case PhysConn_Video_SCSI: return "Video SCSI";
56 case PhysConn_Video_AUX: return "Video AUX";
57 case PhysConn_Video_1394: return "Video 1394";
58 case PhysConn_Video_USB: return "Video USB";
59 case PhysConn_Video_VideoDecoder: return "Video Decoder";
60 case PhysConn_Video_VideoEncoder: return "Video Encoder";
62 case PhysConn_Audio_Tuner: return "Audio Tuner";
63 case PhysConn_Audio_Line: return "Audio Line";
64 case PhysConn_Audio_Mic: return "Audio Microphone";
65 case PhysConn_Audio_AESDigital: return "Audio AES/EBU Digital";
66 case PhysConn_Audio_SPDIFDigital: return "Audio S/PDIF";
67 case PhysConn_Audio_SCSI: return "Audio SCSI";
68 case PhysConn_Audio_AUX: return "Audio AUX";
69 case PhysConn_Audio_1394: return "Audio 1394";
70 case PhysConn_Audio_USB: return "Audio USB";
71 case PhysConn_Audio_AudioDecoder: return "Audio Decoder";
73 default: return "Unknown Type";
76 /*****************************************************************************
77 * DeleteCrossbarRoutes
78 *****************************************************************************/
79 void DeleteCrossbarRoutes( access_sys_t *p_sys )
81 /* Remove crossbar filters from graph */
82 for( int i = 0; i < p_sys->i_crossbar_route_depth; i++ )
84 p_sys->crossbar_routes[i].pXbar->Release();
86 p_sys->i_crossbar_route_depth = 0;
89 /*****************************************************************************
90 * RouteCrossbars (Does not AddRef the returned *Pin)
91 *****************************************************************************/
92 static HRESULT GetCrossbarIPinAtIndex( IAMCrossbar *pXbar, LONG PinIndex,
93 BOOL IsInputPin, IPin ** ppPin )
95 LONG cntInPins, cntOutPins;
97 IBaseFilter *pFilter = NULL;
98 IEnumPins *pins = NULL;
101 if( !pXbar || !ppPin ) return E_POINTER;
105 if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) ) return E_FAIL;
107 LONG TrueIndex = IsInputPin ? PinIndex : PinIndex + cntInPins;
109 if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
111 if( SUCCEEDED(pFilter->EnumPins(&pins)) )
114 while( pins->Next(1, &pP, &n) == S_OK )
129 return *ppPin ? S_OK : E_FAIL;
132 /*****************************************************************************
133 * GetCrossbarIndexFromIPin: Find corresponding index of an IPin on a crossbar
134 *****************************************************************************/
135 static HRESULT GetCrossbarIndexFromIPin( IAMCrossbar * pXbar, LONG * PinIndex,
136 BOOL IsInputPin, IPin * pPin )
138 LONG cntInPins, cntOutPins;
140 IBaseFilter *pFilter = NULL;
141 IEnumPins *pins = NULL;
145 if(!pXbar || !PinIndex || !pPin )
148 if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) )
151 if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
153 if( SUCCEEDED(pFilter->EnumPins(&pins)) )
157 while( pins->Next(1, &pP, &n) == S_OK )
162 *PinIndex = IsInputPin ? i : i - cntInPins;
173 return fOK ? S_OK : E_FAIL;
176 /*****************************************************************************
178 *****************************************************************************/
179 HRESULT FindCrossbarRoutes( vlc_object_t *p_this, access_sys_t *p_sys,
180 IPin *p_input_pin, LONG physicalType, int depth )
182 HRESULT result = S_FALSE;
185 if( FAILED(p_input_pin->ConnectedTo(&p_output_pin)) ) return S_FALSE;
187 // It is connected, so now find out if the filter supports IAMCrossbar
189 if( FAILED(p_output_pin->QueryPinInfo(&pinInfo)) ||
190 PINDIR_OUTPUT != pinInfo.dir )
192 p_output_pin->Release ();
196 IAMCrossbar *pXbar = NULL;
197 if( FAILED(pinInfo.pFilter->QueryInterface(IID_IAMCrossbar,
200 pinInfo.pFilter->Release();
201 p_output_pin->Release ();
205 LONG inputPinCount, outputPinCount;
206 if( FAILED(pXbar->get_PinCounts(&outputPinCount, &inputPinCount)) )
209 pinInfo.pFilter->Release();
210 p_output_pin->Release ();
214 LONG inputPinIndexRelated, outputPinIndexRelated;
215 LONG inputPinPhysicalType = 0, outputPinPhysicalType;
216 LONG inputPinIndex = 0, outputPinIndex;
217 if( FAILED(GetCrossbarIndexFromIPin( pXbar, &outputPinIndex,
218 FALSE, p_output_pin )) ||
219 FAILED(pXbar->get_CrossbarPinInfo( FALSE, outputPinIndex,
220 &outputPinIndexRelated,
221 &outputPinPhysicalType )) )
224 pinInfo.pFilter->Release();
225 p_output_pin->Release ();
230 ** if physical type is 0, then use default/existing route to physical connector
232 if( physicalType == 0 )
234 /* use following as default connector type if we fail to find an existing route */
235 physicalType = PhysConn_Video_Tuner;
236 if( SUCCEEDED(pXbar->get_IsRoutedTo(outputPinIndex, &inputPinIndex)) )
239 if( SUCCEEDED( pXbar->get_CrossbarPinInfo( TRUE, inputPinIndex,
240 &inputPinIndexRelated, &inputPinPhysicalType )) )
242 // remember connector type
243 physicalType = inputPinPhysicalType;
245 msg_Dbg( p_this, "found existing route for output %ld (type %s) to input %ld (type %s)",
246 outputPinIndex, GetPhysicalPinName( outputPinPhysicalType ),
247 inputPinIndex, GetPhysicalPinName( inputPinPhysicalType ) );
249 // fall through to for loop, note 'inputPinIndex' is set to the pin we are looking for
250 // hence, loop iteration should not wind back
255 // reset to first pin for complete loop iteration
261 // for all input pins
263 for( /* inputPinIndex has been set */ ; (S_OK != result) && (inputPinIndex < inputPinCount); ++inputPinIndex )
265 if( FAILED(pXbar->get_CrossbarPinInfo( TRUE, inputPinIndex,
266 &inputPinIndexRelated, &inputPinPhysicalType )) ) continue;
268 // Is this pin matching required connector physical type?
269 if( inputPinPhysicalType != physicalType ) continue;
272 if( FAILED(pXbar->CanRoute(outputPinIndex, inputPinIndex)) ) continue;
276 if( FAILED(GetCrossbarIPinAtIndex( pXbar, inputPinIndex,
277 TRUE, &pPin)) ) continue;
279 result = FindCrossbarRoutes( p_this, p_sys, pPin,
280 physicalType, depth+1 );
282 if( S_OK == result || (S_FALSE == result &&
283 physicalType == inputPinPhysicalType &&
284 (p_sys->i_crossbar_route_depth = depth+1) < MAX_CROSSBAR_DEPTH) )
286 // hold on crossbar, will be released when graph is destroyed
289 // remember crossbar route
290 p_sys->crossbar_routes[depth].pXbar = pXbar;
291 p_sys->crossbar_routes[depth].VideoInputIndex = inputPinIndex;
292 p_sys->crossbar_routes[depth].VideoOutputIndex = outputPinIndex;
293 p_sys->crossbar_routes[depth].AudioInputIndex = inputPinIndexRelated;
294 p_sys->crossbar_routes[depth].AudioOutputIndex = outputPinIndexRelated;
296 msg_Dbg( p_this, "crossbar at depth %d, found route for "
297 "output %ld (type %s) to input %ld (type %s)", depth,
298 outputPinIndex, GetPhysicalPinName( outputPinPhysicalType ),
299 inputPinIndex, GetPhysicalPinName( inputPinPhysicalType ) );
306 pinInfo.pFilter->Release();
307 p_output_pin->Release ();