Forum
[IDEA] Pause download package when video is playing. - Printable Version

+- Forum (http://forum.xbian.org)
+-- Forum: Community (/forum-5.html)
+--- Forum: Support the project (/forum-11.html)
+---- Forum: Contribute (/forum-34.html)
+---- Thread: [IDEA] Pause download package when video is playing. (/thread-374.html)

Pages: 1 2 3


Pause download package when video is playing. - Trinket - 31st Jan, 2013 10:46 PM

Hi! I'm new to the forum altough i have been reading it for quite a while.
I have been working the last days in a script that detects when XBMC starts playing stuff and uses the signal SIGSTOP to pause all proceses related to downloads and then, when the video stops playing, it uses the signal SIGCONT to resume them.
I have created the following folder ~/.xbmc/addons/script.service.procmanager where I store my script with the addon.xml file, the script is as follows:
http://pastebin.com/VmkqecST

I had to call the startall.py and stopall.py with sudo rights because the downloads are managed by other users and xbian user dosen't have the rights to manage them (I added those files with visudo so that i don't need a password to execute them with sudo).
The startall.py and stopall.py are as follows:
http://pastebin.com/aSJnS0Tx

What I did to get the PID of each process related to downloads, is to add each user that started that kind of process to a common group "download" so, for example, sabnzbd is executed by user sabnzbd which is a member of the group download, same for transmission, couchpotato and sickbeard, they all belong to the group download. I just get the process list of that group of users and then i send a sigstop to each process.

The problem with this script is that i can't manage to autostart it within xbmc, my addon.xml looks as follows
http://pastebin.com/H5Tp2hRm
I'm new to python so I'm not sure if what I've done is right, the default.py script is a copy paste of things I found around internet.
According to what I've read, the addon.xml will tell XBMC which script to start and when, but it dosen't seem to work. I found information regarding a workaround involving autoexec.py file, which I couldn't find.

I would really appreciate some help here as I don't know how to continue or how to debug this process.


RE: Pause download package when video is playing. - belese - 1st Feb, 2013 06:31 PM

<extension point="xbmc.service" library="default.py" start="startup|login">

this line is not good, you have to choice either startup or login, in this case, you can choose startup

<extension point="xbmc.service" library="default.py" start="startup">

don't look at the rest.
and your pyhton file must be name default.py or change it in addon.xml to correspond to your file name.
but default.py is a quite standard in xbmc.


RE: Pause download package when video is playing. - Trinket - 2nd Feb, 2013 01:12 AM

I have done what you told me but it dosen't seem to get executed, I checked the XBMC log file and couldn't find anything.
The script name is default.py.
Any other ideas?


RE: Pause download package when video is playing. - belese - 2nd Feb, 2013 02:07 AM

Your code look strange :

PHP Code:
while(1): #Do this in order to know when video isn't playing
        
if xbmc.Player().isPlayingVideo():
                
VIDEO 1
 
        
else:
                
VIDEO 

will never go out, and put a sleep somewhere,
something like :

PHP Code:
while not xbmc.abortRequested():
     
