Page 2 of 4

Re: Prevent hibernate/standby/sleep

PostPosted: Sun May 06, 2012 11:47 pm
by Tigerix
After just watching a movie and being stopped in the middle by standby a few times, I am really frustrated.
So I wanted to give feedback and then realize with searching that its already been discussed here. :-)

Really glad, as I am really hoping for a solution and fix.

Either ways - great piece of software Serviio!!
Thanks and keep up the good work.

Tigerix

Re: Prevent hibernate/standby/sleep

PostPosted: Mon Jun 04, 2012 1:00 pm
by TyrionBot
I think Serviio is a great piece of software. I'm amazed at how well it works, but I also would like this feature added. So, I did some searching to find out how this feature is supposed to be coded.

I found that the winapi call to tell the computer that a program requires the computer to not go to sleep/hibernate is explained here, http://msdn.microsoft.com/en-us/library ... 85%29.aspx.
As far as coding this in Java, ps3mediaserver already supports this feature and here is how they coded it, https://ps3mediaserver.googlecode.com/s ... Utils.java.

Overall, this seems like a pretty simple feature to add. At minimum, it seems like it would require, checking that you are running in windows, calling a function like ps3mediaserver's disableGoToSleep when streaming video or doing any other operation that should be considered active, and calling a function like ps3mediaserver's reenableGoToSleep when the activity is done.

You could also code a setting that allows the user to either have serviio disable the sleep function altogether or only disable sleep/hibernate while streaming content.

Also, as far as OSX, here is a example, but it seems like a kludge instead of the proper way, http://stackoverflow.com/a/53276

Re: Prevent hibernate/standby/sleep

PostPosted: Mon Jun 04, 2012 2:12 pm
by Tigerix
Good news!

I have found an universal solution, which is doing exactly what I was looking for since a long time.
Made me so happy, that I really want to share it with you now. :-)

Whenever downloading / uploading with any Software - you usually don't want it to be interrupted by Standby or Hibernate.
And this is exactly what Coffee (certainly perfect suiting Name) is doing.

You can claim at which download / upload speeds you don't want your machine to become idle.
This means you can keep your Standby/Hibernate settings just as high/low as you want them to be,
with them being ignored as long as your traffic is higher than the thresholds.

Have nice day. :-)
Tigerix


Image

Re: Prevent hibernate/standby/sleep

PostPosted: Tue Jun 05, 2012 9:05 am
by zip
great, thanks

Re: Prevent hibernate/standby/sleep

PostPosted: Wed Jul 25, 2012 2:57 pm
by coalado
If you still want to implement this:
http://pastebin.com/7VtGNUUe

Re: Prevent hibernate/standby/sleep

PostPosted: Thu Aug 02, 2012 6:20 am
by hans
Is this implemented in version 1.0?

Re: Prevent hibernate/standby/sleep

PostPosted: Thu Aug 02, 2012 12:54 pm
by zip
hans wrote:Is this implemented in version 1.0?

no

Re: Prevent hibernate/standby/sleep

PostPosted: Sat Feb 02, 2013 3:16 pm
by Satjit
Wow! I've been looking for this as well. Thanks a ton :D

Re: Prevent hibernate/standby/sleep

PostPosted: Mon Mar 11, 2013 5:47 am
by Darkfalz
Coffee works well but once the system standby period expires and Coffee is keeping it awake, if you pause for longer than a minute your PC will go into standby. I've made a feature request to the Coffee author to allow a configurable delay after network activity stops before sleeping. Would obviously be superb if Serviio had this natively, but all good things to those who wait :)

Re: Prevent hibernate/standby/sleep

PostPosted: Fri May 31, 2013 10:10 am
by J.M.
Hi All,

here is simple workaround for Windows :

1) search/run "Power Options" setting and create 2 power plans. For example one for sleep and one for never sleep etc.

2) Run "powercfg -list" in power shell to idendify GUID of your power plans. You need this in the script in step 3.
  Code:
PS C:\Users\user\Desktop> powercfg -list

Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver) *


3) create simple power shell script for example sleep.ps1. This script checks if there is ESTABLISTED connedtion to any IP 192.168.0.1??. You can change the condition if you know your TV ip is static or you have more TV and want to keep connection only to some of them or whatever reason you have. Command powercfg -s [Power Scheme GUID] sets active the choosen powerplan. So if connetion to TV is found than Power plan never sleep is used, when there is no connection than Power plan sleep is used (check every 5 mins from task scheduler). In my case script looks like this ->

  Code:
