ast_json_pack(): Use safer json ref mechanism.
[asterisk/asterisk.git] / main / sounds_index.c
index bc52639..c792c1b 100644 (file)
@@ -285,13 +285,15 @@ static void sounds_cleanup(void)
 static void format_update_cb(void *data, struct stasis_subscription *sub,
        struct stasis_message *message)
 {
-       ast_sounds_reindex();
+       /* Reindexing during shutdown is pointless. */
+       if (!ast_shutting_down()) {
+               ast_sounds_reindex();
+       }
 }
 
 int ast_sounds_index_init(void)
 {
        int res = 0;
-       sounds_index = NULL;
        if (ast_sounds_reindex()) {
                return -1;
        }
@@ -302,28 +304,31 @@ int ast_sounds_index_init(void)
                return -1;
        }
 
-       res |= stasis_message_router_add(
-               sounds_system_router,
-               ast_format_register_type(),
-               format_update_cb,
-               NULL);
+       if (ast_format_register_type()) {
+               res |= stasis_message_router_add(
+                       sounds_system_router,
+                       ast_format_register_type(),
+                       format_update_cb,
+                       NULL);
+       }
 
-       res |= stasis_message_router_add(
-               sounds_system_router,
-               ast_format_unregister_type(),
-               format_update_cb,
-               NULL);
+       if (ast_format_unregister_type()) {
+               res |= stasis_message_router_add(
+                       sounds_system_router,
+                       ast_format_unregister_type(),
+                       format_update_cb,
+                       NULL);
+       }
 
        if (res) {
                return -1;
        }
 
-       ast_register_atexit(sounds_cleanup);
+       ast_register_cleanup(sounds_cleanup);
        return 0;
 }
 
 struct ast_media_index *ast_sounds_get_index(void)
 {
-       ao2_ref(sounds_index, +1);
-       return sounds_index;
+       return ao2_bump(sounds_index);
 }