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