forked from amigniter/mod_audio_stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_audio_stream.c
311 lines (269 loc) · 13.9 KB
/
mod_audio_stream.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* mod_audio_stream FreeSWITCH module to stream audio to websocket and receive response
*/
#include "mod_audio_stream.h"
#include "audio_streamer_glue.h"
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_audio_stream_shutdown);
SWITCH_MODULE_RUNTIME_FUNCTION(mod_audio_stream_runtime);
SWITCH_MODULE_LOAD_FUNCTION(mod_audio_stream_load);
SWITCH_MODULE_DEFINITION(mod_audio_stream, mod_audio_stream_load, mod_audio_stream_shutdown, NULL /*mod_audio_stream_runtime*/);
static switch_bool_t stream_write_audio(switch_core_session_t *session, switch_media_bug_t *bug);
static void responseHandler(switch_core_session_t* session, const char* eventName, const char* json) {
switch_event_t *event;
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, eventName);
switch_channel_event_set_data(channel, event);
if (json) switch_event_add_body(event, "%s", json);
switch_event_fire(&event);
}
static switch_bool_t capture_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type) {
switch_core_session_t *session = switch_core_media_bug_get_session(bug);
private_t *tech_pvt = (private_t *)user_data;
switch (type) {
case SWITCH_ABC_TYPE_INIT:
break;
case SWITCH_ABC_TYPE_CLOSE:
{
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Got SWITCH_ABC_TYPE_CLOSE.\n");
int channelIsClosing = tech_pvt->close_requested ? 0 : 1;
stream_session_cleanup(session, NULL, channelIsClosing);
}
break;
case SWITCH_ABC_TYPE_READ:
if (tech_pvt->close_requested) {
return SWITCH_FALSE;
}
return stream_frame(bug);
break;
case SWITCH_ABC_TYPE_WRITE_REPLACE:
return stream_write_audio(session, bug); // Использование функции
break;
default:
break;
}
return SWITCH_TRUE;
}
static switch_status_t start_capture(switch_core_session_t *session,
switch_media_bug_flag_t flags,
char* wsUri,
int sampling,
char* metadata)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug;
switch_status_t status;
switch_codec_t* read_codec;
void *pUserData = NULL;
int channels = (flags & SMBF_STEREO) ? 2 : 1;
if (switch_channel_get_private(channel, MY_BUG_NAME)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "mod_audio_stream: bug already attached!\n");
return SWITCH_STATUS_FALSE;
}
read_codec = switch_core_session_get_read_codec(session);
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "mod_audio_stream: channel must have reached pre-answer status before calling start!\n");
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "calling stream_session_init.\n");
if (SWITCH_STATUS_FALSE == stream_session_init(session, responseHandler, read_codec->implementation->actual_samples_per_second,
wsUri, sampling, channels, metadata, &pUserData)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error initializing mod_audio_stream session.\n");
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "adding bug.\n");
if ((status = switch_core_media_bug_add(session, MY_BUG_NAME, NULL, capture_callback, pUserData, 0, flags, &bug)) != SWITCH_STATUS_SUCCESS) {
return status;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "setting bug private data.\n");
switch_channel_set_private(channel, MY_BUG_NAME, bug);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "exiting start_capture.\n");
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t do_stop(switch_core_session_t *session, char* text)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
if (text) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "mod_audio_stream: stop w/ final text %s\n", text);
}
else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "mod_audio_stream: stop\n");
}
status = stream_session_cleanup(session, text, 0);
return status;
}
static switch_status_t do_pauseresume(switch_core_session_t *session, int pause)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "mod_audio_stream: %s\n", pause ? "pause" : "resume");
status = stream_session_pauseresume(session, pause);
return status;
}
static switch_status_t send_text(switch_core_session_t *session, char* text) {
switch_status_t status = SWITCH_STATUS_FALSE;
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug = switch_channel_get_private(channel, MY_BUG_NAME);
if (bug) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "mod_audio_stream: sending text: %s.\n", text);
status = stream_session_send_text(session, text);
}
else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "mod_audio_stream: no bug, failed sending text: %s.\n", text);
}
return status;
}
static switch_bool_t stream_write_audio(switch_core_session_t *session, switch_media_bug_t *bug) {
private_t *tech_pvt = (private_t *)switch_core_media_bug_get_user_data(bug);
if (!tech_pvt || tech_pvt->audio_paused || tech_pvt->close_requested) {
return SWITCH_TRUE;
}
switch_frame_t *frame = switch_core_media_bug_get_write_replace_frame(bug);
if (frame) {
size_t len = ringBufferLen(tech_pvt->buffer);
if (len > 0) {
uint8_t data[len];
ringBufferGetMultiple(tech_pvt->buffer, data, len);
memcpy(frame->data, data, len);
frame->datalen = len;
switch_core_media_bug_set_write_replace_frame(bug, frame);
tech_pvt->audio_playing = 1;
} else {
tech_pvt->audio_playing = 0;
}
}
return SWITCH_TRUE;
}
#define STREAM_API_SYNTAX "<uuid> [start | stop | send_text | pause | resume | graceful-shutdown ] [wss-url | path] [mono | mixed | stereo] [8000 | 16000] [metadata]"
SWITCH_STANDARD_API(stream_function)
{
char *mycmd = NULL, *argv[6] = { 0 };
int argc = 0;
switch_status_t status = SWITCH_STATUS_FALSE;
if (!zstr(cmd) && (mycmd = strdup(cmd))) {
argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
}
assert(cmd);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "mod_audio_stream cmd: %s\n", cmd ? cmd : "");
if (zstr(cmd) || argc < 2 || (0 == strcmp(argv[1], "start") && argc < 4)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error with command %s %s %s.\n", cmd, argv[0], argv[1]);
stream->write_function(stream, "-USAGE: %s\n", STREAM_API_SYNTAX);
goto done;
} else {
switch_core_session_t *lsession = NULL;
if ((lsession = switch_core_session_locate(argv[0]))) {
if (!strcasecmp(argv[1], "stop")) {
if(argc > 2 && (is_valid_utf8(argv[2]) != SWITCH_STATUS_SUCCESS)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"%s contains invalid utf8 characters\n", argv[2]);
switch_core_session_rwunlock(lsession);
goto done;
}
status = do_stop(lsession, argc > 2 ? argv[2] : NULL);
} else if (!strcasecmp(argv[1], "pause")) {
status = do_pauseresume(lsession, 1);
} else if (!strcasecmp(argv[1], "resume")) {
status = do_pauseresume(lsession, 0);
} else if (!strcasecmp(argv[1], "send_text")) {
if (argc < 3) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"send_text requires an argument specifying text to send\n");
switch_core_session_rwunlock(lsession);
goto done;
}
if(is_valid_utf8(argv[2]) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"%s contains invalid utf8 characters\n", argv[2]);
switch_core_session_rwunlock(lsession);
goto done;
}
status = send_text(lsession, argv[2]);
} else if (!strcasecmp(argv[1], "start")) {
char wsUri[MAX_WS_URI];
int sampling = 8000;
switch_media_bug_flag_t flags = SMBF_READ_STREAM;
char *metadata = argc > 5 ? argv[5] : NULL;
if(metadata && (is_valid_utf8(argv[2]) != SWITCH_STATUS_SUCCESS)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"%s contains invalid utf8 characters\n", argv[2]);
switch_core_session_rwunlock(lsession);
goto done;
}
if (0 == strcmp(argv[3], "mixed")) {
flags |= SMBF_WRITE_STREAM;
} else if (0 == strcmp(argv[3], "stereo")) {
flags |= SMBF_WRITE_STREAM;
flags |= SMBF_STEREO;
} else if (0 != strcmp(argv[3], "mono")) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"invalid mix type: %s, must be mono, mixed, or stereo\n", argv[3]);
switch_core_session_rwunlock(lsession);
goto done;
}
if (argc > 4) {
if (0 == strcmp(argv[4], "16k")) {
sampling = 16000;
} else if (0 == strcmp(argv[4], "8k")) {
sampling = 8000;
} else {
sampling = atoi(argv[4]);
}
}
if (!validate_ws_uri(argv[2], &wsUri[0])) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"invalid websocket uri: %s\n", argv[2]);
} else if (sampling % 8000 != 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"invalid sample rate: %s\n", argv[4]);
} else {
status = start_capture(lsession, flags, wsUri, sampling, metadata);
}
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
"unsupported mod_audio_stream cmd: %s\n", argv[1]);
}
switch_core_session_rwunlock(lsession);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error locating session %s\n",
argv[0]);
}
}
if (status == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "+OK Success\n");
} else {
stream->write_function(stream, "-ERR Operation Failed\n");
}
done:
switch_safe_free(mycmd);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_audio_stream_load)
{
switch_api_interface_t *api_interface;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "mod_audio_stream API loading..\n");
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
if (switch_event_reserve_subclass(EVENT_JSON) != SWITCH_STATUS_SUCCESS ||
switch_event_reserve_subclass(EVENT_CONNECT) != SWITCH_STATUS_SUCCESS ||
switch_event_reserve_subclass(EVENT_ERROR) != SWITCH_STATUS_SUCCESS ||
switch_event_reserve_subclass(EVENT_DISCONNECT) != SWITCH_STATUS_SUCCESS ||
switch_event_reserve_subclass(EVENT_MEDIA) != SWITCH_STATUS_SUCCESS) { // Новое событие для медиа-данных
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register an event subclass for mod_audio_stream API.\n");
return SWITCH_STATUS_TERM;
}
SWITCH_ADD_API(api_interface, "uuid_audio_stream", "audio_stream API", stream_function, STREAM_API_SYNTAX);
switch_console_set_complete("add uuid_audio_stream ::console::list_uuid start wss-url metadata");
switch_console_set_complete("add uuid_audio_stream ::console::list_uuid start wss-url");
switch_console_set_complete("add uuid_audio_stream ::console::list_uuid stop");
switch_console_set_complete("add uuid_audio_stream ::console::list_uuid pause");
switch_console_set_complete("add uuid_audio_stream ::console::list_uuid resume");
switch_console_set_complete("add uuid_audio_stream ::console::list_uuid send_text");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "mod_audio_stream API successfully loaded\n");
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_audio_stream_shutdown)
{
switch_event_free_subclass(EVENT_JSON);
switch_event_free_subclass(EVENT_CONNECT);
switch_event_free_subclass(EVENT_DISCONNECT);
switch_event_free_subclass(EVENT_ERROR);
switch_event_free_subclass(EVENT_MEDIA); // Новое событие для медиа-данных
return SWITCH_STATUS_SUCCESS;
}