diff --git a/daemon/media_player.c b/daemon/media_player.c index 79475a9fc..3076838bc 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -1330,6 +1330,27 @@ static struct media_player_media_file *media_player_media_files_get_create(const return fo; } +static struct media_player_media_file *media_player_db_id_get_create(unsigned long long id) { + __auto_type fo = media_player_db_id_get_only(id); + if (fo) + return fo; + + fo = media_player_db_id_read(id); + if (!fo) + return NULL; + + RWLOCK_W(&media_player_db_media_ids_lock); + LOCK(&media_player_db_media_lock); + // someone else may have beaten us to it + if (t_hash_table_is_set(media_player_db_media) && t_hash_table_lookup(media_player_db_media, GUINT_TO_POINTER(id))) + return fo; // return the only reference, will disappear once player finishes + + // insert new reference + media_player_db_id_insert(id, obj_get(fo)); + + return fo; +} + static struct media_player_media_file *(*media_player_media_files_get)(const str *fn) = media_player_media_files_get_only; @@ -2039,6 +2060,13 @@ bool media_player_preload_db(char **ids) { for (char **idp = ids; *idp; idp++) { char *id_s = *idp; + while (*id_s == ' ') + id_s++; + + if (!strcmp(id_s, "ondemand") || !strcmp(id_s, "on demand") || !strcmp(id_s, "on-demand")) { + media_player_db_id_get = media_player_db_id_get_create; + continue; + } char *endp = NULL; unsigned long long id = strtoull(id_s, &endp, 0); diff --git a/docs/rtpengine.md b/docs/rtpengine.md index 271ece8d3..93157b1c1 100644 --- a/docs/rtpengine.md +++ b/docs/rtpengine.md @@ -1176,7 +1176,9 @@ call to inject-DTMF won't be sent to __\-\-dtmf-log-dest=__ or __\-\-listen-tcp- Similar to the __preload-media-files__ option, but preloads media from database instead of reading them from files. Each entry must be an integer - corresponding to an index from the database. + corresponding to an index from the database. On-demand loading is also + supported by supplying the special string `on-demand` instead of an index + number. - __\-\-db-media-reload=__*SECONDS* diff --git a/etc/rtpengine.conf b/etc/rtpengine.conf index 9577e3e48..c73ed4589 100644 --- a/etc/rtpengine.conf +++ b/etc/rtpengine.conf @@ -167,7 +167,7 @@ recording-method = proc # preload-media-files = /var/media/file1.wav ; /var/media/file2.wav ; /var/media/file3.wav ; on-demand # media-files-reload = 60 -# preload-db-media = 1; 2; 3; 4 +# preload-db-media = 1; 2; 3; 4; on-demand # db-media-reload = 3600 # signalling templates (see key `templates` above)