]> git.sesse.net Git - vlc/blob - modules/access/dshow/crossbar.cpp
DShow: update Copyright years
[vlc] / modules / access / dshow / crossbar.cpp
1 /*****************************************************************************
2  * crossbar.c : DirectShow access module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002, 2004, 2009 the VideoLAN team
5  * $Id$
6  *
7  * Author: Damien Fouilleul <damien dot fouilleul at laposte dot net>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #ifndef _MSC_VER
35     /* Work-around a bug in w32api-2.5 */
36 #   define QACONTAINERFLAGS QACONTAINERFLAGS_ANOTHERSOMETHINGELSE
37 #endif
38
39 #include "common.h"
40
41
42
43 // Helper function to associate a crossbar pin name with the type.
44 static const char * GetPhysicalPinName(long lType)
45 {
46     switch (lType)
47     {
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";
61
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";
72
73     default:                              return "Unknown Type";
74     }
75 }
76 /*****************************************************************************
77  * DeleteCrossbarRoutes
78  *****************************************************************************/
79 void DeleteCrossbarRoutes( access_sys_t *p_sys )
80 {
81     /* Remove crossbar filters from graph */
82     for( int i = 0; i < p_sys->i_crossbar_route_depth; i++ )
83     {
84         p_sys->crossbar_routes[i].pXbar->Release();
85     }
86     p_sys->i_crossbar_route_depth = 0;
87 }
88
89 /*****************************************************************************
90  * RouteCrossbars (Does not AddRef the returned *Pin)
91  *****************************************************************************/
92 static HRESULT GetCrossbarIPinAtIndex( IAMCrossbar *pXbar, LONG PinIndex,
93                                        BOOL IsInputPin, IPin ** ppPin )
94 {
95     LONG         cntInPins, cntOutPins;
96     IPin        *pP = NULL;
97     IBaseFilter *pFilter = NULL;
98     IEnumPins   *pins = NULL;
99     ULONG        n;
100
101     if( !pXbar || !ppPin ) return E_POINTER;
102
103     *ppPin = 0;
104
105     if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) ) return E_FAIL;
106
107     LONG TrueIndex = IsInputPin ? PinIndex : PinIndex + cntInPins;
108
109     if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
110     {
111         if( SUCCEEDED(pFilter->EnumPins(&pins)) )
112         {
113             LONG i = 0;
114             while( pins->Next(1, &pP, &n) == S_OK )
115             {
116                 pP->Release();
117                 if( i == TrueIndex )
118                 {
119                     *ppPin = pP;
120                     break;
121                 }
122                 i++;
123             }
124             pins->Release();
125         }
126         pFilter->Release();
127     }
128
129     return *ppPin ? S_OK : E_FAIL;
130 }
131
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 )
137 {
138     LONG         cntInPins, cntOutPins;
139     IPin        *pP = NULL;
140     IBaseFilter *pFilter = NULL;
141     IEnumPins   *pins = NULL;
142     ULONG        n;
143     BOOL         fOK = FALSE;
144
145     if(!pXbar || !PinIndex || !pPin )
146         return E_POINTER;
147
148     if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) )
149         return E_FAIL;
150
151     if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
152     {
153         if( SUCCEEDED(pFilter->EnumPins(&pins)) )
154         {
155             LONG i=0;
156
157             while( pins->Next(1, &pP, &n) == S_OK )
158             {
159                 pP->Release();
160                 if( pPin == pP )
161                 {
162                     *PinIndex = IsInputPin ? i : i - cntInPins;
163                     fOK = TRUE;
164                     break;
165                 }
166                 i++;
167             }
168             pins->Release();
169         }
170         pFilter->Release();
171     }
172
173     return fOK ? S_OK : E_FAIL;
174 }
175
176 /*****************************************************************************
177  * FindCrossbarRoutes
178  *****************************************************************************/
179 HRESULT FindCrossbarRoutes( vlc_object_t *p_this, access_sys_t *p_sys,
180                             IPin *p_input_pin, LONG physicalType, int depth )
181 {
182     HRESULT result = S_FALSE;
183
184     IPin *p_output_pin;
185     if( FAILED(p_input_pin->ConnectedTo(&p_output_pin)) ) return S_FALSE;
186
187     // It is connected, so now find out if the filter supports IAMCrossbar
188     PIN_INFO pinInfo;
189     if( FAILED(p_output_pin->QueryPinInfo(&pinInfo)) ||
190         PINDIR_OUTPUT != pinInfo.dir )
191     {
192         p_output_pin->Release ();
193         return S_FALSE;
194     }
195
196     IAMCrossbar *pXbar = NULL;
197     if( FAILED(pinInfo.pFilter->QueryInterface(IID_IAMCrossbar,
198                                                (void **)&pXbar)) )
199     {
200         pinInfo.pFilter->Release();
201         p_output_pin->Release ();
202         return S_FALSE;
203     }
204
205     LONG inputPinCount, outputPinCount;
206     if( FAILED(pXbar->get_PinCounts(&outputPinCount, &inputPinCount)) )
207     {
208         pXbar->Release();
209         pinInfo.pFilter->Release();
210         p_output_pin->Release ();
211         return S_FALSE;
212     }
213
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 )) )
222     {
223         pXbar->Release();
224         pinInfo.pFilter->Release();
225         p_output_pin->Release ();
226         return S_FALSE;
227     }
228
229     /*
230     ** if physical type is 0, then use default/existing route to physical connector
231     */
232     if( physicalType == 0 )
233     {
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)) )
237         {
238
239             if( SUCCEEDED( pXbar->get_CrossbarPinInfo( TRUE,  inputPinIndex,
240                            &inputPinIndexRelated, &inputPinPhysicalType )) )
241             {
242                 // remember connector type
243                 physicalType = inputPinPhysicalType;
244  
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 ) );
248  
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
251
252             }
253         }
254         else {
255             // reset to first pin for complete loop iteration
256             inputPinIndex = 0;
257         }
258     }
259  
260     //
261     // for all input pins
262     //
263     for( /* inputPinIndex has been set */ ; (S_OK != result) && (inputPinIndex < inputPinCount); ++inputPinIndex )
264     {
265         if( FAILED(pXbar->get_CrossbarPinInfo( TRUE,  inputPinIndex,
266             &inputPinIndexRelated, &inputPinPhysicalType )) ) continue;
267
268         // Is this pin matching required connector physical type?
269         if( inputPinPhysicalType != physicalType ) continue;
270
271         // Can we route it?
272         if( FAILED(pXbar->CanRoute(outputPinIndex, inputPinIndex)) ) continue;
273  
274  
275         IPin *pPin;
276         if( FAILED(GetCrossbarIPinAtIndex( pXbar, inputPinIndex,
277                                            TRUE, &pPin)) ) continue;
278
279         result = FindCrossbarRoutes( p_this, p_sys, pPin,
280                                      physicalType, depth+1 );
281
282         if( S_OK == result || (S_FALSE == result &&
283             physicalType == inputPinPhysicalType &&
284             (p_sys->i_crossbar_route_depth = depth+1) < MAX_CROSSBAR_DEPTH) )
285         {
286             // hold on crossbar, will be released when graph is destroyed
287             pXbar->AddRef();
288
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;
295
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 ) );
300
301             result = S_OK;
302         }
303     }
304
305     pXbar->Release();
306     pinInfo.pFilter->Release();
307     p_output_pin->Release ();
308
309     return result;
310 }