CameraSource now shows names of video input devices in Windows

This commit is contained in:
iperov 2022-05-15 17:49:25 +04:00
commit fa7fddca28
2 changed files with 30 additions and 20 deletions

View file

@ -71,6 +71,7 @@ class _RotationType(IntEnum):
_RotationType_names = ['0 degrees', '90 degrees', '180 degrees', '270 degrees'] _RotationType_names = ['0 degrees', '90 degrees', '180 degrees', '270 degrees']
class CameraSourceWorker(BackendWorker): class CameraSourceWorker(BackendWorker):
def get_state(self) -> 'WorkerState': return super().get_state() def get_state(self) -> 'WorkerState': return super().get_state()
def get_control_sheet(self) -> 'Sheet.Worker': return super().get_control_sheet() def get_control_sheet(self) -> 'Sheet.Worker': return super().get_control_sheet()
@ -86,8 +87,8 @@ class CameraSourceWorker(BackendWorker):
state, cs = self.get_state(), self.get_control_sheet() state, cs = self.get_state(), self.get_control_sheet()
cs.device_idx.call_on_selected(self.on_cs_device_idx_selected)
cs.driver.call_on_selected(self.on_cs_driver_selected) cs.driver.call_on_selected(self.on_cs_driver_selected)
cs.device_idx.call_on_selected(self.on_cs_device_idx_selected)
cs.resolution.call_on_selected(self.on_cs_resolution_selected) cs.resolution.call_on_selected(self.on_cs_resolution_selected)
cs.fps.call_on_number(self.on_cs_fps) cs.fps.call_on_number(self.on_cs_fps)
cs.rotation.call_on_selected(self.on_cs_rotation_selected) cs.rotation.call_on_selected(self.on_cs_rotation_selected)
@ -96,14 +97,24 @@ class CameraSourceWorker(BackendWorker):
cs.load_settings.call_on_signal(self.on_cs_load_settings) cs.load_settings.call_on_signal(self.on_cs_load_settings)
cs.save_settings.call_on_signal(self.on_cs_save_settings) cs.save_settings.call_on_signal(self.on_cs_save_settings)
cs.device_idx.enable()
cs.device_idx.set_choices([*range(16)], none_choice_name='@misc.menu_select')
cs.device_idx.select(state.device_idx)
cs.driver.enable() cs.driver.enable()
cs.driver.set_choices(_DriverType, _DriverType_names, none_choice_name='@misc.menu_select') cs.driver.set_choices(_DriverType, _DriverType_names, none_choice_name='@misc.menu_select')
cs.driver.select(state.driver if state.driver is not None else _DriverType.DSHOW if platform.system() == 'Windows' else _DriverType.COMPATIBLE) cs.driver.select(state.driver if state.driver is not None else _DriverType.DSHOW if platform.system() == 'Windows' else _DriverType.COMPATIBLE)
if platform.system() == 'Windows':
from xlib.api.win32 import ole32
from xlib.api.win32 import dshow
ole32.CoInitializeEx(0,0)
choices = [ f'{idx} : {name}' for idx, name in enumerate(dshow.get_video_input_devices_names()) ]
choices += [ f'{idx}' for idx in range(len(choices), 16) ]
ole32.CoUninitialize()
else:
choices = [ f'{idx}' for idx in range(16) ]
cs.device_idx.enable()
cs.device_idx.set_choices(choices, none_choice_name='@misc.menu_select')
cs.device_idx.select(state.device_idx)
cs.resolution.enable() cs.resolution.enable()
cs.resolution.set_choices(_ResolutionType, _ResolutionType_names, none_choice_name=None) cs.resolution.set_choices(_ResolutionType, _ResolutionType_names, none_choice_name=None)
cs.resolution.select(state.resolution if state.resolution is not None else _ResolutionType.RES_640x480) cs.resolution.select(state.resolution if state.resolution is not None else _ResolutionType.RES_640x480)
@ -144,15 +155,6 @@ class CameraSourceWorker(BackendWorker):
cs.save_settings.enable() cs.save_settings.enable()
else: else:
cs.device_idx.unselect() cs.device_idx.unselect()
cs.driver.unselect()
def on_cs_device_idx_selected(self, idx, device_idx):
cs, state = self.get_control_sheet(), self.get_state()
if state.device_idx != device_idx:
state.device_idx = device_idx
self.save_state()
if self.is_started():
self.restart()
def on_cs_driver_selected(self, idx, driver): def on_cs_driver_selected(self, idx, driver):
cs, state = self.get_control_sheet(), self.get_state() cs, state = self.get_control_sheet(), self.get_state()
@ -162,6 +164,14 @@ class CameraSourceWorker(BackendWorker):
if self.is_started(): if self.is_started():
self.restart() self.restart()
def on_cs_device_idx_selected(self, device_idx, device_name):
cs, state = self.get_control_sheet(), self.get_state()
if state.device_idx != device_idx:
state.device_idx = device_idx
self.save_state()
if self.is_started():
self.restart()
def on_cs_resolution_selected(self, idx, resolution : _ResolutionType): def on_cs_resolution_selected(self, idx, resolution : _ResolutionType):
state, cs = self.get_state(), self.get_control_sheet() state, cs = self.get_state(), self.get_control_sheet()
if state.resolution != resolution: if state.resolution != resolution:

