Skip to content

Commit 44829d7

Browse files
Copilotwysaid
andcommitted
Add optimized device names API and update all examples
Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>
1 parent 891a02f commit 44829d7

6 files changed

Lines changed: 67 additions & 36 deletions

File tree

examples/desktop/0-print_camera_c.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,13 @@ int main() {
3939
}
4040

4141
// Find available devices
42-
char** deviceNames;
43-
size_t deviceCount;
44-
if (ccap_provider_find_device_names(provider, &deviceNames, &deviceCount)) {
45-
printf("Found %zu camera device(s):\n", deviceCount);
46-
for (size_t i = 0; i < deviceCount; i++) {
47-
printf(" %zu: %s\n", i, deviceNames[i]);
42+
CcapDeviceNamesList deviceList;
43+
if (ccap_provider_find_device_names_list(provider, &deviceList)) {
44+
printf("Found %zu camera device(s):\n", deviceList.deviceCount);
45+
for (size_t i = 0; i < deviceList.deviceCount; i++) {
46+
printf(" %zu: %s\n", i, deviceList.deviceNames[i]);
4847
}
4948
printf("\n");
50-
51-
// Free device names
52-
ccap_provider_free_device_names(deviceNames, deviceCount);
5349
} else {
5450
printf("Failed to enumerate devices\n");
5551
}

examples/desktop/3-capture_callback_c.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,11 @@ int main(int argc, char** argv) {
9999
}
100100

101101
// Find and print available devices
102-
char** deviceNames;
103-
size_t deviceCount;
104-
if (ccap_provider_find_device_names(provider, &deviceNames, &deviceCount)) {
105-
for (size_t i = 0; i < deviceCount; i++) {
106-
printf("## Found video capture device: %s\n", deviceNames[i]);
102+
CcapDeviceNamesList deviceList;
103+
if (ccap_provider_find_device_names_list(provider, &deviceList)) {
104+
for (size_t i = 0; i < deviceList.deviceCount; i++) {
105+
printf("## Found video capture device: %s\n", deviceList.deviceNames[i]);
107106
}
108-
ccap_provider_free_device_names(deviceNames, deviceCount);
109107
}
110108

111109
// Set camera properties

examples/desktop/4-example_with_glfw_c.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ int main(int argc, char** argv) {
9494
}
9595

9696
// Find and print available devices
97-
char** deviceNames;
98-
size_t deviceCount;
99-
if (ccap_provider_find_device_names(provider, &deviceNames, &deviceCount)) {
100-
for (size_t i = 0; i < deviceCount; i++) {
101-
printf("## Found video capture device: %s\n", deviceNames[i]);
97+
CcapDeviceNamesList deviceList;
98+
if (ccap_provider_find_device_names_list(provider, &deviceList)) {
99+
for (size_t i = 0; i < deviceList.deviceCount; i++) {
100+
printf("## Found video capture device: %s\n", deviceList.deviceNames[i]);
102101
}
103-
ccap_provider_free_device_names(deviceNames, deviceCount);
104102
}
105103

106104
// Set camera properties

examples/desktop/utils/helper.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ int getCurrentWorkingDirectory(char* buffer, int size) {
3737
}
3838

3939
int selectCamera(CcapProvider* provider) {
40-
char** deviceNames;
41-
size_t deviceCount;
40+
CcapDeviceNamesList deviceList;
4241

43-
if (ccap_provider_find_device_names(provider, &deviceNames, &deviceCount) && deviceCount > 1) {
42+
if (ccap_provider_find_device_names_list(provider, &deviceList) && deviceList.deviceCount > 1) {
4443
printf("Multiple devices found, please select one:\n");
45-
for (size_t i = 0; i < deviceCount; i++) {
46-
printf(" %zu: %s\n", i, deviceNames[i]);
44+
for (size_t i = 0; i < deviceList.deviceCount; i++) {
45+
printf(" %zu: %s\n", i, deviceList.deviceNames[i]);
4746
}
4847

4948
int selectedIndex;
@@ -52,21 +51,16 @@ int selectCamera(CcapProvider* provider) {
5251
selectedIndex = 0;
5352
}
5453

55-
if (selectedIndex < 0 || selectedIndex >= static_cast<int>(deviceCount)) {
54+
if (selectedIndex < 0 || selectedIndex >= static_cast<int>(deviceList.deviceCount)) {
5655
selectedIndex = 0;
57-
fprintf(stderr, "Invalid index, using the first device: %s\n", deviceNames[0]);
56+
fprintf(stderr, "Invalid index, using the first device: %s\n", deviceList.deviceNames[0]);
5857
} else {
59-
printf("Using device: %s\n", deviceNames[selectedIndex]);
58+
printf("Using device: %s\n", deviceList.deviceNames[selectedIndex]);
6059
}
6160

62-
ccap_provider_free_device_names(deviceNames, deviceCount);
6361
return selectedIndex;
6462
}
6563

66-
if (deviceCount > 0) {
67-
ccap_provider_free_device_names(deviceNames, deviceCount);
68-
}
69-
7064
return -1; // One or no device, use default.
7165
}
7266

include/ccap_c.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ typedef enum {
6363

6464
/* ========== Constants ========== */
6565

66+
/** @brief Maximum number of camera devices */
67+
#define CCAP_MAX_DEVICES 32
68+
6669
/** @brief Maximum length for device name including null terminator */
6770
#define CCAP_MAX_DEVICE_NAME_LENGTH 128
6871

@@ -94,6 +97,12 @@ typedef struct {
9497
uint32_t height;
9598
} CcapResolution;
9699

100+
/** @brief Device names list structure */
101+
typedef struct {
102+
char deviceNames[CCAP_MAX_DEVICES][CCAP_MAX_DEVICE_NAME_LENGTH]; /**< Array of device names */
103+
size_t deviceCount; /**< Number of devices found */
104+
} CcapDeviceNamesList;
105+
97106
/** @brief Device information structure */
98107
typedef struct {
99108
char deviceName[CCAP_MAX_DEVICE_NAME_LENGTH]; /**< Device name */
@@ -139,18 +148,28 @@ void ccap_provider_destroy(CcapProvider* provider);
139148
/* ========== Device Discovery ========== */
140149

141150
/**
142-
* @brief Find all available camera device names
151+
* @brief Find all available camera device names (new optimized API)
152+
* @param provider Pointer to CcapProvider instance
153+
* @param deviceList Output parameter for device names list
154+
* @return true on success, false on failure
155+
*/
156+
bool ccap_provider_find_device_names_list(CcapProvider* provider, CcapDeviceNamesList* deviceList);
157+
158+
/**
159+
* @brief Find all available camera device names (legacy API - deprecated)
143160
* @param provider Pointer to CcapProvider instance
144161
* @param deviceNames Output array of device name strings (caller must free each string and the array)
145162
* @param count Output parameter for number of devices found
146163
* @return true on success, false on failure
164+
* @deprecated Use ccap_provider_find_device_names_list instead
147165
*/
148166
bool ccap_provider_find_device_names(CcapProvider* provider, char*** deviceNames, size_t* count);
149167

150168
/**
151-
* @brief Free device names array returned by ccap_provider_find_device_names
169+
* @brief Free device names array returned by ccap_provider_find_device_names (legacy API - deprecated)
152170
* @param deviceNames Array of device name strings
153171
* @param count Number of device names
172+
* @deprecated Use ccap_provider_find_device_names_list instead
154173
*/
155174
void ccap_provider_free_device_names(char** deviceNames, size_t count);
156175

src/ccap_c.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ void ccap_provider_destroy(CcapProvider* provider) {
105105

106106
/* ========== Device Discovery ========== */
107107

108+
bool ccap_provider_find_device_names_list(CcapProvider* provider, CcapDeviceNamesList* deviceList) {
109+
if (!provider || !deviceList) return false;
110+
111+
auto* cppProvider = reinterpret_cast<ccap::Provider*>(provider);
112+
auto devices = cppProvider->findDeviceNames();
113+
114+
// Initialize structure
115+
memset(deviceList, 0, sizeof(CcapDeviceNamesList));
116+
117+
deviceList->deviceCount = devices.size();
118+
if (deviceList->deviceCount > CCAP_MAX_DEVICES) {
119+
deviceList->deviceCount = CCAP_MAX_DEVICES;
120+
}
121+
122+
for (size_t i = 0; i < deviceList->deviceCount; ++i) {
123+
size_t nameLen = devices[i].size();
124+
if (nameLen >= CCAP_MAX_DEVICE_NAME_LENGTH) {
125+
nameLen = CCAP_MAX_DEVICE_NAME_LENGTH - 1;
126+
}
127+
strncpy(deviceList->deviceNames[i], devices[i].c_str(), nameLen);
128+
deviceList->deviceNames[i][nameLen] = '\0';
129+
}
130+
131+
return true;
132+
}
133+
108134
bool ccap_provider_find_device_names(CcapProvider* provider, char*** deviceNames, size_t* count) {
109135
if (!provider || !deviceNames || !count) return false;
110136

0 commit comments

Comments
 (0)