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