1649473351(1).png
H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\modules\video_capture\windows\video_capture_ds.cc

VideoCaptureDS::Init()

  1. int32_t VideoCaptureDS::Init(const char* deviceUniqueIdUTF8) {
  2. const int32_t nameLength = (int32_t)strlen((char*)deviceUniqueIdUTF8);
  3. if (nameLength > kVideoCaptureUniqueNameLength)
  4. return -1;
  5. // Store the device name
  6. _deviceUniqueId = new (std::nothrow) char[nameLength + 1];
  7. memcpy(_deviceUniqueId, deviceUniqueIdUTF8, nameLength + 1);
  8. if (_dsInfo.Init() != 0)
  9. return -1;
  10. _captureFilter = _dsInfo.GetDeviceFilter(deviceUniqueIdUTF8);
  11. if (!_captureFilter) {
  12. RTC_LOG(LS_INFO) << "Failed to create capture filter.";
  13. return -1;
  14. }
  15. // Get the interface for DirectShow's GraphBuilder
  16. HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  17. IID_IGraphBuilder, (void**)&_graphBuilder);
  18. if (FAILED(hr)) {
  19. RTC_LOG(LS_INFO) << "Failed to create graph builder.";
  20. return -1;
  21. }
  22. hr = _graphBuilder->QueryInterface(IID_IMediaControl, (void**)&_mediaControl);
  23. if (FAILED(hr)) {
  24. RTC_LOG(LS_INFO) << "Failed to create media control builder.";
  25. return -1;
  26. }
  27. hr = _graphBuilder->AddFilter(_captureFilter, CAPTURE_FILTER_NAME);
  28. if (FAILED(hr)) {
  29. RTC_LOG(LS_INFO) << "Failed to add the capture device to the graph.";
  30. return -1;
  31. }
  32. _outputCapturePin = GetOutputPin(_captureFilter, PIN_CATEGORY_CAPTURE);
  33. if (!_outputCapturePin) {
  34. RTC_LOG(LS_INFO) << "Failed to get output capture pin";
  35. return -1;
  36. }
  37. // Create the sink filte used for receiving Captured frames.
  38. sink_filter_ = new ComRefCount<CaptureSinkFilter>(this);
  39. hr = _graphBuilder->AddFilter(sink_filter_, SINK_FILTER_NAME);
  40. if (FAILED(hr)) {
  41. RTC_LOG(LS_INFO) << "Failed to add the send filter to the graph.";
  42. return -1;
  43. }
  44. _inputSendPin = GetInputPin(sink_filter_);
  45. if (!_inputSendPin) {
  46. RTC_LOG(LS_INFO) << "Failed to get input send pin";
  47. return -1;
  48. }
  49. // Temporary connect here.
  50. // This is done so that no one else can use the capture device.
  51. if (SetCameraOutput(_requestedCapability) != 0) {
  52. return -1;
  53. }
  54. hr = _mediaControl->Pause();
  55. if (FAILED(hr)) {
  56. RTC_LOG(LS_INFO)
  57. << "Failed to Pause the Capture device. Is it already occupied? " << hr;
  58. return -1;
  59. }
  60. RTC_LOG(LS_INFO) << "Capture device '" << deviceUniqueIdUTF8
  61. << "' initialized.";
  62. return 0;
  63. }

通过DeviceInfo::GetDeviceFilter就可以采集视频数据了。
IGraphBuilder做了两件事:
1、创建IMediaControl,控制啥时候开始或停止采集视频数据
2、AddFilter添加Filter到FilterGraph,接收采集数据传输到上层