resource_playbacks.c 4.1 KB

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