bc07d55992040edec2f72bcdf4fa3433ae3597e6
[asterisk/asterisk.git] / res / ari / resource_recordings.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 /api-docs/recordings.{format} implementation- Recording resources
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/stasis_app_recording.h"
31 #include "resource_recordings.h"
32
33 void ast_ari_recordings_list_stored(struct ast_variable *headers,
34         struct ast_ari_recordings_list_stored_args *args,
35         struct ast_ari_response *response)
36 {
37         RAII_VAR(struct ao2_container *, recordings, NULL, ao2_cleanup);
38         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
39         struct ao2_iterator i;
40         void *obj;
41
42         recordings = stasis_app_stored_recording_find_all();
43
44         if (!recordings) {
45                 ast_ari_response_alloc_failed(response);
46                 return;
47         }
48
49         json = ast_json_array_create();
50         if (!json) {
51                 ast_ari_response_alloc_failed(response);
52                 return;
53         }
54
55         i = ao2_iterator_init(recordings, 0);
56         while ((obj = ao2_iterator_next(&i))) {
57                 RAII_VAR(struct stasis_app_stored_recording *, recording, obj,
58                         ao2_cleanup);
59
60                 int r = ast_json_array_append(
61                         json, stasis_app_stored_recording_to_json(recording));
62                 if (r != 0) {
63                         ast_ari_response_alloc_failed(response);
64                         ao2_iterator_destroy(&i);
65                         return;
66                 }
67         }
68         ao2_iterator_destroy(&i);
69
70         ast_ari_response_ok(response, ast_json_ref(json));
71 }
72
73 void ast_ari_recordings_get_stored(struct ast_variable *headers,
74         struct ast_ari_recordings_get_stored_args *args,
75         struct ast_ari_response *response)
76 {
77         RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
78                 ao2_cleanup);
79         struct ast_json *json;
80
81         recording = stasis_app_stored_recording_find_by_name(
82                 args->recording_name);
83         if (recording == NULL) {
84                 ast_ari_response_error(response, 404, "Not Found",
85                         "Recording not found");
86                 return;
87         }
88
89         json = stasis_app_stored_recording_to_json(recording);
90         if (json == NULL) {
91                 ast_ari_response_error(response, 500,
92                         "Internal Server Error", "Error building response");
93                 return;
94         }
95
96         ast_ari_response_ok(response, json);
97 }
98
99 void ast_ari_recordings_delete_stored(struct ast_variable *headers,
100         struct ast_ari_recordings_delete_stored_args *args,
101         struct ast_ari_response *response)
102 {
103         RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
104                 ao2_cleanup);
105         int res;
106
107         recording = stasis_app_stored_recording_find_by_name(
108                 args->recording_name);
109         if (recording == NULL) {
110                 ast_ari_response_error(response, 404, "Not Found",
111                         "Recording not found");
112                 return;
113         }
114
115         res = stasis_app_stored_recording_delete(recording);
116
117         if (res != 0) {
118                 switch (errno) {
119                 case EACCES:
120                 case EPERM:
121                         ast_ari_response_error(response, 500,
122                                 "Internal Server Error",
123                                 "Delete failed");
124                         break;
125                 default:
126                         ast_log(LOG_WARNING,
127                                 "Unexpected error deleting recording %s: %s\n",
128                                 args->recording_name, strerror(errno));
129                         ast_ari_response_error(response, 500,
130                                 "Internal Server Error",
131                                 "Delete failed");
132                         break;
133                 }
134                 return;
135         }
136
137         ast_ari_response_no_content(response);
138 }
139
140 void ast_ari_recordings_get_live(struct ast_variable *headers,
141         struct ast_ari_recordings_get_live_args *args,
142         struct ast_ari_response *response)
143 {
144         RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
145         struct ast_json *json;
146
147         recording = stasis_app_recording_find_by_name(args->recording_name);
148         if (recording == NULL) {
149                 ast_ari_response_error(response, 404, "Not Found",
150                         "Recording not found");
151                 return;
152         }
153
154         json = stasis_app_recording_to_json(recording);
155         if (json == NULL) {
156                 ast_ari_response_error(response, 500,
157                         "Internal Server Error", "Error building response");
158                 return;
159         }
160
161         ast_ari_response_ok(response, json);
162 }
163
164 static void control_recording(const char *name,
165         enum stasis_app_recording_media_operation operation,
166         struct ast_ari_response *response)
167 {
168         RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
169         enum stasis_app_recording_oper_results res;
170
171         recording = stasis_app_recording_find_by_name(name);
172         if (recording == NULL) {
173                 ast_ari_response_error(response, 404, "Not Found",
174                         "Recording not found");
175                 return;
176         }
177
178         res = stasis_app_recording_operation(recording, operation);
179
180         switch (res) {
181         case STASIS_APP_RECORDING_OPER_OK:
182                 ast_ari_response_no_content(response);
183                 return;
184         case STASIS_APP_RECORDING_OPER_FAILED:
185                 ast_ari_response_error(response, 500,
186                         "Internal Server Error", "Recording operation failed");
187                 return;
188         case STASIS_APP_RECORDING_OPER_NOT_RECORDING:
189                 ast_ari_response_error(response, 409,
190                         "Conflict", "Recording not in session");
191         }
192 }
193
194 void ast_ari_recordings_cancel(struct ast_variable *headers,
195         struct ast_ari_recordings_cancel_args *args,
196         struct ast_ari_response *response)
197 {
198         control_recording(args->recording_name, STASIS_APP_RECORDING_CANCEL,
199                 response);
200 }
201
202 void ast_ari_recordings_stop(struct ast_variable *headers,
203         struct ast_ari_recordings_stop_args *args,
204         struct ast_ari_response *response)
205 {
206         control_recording(args->recording_name, STASIS_APP_RECORDING_STOP,
207                 response);
208 }
209
210 void ast_ari_recordings_pause(struct ast_variable *headers,
211         struct ast_ari_recordings_pause_args *args,
212         struct ast_ari_response *response)
213 {
214         control_recording(args->recording_name, STASIS_APP_RECORDING_PAUSE,
215                 response);
216 }
217
218 void ast_ari_recordings_unpause(struct ast_variable *headers,
219         struct ast_ari_recordings_unpause_args *args,
220         struct ast_ari_response *response)
221 {
222         control_recording(args->recording_name, STASIS_APP_RECORDING_UNPAUSE,
223                 response);
224 }
225
226 void ast_ari_recordings_mute(struct ast_variable *headers,
227         struct ast_ari_recordings_mute_args *args,
228         struct ast_ari_response *response)
229 {
230         control_recording(args->recording_name, STASIS_APP_RECORDING_MUTE,
231                 response);
232 }
233
234 void ast_ari_recordings_unmute(struct ast_variable *headers,
235         struct ast_ari_recordings_unmute_args *args,
236         struct ast_ari_response *response)
237 {
238         control_recording(args->recording_name, STASIS_APP_RECORDING_UNMUTE,
239                 response);
240 }