ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
video/webcam/webcam.c
Go to the documentation of this file.
1
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,
50 "* Say \"yes\" to the popup about system camera access that you see when running this program for the "
51 "first time.\n");
53 stderr, "* If you said \"no\" to the popup, go to System Preferences > Security & Privacy > Privacy > Camera.\n");
54 safe_fprintf(stderr,
55 " Now flip the switch next to your terminal application in that privacy list to allow ascii-chat to "
56 "access your camera.\n");
57 safe_fprintf(stderr, " Then just run this program again.\n");
58 (void)fflush(stderr);
59#elif defined(_WIN32)
61 // Device is in use by another application - this is a fatal error on Windows
62 safe_fprintf(stderr, "\n");
63 safe_fprintf(stderr, "Webcam is already in use by another application.\n");
64 safe_fprintf(stderr, "Windows allows only one application to access the webcam at a time.\n");
65 safe_fprintf(stderr, "\n");
66 safe_fprintf(stderr, "To use ascii-chat with multiple clients, try these alternatives:\n");
67 safe_fprintf(stderr, " --test-pattern Generate a colorful test pattern instead of using webcam\n");
68 safe_fprintf(stderr, " --file VIDEO.mp4 Use a video file as input (to be implemented)\n");
69 safe_fprintf(stderr, "\n");
70 safe_fprintf(stderr, "Example: ascii-chat client --test-pattern\n");
71 (void)fflush(stderr);
72 } else {
73 // Other webcam errors - general failure
74 safe_fprintf(stderr, "\n");
75 safe_fprintf(stderr, "On Windows, this might be because:\n");
76 safe_fprintf(stderr, "* Camera permissions are not granted\n");
77 safe_fprintf(stderr, "* Camera driver issues\n");
78 safe_fprintf(stderr, "* No webcam device found\n");
79 (void)fflush(stderr);
80 }
81#else
82 // Unknown platform
83 (void)error_code;
84 safe_fprintf(stderr, "\nWebcam initialization failed on unsupported platform.\n");
85 (void)fflush(stderr);
86#endif
87}
88
89// Fallback implementations for unsupported platforms
90#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
91asciichat_error_t webcam_init_context(webcam_context_t **ctx, unsigned short int device_index) {
92 (void)ctx;
93 (void)device_index;
94 SET_ERRNO(ERROR_WEBCAM, "Webcam platform not supported on this system");
95 return ERROR_WEBCAM;
96}
97
99 (void)ctx;
100 log_warn("Webcam cleanup called on unsupported platform");
101}
102
104 (void)ctx;
105 // No-op on unsupported platforms
106}
107
109 (void)ctx;
110 SET_ERRNO(ERROR_WEBCAM, "Webcam read not supported on this platform");
111 return NULL;
112}
113
115 (void)ctx;
116 (void)width;
117 (void)height;
118 return SET_ERRNO(ERROR_WEBCAM, "Webcam get dimensions not supported on this platform");
119}
120
121asciichat_error_t webcam_list_devices(webcam_device_info_t **out_devices, unsigned int *out_count) {
122 if (out_devices)
123 *out_devices = NULL;
124 if (out_count)
125 *out_count = 0;
126 return SET_ERRNO(ERROR_WEBCAM, "Webcam device enumeration not supported on this platform");
127}
128
130 (void)devices;
131 // No-op on unsupported platforms
132}
133#endif
asciichat_error_t error_code
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_WEBCAM_IN_USE
Definition error_codes.h:65
@ ERROR_WEBCAM_PERMISSION
Definition error_codes.h:66
@ ERROR_WEBCAM
Definition error_codes.h:64
#define log_warn(...)
Log a WARN message.
Definition log/log.h:574
int safe_fprintf(FILE *stream, const char *format,...)
Safe formatted output to file stream.
Definition system.c:172
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:150
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.
Image structure.
Webcam device information structure.
Definition webcam.h:86