Remove ASTERISK_REGISTER_FILE.
[asterisk/asterisk.git] / res / ari / resource_playbacks.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/playbacks.{format} implementation- Playback control resources
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend type="module">res_stasis_playback</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 #include "asterisk/stasis_app_playback.h"
34 #include "resource_playbacks.h"
35
36 void ast_ari_playbacks_get(struct ast_variable *headers,
37         struct ast_ari_playbacks_get_args *args,
38         struct ast_ari_response *response)
39 {
40         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
41         struct ast_json *json;
42
43         playback = stasis_app_playback_find_by_id(args->playback_id);
44         if (playback == NULL) {
45                 ast_ari_response_error(response, 404, "Not Found",
46                         "Playback not found");
47                 return;
48         }
49
50         json = stasis_app_playback_to_json(playback);
51         if (json == NULL) {
52                 ast_ari_response_error(response, 500,
53                         "Internal Server Error", "Error building response");
54                 return;
55         }
56
57         ast_ari_response_ok(response, json);
58 }
59 void ast_ari_playbacks_stop(struct ast_variable *headers,
60         struct ast_ari_playbacks_stop_args *args,
61         struct ast_ari_response *response)
62 {
63         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
64         enum stasis_playback_oper_results res;
65
66         playback = stasis_app_playback_find_by_id(args->playback_id);
67         if (playback == NULL) {
68                 ast_ari_response_error(response, 404, "Not Found",
69                         "Playback not found");
70                 return;
71         }
72
73         res = stasis_app_playback_operation(playback, STASIS_PLAYBACK_STOP);
74         switch (res) {
75         case STASIS_PLAYBACK_OPER_OK:
76                 ast_ari_response_no_content(response);
77                 return;
78         case STASIS_PLAYBACK_OPER_FAILED:
79                 ast_ari_response_error(response, 500,
80                         "Internal Server Error", "Could not stop playback");
81                 return;
82         case STASIS_PLAYBACK_OPER_NOT_PLAYING:
83                 /* Stop operation should be valid even when not playing */
84                 ast_assert(0);
85                 ast_ari_response_error(response, 500,
86                         "Internal Server Error", "Could not stop playback");
87                 return;
88         }
89 }
90 void ast_ari_playbacks_control(struct ast_variable *headers,
91         struct ast_ari_playbacks_control_args *args,
92         struct ast_ari_response *response)
93 {
94         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
95         enum stasis_app_playback_media_operation oper;
96         enum stasis_playback_oper_results res;
97
98         if (!args->operation) {
99                 ast_ari_response_error(response, 400,
100                         "Bad Request", "Missing operation");
101                 return;
102         }
103         if (strcmp(args->operation, "unpause") == 0) {
104                 oper = STASIS_PLAYBACK_UNPAUSE;
105         } else if (strcmp(args->operation, "pause") == 0) {
106                 oper = STASIS_PLAYBACK_PAUSE;
107         } else if (strcmp(args->operation, "restart") == 0) {
108                 oper = STASIS_PLAYBACK_RESTART;
109         } else if (strcmp(args->operation, "reverse") == 0) {
110                 oper = STASIS_PLAYBACK_REVERSE;
111         } else if (strcmp(args->operation, "forward") == 0) {
112                 oper = STASIS_PLAYBACK_FORWARD;
113         } else {
114                 ast_ari_response_error(response, 400,
115                         "Bad Request", "Invalid operation %s",
116                         args->operation);
117                 return;
118         }
119
120         playback = stasis_app_playback_find_by_id(args->playback_id);
121         if (playback == NULL) {
122                 ast_ari_response_error(response, 404, "Not Found",
123                         "Playback not found");
124                 return;
125         }
126
127         res = stasis_app_playback_operation(playback, oper);
128         switch (res) {
129         case STASIS_PLAYBACK_OPER_OK:
130                 ast_ari_response_no_content(response);
131                 return;
132         case STASIS_PLAYBACK_OPER_FAILED:
133                 ast_ari_response_error(response, 500,
134                         "Internal Server Error", "Could not %s playback",
135                         args->operation);
136                 return;
137         case STASIS_PLAYBACK_OPER_NOT_PLAYING:
138                 ast_ari_response_error(response, 409, "Conflict",
139                         "Can only %s while media is playing", args->operation);
140                 return;
141         }
142 }