$str = netstat -a -n | select-string -pattern "ESTABLISHED" | select-string -pattern  "192.168.0.1\d{2}"
if ($str) {
   "$str"
   "String is not empty"
    POWERCFG -S 381b4222-f694-41f0-9685-ff5bb260df2e
} else {
   "$str"
   "String is null or empty"
    POWERCFG -S a1841308-3541-4fab-bc81-f71556f20b4a
}


4) search/run "Task Scheduler" and create new task triggered every 5 minutes which executes the script above.

5) thats all. If you have problem with running power shell scripts than google it or start it as I start it -> powershell.exe -noprofile -executionpolicy bypass -file C:\xxxxx\sleep.ps1

6) I forget about serviio service needs to be restarted after wake up from sleep. For this create new schedule in Task Scheduler triggered by event : Log=System; Source=Power-Troubleshooter; Event ID=1. This schedule is started every time your pc wakes up and it should execute simple script :
  Code:
net stop serviio
net start serviio


7) thats probably all you need to watch TV uninterupted and save some trees :-)

P.S. I think you can apply same logic of the workaround for Linux.

Re: Prevent hibernate/standby/sleep

PostPosted: Sat Jun 01, 2013 3:50 am
by blopa
J.M.: worked like a charm!
I've been looking for something like this since a few months ago
however, I had to remove the "\d{2}" or it won't work. what is that for? sorry I'm totally new to scripts :)

A big thanks for your contribution!!!

Pablo

Re: Prevent hibernate/standby/sleep

PostPosted: Sat Jun 01, 2013 8:40 am
by J.M.
Hi blopa,

\d{2} should be 2 decimal number. Because my TV receives ip from DHCP usually from 192.168.0.100 to 192.168.0.103 - depending on which network device starts fist and gets the lower ip from router. If 192.168.0.1 is working for you than your TV ip is probably 192.168.0.1 or there are no more ESTABLISHED connections to any other devices. You can test in power shell command
  Code:
netstat -a -n | select-string -pattern "ESTABLISHED"
before and after you play movie from serviio and see difference - new established connections are from your TV -> you have to change the if condition to filter only this connections.

Re: Prevent hibernate/standby/sleep

PostPosted: Sat Jun 01, 2013 10:16 am
by DenyAll
Hi J.M.

Firstly, thanks for this. This is a great approach to the issue.
Secondly, I now have to blame you for a sleepless night last night ;). I spent most of last night using your method to try a slightly different approach. To be fair, most of my time was spent getting the task scheduler to work (finicky thing that is).

I have made some mods to your script - I have never programmed with Powershell before so if you see any potential enhancements (eg. not sure how to define constants) please let me know. What I have done is more complex than what you did, but it removes the requirement for steps 1 and 2 in your post. It also removes the (very slim) risk of your PC being left in the wrong power scheme state (eg. if someone turns the PC off before the scheme is reset).

My code for step 3 above uses a bit of C programming embedded in a powershell script:
  Code:
$code=@'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
  static extern void SetThreadExecutionState(uint esFlags);

public static void KeepSystemAwake(uint esFlag)
    {
        SetThreadExecutionState(esFlag);
    }
'@

$keepAwake=Add-Type -memberDefinition $code -name "SystemSleep" -namespace SleepFunctions -passThru
$ES_CONTINUOUS = [uint32]"0x80000000"
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040"
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002"
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"

$str = netstat -a -n | select-string -pattern "ESTABLISHED" | select-string -pattern  "192.168.0.9\d{1}"
if ($str) {
   "$str"
   "String is not empty"
    $keepAwake::KeepSystemAwake($ES_CONTINUOUS -and $ES_AWAYMODE_REQUIRED)
} else {
   "String is null or empty"
    $keepAwake::KeepSystemAwake($ES_CONTINUOUS)
}
Instead of setting different power schemes as you have done, I have used the WIN API function SetThreadExecutionState (see MSDN page here for details) to tell your PC to enter an Away Mode state while a connection exists. The Away Mode state is a special state recommended for media streaming - your PC goes into a partial sleep mode (eg. screen is shut off) but full processor etc is available.

I have tested this and all works well so far. Any feedback most welcome.

Windows XP and Windows Server 2003 users: the above won't work as-is, as these OS's do not support Away Mode. You need to change the line $keepAwake::KeepSystemAwake($ES_CONTINUOUS -and $ES_AWAYMODE_REQUIRED) to $keepAwake::KeepSystemAwake($ES_SYSTEM_REQUIRED) in the above. This will simply stop the PC entering sleep (rather than entering Away Mode).

ps. @blopa: my code uses "192.168.0.9\d{1}". To illustrate J.M.'s answer to your post, my code matches any IP's in the range 192.168.0.90 to 192.168.0.99. This is where I have statically assigned all of my renderers (eg. my PS3 is on .90 and my Panasonic is on .95)

