This patch adds support for controlling a playback operation from the
[asterisk/asterisk.git] / res / stasis_http / resource_playback.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/playback.{format} implementation- Playback control 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_playback.h"
31 #include "resource_playback.h"
32
33 void stasis_http_get_playback(struct ast_variable *headers,
34         struct ast_get_playback_args *args,
35         struct stasis_http_response *response)
36 {
37         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
38         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
39
40         playback = stasis_app_playback_find_by_id(args->playback_id);
41         if (playback == NULL) {
42                 stasis_http_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                 stasis_http_response_error(response, 500,
50                         "Internal Server Error", "Error building response");
51                 return;
52         }
53
54         stasis_http_response_ok(response, ast_json_ref(json));
55 }
56 void stasis_http_stop_playback(struct ast_variable *headers,
57         struct ast_stop_playback_args *args,
58         struct stasis_http_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                 stasis_http_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
72         switch (res) {
73         case STASIS_PLAYBACK_OPER_OK:
74                 stasis_http_response_no_content(response);
75                 return;
76         case STASIS_PLAYBACK_OPER_FAILED:
77                 stasis_http_response_error(response, 500,
78                         "Internal Server Error", "Could not stop playback");
79                 return;
80         case STASIS_PLAYBACK_OPER_NOT_PLAYING:
81                 /* Stop operation should be valid even when not playing */
82                 ast_assert(0);
83                 stasis_http_response_error(response, 500,
84                         "Internal Server Error", "Could not stop playback");
85                 return;
86         }
87 }
88 void stasis_http_control_playback(struct ast_variable *headers,
89         struct ast_control_playback_args *args,
90         struct stasis_http_response *response)
91 {
92         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
93         enum stasis_app_playback_media_operation oper;
94         enum stasis_playback_oper_results res;
95
96         if (strcmp(args->operation, "unpause") == 0) {
97                 oper = STASIS_PLAYBACK_UNPAUSE;
98         } else if (strcmp(args->operation, "pause") == 0) {
99                 oper = STASIS_PLAYBACK_PAUSE;
100         } else if (strcmp(args->operation, "restart") == 0) {
101                 oper = STASIS_PLAYBACK_RESTART;
102         } else if (strcmp(args->operation, "reverse") == 0) {
103                 oper = STASIS_PLAYBACK_REVERSE;
104         } else if (strcmp(args->operation, "forward") == 0) {
105                 oper = STASIS_PLAYBACK_FORWARD;
106         } else {
107                 stasis_http_response_error(response, 400,
108                         "Bad Request", "Invalid operation %s",
109                         args->operation);
110                 return;
111
112         }
113
114         playback = stasis_app_playback_find_by_id(args->playback_id);
115         if (playback == NULL) {
116                 stasis_http_response_error(response, 404, "Not Found",
117                         "Playback not found");
118                 return;
119         }
120
121         res = stasis_app_playback_operation(playback, oper);
122
123         switch (res) {
124         case STASIS_PLAYBACK_OPER_OK:
125                 stasis_http_response_no_content(response);
126                 return;
127         case STASIS_PLAYBACK_OPER_FAILED:
128                 stasis_http_response_error(response, 500,
129                         "Internal Server Error", "Could not %s playback",
130                         args->operation);
131                 return;
132         case STASIS_PLAYBACK_OPER_NOT_PLAYING:
133                 stasis_http_response_error(response, 409, "Conflict",
134                         "Can only %s while media is playing", args->operation);
135                 return;
136         }
137 }