View file

@ -15,12 +15,12 @@ class QCameraSource(QBackendPanel):
def __init__(self, backend : CameraSource): def __init__(self, backend : CameraSource):
cs = backend.get_control_sheet() cs = backend.get_control_sheet()
q_device_idx_label = QLabelPopupInfo(label=L('@QCameraSource.device_index') )
q_device_idx = QComboBoxCSWDynamicSingleSwitch(cs.device_idx, reflect_state_widgets=[q_device_idx_label])
q_driver_label = QLabelPopupInfo(label=L('@QCameraSource.driver'), popup_info_text=L('@QCameraSource.help.driver') ) q_driver_label = QLabelPopupInfo(label=L('@QCameraSource.driver'), popup_info_text=L('@QCameraSource.help.driver') )
q_driver = QComboBoxCSWDynamicSingleSwitch(cs.driver, reflect_state_widgets=[q_driver_label]) q_driver = QComboBoxCSWDynamicSingleSwitch(cs.driver, reflect_state_widgets=[q_driver_label])
q_device_idx_label = QLabelPopupInfo(label=L('@QCameraSource.device_index') )
q_device_idx = QComboBoxCSWDynamicSingleSwitch(cs.device_idx, reflect_state_widgets=[q_device_idx_label])
q_resolution_label = QLabelPopupInfo(label=L('@QCameraSource.resolution'), popup_info_text=L('@QCameraSource.help.resolution') ) q_resolution_label = QLabelPopupInfo(label=L('@QCameraSource.resolution'), popup_info_text=L('@QCameraSource.help.resolution') )
q_resolution = QComboBoxCSWDynamicSingleSwitch(cs.resolution, reflect_state_widgets=[q_resolution_label]) q_resolution = QComboBoxCSWDynamicSingleSwitch(cs.resolution, reflect_state_widgets=[q_resolution_label])
@ -41,12 +41,12 @@ class QCameraSource(QBackendPanel):
grid_l = qtx.QXGridLayout(spacing=5) grid_l = qtx.QXGridLayout(spacing=5)
row = 0 row = 0
grid_l.addWidget(q_device_idx_label, row, 0, alignment=qtx.AlignRight | qtx.AlignVCenter )
grid_l.addWidget(q_device_idx, row, 1, alignment=qtx.AlignLeft )
row += 1
grid_l.addWidget(q_driver_label, row, 0, alignment=qtx.AlignRight | qtx.AlignVCenter ) grid_l.addWidget(q_driver_label, row, 0, alignment=qtx.AlignRight | qtx.AlignVCenter )
grid_l.addWidget(q_driver, row, 1, alignment=qtx.AlignLeft ) grid_l.addWidget(q_driver, row, 1, alignment=qtx.AlignLeft )
row += 1 row += 1
grid_l.addWidget(q_device_idx_label, row, 0, alignment=qtx.AlignRight | qtx.AlignVCenter )
grid_l.addWidget(q_device_idx, row, 1, alignment=qtx.AlignLeft )
row += 1
grid_l.addWidget(q_resolution_label, row, 0, alignment=qtx.AlignRight | qtx.AlignVCenter ) grid_l.addWidget(q_resolution_label, row, 0, alignment=qtx.AlignRight | qtx.AlignVCenter )
grid_l.addWidget(q_resolution, row, 1, alignment=qtx.AlignLeft ) grid_l.addWidget(q_resolution, row, 1, alignment=qtx.AlignLeft )
row += 1 row += 1