dshow_crossbar.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * DirectShow capture interface
  3. * Copyright (c) 2015 Roger Pack
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "dshow_capture.h"
  22. static const char *
  23. GetPhysicalPinName(long pin_type)
  24. {
  25. switch (pin_type)
  26. {
  27. case PhysConn_Video_Tuner: return "Video Tuner";
  28. case PhysConn_Video_Composite: return "Video Composite";
  29. case PhysConn_Video_SVideo: return "S-Video";
  30. case PhysConn_Video_RGB: return "Video RGB";
  31. case PhysConn_Video_YRYBY: return "Video YRYBY";
  32. case PhysConn_Video_SerialDigital: return "Video Serial Digital";
  33. case PhysConn_Video_ParallelDigital: return "Video Parallel Digital";
  34. case PhysConn_Video_SCSI: return "Video SCSI";
  35. case PhysConn_Video_AUX: return "Video AUX";
  36. case PhysConn_Video_1394: return "Video 1394";
  37. case PhysConn_Video_USB: return "Video USB";
  38. case PhysConn_Video_VideoDecoder: return "Video Decoder";
  39. case PhysConn_Video_VideoEncoder: return "Video Encoder";
  40. case PhysConn_Audio_Tuner: return "Audio Tuner";
  41. case PhysConn_Audio_Line: return "Audio Line";
  42. case PhysConn_Audio_Mic: return "Audio Microphone";
  43. case PhysConn_Audio_AESDigital: return "Audio AES/EBU Digital";
  44. case PhysConn_Audio_SPDIFDigital: return "Audio S/PDIF";
  45. case PhysConn_Audio_SCSI: return "Audio SCSI";
  46. case PhysConn_Audio_AUX: return "Audio AUX";
  47. case PhysConn_Audio_1394: return "Audio 1394";
  48. case PhysConn_Audio_USB: return "Audio USB";
  49. case PhysConn_Audio_AudioDecoder: return "Audio Decoder";
  50. default: return "Unknown Crossbar Pin Type—Please report!";
  51. }
  52. }
  53. static HRESULT
  54. setup_crossbar_options(IAMCrossbar *cross_bar, enum dshowDeviceType devtype, AVFormatContext *avctx)
  55. {
  56. struct dshow_ctx *ctx = avctx->priv_data;
  57. long count_output_pins, count_input_pins;
  58. int i;
  59. int log_level = ctx->list_options ? AV_LOG_INFO : AV_LOG_DEBUG;
  60. int video_input_pin = ctx->crossbar_video_input_pin_number;
  61. int audio_input_pin = ctx->crossbar_audio_input_pin_number;
  62. const char *device_name = ctx->device_name[devtype];
  63. HRESULT hr;
  64. av_log(avctx, log_level, "Crossbar Switching Information for %s:\n", device_name);
  65. hr = IAMCrossbar_get_PinCounts(cross_bar, &count_output_pins, &count_input_pins);
  66. if (hr != S_OK) {
  67. av_log(avctx, AV_LOG_ERROR, "Unable to get crossbar pin counts\n");
  68. return hr;
  69. }
  70. for (i = 0; i < count_output_pins; i++)
  71. {
  72. int j;
  73. long related_pin, pin_type, route_to_pin;
  74. hr = IAMCrossbar_get_CrossbarPinInfo(cross_bar, FALSE, i, &related_pin, &pin_type);
  75. if (pin_type == PhysConn_Video_VideoDecoder) {
  76. /* assume there is only one "Video (and one Audio) Decoder" output pin, and it's all we care about routing to...for now */
  77. if (video_input_pin != -1) {
  78. av_log(avctx, log_level, "Routing video input from pin %d\n", video_input_pin);
  79. hr = IAMCrossbar_Route(cross_bar, i, video_input_pin);
  80. if (hr != S_OK) {
  81. av_log(avctx, AV_LOG_ERROR, "Unable to route video input from pin %d\n", video_input_pin);
  82. return AVERROR(EIO);
  83. }
  84. }
  85. } else if (pin_type == PhysConn_Audio_AudioDecoder) {
  86. if (audio_input_pin != -1) {
  87. av_log(avctx, log_level, "Routing audio input from pin %d\n", audio_input_pin);
  88. hr = IAMCrossbar_Route(cross_bar, i, audio_input_pin);
  89. if (hr != S_OK) {
  90. av_log(avctx, AV_LOG_ERROR, "Unable to route audio input from pin %d\n", audio_input_pin);
  91. return hr;
  92. }
  93. }
  94. } else {
  95. av_log(avctx, AV_LOG_WARNING, "Unexpected output pin type, please report the type if you want to use this (%s)", GetPhysicalPinName(pin_type));
  96. }
  97. hr = IAMCrossbar_get_IsRoutedTo(cross_bar, i, &route_to_pin);
  98. if (hr != S_OK) {
  99. av_log(avctx, AV_LOG_ERROR, "Unable to get crossbar is routed to from pin %d\n", i);
  100. return hr;
  101. }
  102. av_log(avctx, log_level, " Crossbar Output pin %d: \"%s\" related output pin: %ld ", i, GetPhysicalPinName(pin_type), related_pin);
  103. av_log(avctx, log_level, "current input pin: %ld ", route_to_pin);
  104. av_log(avctx, log_level, "compatible input pins: ");
  105. for (j = 0; j < count_input_pins; j++)
  106. {
  107. hr = IAMCrossbar_CanRoute(cross_bar, i, j);
  108. if (hr == S_OK)
  109. av_log(avctx, log_level ,"%d ", j);
  110. }
  111. av_log(avctx, log_level, "\n");
  112. }
  113. for (i = 0; i < count_input_pins; i++)
  114. {
  115. long related_pin, pin_type;
  116. hr = IAMCrossbar_get_CrossbarPinInfo(cross_bar, TRUE, i, &related_pin, &pin_type);
  117. if (hr != S_OK) {
  118. av_log(avctx, AV_LOG_ERROR, "unable to get crossbar info audio input from pin %d\n", i);
  119. return hr;
  120. }
  121. av_log(avctx, log_level, " Crossbar Input pin %d - \"%s\" ", i, GetPhysicalPinName(pin_type));
  122. av_log(avctx, log_level, "related input pin: %ld\n", related_pin);
  123. }
  124. return S_OK;
  125. }
  126. /**
  127. * Given a fully constructed graph, check if there is a cross bar filter, and configure its pins if so.
  128. */
  129. HRESULT
  130. dshow_try_setup_crossbar_options(ICaptureGraphBuilder2 *graph_builder2,
  131. IBaseFilter *device_filter, enum dshowDeviceType devtype, AVFormatContext *avctx)
  132. {
  133. struct dshow_ctx *ctx = avctx->priv_data;
  134. IAMCrossbar *cross_bar = NULL;
  135. IBaseFilter *cross_bar_base_filter = NULL;
  136. IAMTVTuner *tv_tuner_filter = NULL;
  137. IBaseFilter *tv_tuner_base_filter = NULL;
  138. IAMAudioInputMixer *tv_audio_filter = NULL;
  139. IBaseFilter *tv_audio_base_filter = NULL;
  140. HRESULT hr;
  141. hr = ICaptureGraphBuilder2_FindInterface(graph_builder2, &LOOK_UPSTREAM_ONLY, (const GUID *) NULL,
  142. device_filter, &IID_IAMCrossbar, (void**) &cross_bar);
  143. if (hr != S_OK) {
  144. /* no crossbar found */
  145. hr = S_OK;
  146. goto end;
  147. }
  148. /* TODO some TV tuners apparently have multiple crossbars? */
  149. if (devtype == VideoDevice && ctx->show_video_crossbar_connection_dialog ||
  150. devtype == AudioDevice && ctx->show_audio_crossbar_connection_dialog) {
  151. hr = IAMCrossbar_QueryInterface(cross_bar, &IID_IBaseFilter, (void **) &cross_bar_base_filter);
  152. if (hr != S_OK)
  153. goto end;
  154. dshow_show_filter_properties(cross_bar_base_filter, avctx);
  155. }
  156. if (devtype == VideoDevice && ctx->show_analog_tv_tuner_dialog) {
  157. hr = ICaptureGraphBuilder2_FindInterface(graph_builder2, &LOOK_UPSTREAM_ONLY, NULL,
  158. device_filter, &IID_IAMTVTuner, (void**) &tv_tuner_filter);
  159. if (hr == S_OK) {
  160. hr = IAMCrossbar_QueryInterface(tv_tuner_filter, &IID_IBaseFilter, (void **) &tv_tuner_base_filter);
  161. if (hr != S_OK)
  162. goto end;
  163. dshow_show_filter_properties(tv_tuner_base_filter, avctx);
  164. } else {
  165. av_log(avctx, AV_LOG_WARNING, "unable to find a tv tuner to display dialog for!");
  166. }
  167. }
  168. if (devtype == AudioDevice && ctx->show_analog_tv_tuner_audio_dialog) {
  169. hr = ICaptureGraphBuilder2_FindInterface(graph_builder2, &LOOK_UPSTREAM_ONLY, NULL,
  170. device_filter, &IID_IAMTVAudio, (void**) &tv_audio_filter);
  171. if (hr == S_OK) {
  172. hr = IAMCrossbar_QueryInterface(tv_audio_filter, &IID_IBaseFilter, (void **) &tv_audio_base_filter);
  173. if (hr != S_OK)
  174. goto end;
  175. dshow_show_filter_properties(tv_audio_base_filter, avctx);
  176. } else {
  177. av_log(avctx, AV_LOG_WARNING, "unable to find a tv audio tuner to display dialog for!");
  178. }
  179. }
  180. hr = setup_crossbar_options(cross_bar, devtype, avctx);
  181. if (hr != S_OK)
  182. goto end;
  183. end:
  184. if (cross_bar)
  185. IAMCrossbar_Release(cross_bar);
  186. if (cross_bar_base_filter)
  187. IBaseFilter_Release(cross_bar_base_filter);
  188. if (tv_tuner_filter)
  189. IAMTVTuner_Release(tv_tuner_filter);
  190. if (tv_tuner_base_filter)
  191. IBaseFilter_Release(tv_tuner_base_filter);
  192. return hr;
  193. }