1 /*****************************************************************************
2 * crossbar.c : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 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 *****************************************************************************/
35 /* Work-around a bug in w32api-2.5 */
36 # define QACONTAINERFLAGS QACONTAINERFLAGS_ANOTHERSOMETHINGELSE
41 /*****************************************************************************
42 * DeleteCrossbarRoutes
43 *****************************************************************************/
44 void DeleteCrossbarRoutes( access_sys_t *p_sys )
46 /* Remove crossbar filters from graph */
47 for( int i = 0; i < p_sys->i_crossbar_route_depth; i++ )
49 p_sys->crossbar_routes[i].pXbar->Release();
51 p_sys->i_crossbar_route_depth = 0;
54 /*****************************************************************************
55 * RouteCrossbars (Does not AddRef the returned *Pin)
56 *****************************************************************************/
57 static HRESULT GetCrossbarIPinAtIndex( IAMCrossbar *pXbar, LONG PinIndex,
58 BOOL IsInputPin, IPin ** ppPin )
60 LONG cntInPins, cntOutPins;
62 IBaseFilter *pFilter = NULL;
66 if( !pXbar || !ppPin ) return E_POINTER;
70 if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) ) return E_FAIL;
72 LONG TrueIndex = IsInputPin ? PinIndex : PinIndex + cntInPins;
74 if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
76 if( SUCCEEDED(pFilter->EnumPins(&pins)) )
79 while( pins->Next(1, &pP, &n) == S_OK )
94 return *ppPin ? S_OK : E_FAIL;
97 /*****************************************************************************
98 * GetCrossbarIndexFromIPin: Find corresponding index of an IPin on a crossbar
99 *****************************************************************************/
100 static HRESULT GetCrossbarIndexFromIPin( IAMCrossbar * pXbar, LONG * PinIndex,
101 BOOL IsInputPin, IPin * pPin )
103 LONG cntInPins, cntOutPins;
105 IBaseFilter *pFilter = NULL;
110 if(!pXbar || !PinIndex || !pPin )
113 if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) )
116 if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
118 if( SUCCEEDED(pFilter->EnumPins(&pins)) )
122 while( pins->Next(1, &pP, &n) == S_OK )
127 *PinIndex = IsInputPin ? i : i - cntInPins;
138 return fOK ? S_OK : E_FAIL;
141 /*****************************************************************************
143 *****************************************************************************/
144 HRESULT FindCrossbarRoutes( vlc_object_t *p_this, access_sys_t *p_sys,
145 IPin *p_input_pin, LONG physicalType, int depth )
147 HRESULT result = S_FALSE;
150 if( FAILED(p_input_pin->ConnectedTo(&p_output_pin)) ) return S_FALSE;
152 // It is connected, so now find out if the filter supports IAMCrossbar
154 if( FAILED(p_output_pin->QueryPinInfo(&pinInfo)) ||
155 PINDIR_OUTPUT != pinInfo.dir )
157 p_output_pin->Release ();
161 IAMCrossbar *pXbar=0;
162 if( FAILED(pinInfo.pFilter->QueryInterface(IID_IAMCrossbar,
165 pinInfo.pFilter->Release();
166 p_output_pin->Release ();
170 LONG inputPinCount, outputPinCount;
171 if( FAILED(pXbar->get_PinCounts(&outputPinCount, &inputPinCount)) )
174 pinInfo.pFilter->Release();
175 p_output_pin->Release ();
179 LONG inputPinIndexRelated, outputPinIndexRelated;
180 LONG inputPinPhysicalType = 0, outputPinPhysicalType;
181 LONG inputPinIndex = 0, outputPinIndex;
182 if( FAILED(GetCrossbarIndexFromIPin( pXbar, &outputPinIndex,
183 FALSE, p_output_pin )) ||
184 FAILED(pXbar->get_CrossbarPinInfo( FALSE, outputPinIndex,
185 &outputPinIndexRelated,
186 &outputPinPhysicalType )) )
189 pinInfo.pFilter->Release();
190 p_output_pin->Release ();
195 ** if physical type is 0, then use default/existing route to physical connector
197 if( physicalType == 0 )
199 /* use following as default connector type if we fail to find an existing route */
200 physicalType = PhysConn_Video_Tuner;
201 if( SUCCEEDED(pXbar->get_IsRoutedTo(outputPinIndex, &inputPinIndex)) )
204 if( SUCCEEDED( pXbar->get_CrossbarPinInfo( TRUE, inputPinIndex,
205 &inputPinIndexRelated, &inputPinPhysicalType )) )
207 // remember connector type
208 physicalType = inputPinPhysicalType;
210 msg_Dbg( p_this, "found existing route for output %ld (type %ld) to input %ld (type %ld)",
211 outputPinIndex, outputPinPhysicalType, inputPinIndex,
212 inputPinPhysicalType );
214 // fall through to for loop, note 'inputPinIndex' is set to the pin we are looking for
215 // hence, loop iteration should not wind back
220 // reset to first pin for complete loop iteration
226 // for all input pins
228 for( /* inputPinIndex has been set */ ; (S_OK != result) && (inputPinIndex < inputPinCount); ++inputPinIndex )
230 if( FAILED(pXbar->get_CrossbarPinInfo( TRUE, inputPinIndex,
231 &inputPinIndexRelated, &inputPinPhysicalType )) ) continue;
233 // Is this pin matching required connector physical type?
234 if( inputPinPhysicalType != physicalType ) continue;
237 if( FAILED(pXbar->CanRoute(outputPinIndex, inputPinIndex)) ) continue;
241 if( FAILED(GetCrossbarIPinAtIndex( pXbar, inputPinIndex,
242 TRUE, &pPin)) ) continue;
244 result = FindCrossbarRoutes( p_this, p_sys, pPin,
245 physicalType, depth+1 );
247 if( S_OK == result || (S_FALSE == result &&
248 physicalType == inputPinPhysicalType &&
249 (p_sys->i_crossbar_route_depth = depth+1) < MAX_CROSSBAR_DEPTH) )
251 // hold on crossbar, will be released when graph is destroyed
254 // remember crossbar route
255 p_sys->crossbar_routes[depth].pXbar = pXbar;
256 p_sys->crossbar_routes[depth].VideoInputIndex = inputPinIndex;
257 p_sys->crossbar_routes[depth].VideoOutputIndex = outputPinIndex;
258 p_sys->crossbar_routes[depth].AudioInputIndex = inputPinIndexRelated;
259 p_sys->crossbar_routes[depth].AudioOutputIndex = outputPinIndexRelated;
261 msg_Dbg( p_this, "crossbar at depth %d, found route for "
262 "output %ld (type %ld) to input %ld (type %ld)", depth,
263 outputPinIndex, outputPinPhysicalType, inputPinIndex,
264 inputPinPhysicalType );
271 pinInfo.pFilter->Release();
272 p_output_pin->Release ();