signalwire / freeswitch

FreeSWITCH is a Software Defined Telecom Stack enabling the digital transformation from proprietary telecom switches to a versatile software implementation that runs on any commodity hardware. From a Raspberry PI to a multi-core server, FreeSWITCH can unlock the telecommunications potential of any device.
https://freeswitch.com/#getting-started
Other
3.61k stars 1.42k forks source link

Get current playback position (without stopping playback) #1316

Open avimar opened 3 years ago

avimar commented 3 years ago

Is your feature request related to a problem? Please describe. I'm implementing a bookmark feature. dptools:playback supports providing a sample position @@<seek-offset>

But getting the current playback position is difficult: you have to actually stop the playback (e.g. api:uuid_fileman $uuid stop). That sets a channel variable and triggers an event PLAYBACK_STOP with playback_last_offset_pos which seems to be the information I need.

However, that stopped the playback. How to restart? I use bind_digit_action (which seems to only be a dialplan tool) to set several controls during playback - pause, rewind, fast forward, bookmark... and when I transfer back to my control extension to restart the playback from it's position, I've lost my place in the IVR. I don't see a way to re-enter an IVR in a specific position, if I could even keep track of it.

Describe the solution you'd like A new option, e.g. uuid_fileman $uuid position that would return the current offset (or trigger event or set channel variable)

Describe alternatives you've considered I see that I could implement this all inside javascript with streamFile and setting callbacks. But:

Would a bounty help this?

Package version or git hash FreeSWITCH Version 1.10.6-release-18-1ff9d0a60e~64bit (-release-18-1ff9d0a60e 64bit)

avimar commented 3 years ago

Also, when https://github.com/signalwire/freeswitch/pull/244 is merged we'll be able to easily adjust the playback speed without pitch change/chipmunk effect.

Stopping the playback to get the position would reset the current playback speed.

avimar commented 3 years ago

I'm seeing that there's multiple way to refer to location inside the file - either in milliseconds (ms) or how many samples.

Session:streamFile has an API to control the playback which is based on milliseconds, and it has to figure out how many samples to skip.

[uuid_fileman] also says to seek by seconds.

playback asks for a seek-offset.

In regular WAVs that's easily calculatable. But I'm usually playing back long MP3s This works in a predictable way in PCM files, less predictably with compressed files such as mp3.

So in order to use uuid_fileman or streamFile to bookmark and resume, I need an accurate duration. The duration in the events looked off, but I didn't extensively test. Perhaps they were the duration of actual playback so far, but if I did a seek, it didn't update to the correct position.

So getting the offset by event and using playback with seek-offset seems to currently be the only accurate way to save and recover the playback position for non-WAVs.

greenbea commented 3 years ago

I also have implemented a bookmark feature in the passed with lua, the only way was to stop playback to get the offset position and then restart the playback and pass the offset to session:streamFile. Getting the position while playing would be nice.

seven1240 commented 3 years ago

I'm not sure I understand what's you use case, you have to pause or stop the playing before you can get the accurate position right?

avimar commented 3 years ago

@seven1240 Correct - you have to stop the playback to retrieve the playback position via an event.

Besides for the inconvenience of multiple API calls, it's destructive: if I want to continue the playback afterwards I have to start a new playback and that removes my position in IVRs, etc.

greenbea commented 3 years ago

@avimar you don't need to listen to the events to get the position, once the recording playback stopped you can get it from the playback_last_offset_pos channel variable