|
| 1 | +/* |
| 2 | + * libwebsockets - small server side websockets and web server implementation |
| 3 | + * |
| 4 | + * Copyright (C) 2010 - 2026 Andy Green <andy@warmcat.com> |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <libwebsockets.h> |
| 18 | + |
| 19 | +#if defined(LWS_WITH_OPUS) && defined(LWS_WITH_ALSA) |
| 20 | + |
| 21 | +#include <opus/opus.h> |
| 22 | +#include <string.h> |
| 23 | + |
| 24 | +struct lws_alsa_opus_capture { |
| 25 | + struct lws_alsa_ctx *alsa_ctx; |
| 26 | + OpusEncoder *opus_enc; |
| 27 | + lws_alsa_opus_encoded_cb_t cb; |
| 28 | + void *user_data; |
| 29 | + |
| 30 | + uint32_t samples_per_frame; |
| 31 | + int16_t *audio_samples; |
| 32 | + uint8_t opus_out[512]; |
| 33 | +}; |
| 34 | + |
| 35 | +struct lws_alsa_opus_capture * |
| 36 | +lws_alsa_opus_capture_create(const struct lws_alsa_info *info, lws_alsa_opus_encoded_cb_t cb, void *user_data) |
| 37 | +{ |
| 38 | + struct lws_alsa_opus_capture *cap; |
| 39 | + int err; |
| 40 | + |
| 41 | + cap = lws_malloc(sizeof(*cap), __func__); |
| 42 | + if (!cap) |
| 43 | + return NULL; |
| 44 | + |
| 45 | + memset(cap, 0, sizeof(*cap)); |
| 46 | + |
| 47 | + cap->alsa_ctx = lws_alsa_create_capture(info); |
| 48 | + if (!cap->alsa_ctx) { |
| 49 | + lwsl_err("%s: Failed to create ALSA capture\n", __func__); |
| 50 | + goto bail; |
| 51 | + } |
| 52 | + |
| 53 | + cap->opus_enc = opus_encoder_create((opus_int32)info->rate, (int)info->channels, OPUS_APPLICATION_VOIP, &err); |
| 54 | + if (!cap->opus_enc || err != OPUS_OK) { |
| 55 | + lwsl_err("%s: Failed to create Opus encoder\n", __func__); |
| 56 | + goto bail; |
| 57 | + } |
| 58 | + |
| 59 | + cap->samples_per_frame = info->samples_per_frame; |
| 60 | + cap->audio_samples = lws_malloc(cap->samples_per_frame * sizeof(int16_t) * info->channels, __func__); |
| 61 | + if (!cap->audio_samples) |
| 62 | + goto bail; |
| 63 | + |
| 64 | + cap->cb = cb; |
| 65 | + cap->user_data = user_data; |
| 66 | + |
| 67 | + return cap; |
| 68 | + |
| 69 | +bail: |
| 70 | + lws_alsa_opus_capture_destroy(&cap); |
| 71 | + return NULL; |
| 72 | +} |
| 73 | + |
| 74 | +void |
| 75 | +lws_alsa_opus_capture_destroy(struct lws_alsa_opus_capture **_cap) |
| 76 | +{ |
| 77 | + struct lws_alsa_opus_capture *cap = *_cap; |
| 78 | + |
| 79 | + if (!cap) |
| 80 | + return; |
| 81 | + |
| 82 | + if (cap->alsa_ctx) |
| 83 | + lws_alsa_destroy(&cap->alsa_ctx); |
| 84 | + |
| 85 | + if (cap->opus_enc) |
| 86 | + opus_encoder_destroy(cap->opus_enc); |
| 87 | + |
| 88 | + if (cap->audio_samples) |
| 89 | + lws_free(cap->audio_samples); |
| 90 | + |
| 91 | + lws_free(cap); |
| 92 | + *_cap = NULL; |
| 93 | +} |
| 94 | + |
| 95 | +int |
| 96 | +lws_alsa_opus_get_fd(struct lws_alsa_opus_capture *cap) |
| 97 | +{ |
| 98 | + if (!cap || !cap->alsa_ctx) |
| 99 | + return -1; |
| 100 | + return lws_alsa_get_fd(cap->alsa_ctx); |
| 101 | +} |
| 102 | + |
| 103 | +int |
| 104 | +lws_alsa_opus_read(struct lws_alsa_opus_capture *cap) |
| 105 | +{ |
| 106 | + int n, opus_len; |
| 107 | + |
| 108 | + if (!cap || !cap->alsa_ctx || !cap->opus_enc) |
| 109 | + return -1; |
| 110 | + |
| 111 | + n = lws_alsa_read(cap->alsa_ctx, cap->audio_samples, cap->samples_per_frame); |
| 112 | + if (n <= 0) |
| 113 | + return 0; |
| 114 | + |
| 115 | + opus_len = opus_encode(cap->opus_enc, cap->audio_samples, n, cap->opus_out, sizeof(cap->opus_out)); |
| 116 | + if (opus_len > 0 && cap->cb) { |
| 117 | + cap->cb(cap->user_data, cap->opus_out, (size_t)opus_len); |
| 118 | + } |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +struct json_dump_ctx { |
| 124 | + char *p; |
| 125 | + char *end; |
| 126 | + int first; |
| 127 | +}; |
| 128 | + |
| 129 | +static int alsa_json_control_cb(void *user, const struct lws_alsa_control *c) |
| 130 | +{ |
| 131 | + struct json_dump_ctx *j = (struct json_dump_ctx *)user; |
| 132 | + char safe_name[256]; |
| 133 | + int len; |
| 134 | + |
| 135 | + if (lws_ptr_diff_size_t(j->end, j->p) < 128) |
| 136 | + return 1; |
| 137 | + |
| 138 | + if (!j->first) |
| 139 | + *j->p++ = ','; |
| 140 | + |
| 141 | + j->first = 0; |
| 142 | + |
| 143 | + lws_json_purify(safe_name, c->name, sizeof(safe_name), &len); |
| 144 | + |
| 145 | + j->p += lws_snprintf( |
| 146 | + j->p, lws_ptr_diff_size_t(j->end, j->p), |
| 147 | + "{\"id\":%u,\"name\":\"%s\"," |
| 148 | + "\"min\":%ld,\"max\":%ld,\"step\":%ld,\"val\":%ld}", |
| 149 | + c->id, safe_name, c->min, c->max, c->step, c->val); |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
| 153 | + |
| 154 | +int |
| 155 | +lws_alsa_opus_send_capabilities(struct lws_alsa_opus_capture *cap, char *buf, size_t max_len) |
| 156 | +{ |
| 157 | + struct json_dump_ctx j; |
| 158 | + |
| 159 | + if (!cap || !cap->alsa_ctx) |
| 160 | + return -1; |
| 161 | + |
| 162 | + j.p = buf; |
| 163 | + j.end = buf + max_len; |
| 164 | + j.first = 1; |
| 165 | + |
| 166 | + j.p += lws_snprintf(j.p, lws_ptr_diff_size_t(j.end, j.p), |
| 167 | + "{\"type\":\"capabilities\",\"kind\":\"audio\",\"controls\":["); |
| 168 | + |
| 169 | + lws_alsa_enum_controls(cap->alsa_ctx, alsa_json_control_cb, &j); |
| 170 | + |
| 171 | + j.p += lws_snprintf(j.p, lws_ptr_diff_size_t(j.end, j.p), "]}"); |
| 172 | + |
| 173 | + return (int)lws_ptr_diff_size_t(j.p, buf); |
| 174 | +} |
| 175 | + |
| 176 | +int |
| 177 | +lws_alsa_opus_set_control(struct lws_alsa_opus_capture *cap, uint32_t id, long val) |
| 178 | +{ |
| 179 | + if (!cap || !cap->alsa_ctx) |
| 180 | + return -1; |
| 181 | + return lws_alsa_set_control(cap->alsa_ctx, id, val); |
| 182 | +} |
| 183 | + |
| 184 | +#endif |
0 commit comments