mf_devices.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 2013 Mamadou DIOP
  2. * Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. #include "mf_devices.h"
  20. #include "mf_utils.h"
  21. DeviceList::DeviceList()
  22. : m_ppDevices(NULL)
  23. , m_cDevices(0)
  24. {
  25. }
  26. DeviceList::~DeviceList()
  27. {
  28. Clear();
  29. }
  30. UINT32 DeviceList::Count()const
  31. {
  32. return m_cDevices;
  33. }
  34. void DeviceList::Clear()
  35. {
  36. for (UINT32 i = 0; i < m_cDevices; i++) {
  37. SafeRelease(&m_ppDevices[i]);
  38. }
  39. CoTaskMemFree(m_ppDevices);
  40. m_ppDevices = NULL;
  41. m_cDevices = 0;
  42. }
  43. HRESULT DeviceList::EnumerateDevices(const GUID& sourceType)
  44. {
  45. HRESULT hr = S_OK;
  46. IMFAttributes *pAttributes = NULL;
  47. Clear();
  48. // Initialize an attribute store. We will use this to
  49. // specify the enumeration parameters.
  50. hr = MFCreateAttributes(&pAttributes, 1);
  51. // Ask for source type = video capture devices
  52. if (SUCCEEDED(hr)) {
  53. hr = pAttributes->SetGUID(
  54. MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
  55. sourceType
  56. );
  57. }
  58. // Enumerate devices.
  59. if (SUCCEEDED(hr)) {
  60. hr = MFEnumDeviceSources(pAttributes, &m_ppDevices, &m_cDevices);
  61. }
  62. SafeRelease(&pAttributes);
  63. return hr;
  64. }
  65. HRESULT DeviceList::GetDeviceAtIndex(UINT32 index, IMFActivate **ppActivate)
  66. {
  67. if (index >= Count()) {
  68. return E_INVALIDARG;
  69. }
  70. *ppActivate = m_ppDevices[index];
  71. (*ppActivate)->AddRef();
  72. return S_OK;
  73. }
  74. HRESULT DeviceList::GetDeviceBest(IMFActivate **ppActivate, WCHAR *pszName /*= NULL*/)
  75. {
  76. UINT32 index = 0;
  77. if(pszName) {
  78. WCHAR *_pszName = NULL;
  79. BOOL bFound = FALSE;
  80. for(UINT32 i = 0; i < Count() && !bFound; ++i) {
  81. if((SUCCEEDED(GetDeviceName(i, &_pszName)))) {
  82. if(wcscmp(_pszName, pszName) == 0) {
  83. index = i;
  84. bFound = TRUE;
  85. // do not break the loop because we need to free(_pszName)
  86. }
  87. }
  88. if(_pszName) {
  89. CoTaskMemFree(_pszName), _pszName = NULL;
  90. }
  91. }
  92. }
  93. return GetDeviceAtIndex(index, ppActivate);
  94. }
  95. // The caller must free the memory for the string by calling CoTaskMemFree
  96. HRESULT DeviceList::GetDeviceName(UINT32 index, WCHAR **ppszName)
  97. {
  98. if (index >= Count()) {
  99. return E_INVALIDARG;
  100. }
  101. HRESULT hr = S_OK;
  102. hr = m_ppDevices[index]->GetAllocatedString(
  103. MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,
  104. ppszName,
  105. NULL
  106. );
  107. return hr;
  108. }
  109. HRESULT DeviceListAudio::EnumerateDevices()
  110. {
  111. // call base class function
  112. return DeviceList::EnumerateDevices(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID);
  113. }
  114. HRESULT DeviceListVideo::EnumerateDevices()
  115. {
  116. // call base class function
  117. return DeviceList::EnumerateDevices(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
  118. }