res_ari_channels: Add ring operation, dtmf operation, hangup reasons, and tweak early...
[asterisk/asterisk.git] / res / res_stasis_playback.c
index 483aff8..f112e8b 100644 (file)
@@ -25,6 +25,7 @@
 
 /*** MODULEINFO
        <depend type="module">res_stasis</depend>
+       <depend type="module">res_stasis_recording</depend>
        <support_level>core</support_level>
  ***/
 
@@ -34,12 +35,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/app.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/bridge.h"
+#include "asterisk/bridge_internal.h"
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/module.h"
 #include "asterisk/paths.h"
 #include "asterisk/stasis_app_impl.h"
 #include "asterisk/stasis_app_playback.h"
+#include "asterisk/stasis_app_recording.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/stringfields.h"
 #include "asterisk/uuid.h"
@@ -73,12 +77,56 @@ struct stasis_app_playback {
        /*! Number of milliseconds to skip for forward/reverse operations */
        int skipms;
 
+       /*! Set when playback has been completed */
+       int done;
+       /*! Condition for waiting on done to be set */
+       ast_cond_t done_cond;
        /*! Number of milliseconds of media that has been played */
        long playedms;
        /*! Current playback state */
        enum stasis_app_playback_state state;
 };
 
+static void playback_dtor(void *obj)
+{
+       struct stasis_app_playback *playback = obj;
+
+       ast_string_field_free_memory(playback);
+       ast_cond_destroy(&playback->done_cond);
+}
+
+static struct stasis_app_playback *playback_create(
+       struct stasis_app_control *control)
+{
+       RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
+       char id[AST_UUID_STR_LEN];
+       int res;
+
+       if (!control) {
+               return NULL;
+       }
+
+       playback = ao2_alloc(sizeof(*playback), playback_dtor);
+       if (!playback || ast_string_field_init(playback, 128)) {
+               return NULL;
+       }
+
+       res = ast_cond_init(&playback->done_cond, NULL);
+       if (res != 0) {
+               ast_log(LOG_ERROR, "Error creating done condition: %s\n",
+                       strerror(errno));
+               return NULL;
+       }
+
+       ast_uuid_generate_str(id, sizeof(id));
+       ast_string_field_set(playback, id, id);
+
+       playback->control = control;
+
+       ao2_ref(playback, +1);
+       return playback;
+}
+
 static int playback_hash(const void *obj, int flags)
 {
        const struct stasis_app_playback *playback = obj;
@@ -144,12 +192,6 @@ static void playback_publish(struct stasis_app_playback *playback)
        stasis_app_control_publish(playback->control, message);
 }
 
