This repository was archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 640
Expand file tree
/
Copy pathk4asourceselectiondockcontrol.cpp
More file actions
153 lines (128 loc) · 4.09 KB
/
k4asourceselectiondockcontrol.cpp
File metadata and controls
153 lines (128 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Associated header
//
#include "k4asourceselectiondockcontrol.h"
// System headers
//
#include <sstream>
// Library headers
//
#include "k4aimgui_all.h"
#include <k4a/k4a.hpp>
#include <k4arecord/playback.hpp>
// Project headers
//
#include "filesystem17.h"
#include "k4aaudiomanager.h"
#include "k4aimguiextensions.h"
#include "k4aviewererrormanager.h"
#include "k4arecordingdockcontrol.h"
#include "k4aviewerutil.h"
#include "k4awindowmanager.h"
using namespace k4aviewer;
K4ASourceSelectionDockControl::K4ASourceSelectionDockControl()
{
RefreshDevices();
}
K4ADockControlStatus K4ASourceSelectionDockControl::Show()
{
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("Open Device"))
{
ImGuiExtensions::K4AComboBox("Device S/N",
"(No available devices)",
ImGuiComboFlags_None,
m_connectedDevices,
&m_selectedDevice);
if (ImGui::Button("Refresh Devices"))
{
RefreshDevices();
}
ImGui::SameLine();
const bool openAvailable = !m_connectedDevices.empty();
{
ImGuiExtensions::ButtonColorChanger colorChanger(ImGuiExtensions::ButtonColor::Green, openAvailable);
if (ImGuiExtensions::K4AButton("Open Device", openAvailable))
{
OpenDevice();
}
}
ImGui::TreePop();
}
ImGui::NewLine();
ImGui::Separator();
ImGui::NewLine();
if (ImGui::TreeNode("Open Recording"))
{
if (m_filePicker.Show())
{
OpenRecording(m_filePicker.GetPath());
}
ImGui::TreePop();
}
return K4ADockControlStatus::Ok;
}
void K4ASourceSelectionDockControl::RefreshDevices()
{
m_selectedDevice = -1;
const uint32_t installedDevices = k4a_device_get_installed_count();
m_connectedDevices.clear();
for (uint32_t i = 0; i < installedDevices; i++)
{
try
{
k4a::device device = k4a::device::open(i);
m_connectedDevices.emplace_back(std::make_pair(i, device.get_serialnum()));
}
catch (const k4a::error &)
{
// We can't have 2 handles to the same device, and we need to open a device handle to check
// its serial number, so we expect devices we already have open to fail here. Ignore those.
//
continue;
}
}
if (!m_connectedDevices.empty())
{
m_selectedDevice = m_connectedDevices[0].first;
}
const int audioRefreshStatus = K4AAudioManager::Instance().RefreshDevices();
if (audioRefreshStatus != SoundIoErrorNone)
{
std::stringstream errorBuilder;
errorBuilder << "Failed to refresh audio devices: " << soundio_strerror(audioRefreshStatus) << "!" << std::endl
<< "Attempting to open microphones may fail!";
K4AViewerErrorManager::Instance().SetErrorStatus(errorBuilder.str());
}
}
void K4ASourceSelectionDockControl::OpenDevice()
{
try
{
if (m_selectedDevice < 0)
{
K4AViewerErrorManager::Instance().SetErrorStatus("No device selected!");
return;
}
k4a::device device = k4a::device::open(static_cast<uint32_t>(m_selectedDevice));
K4AWindowManager::Instance().PushLeftDockControl(std14::make_unique<K4ADeviceDockControl>(std::move(device)));
}
catch (const k4a::error &e)
{
K4AViewerErrorManager::Instance().SetErrorStatus(e.what());
}
}
void K4ASourceSelectionDockControl::OpenRecording(const std17::filesystem::path &path)
{
try
{
k4a::playback recording = k4a::playback::open(path.c_str());
K4AWindowManager::Instance().PushLeftDockControl(
std14::make_unique<K4ARecordingDockControl>(path.string(), std::move(recording)));
}
catch (const k4a::error &e)
{
K4AViewerErrorManager::Instance().SetErrorStatus(e.what());
}
}