Where to place keepalive logic?
#1
Hi,

I am writing an addon for a local TV broadcaster and they require a keepalive request every now and then, otherwise the manifest URL expires. Keepalive is only necessary if the player isn't actively playing content as long as chunks are requested, not even their web app sends keepalives.

So I want to send keepalive requests only if the user pauses the playback + if the playback gets stopped, I want to send a teardown request.

For that I read a couple of methods. I have seen some existing addons checking on the player in a loop right inside their playback initiating method. This has the benefit that I already have some context (media id and I can assume that the playback belongs to my addon).

But I have also seen solutions involving background services and threads. Not sure if I need that or not.

I have cooked up some example here: eqaboduxic (paste) It's very far from working, but it should give you an idea of what I am trying to do. Right now, the line where I put the "if kodi isn't playing any file" comment is necessary, since it can happen that kodi isnt playing any file anymore. Not sure why. And the pause handler isn't getting executed as the self.player.getPlayingFile().startswith("plugin://plugin.video.addonname/") never gets executed. Seemingly that contains the actual URL for the stream. It's a random CDN URL therefore I can't rely on the startswith method. Is there any other way to check if the playback is from my addon?

Also, the teardown call never gets executed now either. I am not really sure why.

I am not begging for spoonfeeding, but I am struggling to get it working. If you could at least direct me to the right way by providing an existing addon source that does something similar properly, I would already be grateful.

Thanks
Reply
#2
i think you are on the right track but you can amend the monitor to utilize onPlaybackPaused and onPlaybackResumed to get the call you are missing

the classes are here but i couldnt locate 'official' documentation for the classes so just a function name reference - https://codedocs.xyz/xbmc/xbmc/classXBMC...layer.html

i did, however, find those calls being used by this addon - https://github.com/hyperion-project/hype...service.py

my thought is to use the onpause and onresume to do your keepalive messages just need to find out what kodi is playing during those events


disclaimer: the addon was found using a search for the function i don't know what it's intended purpose is and i don't endorse it as an addon - just a reference
Reply
#3
Thanks for confirming and the quick reply!

I was thinking that monitor is needed to properly handle the close signal in case the user wants to exit kodi. And the whole threading logic is there in case if the previous keepalive call didn't finish but there is already a new playback. This other addon uses monitor as well. So I am not sure what you meant.

Thanks for the linked addon though, will study it tomorrow.
Reply
#4
(2023-07-07, 00:22)Mr Dini Wrote: I have cooked up some example here: eqaboduxic (paste) It's very far from working, but it should give you an idea of what I am trying to do. Right now, the line where I put the "if kodi isn't playing any file" comment is necessary, since it can happen that kodi isnt playing any file anymore. Not sure why. And the pause handler isn't getting executed as the self.player.getPlayingFile().startswith("plugin://plugin.video.addonname/") never gets executed. Seemingly that contains the actual URL for the stream. It's a random CDN URL therefore I can't rely on the startswith method. Is there any other way to check if the playback is from my addon?

Also, the teardown call never gets executed now either. I am not really sure why.

I am not begging for spoonfeeding, but I am struggling to get it working. If you could at least direct me to the right way by providing an existing addon source that does something similar properly, I would already be gratefu

Here's part of a service addon that I wrote and maintain in the Kodi repo.  it might help some.  it monitors playback and takes actions based upon certain events.  I suspect it could be modified for your needs or give you some ideas.  One thing to keep in mind is whether Kodi is playing the file you want or not.  If Kodi is dedicated to this then that isn't an issue but a service monitor class type addon will monitor all playback, including things you may not want.  There are ways to identify that with infotags and similar.  Here's a more advanced version where I do some of that checking.


Thanks,

Jeff
Running with the Mezzmo Kodi addon.  The easier way to share your media with multiple Kodi clients.
Service.autostop , CBC Sports, Kodi Selective Cleaner and Mezzmo Kodi addon author.
Reply
#5
I got it working, thanks a bunch!

Now there is only one remaining question. How do I obtain the time the playback stopped at? I assume kodi keeps it somewhere since it offers to continue from there when I try to play it later. player.getTime() would throw an exception I assume since there is no active playback anymore when onPlaybackStopped is called.
Reply
#6
(2023-07-08, 00:44)Mr Dini Wrote: I got it working, thanks a bunch!

Now there is only one remaining question. How do I obtain the time the playback stopped at? I assume kodi keeps it somewhere since it offers to continue from there when I try to play it later. player.getTime() would throw an exception I assume since there is no active playback anymore when onPlaybackStopped is called.

I'd just use my own counter.  Start it when playback starts, increment it as playback continues, pause it when playback is paused and grab the value when playback is stopped, do what you need to do and then reset the counter back to 0.  The plcount variable in the autostop addon does exactly this.  When plstop > plcount and the sleep timer is set, it stops playback.  In this case it appears you won't be pausing so the logic would be far more simple.  


Thanks,

Jeff
Running with the Mezzmo Kodi addon.  The easier way to share your media with multiple Kodi clients.
Service.autostop , CBC Sports, Kodi Selective Cleaner and Mezzmo Kodi addon author.
Reply
#7
good suggestion keeping your own play time counter @jbinkley60

i might take a different approach to it's implementation using time, store time.time() in a global variable then onPause or onStop check that time against time.time() again

-----

will get interesting if seek is involved though
Reply
#8
(2023-07-08, 03:12)jepsizofye Wrote: good suggestion keeping your own play time counter @jbinkley60

i might take a different approach to it's implementation using time, store time.time() in a global variable then onPause or onStop check that time against time.time() again

That will work too as long as accounting for paused playback isn't needed.  It is a simpler approach.  I have a habit of defaulting to "I know I already have some working code around here that already does what I want"  Smile


Jeff
Running with the Mezzmo Kodi addon.  The easier way to share your media with multiple Kodi clients.
Service.autostop , CBC Sports, Kodi Selective Cleaner and Mezzmo Kodi addon author.
Reply

Logout Mark Read Team Forum Stats Members Help
Where to place keepalive logic?0