45fa09d13d94f7c6ee16824825f05897ca3ce289
[asterisk/asterisk.git] / res / ari / resource_bridges.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Implementation for ARI stubs.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "resource_bridges.h"
35 #include "asterisk/stasis.h"
36 #include "asterisk/stasis_bridges.h"
37 #include "asterisk/stasis_app.h"
38 #include "asterisk/stasis_app_playback.h"
39 #include "asterisk/stasis_app_recording.h"
40 #include "asterisk/stasis_channels.h"
41 #include "asterisk/core_unreal.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/bridge.h"
44 #include "asterisk/format_cap.h"
45 #include "asterisk/file.h"
46 #include "asterisk/musiconhold.h"
47 #include "asterisk/format_cache.h"
48
49 /*!
50  * \brief Finds a bridge, filling the response with an error, if appropriate.
51  *
52  * \param[out] response Response to fill with an error if control is not found.
53  * \param bridge_id ID of the bridge to lookup.
54  *
55  * \return Bridget.
56  * \return \c NULL if bridge does not exist.
57  */
58 static struct ast_bridge *find_bridge(
59         struct ast_ari_response *response,
60         const char *bridge_id)
61 {
62         RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
63
64         ast_assert(response != NULL);
65
66         bridge = stasis_app_bridge_find_by_id(bridge_id);
67         if (bridge == NULL) {
68                 RAII_VAR(struct ast_bridge_snapshot *, snapshot,
69                         ast_bridge_snapshot_get_latest(bridge_id), ao2_cleanup);
70                 if (!snapshot) {
71                         ast_ari_response_error(response, 404, "Not found",
72                                 "Bridge not found");
73                         return NULL;
74                 }
75
76                 ast_ari_response_error(response, 409, "Conflict",
77                         "Bridge not in Stasis application");
78                 return NULL;
79         }
80
81         ao2_ref(bridge, +1);
82         return bridge;
83 }
84
85 /*!
86  * \brief Finds the control object for a channel, filling the response with an
87  * error, if appropriate.
88  * \param[out] response Response to fill with an error if control is not found.
89  * \param channel_id ID of the channel to lookup.
90  * \return Channel control object.
91  * \return \c NULL if control object does not exist.
92  */
93 static struct stasis_app_control *find_channel_control(
94         struct ast_ari_response *response,
95         const char *channel_id)
96 {
97         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
98
99         ast_assert(response != NULL);
100
101         control = stasis_app_control_find_by_channel_id(channel_id);
102         if (control == NULL) {
103                 /* Distinguish between 400 and 422 errors */
104                 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL,
105                         ao2_cleanup);
106                 snapshot = ast_channel_snapshot_get_latest(channel_id);
107                 if (snapshot == NULL) {
108                         ast_log(LOG_DEBUG, "Couldn't find '%s'\n", channel_id);
109                         ast_ari_response_error(response, 400, "Bad Request",
110                                 "Channel not found");
111                         return NULL;
112                 }
113
114                 ast_log(LOG_DEBUG, "Found non-stasis '%s'\n", channel_id);
115                 ast_ari_response_error(response, 422, "Unprocessable Entity",
116                         "Channel not in Stasis application");
117                 return NULL;
118         }
119
120         ao2_ref(control, +1);
121         return control;
122 }
123
124 struct control_list {
125         size_t count;
126         struct stasis_app_control *controls[];
127 };
128
129 static void control_list_dtor(void *obj) {
130         struct control_list *list = obj;
131         size_t i;
132
133         for (i = 0; i < list->count; ++i) {
134                 ao2_cleanup(list->controls[i]);
135                 list->controls[i] = NULL;
136         }
137 }
138
139 static struct control_list *control_list_create(struct ast_ari_response *response, size_t count, const char **channels) {
140         RAII_VAR(struct control_list *, list, NULL, ao2_cleanup);
141         size_t i;
142
143         if (count == 0 || !channels) {
144                 ast_ari_response_error(response, 400, "Bad Request", "Missing parameter channel");
145                 return NULL;
146         }
147
148         list = ao2_alloc(sizeof(*list) + count * sizeof(list->controls[0]), control_list_dtor);
149         if (!list) {
150                 ast_ari_response_alloc_failed(response);
151                 return NULL;
152         }
153
154         for (i = 0; i < count; ++i) {
155                 if (ast_strlen_zero(channels[i])) {
156                         continue;
157                 }
158                 list->controls[list->count] =
159                         find_channel_control(response, channels[i]);
160                 if (!list->controls[list->count]) {
161                         /* response filled in by find_channel_control() */
162                         return NULL;
163                 }
164                 ++list->count;
165         }
166
167         if (list->count == 0) {
168                 ast_ari_response_error(response, 400, "Bad Request", "Missing parameter channel");
169                 return NULL;
170         }
171
172         ao2_ref(list, +1);
173         return list;
174 }
175
176 static int check_add_remove_channel(struct ast_ari_response *response,
177                                     struct stasis_app_control *control,
178                                     enum stasis_app_control_channel_result result)
179 {
180         switch (result) {
181         case STASIS_APP_CHANNEL_RECORDING :
182                 ast_ari_response_error(
183                         response, 409, "Conflict", "Channel %s currently recording",
184                         stasis_app_control_get_channel_id(control));
185                 return -1;
186         case STASIS_APP_CHANNEL_OKAY:
187                 return 0;
188         }
189         return 0;
190 }
191
192 void ast_ari_bridges_add_channel(struct ast_variable *headers,
193         struct ast_ari_bridges_add_channel_args *args,
194         struct ast_ari_response *response)
195 {
196         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
197         RAII_VAR(struct control_list *, list, NULL, ao2_cleanup);
198         size_t i;
199         int has_error = 0;
200
201         if (!bridge) {
202                 /* Response filled in by find_bridge() */
203                 return;
204         }
205
206         list = control_list_create(response, args->channel_count, args->channel);
207         if (!list) {
208                 /* Response filled in by control_list_create() */
209                 return;
210         }
211
212         for (i = 0; i < list->count; ++i) {
213                 stasis_app_control_clear_roles(list->controls[i]);
214                 if (!ast_strlen_zero(args->role)) {
215                         if (stasis_app_control_add_role(list->controls[i], args->role)) {
216                                 ast_ari_response_alloc_failed(response);
217                                 return;
218                         }
219                 }
220         }
221
222         for (i = 0; i < list->count; ++i) {
223                 if ((has_error = check_add_remove_channel(response, list->controls[i],
224                              stasis_app_control_add_channel_to_bridge(
225                                     list->controls[i], bridge)))) {
226                         break;
227                 }
228         }
229
230         if (!has_error) {
231                 ast_ari_response_no_content(response);
232         }
233 }
234
235 void ast_ari_bridges_remove_channel(struct ast_variable *headers,
236         struct ast_ari_bridges_remove_channel_args *args,
237         struct ast_ari_response *response)
238 {
239         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
240         RAII_VAR(struct control_list *, list, NULL, ao2_cleanup);
241         size_t i;
242
243         if (!bridge) {
244                 /* Response filled in by find_bridge() */
245                 return;
246         }
247
248         list = control_list_create(response, args->channel_count, args->channel);
249         if (!list) {
250                 /* Response filled in by control_list_create() */
251                 return;
252         }
253
254         /* Make sure all of the channels are in this bridge */
255         for (i = 0; i < list->count; ++i) {
256                 if (stasis_app_get_bridge(list->controls[i]) != bridge) {
257                         ast_log(LOG_WARNING, "Channel %s not in bridge %s\n",
258                                 args->channel[i], args->bridge_id);
259                         ast_ari_response_error(response, 422,
260                                 "Unprocessable Entity",
261                                 "Channel not in this bridge");
262                         return;
263                 }
264         }
265
266         /* Now actually remove it */
267         for (i = 0; i < list->count; ++i) {
268                 stasis_app_control_remove_channel_from_bridge(list->controls[i],
269                         bridge);
270         }
271
272         ast_ari_response_no_content(response);
273 }
274
275 struct bridge_channel_control_thread_data {
276         struct ast_channel *bridge_channel;
277         struct stasis_app_control *control;
278         struct stasis_forward *forward;
279 };
280
281 static void *bridge_channel_control_thread(void *data)
282 {
283         struct bridge_channel_control_thread_data *thread_data = data;
284         struct ast_channel *bridge_channel = thread_data->bridge_channel;
285         struct stasis_app_control *control = thread_data->control;
286         struct stasis_forward *forward = thread_data->forward;
287
288         RAII_VAR(struct ast_callid *, callid, ast_channel_callid(bridge_channel), ast_callid_cleanup);
289
290         if (callid) {
291                 ast_callid_threadassoc_add(callid);
292         }
293
294         ast_free(thread_data);
295         thread_data = NULL;
296
297         stasis_app_control_execute_until_exhausted(bridge_channel, control);
298
299         ast_hangup(bridge_channel);
300         ao2_cleanup(control);
301         stasis_forward_cancel(forward);
302         return NULL;
303 }
304
305 static struct ast_channel *prepare_bridge_media_channel(const char *type)
306 {
307         RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup);
308         struct ast_channel *chan;
309
310         cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
311         if (!cap) {
312                 return NULL;
313         }
314
315         ast_format_cap_append(cap, ast_format_slin, 0);
316
317         chan = ast_request(type, cap, NULL, NULL, "ARI", NULL);
318         if (!chan) {
319                 return NULL;
320         }
321
322         if (stasis_app_channel_unreal_set_internal(chan)) {
323                 ast_channel_cleanup(chan);
324                 return NULL;
325         }
326         return chan;
327 }
328
329 /*!
330  * \brief Performs common setup for a bridge playback operation
331  * with both new controls and when existing controls are  found.
332  *
333  * \param args_media media string split from arguments
334  * \param args_lang language string split from arguments
335  * \param args_offset_ms milliseconds offset split from arguments
336  * \param args_playback_id string to use for playback split from
337  *        arguments (null valid)
338  * \param response ARI response being built
339  * \param bridge Bridge the playback is being peformed on
340  * \param control Control being used for the playback channel
341  * \param json contents of the response to ARI
342  * \param playback_url stores playback URL for use with response
343  *
344  * \retval -1 operation failed
345  * \retval operation was successful
346  */
347 static int ari_bridges_play_helper(const char *args_media,
348         const char *args_lang,
349         int args_offset_ms,
350         int args_skipms,
351         const char *args_playback_id,
352         struct ast_ari_response *response,
353         struct ast_bridge *bridge,
354         struct stasis_app_control *control,
355         struct ast_json **json,
356         char **playback_url)
357 {
358         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
359         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
360
361         const char *language;
362
363         snapshot = stasis_app_control_get_snapshot(control);
364         if (!snapshot) {
365                 ast_ari_response_error(
366                         response, 500, "Internal Error", "Failed to get control snapshot");
367                 return -1;
368         }
369
370         language = S_OR(args_lang, snapshot->language);
371
372         playback = stasis_app_control_play_uri(control, args_media, language,
373                 bridge->uniqueid, STASIS_PLAYBACK_TARGET_BRIDGE, args_skipms,
374                 args_offset_ms, args_playback_id);
375
376         if (!playback) {
377                 ast_ari_response_alloc_failed(response);
378                 return -1;
379         }
380
381         if (ast_asprintf(playback_url, "/playback/%s",
382                         stasis_app_playback_get_id(playback)) == -1) {
383                 playback_url = NULL;
384                 ast_ari_response_alloc_failed(response);
385                 return -1;
386         }
387
388         *json = stasis_app_playback_to_json(playback);
389         if (!*json) {
390                 ast_ari_response_alloc_failed(response);
391                 return -1;
392         }
393
394         return 0;
395 }
396
397 static void ari_bridges_play_new(const char *args_media,
398         const char *args_lang,
399         int args_offset_ms,
400         int args_skipms,
401         const char *args_playback_id,
402         struct ast_ari_response *response,
403         struct ast_bridge *bridge)
404 {
405         RAII_VAR(struct ast_channel *, play_channel, NULL, ast_hangup);
406         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
407         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
408         RAII_VAR(struct stasis_forward *, channel_forward, NULL, stasis_forward_cancel);
409         RAII_VAR(char *, playback_url, NULL, ast_free);
410
411         struct stasis_topic *channel_topic;
412         struct stasis_topic *bridge_topic;
413         struct bridge_channel_control_thread_data *thread_data;
414         pthread_t threadid;
415
416         if (!(play_channel = prepare_bridge_media_channel("Announcer"))) {
417                 ast_ari_response_error(
418                         response, 500, "Internal Error", "Could not create playback channel");
419                 return;
420         }
421         ast_debug(1, "Created announcer channel '%s'\n", ast_channel_name(play_channel));
422
423         bridge_topic = ast_bridge_topic(bridge);
424         channel_topic = ast_channel_topic(play_channel);
425
426         /* Forward messages from the playback channel topic to the bridge topic so that anything listening for
427          * messages on the bridge topic will receive the playback start/stop messages. Other messages that would
428          * go to this channel will be suppressed since the channel is marked as internal.
429          */
430         if (!bridge_topic || !channel_topic || !(channel_forward = stasis_forward_all(channel_topic, bridge_topic))) {
431                 ast_ari_response_error(
432                         response, 500, "Internal Error", "Could not forward playback channel stasis messages to bridge topic");
433                 return;
434         }
435
436         if (ast_unreal_channel_push_to_bridge(play_channel, bridge,
437                 AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
438                 ast_ari_response_error(
439                         response, 500, "Internal Error", "Failed to put playback channel into the bridge");
440                 return;
441         }
442
443         control = stasis_app_control_create(play_channel);
444         if (control == NULL) {
445                 ast_ari_response_alloc_failed(response);
446                 return;
447         }
448
449         ao2_lock(control);
450         if (ari_bridges_play_helper(args_media, args_lang, args_offset_ms,
451                         args_skipms, args_playback_id, response, bridge, control,
452                         &json, &playback_url)) {
453                 ao2_unlock(control);
454                 return;
455         }
456         ao2_unlock(control);
457
458         if (stasis_app_bridge_playback_channel_add(bridge, play_channel, control)) {
459                 ast_ari_response_alloc_failed(response);
460                 return;
461         }
462
463         /* Give play_channel and control reference to the thread data */
464         thread_data = ast_calloc(1, sizeof(*thread_data));
465         if (!thread_data) {
466                 ast_ari_response_alloc_failed(response);
467                 return;
468         }
469
470         thread_data->bridge_channel = play_channel;
471         thread_data->control = control;
472         thread_data->forward = channel_forward;
473
474         if (ast_pthread_create_detached(&threadid, NULL, bridge_channel_control_thread, thread_data)) {
475                 ast_ari_response_alloc_failed(response);
476                 ast_free(thread_data);
477                 return;
478         }
479
480         /* These are owned by the other thread now, so we don't want RAII_VAR disposing of them. */
481         play_channel = NULL;
482         control = NULL;
483         channel_forward = NULL;
484
485         ast_ari_response_created(response, playback_url, ast_json_ref(json));
486 }
487
488 enum play_found_result {
489         PLAY_FOUND_SUCCESS,
490         PLAY_FOUND_FAILURE,
491         PLAY_FOUND_CHANNEL_UNAVAILABLE,
492 };
493
494 /*!
495  * \brief Performs common setup for a bridge playback operation
496  * with both new controls and when existing controls are  found.
497  *
498  * \param args_media media string split from arguments
499  * \param args_lang language string split from arguments
500  * \param args_offset_ms milliseconds offset split from arguments
501  * \param args_playback_id string to use for playback split from
502  *        arguments (null valid)
503  * \param response ARI response being built
504  * \param bridge Bridge the playback is being peformed on
505  * \param found_channel The channel that was found controlling playback
506  *
507  * \retval PLAY_FOUND_SUCCESS The operation was successful
508  * \retval PLAY_FOUND_FAILURE The operation failed (terminal failure)
509  * \retval PLAY_FOUND_CHANNEL_UNAVAILABLE The operation failed because
510  * the channel requested to playback with is breaking down.
511  */
512 static enum play_found_result ari_bridges_play_found(const char *args_media,
513         const char *args_lang,
514         int args_offset_ms,
515         int args_skipms,
516         const char *args_playback_id,
517         struct ast_ari_response *response,
518         struct ast_bridge *bridge,
519         struct ast_channel *found_channel)
520 {
521         RAII_VAR(struct ast_channel *, play_channel, found_channel, ao2_cleanup);
522         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
523         RAII_VAR(char *, playback_url, NULL, ast_free);
524         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
525
526         control = stasis_app_control_find_by_channel(play_channel);
527         if (!control) {
528                 ast_ari_response_error(
529                         response, 500, "Internal Error", "Failed to get control snapshot");
530                 return PLAY_FOUND_FAILURE;
531         }
532
533         ao2_lock(control);
534         if (stasis_app_control_is_done(control)) {
535                 /* We failed to queue the action. Bailout and return that we aren't terminal. */
536                 ao2_unlock(control);
537                 return PLAY_FOUND_CHANNEL_UNAVAILABLE;
538         }
539
540         if (ari_bridges_play_helper(args_media, args_lang, args_offset_ms,
541                         args_skipms, args_playback_id, response, bridge, control,
542                         &json, &playback_url)) {
543                 ao2_unlock(control);
544                 return PLAY_FOUND_FAILURE;
545         }
546         ao2_unlock(control);
547
548         ast_ari_response_created(response, playback_url, ast_json_ref(json));
549         return PLAY_FOUND_SUCCESS;
550 }
551
552 static void ari_bridges_handle_play(
553         const char *args_bridge_id,
554         const char *args_media,
555         const char *args_lang,
556         int args_offset_ms,
557         int args_skipms,
558         const char *args_playback_id,
559         struct ast_ari_response *response)
560 {
561         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args_bridge_id), ao2_cleanup);
562         struct ast_channel *play_channel;
563
564         ast_assert(response != NULL);
565
566         if (!bridge) {
567                 return;
568         }
569
570         while ((play_channel = stasis_app_bridge_playback_channel_find(bridge))) {
571                 /* If ari_bridges_play_found fails because the channel is unavailable for
572                  * playback, The channel will be removed from the playback list soon. We
573                  * can keep trying to get channels from the list until we either get one
574                  * that will work or else there isn't a channel for this bridge anymore,
575                  * in which case we'll revert to ari_bridges_play_new.
576                  */
577                 if (ari_bridges_play_found(args_media, args_lang, args_offset_ms,
578                                 args_skipms, args_playback_id, response,bridge,
579                                 play_channel) == PLAY_FOUND_CHANNEL_UNAVAILABLE) {
580                         continue;
581                 }
582                 return;
583         }
584
585         ari_bridges_play_new(args_media, args_lang, args_offset_ms,
586                 args_skipms, args_playback_id, response, bridge);
587 }
588
589
590 void ast_ari_bridges_play(struct ast_variable *headers,
591         struct ast_ari_bridges_play_args *args,
592         struct ast_ari_response *response)
593 {
594         ari_bridges_handle_play(args->bridge_id,
595         args->media,
596         args->lang,
597         args->offsetms,
598         args->skipms,
599         args->playback_id,
600         response);
601 }
602
603 void ast_ari_bridges_play_with_id(struct ast_variable *headers,
604         struct ast_ari_bridges_play_with_id_args *args,
605         struct ast_ari_response *response)
606 {
607         ari_bridges_handle_play(args->bridge_id,
608         args->media,
609         args->lang,
610         args->offsetms,
611         args->skipms,
612         args->playback_id,
613         response);
614 }
615
616 void ast_ari_bridges_record(struct ast_variable *headers,
617         struct ast_ari_bridges_record_args *args,
618         struct ast_ari_response *response)
619 {
620         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
621         RAII_VAR(struct ast_channel *, record_channel, NULL, ast_hangup);
622         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
623         RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
624         RAII_VAR(char *, recording_url, NULL, ast_free);
625         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
626         RAII_VAR(struct stasis_app_recording_options *, options, NULL, ao2_cleanup);
627         RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
628         RAII_VAR(struct stasis_forward *, channel_forward, NULL, stasis_forward_cancel);
629
630         struct stasis_topic *channel_topic;
631         struct stasis_topic *bridge_topic;
632         size_t uri_name_maxlen;
633         struct bridge_channel_control_thread_data *thread_data;
634         pthread_t threadid;
635
636         ast_assert(response != NULL);
637
638         if (bridge == NULL) {
639                 return;
640         }
641
642         if (!(record_channel = prepare_bridge_media_channel("Recorder"))) {
643                 ast_ari_response_error(
644                         response, 500, "Internal Server Error", "Failed to create recording channel");
645                 return;
646         }
647
648         bridge_topic = ast_bridge_topic(bridge);
649         channel_topic = ast_channel_topic(record_channel);
650
651         /* Forward messages from the recording channel topic to the bridge topic so that anything listening for
652          * messages on the bridge topic will receive the recording start/stop messages. Other messages that would
653          * go to this channel will be suppressed since the channel is marked as internal.
654          */
655         if (!bridge_topic || !channel_topic || !(channel_forward = stasis_forward_all(channel_topic, bridge_topic))) {
656                 ast_ari_response_error(
657                         response, 500, "Internal Error", "Could not forward record channel stasis messages to bridge topic");
658                 return;
659         }
660
661         if (ast_unreal_channel_push_to_bridge(record_channel, bridge,
662                 AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
663                 ast_ari_response_error(
664                         response, 500, "Internal Error", "Failed to put recording channel into the bridge");
665                 return;
666         }
667
668         control = stasis_app_control_create(record_channel);
669         if (control == NULL) {
670                 ast_ari_response_alloc_failed(response);
671                 return;
672         }
673
674         options = stasis_app_recording_options_create(args->name, args->format);
675         if (options == NULL) {
676                 ast_ari_response_alloc_failed(response);
677                 return;
678         }
679
680         ast_string_field_build(options, target, "bridge:%s", args->bridge_id);
681         options->max_silence_seconds = args->max_silence_seconds;
682         options->max_duration_seconds = args->max_duration_seconds;
683         options->terminate_on =
684                 stasis_app_recording_termination_parse(args->terminate_on);
685         options->if_exists =
686                 stasis_app_recording_if_exists_parse(args->if_exists);
687         options->beep = args->beep;
688
689         if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
690                 ast_ari_response_error(
691                         response, 400, "Bad Request",
692                         "terminateOn invalid");
693                 return;
694         }
695
696         if (options->if_exists == -1) {
697                 ast_ari_response_error(
698                         response, 400, "Bad Request",
699                         "ifExists invalid");
700                 return;
701         }
702
703         if (!ast_get_format_for_file_ext(options->format)) {
704                 ast_ari_response_error(
705                         response, 422, "Unprocessable Entity",
706                         "specified format is unknown on this system");
707                 return;
708         }
709
710         recording = stasis_app_control_record(control, options);
711         if (recording == NULL) {
712                 switch(errno) {
713                 case EINVAL:
714                         /* While the arguments are invalid, we should have
715                          * caught them prior to calling record.
716                          */
717                         ast_ari_response_error(
718                                 response, 500, "Internal Server Error",
719                                 "Error parsing request");
720                         break;
721                 case EEXIST:
722                         ast_ari_response_error(response, 409, "Conflict",
723                                 "Recording '%s' already exists and can not be overwritten",
724                                 args->name);
725                         break;
726                 case ENOMEM:
727                         ast_ari_response_alloc_failed(response);
728                         break;
729                 case EPERM:
730                         ast_ari_response_error(
731                                 response, 400, "Bad Request",
732                                 "Recording name invalid");
733                         break;
734                 default:
735                         ast_log(LOG_WARNING,
736                                 "Unrecognized recording error: %s\n",
737                                 strerror(errno));
738                         ast_ari_response_error(
739                                 response, 500, "Internal Server Error",
740                                 "Internal Server Error");
741                         break;
742                 }
743                 return;
744         }
745
746         uri_name_maxlen = strlen(args->name) * 3;
747         uri_encoded_name = ast_malloc(uri_name_maxlen);
748         if (!uri_encoded_name) {
749                 ast_ari_response_alloc_failed(response);
750                 return;
751         }
752         ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen, ast_uri_http);
753
754         if (ast_asprintf(&recording_url, "/recordings/live/%s",
755                         uri_encoded_name) == -1) {
756                 recording_url = NULL;
757                 ast_ari_response_alloc_failed(response);
758                 return;
759         }
760
761         json = stasis_app_recording_to_json(recording);
762         if (!json) {
763                 ast_ari_response_alloc_failed(response);
764                 return;
765         }
766
767         thread_data = ast_calloc(1, sizeof(*thread_data));
768         if (!thread_data) {
769                 ast_ari_response_alloc_failed(response);
770                 return;
771         }
772
773         thread_data->bridge_channel = record_channel;
774         thread_data->control = control;
775         thread_data->forward = channel_forward;
776
777         if (ast_pthread_create_detached(&threadid, NULL, bridge_channel_control_thread, thread_data)) {
778                 ast_ari_response_alloc_failed(response);
779                 ast_free(thread_data);
780                 return;
781         }
782
783         /* These are owned by the other thread now, so we don't want RAII_VAR disposing of them. */
784         record_channel = NULL;
785         control = NULL;
786         channel_forward = NULL;
787
788         ast_ari_response_created(response, recording_url, ast_json_ref(json));
789 }
790
791 void ast_ari_bridges_start_moh(struct ast_variable *headers,
792         struct ast_ari_bridges_start_moh_args *args,
793         struct ast_ari_response *response)
794 {
795         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
796         struct ast_channel *moh_channel;
797         const char *moh_class = args->moh_class;
798
799         if (!bridge) {
800                 /* The response is provided by find_bridge() */
801                 return;
802         }
803
804         moh_channel = stasis_app_bridge_moh_channel(bridge);
805         if (!moh_channel) {
806                 ast_ari_response_alloc_failed(response);
807                 return;
808         }
809
810         ast_moh_start(moh_channel, moh_class, NULL);
811
812         ast_ari_response_no_content(response);
813
814 }
815
816 void ast_ari_bridges_stop_moh(struct ast_variable *headers,
817         struct ast_ari_bridges_stop_moh_args *args,
818         struct ast_ari_response *response)
819 {
820         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
821
822         if (!bridge) {
823                 /* the response is provided by find_bridge() */
824                 return;
825         }
826
827         if (stasis_app_bridge_moh_stop(bridge)) {
828                 ast_ari_response_error(
829                         response, 409, "Conflict",
830                         "Bridge isn't playing music");
831                 return;
832         }
833
834         ast_ari_response_no_content(response);
835 }
836
837 void ast_ari_bridges_get(struct ast_variable *headers,
838         struct ast_ari_bridges_get_args *args,
839         struct ast_ari_response *response)
840 {
841         RAII_VAR(struct ast_bridge_snapshot *, snapshot, ast_bridge_snapshot_get_latest(args->bridge_id), ao2_cleanup);
842         if (!snapshot) {
843                 ast_ari_response_error(
844                         response, 404, "Not Found",
845                         "Bridge not found");
846                 return;
847         }
848
849         ast_ari_response_ok(response,
850                 ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
851 }
852
853 void ast_ari_bridges_destroy(struct ast_variable *headers,
854         struct ast_ari_bridges_destroy_args *args,
855         struct ast_ari_response *response)
856 {
857         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
858         if (!bridge) {
859                 return;
860         }
861
862         stasis_app_bridge_destroy(args->bridge_id);
863         ast_ari_response_no_content(response);
864 }
865
866 void ast_ari_bridges_list(struct ast_variable *headers,
867         struct ast_ari_bridges_list_args *args,
868         struct ast_ari_response *response)
869 {
870         RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
871         RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
872         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
873         struct ao2_iterator i;
874         void *obj;
875
876         cache = ast_bridge_cache();
877         if (!cache) {
878                 ast_ari_response_error(
879                         response, 500, "Internal Server Error",
880                         "Message bus not initialized");
881                 return;
882         }
883         ao2_ref(cache, +1);
884
885         snapshots = stasis_cache_dump(cache, ast_bridge_snapshot_type());
886         if (!snapshots) {
887                 ast_ari_response_alloc_failed(response);
888                 return;
889         }
890
891         json = ast_json_array_create();
892         if (!json) {
893                 ast_ari_response_alloc_failed(response);
894                 return;
895         }
896
897         i = ao2_iterator_init(snapshots, 0);
898         while ((obj = ao2_iterator_next(&i))) {
899                 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
900                 struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
901                 struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
902
903                 if (!json_bridge || ast_json_array_append(json, json_bridge)) {
904                         ao2_iterator_destroy(&i);
905                         ast_ari_response_alloc_failed(response);
906                         return;
907                 }
908         }
909         ao2_iterator_destroy(&i);
910
911         ast_ari_response_ok(response, ast_json_ref(json));
912 }
913
914 void ast_ari_bridges_create(struct ast_variable *headers,
915         struct ast_ari_bridges_create_args *args,
916         struct ast_ari_response *response)
917 {
918         RAII_VAR(struct ast_bridge *, bridge, stasis_app_bridge_create(args->type, args->name, args->bridge_id), ao2_cleanup);
919         RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
920
921         if (!bridge) {
922                 ast_ari_response_error(
923                         response, 500, "Internal Error",
924                         "Unable to create bridge");
925                 return;
926         }
927
928         ast_bridge_lock(bridge);
929         snapshot = ast_bridge_snapshot_create(bridge);
930         ast_bridge_unlock(bridge);
931
932         if (!snapshot) {
933                 ast_ari_response_error(
934                         response, 500, "Internal Error",
935                         "Unable to create snapshot for new bridge");
936                 return;
937         }
938
939         ast_ari_response_ok(response,
940                 ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
941 }
942
943 void ast_ari_bridges_create_or_update_with_id(struct ast_variable *headers,
944         struct ast_ari_bridges_create_or_update_with_id_args *args,
945         struct ast_ari_response *response)
946 {
947         RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
948         RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
949
950         if (bridge) {
951                 /* update */
952                 if (strcmp(args->name, bridge->name)) {
953                         ast_ari_response_error(
954                                 response, 500, "Internal Error",
955                                 "Changing bridge name is not implemented");
956                         return;
957                 }
958                 if (!ast_strlen_zero(args->type)) {
959                         ast_ari_response_error(
960                                 response, 500, "Internal Error",
961                                 "Changing bridge type is not implemented");
962                         return;
963                 }
964                 ast_ari_response_ok(response,
965                         ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
966                 return;
967         }
968
969         bridge = stasis_app_bridge_create(args->type, args->name, args->bridge_id);
970         if (!bridge) {
971                 ast_ari_response_error(
972                         response, 500, "Internal Error",
973                         "Unable to create bridge");
974                 return;
975         }
976
977         ast_bridge_lock(bridge);
978         snapshot = ast_bridge_snapshot_create(bridge);
979         ast_bridge_unlock(bridge);
980
981         if (!snapshot) {
982                 ast_ari_response_error(
983                         response, 500, "Internal Error",
984                         "Unable to create snapshot for new bridge");
985                 return;
986         }
987
988         ast_ari_response_ok(response,
989                 ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
990 }