summaryrefslogtreecommitdiff
path: root/res/ari/resource_playbacks.c
blob: fc05a8d03a0543db78f6f2579498dac3059df846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012 - 2013, Digium, Inc.
 *
 * David M. Lee, II <dlee@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief /api-docs/playbacks.{format} implementation- Playback control resources
 *
 * \author David M. Lee, II <dlee@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/stasis_app_playback.h"
#include "resource_playbacks.h"

void ast_ari_playbacks_get(struct ast_variable *headers,
	struct ast_ari_playbacks_get_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
	struct ast_json *json;

	playback = stasis_app_playback_find_by_id(args->playback_id);
	if (playback == NULL) {
		ast_ari_response_error(response, 404, "Not Found",
			"Playback not found");
		return;
	}

	json = stasis_app_playback_to_json(playback);
	if (json == NULL) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}
void ast_ari_playbacks_stop(struct ast_variable *headers,
	struct ast_ari_playbacks_stop_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
	enum stasis_playback_oper_results res;

	playback = stasis_app_playback_find_by_id(args->playback_id);
	if (playback == NULL) {
		ast_ari_response_error(response, 404, "Not Found",
			"Playback not found");
		return;
	}

	res = stasis_app_playback_operation(playback, STASIS_PLAYBACK_STOP);
	switch (res) {
	case STASIS_PLAYBACK_OPER_OK:
		ast_ari_response_no_content(response);
		return;
	case STASIS_PLAYBACK_OPER_FAILED:
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Could not stop playback");
		return;
	case STASIS_PLAYBACK_OPER_NOT_PLAYING:
		/* Stop operation should be valid even when not playing */
		ast_assert(0);
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Could not stop playback");
		return;
	}
}
void ast_ari_playbacks_control(struct ast_variable *headers,
	struct ast_ari_playbacks_control_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
	enum stasis_app_playback_media_operation oper;
	enum stasis_playback_oper_results res;

	if (!args->operation) {
		ast_ari_response_error(response, 400,
			"Bad Request", "Missing operation");
		return;
	}
	if (strcmp(args->operation, "unpause") == 0) {
		oper = STASIS_PLAYBACK_UNPAUSE;
	} else if (strcmp(args->operation, "pause") == 0) {
		oper = STASIS_PLAYBACK_PAUSE;
	} else if (strcmp(args->operation, "restart") == 0) {
		oper = STASIS_PLAYBACK_RESTART;
	} else if (strcmp(args->operation, "reverse") == 0) {
		oper = STASIS_PLAYBACK_REVERSE;
	} else if (strcmp(args->operation, "forward") == 0) {
		oper = STASIS_PLAYBACK_FORWARD;
	} else {
		ast_ari_response_error(response, 400,
			"Bad Request", "Invalid operation %s",
			args->operation);
		return;
	}

	playback = stasis_app_playback_find_by_id(args->playback_id);
	if (playback == NULL) {
		ast_ari_response_error(response, 404, "Not Found",
			"Playback not found");
		return;
	}

	res = stasis_app_playback_operation(playback, oper);
	switch (res) {
	case STASIS_PLAYBACK_OPER_OK:
		ast_ari_response_no_content(response);
		return;
	case STASIS_PLAYBACK_OPER_FAILED:
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Could not %s playback",
			args->operation);
		return;
	case STASIS_PLAYBACK_OPER_NOT_PLAYING:
		ast_ari_response_error(response, 409, "Conflict",
			"Can only %s while media is playing", args->operation);
		return;
	}
}