res/res_ari: Added ARI resource /ari/channels/{channelId}/rtp_statistics
[asterisk/asterisk.git] / res / ari / resource_channels.c
index 08f97f1..c195eef 100644 (file)
 #include "asterisk/core_local.h"
 #include "asterisk/dial.h"
 #include "asterisk/max_forwards.h"
+#include "asterisk/rtp_engine.h"
 #include "resource_channels.h"
 
 #include <limits.h>
 
+
 /*!
  * \brief Ensure channel is in a state that allows operation to be performed.
  *
@@ -217,6 +219,26 @@ void ast_ari_channels_continue_in_dialplan(
        ast_ari_response_no_content(response);
 }
 
+void ast_ari_channels_move(struct ast_variable *headers,
+       struct ast_ari_channels_move_args *args,
+       struct ast_ari_response *response)
+{
+       RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+
+       control = find_control(response, args->channel_id);
+       if (!control) {
+               return;
+       }
+
+       if (stasis_app_control_move(control, args->app, args->app_args)) {
+               ast_ari_response_error(response, 500, "Internal Server Error",
+                       "Failed to switch Stasis applications");
+               return;
+       }
+
+       ast_ari_response_no_content(response);
+}
+
 void ast_ari_channels_redirect(struct ast_variable *headers,
        struct ast_ari_channels_redirect_args *args,
        struct ast_ari_response *response)
@@ -1946,3 +1968,59 @@ void ast_ari_channels_dial(struct ast_variable *headers,
 
        ast_ari_response_no_content(response);
 }
+
+void ast_ari_channels_rtpstatistics(struct ast_variable *headers,
+               struct ast_ari_channels_rtpstatistics_args *args,
+               struct ast_ari_response *response)
+{
+       RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
+       RAII_VAR(struct ast_rtp_instance *, rtp, NULL, ao2_cleanup);
+       struct ast_json *j_res;
+       const struct ast_channel_tech *tech;
+       struct ast_rtp_glue *glue;
+
+       chan = ast_channel_get_by_name(args->channel_id);
+       if (!chan) {
+               ast_ari_response_error(response, 404, "Not Found",
+                       "Channel not found");
+               return;
+       }
+
+       ast_channel_lock(chan);
+       tech = ast_channel_tech(chan);
+       if (!tech) {
+               ast_channel_unlock(chan);
+               ast_ari_response_error(response, 404, "Not Found",
+                       "Channel's tech not found");
+               return;
+       }
+
+       glue = ast_rtp_instance_get_glue(tech->type);
+       if (!glue) {
+               ast_channel_unlock(chan);
+               ast_ari_response_error(response, 403, "Forbidden",
+                       "Unsupported channel type");
+               return;
+       }
+
+       glue->get_rtp_info(chan, &rtp);
+       if (!rtp) {
+               ast_channel_unlock(chan);
+               ast_ari_response_error(response, 404, "Not Found",
+                       "RTP info not found");
+               return;
+       }
+
+       j_res = ast_rtp_instance_get_stats_all_json(rtp);
+       if (!j_res) {
+               ast_channel_unlock(chan);
+               ast_ari_response_error(response, 404, "Not Found",
+                       "Statistics not found");
+               return;
+       }
+
+       ast_channel_unlock(chan);
+       ast_ari_response_ok(response, j_res);
+
+       return;
+}