MT#66375 play media: add `stop-on-ingress` flag

For the case when the media playback is only meant
to fill a gap between the start of playing media with
the `play_media()` and the time a real remote end-point
starts sending rtp stream.

For example, the ringbacktone is emulated towards the caller
(triggered by previously seen 183 with SDP), while the callee
not actually sending anything (no media coming). In case such
a peer stays silent, keep playing the file, blob or db-id given
file until the peer actually sends at least one rtp packet from
the previously advertised source.

(The same way for the `stop media`, media forwarding resumes).

Important, the flag implies `block-egress`, so the other
parties media stays blocked while the playback is ongoing.

The check runs in the packet path (with the call locked on R),
so it only marks the player and schedules its timer object immediately.

The stop itself then runs from the media player thread
(but this time with the call locked on W).

Unfortunately, those blocked destinations are excluded from kernel forwarding,
because we need to let the daemon see the packets coming. But that's not a big
issue, because as soon as the real end-point exchange comes (final 200OK with SDP),
kernel space processing will be used again.

As the NG caller still sends its own `stop media` afterwards,
a second shutdown of the player becomes common.
Don't flush the send queue then, because without a coder handler, the flush would match
and drop packets that don't belong to the player.

Change-Id: I41c02c1278f380ccd46dd1254f140e306afd4c67
pull/2221/head
Donat Zenichev 4 weeks ago
parent 5cb0130636
commit 897cf7420c

