ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
video/webcam/webcam.c
Go to the documentation of this file.
1
7
8#include <stdio.h>
9
11#include <ascii-chat/common.h>
13
15 // Platform-specific error messages and troubleshooting help
16#ifdef __linux__
17 safe_fprintf(stderr, "\n");
18
19 if (error_code == ERROR_WEBCAM) {
20 safe_fprintf(stderr, "Webcam initialization failed on Linux.\n\n");
21 safe_fprintf(stderr, "Common solutions:\n");
22 safe_fprintf(stderr, " 1. Check if a camera is connected:\n");
23 safe_fprintf(stderr, " ls /dev/video*\n\n");
24 safe_fprintf(stderr, " 2. If no camera is available, use test pattern mode:\n");
25 safe_fprintf(stderr, " ascii-chat client --test-pattern\n\n");
26 safe_fprintf(stderr, " 3. Install V4L2 drivers if needed:\n");
27 safe_fprintf(stderr, " sudo apt-get install v4l-utils\n");
28 } else if (error_code == ERROR_WEBCAM_PERMISSION) {
29 safe_fprintf(stderr, "Camera permission denied.\n\n");
30 safe_fprintf(stderr, "Fix permissions with:\n");
31 safe_fprintf(stderr, " sudo usermod -a -G video $USER\n");
32 safe_fprintf(stderr, "Then log out and log back in for changes to take effect.\n");
33 } else if (error_code == ERROR_WEBCAM_IN_USE) {
34 safe_fprintf(stderr, "Camera is already in use by another application.\n\n");
35 safe_fprintf(stderr, "Try closing other camera apps or use test pattern mode:\n");
36 safe_fprintf(stderr, " ascii-chat client --test-pattern\n");
37 } else {
38 safe_fprintf(stderr, "Webcam error on Linux.\n\n");
39 safe_fprintf(stderr, "General troubleshooting:\n");
40 safe_fprintf(stderr, "* Check camera: ls /dev/video*\n");
41 safe_fprintf(stderr, "* Check permissions: groups | grep video\n");
42 safe_fprintf(stderr, "* Use test pattern: ascii-chat client --test-pattern\n");
43 }
44 (void)fflush(stderr);
45#elif defined(__APPLE__)
46 (void)error_code;
47 safe_fprintf(stderr, "\n");
48 safe_fprintf(stderr, "On macOS, you may need to grant camera permissions:\n");
49 safe_fprintf(stderr, "* Say \"yes\" to the popup about system camera access that you see when running this program for the "
50 "first time.\n");
51 safe_fprintf(stderr, "* If you said \"no\" to the popup, go to System Preferences > Security & Privacy > Privacy > Camera.\n");
52 safe_fprintf(stderr, " Now flip the switch next to your terminal application in that privacy list to allow ascii-chat to "
53 "access your camera.\n");
54 safe_fprintf(stderr, " Then just run this program again.\n");
55 (void)fflush(stderr);
56#elif defined(_WIN32)
57 if (error_code == ERROR_WEBCAM_IN_USE) {
58 // Device is in use by another application - this is a fatal error on Windows
59 safe_fprintf(stderr, "\n");
60 safe_fprintf(stderr, "Webcam is already in use by another application.\n");
61 safe_fprintf(stderr, "Windows allows only one application to access the webcam at a time.\n");
62 safe_fprintf(stderr, "\n");
63 safe_fprintf(stderr, "To use ascii-chat with multiple clients, try these alternatives:\n");
64 safe_fprintf(stderr, " --test-pattern Generate a colorful test pattern instead of using webcam\n");
65 safe_fprintf(stderr, " --file VIDEO.mp4 Use a video file as input (to be implemented)\n");
66 safe_fprintf(stderr, "\n");
67 safe_fprintf(stderr, "Example: ascii-chat client --test-pattern\n");
68 (void)fflush(stderr);
69 } else {
70 // Other webcam errors - general failure
71 safe_fprintf(stderr, "\n");
72 safe_fprintf(stderr, "On Windows, this might be because:\n");
73 safe_fprintf(stderr, "* Camera permissions are not granted\n");
74 safe_fprintf(stderr, "* Camera driver issues\n");
75 safe_fprintf(stderr, "* No webcam device found\n");
76 (void)fflush(stderr);
77 }
78#else
79 // Unknown platform
80 (void)error_code;
81 safe_fprintf(stderr, "\nWebcam initialization failed on unsupported platform.\n");
82 (void)fflush(stderr);
83#endif
84}
85
86// Fallback implementations for unsupported platforms
87#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
88asciichat_error_t webcam_init_context(webcam_context_t **ctx, unsigned short int device_index) {
89 (void)ctx;
90 (void)device_index;
91 SET_ERRNO(ERROR_WEBCAM, "Webcam platform not supported on this system");
92 return ERROR_WEBCAM;
93}
94
96 (void)ctx;
97 log_warn("Webcam cleanup called on unsupported platform");
98}
99
101 (void)ctx;
102 // No-op on unsupported platforms
103}
104
106 (void)ctx;
107 SET_ERRNO(ERROR_WEBCAM, "Webcam read not supported on this platform");
108 return NULL;
109}
110
112 (void)ctx;
113 return 0.0;
114}
115
117 (void)ctx;
118 (void)width;
119 (void)height;
120 return SET_ERRNO(ERROR_WEBCAM, "Webcam get dimensions not supported on this platform");
121}
122
123asciichat_error_t webcam_list_devices(webcam_device_info_t **out_devices, unsigned int *out_count) {
124 if (out_devices)
125 *out_devices = NULL;
126 if (out_count)
127 *out_count = 0;
128 return SET_ERRNO(ERROR_WEBCAM, "Webcam device enumeration not supported on this platform");
129}
130
132 (void)devices;
133 // No-op on unsupported platforms
134}
135#endif
#define SET_ERRNO(code,...)
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_WEBCAM_IN_USE
Definition error_codes.h:64
@ ERROR_WEBCAM_PERMISSION
Definition error_codes.h:65
@ ERROR_WEBCAM
Definition error_codes.h:63
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
asciichat_error_t webcam_init_context(webcam_context_t **ctx, unsigned short int device_index)
Initialize webcam context for advanced operations.
asciichat_error_t webcam_list_devices(webcam_device_info_t **out_devices, unsigned int *out_count)
Enumerate available webcam devices.
void webcam_free_device_list(webcam_device_info_t *devices)
Free device list returned by webcam_list_devices().
void webcam_flush_context(webcam_context_t *ctx)
Flush/interrupt pending read operations on webcam context.
void webcam_print_init_error_help(asciichat_error_t error_code)
Print helpful error diagnostics for webcam initialization failures.
struct webcam_context_t webcam_context_t
Opaque webcam context structure.
Definition webcam.h:157
asciichat_error_t webcam_get_dimensions(webcam_context_t *ctx, int *width, int *height)
Get webcam frame dimensions.
image_t * webcam_read_context(webcam_context_t *ctx)
Capture a frame from webcam context.
void webcam_cleanup_context(webcam_context_t *ctx)
Clean up webcam context and release resources.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Webcam device information structure.
Definition webcam.h:93
double webcam_get_fps(webcam_context_t *ctx)