resource_playbacks.c 4.0 KB

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