Re: Prevent hibernate/standby/sleep

PostPosted: Sat Jun 01, 2013 3:46 pm
by J.M.
Hi DenyAll,

sorry for sleepless night :-). I am also not familiar to power shell, the few commands I used I had to google. I was first also thinking about using SetThreadExecutionState but because I am not programmer I gave up :-).

P.S. If PC being left in the wrong power scheme state is not big problem because task is scheduled every 5 mins so it will be corrected in 5mins.

Re: Prevent hibernate/standby/sleep

PostPosted: Sat Jun 01, 2013 11:54 pm
by DenyAll
....is not big problem because task is scheduled every 5 mins so it will be corrected in 5mins.
Of course it will, sorry didn't think that thru.
I am still testing all combinations (eg. pausing, multiple renderers playing) and so far all good. I have also added a for loop so that you can simply list all the ip addresses for the renderers you want to monitor. Will post that later today. If you are using this, let me know of any issues.

Re: Prevent hibernate/standby/sleep

PostPosted: Sun Jun 02, 2013 2:12 am
by blopa
J.M. and DenyAll, thanks for illustrating me about the option to add decimals to the IP number. My TV uses a static IP so I'm clear now that I don't need to add it.

zip, you probably want to keep an eye on this post, something good is going on here :). Maybe a link to the FAQs until a native solution can be developed?

Serviio rocks and its community too!

Re: Prevent hibernate/standby/sleep

PostPosted: Sun Jun 02, 2013 7:01 am
by DenyAll
Hi again. In my last post I mentioned using a loop for all renderer IP addresses. While that was working fine, I have had a better thought... I changed the loop to work for TCP Ports.

This new code below searches the NETSTAT command output based on established sessions to TCP port's 8895 and 23424 (for streaming and Mediabrowser respectively). This has the advantage that it doesn't matter if IP addresses change due to dynamic allocation, or if you add a new renderer device. Its all catered for without changing the code.

  Code:
$code=@'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
  static extern void SetThreadExecutionState(uint esFlags);

public static void KeepSystemAwake(uint esFlag)
    {
        SetThreadExecutionState(esFlag);
    }
'@

$keepAwake=Add-Type -memberDefinition $code -name "SystemSleep" -namespace SleepFunctions -passThru
$ES_CONTINUOUS = [uint32]"0x80000000"
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040"
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002"
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"

$str=""
$Ports = @("8895", "23424")  <# Monitored Serviio Ports #>

$NetStat = netstat -a -n | select-string -pattern "ESTABLISHED"
foreach ($element in $Ports) {
    $str = $str + ($NetStat | select-string -pattern (":" + $element))
}

if ($str) {
   "$str"
    $keepAwake::KeepSystemAwake($ES_CONTINUOUS -and $ES_AWAYMODE_REQUIRED)
} else {
   "String is null or empty"
    $keepAwake::KeepSystemAwake($ES_CONTINUOUS)
}
I have tested with three renders going simultaneously. Have tested pause (the failing of other approaches like coffee.exe).

@zip if you are monitoring this thread (or any others) - are there any other ports we should consider eg. for ServiiGo (will?). Note that we are limited to TCP sessions (not UDP ports).
ps. in case you saw my earlier post with the IP address loop - I deleted it in favour of this approach.

Re: Prevent hibernate/standby/sleep

PostPosted: Sun Jun 02, 2013 1:15 pm
by zip
No, just these 2 re used for streaming

Re: Prevent hibernate/standby/sleep

PostPosted: Sun Jun 02, 2013 1:46 pm
by DenyAll
Thanks. I think the above is in its final state pending any feedback from users.

Re: Prevent hibernate/standby/sleep

PostPosted: Fri Jun 07, 2013 1:48 am
by blopa
Guys, my smart TV is on 192.168.1.5 (static) but even when turned off, the TCP connection shows as "Established". This is from the netstat command:

"TCP 192.168.1.4:8895 192.168.1.5:46237 ESTABLISHED"

I left the computer alone for about 1hr but the "ESTABLISHED" status was still there. How is that possible? I even unplugged the TV but nothing.

In my testings, when I play a movie from TV and after a few mins I turn it off, the connection seems to be dropped correctly
Is seems to happen only when the TV is connected for +1hr (very inaccurate calculation).

I'running Windows 7 and my rendered is a Samsung SmartTV ES6800
Any experiencies or suggestions are very welcome

@DenyAll, would you please explain what your script does exactly? will it change the power plan if a something is connected to port 8895? if not, how/when a pc will enter in sleep mode? I can't read scripts...sorry about that!

@J.M., your script is still working great. The problem I described above seems to be from my side. If you faced something similar please let me know

Thanks in advance,

Blopa




Blopa