-static void playback_cleanup(struct stasis_app_playback *playback)
-{
-       ao2_unlink_flags(playbacks, playback,
-               OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
-}
-
 static int playback_first_update(struct stasis_app_playback *playback,
        const char *uniqueid)
 {
@@ -191,11 +233,21 @@ static void playback_final_update(struct stasis_app_playback *playback,
        playback_publish(playback);
 }
 
-static void *play_uri(struct stasis_app_control *control,
-       struct ast_channel *chan, void *data)
+/*!
+ * \brief RAII_VAR function to mark a playback as done when leaving scope.
+ */
+static void mark_as_done(struct stasis_app_playback *playback)
 {
-       RAII_VAR(struct stasis_app_playback *, playback, NULL,
-               playback_cleanup);
+       SCOPED_AO2LOCK(lock, playback);
+       playback->done = 1;
+       ast_cond_broadcast(&playback->done_cond);
+}
+
+static void play_on_channel(struct stasis_app_playback *playback,
+       struct ast_channel *chan)
+{
+       RAII_VAR(struct stasis_app_playback *, mark_when_done, playback,
+               mark_as_done);
        RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
        RAII_VAR(char *, file, NULL, ast_free);
        int res;
@@ -210,7 +262,6 @@ static void *play_uri(struct stasis_app_control *control,
        const char *pause = NULL;
        const char *restart = NULL;
 
-       playback = data;
        ast_assert(playback != NULL);
 
        offsetms = playback->offsetms;
@@ -218,11 +269,11 @@ static void *play_uri(struct stasis_app_control *control,
        res = playback_first_update(playback, ast_channel_uniqueid(chan));
 
        if (res != 0) {
-               return NULL;
+               return;
        }
 
        if (ast_channel_state(chan) != AST_STATE_UP) {
-               ast_answer(chan);
+               ast_indicate(chan, AST_CONTROL_PROGRESS);
        }
 
        if (ast_begins_with(playback->media, SOUND_URI_SCHEME)) {
@@ -230,22 +281,23 @@ static void *play_uri(struct stasis_app_control *control,
                file = ast_strdup(playback->media + strlen(SOUND_URI_SCHEME));
        } else if (ast_begins_with(playback->media, RECORDING_URI_SCHEME)) {
                /* Play recording */
+               RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
+                       ao2_cleanup);
                const char *relname =
                        playback->media + strlen(RECORDING_URI_SCHEME);
-               if (relname[0] == '/') {
-                       file = ast_strdup(relname);
-               } else {
-                       ast_asprintf(&file, "%s/%s",
-                               ast_config_AST_RECORDING_DIR, relname);
+               recording = stasis_app_stored_recording_find_by_name(relname);
+               if (recording) {
+                       file = ast_strdup(stasis_app_stored_recording_get_file(
+                                       recording));
                }
        } else {
                /* Play URL */
                ast_log(LOG_ERROR, "Unimplemented\n");
-               return NULL;
+               return;
        }
 
        if (!file) {
-               return NULL;
+               return;
        }
 
        res = ast_control_streamfile_lang(chan, file, fwd, rev, stop, pause,
@@ -254,14 +306,87 @@ static void *play_uri(struct stasis_app_control *control,
        playback_final_update(playback, offsetms, res,
                ast_channel_uniqueid(chan));
 
-       return NULL;
+       return;
 }
 
-static void playback_dtor(void *obj)
+/*!
+ * \brief Special case code to play while a channel is in a bridge.
+ *
+ * \param bridge_channel The channel's bridge_channel.
+ * \param playback_id Id of the playback to start.
+ */
+static void play_on_channel_in_bridge(struct ast_bridge_channel *bridge_channel,
+       const char *playback_id)
 {
-       struct stasis_app_playback *playback = obj;
+       RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
 
-       ast_string_field_free_memory(playback);
+       playback = stasis_app_playback_find_by_id(playback_id);
+       if (!playback) {
+               ast_log(LOG_ERROR, "Couldn't find playback %s\n",
+                       playback_id);
+               return;
+       }
+
+       play_on_channel(playback, bridge_channel->chan);
+}
+
+/*!
+ * \brief \ref RAII_VAR function to remove a playback from the global list when
+ * leaving scope.
+ */
+static void remove_from_playbacks(struct stasis_app_playback *playback)
+{
+       ao2_unlink_flags(playbacks, playback,
+               OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
+}
+
+static void *play_uri(struct stasis_app_control *control,
+       struct ast_channel *chan, void *data)
+{
+       RAII_VAR(struct stasis_app_playback *, playback, NULL,
+               remove_from_playbacks);
+       struct ast_bridge *bridge;
+       int res;
+
+       playback = data;
+
+       if (!control) {
+               return NULL;
+       }
+
+       bridge = stasis_app_get_bridge(control);
+       if (bridge) {
+               struct ast_bridge_channel *bridge_chan;
+
+               /* Queue up playback on the bridge */
+               ast_bridge_lock(bridge);
+               bridge_chan = bridge_find_channel(bridge, chan);
+               if (bridge_chan) {
+                       ast_bridge_channel_queue_playfile(
+                               bridge_chan,
+                               play_on_channel_in_bridge,
+                               playback->id,
+                               NULL); /* moh_class */
+               }
+               ast_bridge_unlock(bridge);
+
+               /* Wait for playback to complete */
+               ao2_lock(playback);
+               while (!playback->done) {
+                       res = ast_cond_wait(&playback->done_cond,
+                               ao2_object_get_lockaddr(playback));
+                       if (res != 0) {
+                               ast_log(LOG_ERROR,
+                                       "Error waiting for playback to complete: %s\n",
+                                       strerror(errno));
+                       }
+               }
+               ao2_unlock(playback);
+       } else {
+               play_on_channel(playback, chan);
+       }
+
+       return NULL;
 }
 
 static void set_target_uri(
@@ -291,7 +416,6 @@ struct stasis_app_playback *stasis_app_control_play_uri(
        int skipms, long offsetms)
 {
        RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
-       char id[AST_UUID_STR_LEN];
 
        if (skipms < 0 || offsetms < 0) {
                return NULL;
@@ -300,21 +424,15 @@ struct stasis_app_playback *stasis_app_control_play_uri(
        ast_debug(3, "%s: Sending play(%s) command\n",
                stasis_app_control_get_channel_id(control), uri);
 
-       playback = ao2_alloc(sizeof(*playback), playback_dtor);
-       if (!playback || ast_string_field_init(playback, 128)) {
-               return NULL;
-       }
+       playback = playback_create(control);
 
        if (skipms == 0) {
                skipms = PLAYBACK_DEFAULT_SKIPMS;
        }
 
-       ast_uuid_generate_str(id, sizeof(id));
-       ast_string_field_set(playback, id, id);
        ast_string_field_set(playback, media, uri);
        ast_string_field_set(playback, language, language);
        set_target_uri(playback, target_type, target_id);
-       playback->control = control;
        playback->skipms = skipms;
        playback->offsetms = offsetms;
        ao2_link(playbacks, playback);
@@ -346,15 +464,7 @@ const char *stasis_app_playback_get_id(
 
 struct stasis_app_playback *stasis_app_playback_find_by_id(const char *id)
 {
-       RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
-
-       playback = ao2_find(playbacks, id, OBJ_KEY);
-       if (playback == NULL) {
-               return NULL;
-       }
-
-       ao2_ref(playback, +1);
-       return playback;
+       return ao2_find(playbacks, id, OBJ_KEY);
 }
 
 struct ast_json *stasis_app_playback_to_json(
@@ -520,4 +630,4 @@ static int unload_module(void)
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application playback support",
        .load = load_module,
        .unload = unload_module,
-       .nonoptreq = "res_stasis");
+       .nonoptreq = "res_stasis,res_stasis_recording");