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