@ -973,6 +973,10 @@ const char *call_ng_flags_flags(str *s, unsigned int idx, helper_arg arg) {
case CSH_LOOKUP("static codecs"):
out->static_codecs = true;
break;
case CSH_LOOKUP("stop-on-ingress"):
case CSH_LOOKUP("stop on ingress"):
out->stop_on_ingress = true;
break;
case CSH_LOOKUP("strict-source"):
case CSH_LOOKUP("strict source"):
out->strict_source = true;

@ -2002,7 +2002,9 @@ const char *call_play_media_ng(ng_command_ctx_t *ctx) {
.repeat = flags.repeat_times,
.duration_spent = flags.repeat_duration,
.start_pos = flags.start_pos,
.block_egress = !!flags.block_egress,
/* other parties media must stay blocked until it takes over */
.block_egress = flags.block_egress || flags.stop_on_ingress,
.stop_on_ingress = !!flags.stop_on_ingress,
.codec_set = flags.codec_set,
.file = flags.file,
.blob = flags.blob,

@ -172,8 +172,11 @@ static void media_player_shutdown(struct media_player *mp) {
//ilog(LOG_DEBUG, "shutting down media_player");
timerthread_obj_deschedule(&mp->tt_obj);
mp->next_run = 0;
mp->stop_on_ingress_armed = false;
if (mp->sink.sink) {
/* no handler means already shutdown (e.g. `stop media` after a `stop-on-ingress` stop),
* and flushing with a NULL source would remove packets not belonging to this player */
if (mp->sink.sink && mp->coder.handler) {
unsigned int num = send_timer_flush(mp->sink.sink->send_timer, mp->coder.handler);
ilog(LOG_DEBUG, "%u packets removed from send queue", num);
// roll back seq numbers already used
@ -217,6 +220,24 @@ long long media_player_stop(struct media_player *mp) {
}
/**
* called with call->master_lock held in R, for each RTP packet forwarded to `m`
*/
void media_player_check_stop_on_ingress(struct call_media *m) {
#ifdef WITH_TRANSCODING
struct media_player *mp = m->players[MP_DEFAULT];
if (!mp || !mp->stop_on_ingress_armed)
return;
/* only the first packet schedules the stop */
if (atomic_exchange_na(&mp->ingress_seen, true))
return;
/* the stop needs the call locked in W, let the player's timer thread do it */
ilog(LOG_DEBUG, "Media received from other party, scheduling stop of media playback");
timerthread_obj_schedule_abs(&mp->tt_obj, rtpe_now);
#endif
}
#ifdef WITH_TRANSCODING
static void __media_player_free(struct media_player *mp) {
media_player_shutdown(mp);
@ -1537,6 +1558,9 @@ static void __media_player_set_opts(struct media_player *mp, const media_player_
if (mp->media && mp->opts.block_egress)
MEDIA_SET(mp->media, BLOCK_EGRESS);
mp->stop_on_ingress_armed = mp->opts.stop_on_ingress;
atomic_set_na(&mp->ingress_seen, false);
}
@ -2120,6 +2144,32 @@ static mp_cached_code __media_player_add_db(struct media_player *mp,
return __media_player_add_blob_id(mp, opts, dst_pt);
}
/**
* called with nothing locked, after `media_player_check_stop_on_ingress()` has seen media
*/
static void media_player_stop_on_ingress(struct media_player *mp) {
call_t *call = mp->call;
rwlock_lock_w(&call->master_lock);
/* re-check under lock, the playback may have been stopped or restarted in the meanwhile */
if (mp->stop_on_ingress_armed && atomic_get_na(&mp->ingress_seen)) {
mp->stop_on_ingress_armed = false;
if (mp->media && mp->media->players[MP_DEFAULT] == mp) {
ilog(LOG_INFO, "Media received from other party, stopping media playback");
call_stop_media(mp->media, MP_DEFAULT);
}
}
/* unless stopped, `media_player_run()` skipped `run_func`, which is what reschedules
* the player: do it here (a no-op if a restart has rescheduled it already) */
if (mp->next_run)
timerthread_obj_schedule_abs(&mp->tt_obj, mp->next_run);
rwlock_unlock_w(&call->master_lock);
}
static void media_player_run(void *ptr) {
struct media_player *mp = ptr;
call_t *call = mp->call;
@ -2132,16 +2182,21 @@ static void media_player_run(void *ptr) {
mutex_lock(&mp->lock);
bool finished = false;
if (mp->next_run)
/* stopping needs the call locked in W, done below */
bool stop = mp->stop_on_ingress_armed && atomic_get_na(&mp->ingress_seen);
if (!stop && mp->next_run)
finished = mp->run_func(mp);
mutex_unlock(&mp->lock);
rwlock_unlock_r(&call->master_lock);
if (finished) {
if (stop)
media_player_stop_on_ingress(mp);
else if (finished) {
rwlock_lock_w(&call->master_lock);
mp->next_run = 0;
mp->stop_on_ingress_armed = false;
if (mp->opts.block_egress)
MEDIA_CLEAR(mp->media, BLOCK_EGRESS);

@ -3722,6 +3722,10 @@ static int stream_packet(struct packet_handler_ctx *phc) {
goto err_next;
}
else {
/* the real media from another party may end a `stop-on-ingress` playback */
if (phc->mp.rtp && phc->mp.media_out->monologue != phc->mp.media->monologue)
media_player_check_stop_on_ingress(phc->mp.media_out);
struct codec_handler *transcoder = codec_handler_get(phc->mp.media, phc->payload_type,
phc->mp.media_out, sh);
// this transfers the packet from 's' to 'packets_out'

@ -279,6 +279,7 @@ RTPE_NG_FLAGS_STR_CASE_HT_PARAMS
detect_dtmf:1,
block_dtmf:1,
block_egress:1,
stop_on_ingress:1,
t38_decode:1,
t38_force:1,
t38_stop:1,

@ -25,7 +25,8 @@ typedef struct {
long long duration_spent; /* in milliseconds */
str_case_value_ht codec_set;
unsigned int block_egress:1,
moh:1;
moh:1,
stop_on_ingress:1;
str file, blob;
unsigned long long db_id;
} media_player_opts_t;
@ -73,6 +74,10 @@ struct media_player {
int64_t next_run;
media_player_opts_t opts;
/* `stop-on-ingress` playback waiting for media, only written with call->master_lock in W */
bool stop_on_ingress_armed;
/* media seen while armed, set from the packet path, accessed atomically */
bool ingress_seen;
struct media_player_coder coder;
struct media_player_content_index cache_index;
@ -128,6 +133,7 @@ bool media_player_play(struct media_player *mp, media_player_opts_t *opts);
bool media_player_start(struct media_player *);
long long media_player_stop(struct media_player *);
bool media_player_is_active(struct call_media *);
void media_player_check_stop_on_ingress(struct call_media *);
int media_player_setup(struct media_player *mp, const rtp_payload_type *src_pt,
const rtp_payload_type *dst_pt, str_case_value_ht codec_set);

@ -54,6 +54,7 @@ my @flags = qw(
block-short
recording-vsc
block-egress
stop-on-ingress
directional
allow-no-codec-media
bidirectional

Loading…
Cancel
Save