time.sleep(1


and why call python function with os.system,
it' python in python so call the python function directly, and import your files.

and do a print somewhere at the beginning to see if service is started, it'll appear in xbmc.log

and instead of use ps, you can take a look here :
https://github.com/xbianonpi/xbian-config-bash/wiki/Services
but you have to update xbian config if you are on alpha4

and a python wrapper for xbian-config is done here :
https://github.com/xbianonpi/xbian-config-python/blob/master/resources/lib/xbianconfig.py

Read a bit more you code,
but i think you can't override player like you do to do this,
if you override player like this, it will react only if you call your player from your script
but maybe i'm wrong, need to be tested.

eidt : no, sure i'm wrong, it should work also.

here's a basic code (not tested at all)
PHP Code:
import threading,time

waitvideostop 
threading.Event()

def startDL() :
   
#do some stuff here

def stopDL() :
   
#do some other stuff here
   
waitvideostop.set()


while 
not xbmc.abortRequested():
     if 
xbmc.isPlayingVideo():
           
myplayer xbmc.Player()
           
myplayer.onPlayBackStarted stopDl
           myplayer
.onPlayBackEnded startDl
           waitvideostop
.clear()
           
waitvideostop.wait()
    else :
          
time.sleep(1
and onPlayBackEnded is called when you do pause (in Eden, don't test in frodo), so perhaps you have to check is player is not in pause before restarting all dl


RE: Pause download package when video is playing. - Trinket - 2nd Feb, 2013 11:47 AM

Thank you very much for your help!
I will make the script print something in order to see it in the log, regarding the while loop, I didn't find any other way of telling the "onPlayBackEnded" function when XBMC wasn't playing video, I thought it would work that way, if video is playing funcion "onPlayBackStarted" is called and the While loop keeps VIDEO = 1 until video stops playing, then the whole script get's executed again.
I had to call that python function with os.system because I have to execute it with sudo, and i don't know how i can do that without executing XBMC as root. In order to execute the python function that closes services with sudo, I configure it with Visudo so i can avoid password input.
I'm going to see if it is actually getting executed.
EDIT: It dosn't seem to get executed at startup. Here is the log file:
http://pastebin.com/TdwtfYPD
Near line 100 scripts get executed.
I have added the following lines to the default.py:
http://pastebin.com/VmkqecST
I'm unsure if thats the right way of doing it, I found the xbmc.log method here:
http://mirrors.xbmc.org/docs/python-docs/xbmc.html#-log


RE: Pause download package when video is playing. - Trinket - 3rd Feb, 2013 10:40 AM

Just an update.
I have followed what you told me and i realised i was placing the script in the wrong place!
I had it in /home/xbian/.xbmc/addons/ I supposed that i had to store it there because XBMC logs are stored within the same directory, but i still don't know why the correct place is /usr/local/share/xbmc/addons/
Now i have managed to pause downloads when playback starts, but it seems that "onPlayBackEnded" isn't getting called when playback stops, I have yet to fix that issue, will try it tomorrow!
Here is my script, I replaced the while loop as you told, I now undrestand what you told me! Thank you!
Code:
import xbmc
import subprocess,os
class MyPlayer(xbmc.Player) :
        print("[CUSTOM SCRIPT]Checking PlayBackState")
        def __init__ (self):
                xbmc.Player.__init__(self)

        def onPlayBackStarted(self):
                if xbmc.Player().isPlayingVideo():
                        print("[CUSTOM SCRIPT]Video is playing")
                        os.system("sudo python /usr/local/share/xbmc/addons/service.procmanager/resources/stopall.py") #Calls another script to stop proce$

        def onPlayBackEnded(self): #Dosen't seem to get called
                        print("[CUSTOM SCRIPT]Video isn't playing")
                        os.system("sudo python /usr/local/share/xbmc/addons/service.procmanager/resources/startall.py") #Calls another script to start pro$


print("[CUSTOM SCRIPT]**Working**")

player=MyPlayer()

while not xbmc.abortRequested(): #End if XBMC closes
        xbmc.sleep(3000) #Repeat (ms)



RE: Pause download package when video is playing. - belese - 3rd Feb, 2013 07:44 PM

you should place your script here :
/home/xbian/.xbmc/addons/
and it should work with this.
i've done some services, and they runs from there

the path /usr/local/share/xbmc/addons/
is normally reserved for default xbmc addon and shouldn't be modified (will be erased at each xbmc update).

otherwise, it was in my plan to integrate it with xbian-config-python,
but not on the first version.
my first idea was to start service when xbmc screesaver is on, and stop when screensaver is off.


RE: Pause download package when video is playing. - BartOtten - 4th Feb, 2013 04:31 AM

The screensaver edition is better I think: Sometimes I stop a movie for a short while. With the playbackEnd all services should 'start to stop' as soon as I resume and that would probably cause some slowness....

+2 for the idea and the work you've been putting into it. Those things make XBian/XBMC better Big Grin


RE: Pause download package when video is playing. - Trinket - 4th Feb, 2013 04:40 AM

Thank you very much! Smile
Great idea, you are right, making it stop|start downloads according to the screensaver is the best solution!
I don't know why, but I'm sure that my script is only activated when I put it in /usr/local/share/xbmc.. when I moved the script folder there it started to work. Will check it later when I arrive home.


RE: Pause download package when video is playing. - belese - 4th Feb, 2013 06:31 AM

i've done a mistake,
this is :
xbmc.abortRequested
and not
xbmc.abortRequested()


RE: Pause download package when video is playing. - Trinket - 4th Feb, 2013 09:19 AM

Thank you! I have it fully working as I wanted it to.
I implemented the scripts to be called upon screensaver start|stop and when XBMC gets closed. It seems to be working flawlessly.
It seemed I had a mistake in the addon.xml file which I corrected when moving it to /usr/home/share, when I moved it back to /home/xbian as you told it kept working.
The final script is:
http://pastebin.com/2mCLmAeB
PHP Code:
import xbmc
import subprocess
,os
class Screensaver(xbmc.Monitor) :
        print(
"[CUSTOM SCRIPT]Checking PlayBackState")
        
def __init__ (self):
                
xbmc.Monitor.__init__(self)

        
def onScreensaverDeactivated(self):
                print(
"[CUSTOM SCRIPT]XBMC in use")
                
os.system("sudo python /home/xbian/.xbmc/addons/service.procmanager/resources/stopall.py"#Calls another script to stop proc

        
def onScreensaverActivated(self):
                print(
"[CUSTOM SCRIPT]XBMC in Standby")
                
os.system("sudo python /home/xbian/.xbmc/addons/service.procmanager/resources/startall.py"#Calls another script to start proc

        
def onAbortRequested(self):
                print(
"[CUSTOM SCRIPT]XBMC is closing")
                
os.system("sudo python /home/xbian/.xbmc/addons/service.procmanager/resources/startall.py"#Makes sure processes are running if xbmc gets closed

print("[CUSTOM SCRIPT]**Working**")

monitor=Screensaver()

while 
not xbmc.abortRequested#End if XBMC closes
        
xbmc.sleep(5000#Repeat (ms) 



RE: Pause download package when video is playing. - belese - 4th Feb, 2013 09:46 PM

great,

but
Code:
def __init__ (self):
       xbmc.Monitor.__init__(self)
is not necessary because you do anything in init, so the init will be called by default.

and you can put more than 5000 in xbmc.sleep because it break if xbmc is aborted


RE: Pause download package when video is playing. - BartOtten - 5th Feb, 2013 11:28 AM

(4th Feb, 2013 09:46 PM)belese Wrote:  and you can put more than 5000 in xbmc.sleep because it break if xbmc is aborted

Saw my script being killed because it was longer than 5 seconds 'inactive'. Or is that only with boot-scripts/services?


RE: Pause download package when video is playing. - belese - 5th Feb, 2013 09:24 PM

Quote:Saw my script being killed because it was longer than 5 seconds 'inactive'. Or is that only with boot-scripts/services?

Don't understand what you mean.
Script is ended when it reached end of the script.
Thread stay alive after, but in this case (no thread), you have to do a sleep at the end otherwise monitoring will stop when reached the end.

What i want to tell, is that you can put more than 5 sec in xbmc.sleep,
if you put 1 minute, and xbmc stop is requested, it will go out from xbmc.sleep, even if the minutes was not achieved. not really a big problem in this case to put 3 sec, but if you put 1 minutes or more, there are less loop.


RE: Pause download package when video is playing. - Trinket - 6th Feb, 2013 05:48 AM

Sorry for not posting earlier, I had a problem with the Hard drive used by the Raspberry (I'm still fixing it).
Regarding the script, I set it to 1minute (60000) and deleted the init function and it worked as you told!
Thank you very much for your help