Hi Andrew, really appreciate your responsiveness and in-depth insights.
I figured it's some nastiness deep in the TV code, my Bravia works no problem! I love the new TV and I got a good deal on it so frankly I am going to workaround it in this instance.
I came up with this PowerShell solution, which I run from a scheduled task - hope it helps someone else if they get into similar woes.
Edit - came up with a more complete version that actually is capable of working out if the server is streaming specifically to the T.V. or not.This version therefore relies on:
VS 2013 runtime library.
pscap - which is a custom PowerShell module....
# start at 9 am (the earliest likey time we start streaming a movie.) Scheduled Task action = Powershell.exe (argument section) = -windowstyle hidden c:\myscript.ps1
# See if it's running and if not - start it.
if (get-process | where {$_.name -like 'media * 19'}) {Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Information –EventID 0 –Message “JRiver Already Running.”} else {
start-process 'C:\Program Files (x86)\J River\Media Center 19\Media Center 19.exe' -ArgumentList '/boot'
# Report.
if (get-process | where {$_.name -like 'media * 19'}) {
Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Information –EventID 0 –Message “JRiver sucessfully Started.”}
else
{Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Error –EventID 3 –Message “JRiver NOT sucessfully Started.”}
}
$count = 0
$sw = [Diagnostics.Stopwatch]::StartNew()
DO {
# Is the TV alive ?
if (test-connection -count 1 192.168.1.76 -quiet) {
# It is alive - therefore are we streaming or not?
#Testing shows at idle the trafic over 100 seconds is around 5k per second for SOAP and other noise - when the TV is on.
#We'll NEVER stream a film at 10k per second and this gives us a comfortable margin. Test to see if the average bytes over 10 seconds is more than 100k.
start-process 'Nmcap' -wait -WindowStyle Hidden -ArgumentList '/network * /capture tcp /file "D:\Sysinternals\server.cap" /StopWhen /TimeAfter 10 seconds'
if ((((Get-CaptureP2PStats -CaptureFile "D:\Sysinternals\server.cap") | where {$_.source -like '192.168.1.120' -and $_.destination -like '192.168.1.76'}).bytes /1kb -shr 0) -gt '100') {
# Cleanup
Remove-Item "D:\Sysinternals\server.cap" -ErrorAction Ignore
# OK we are streaming to the TV so we need to kill MC after were done streaming.
Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Information –EventID 2 –Message “JRiver is detected as streaming to 192.168.1.76 - waiting for exit.”
DO
# Ping the TV every 10 minutes to see if it's been switched off yet. . .
{
$ping = test-connection -count 1 192.168.1.76 -quiet
Sleep 600
} until (!$ping)
# TV is off - see if we are streaming to something else at all. . .
DO
{
$still_streaming = '1'
# Testing shows that MC periodically generates a few frames of 2k per second when completely idle - extending test period to 60 sec to compensate.
# a 64k MP3 file is 8k per second on the wire so if were over that - were streaming.
$bandwidth_in_use = Get-Counter -Counter "\process(media center 19)\io other bytes/sec" -MaxSamples 60
$bandwidth_in_use |% {$total_bytes += [convert]::toint32((($_.readings -split ':')[1]) -shr 0,10)}
if (($total_bytes / $bandwidth_in_use.count) -gt '8192') {
# We are streaming - wait 10 mins before checking again that we have finished streaming. . .
Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Information –EventID 2 –Message “JRiver is detected as streaming - waiting for exit.”
remove-variable total_bytes
$still_streaming = '1'
Sleep 600
}
else
{
remove-variable total_bytes
$still_streaming = '0'
}
} while ($still_streaming -eq '1')
# We are no longer streaming stop MC!
get-process | where {$_.name -like 'media * 19'} | stop-process
#Report.
if (get-process | where {$_.name -like 'media * 19'}) {
Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Error –EventID 3 –Message “JRiver NOT sucessfully Terminated.”}
else
{Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Information –EventID 1 –Message “JRiver sucessfully terminated.”}
sleep 2
# Restart MC.
start-process 'C:\Program Files (x86)\J River\Media Center 19\Media Center 19.exe' -ArgumentList '/boot'
#Report.
if (get-process | where {$_.name -like 'media * 19'}) {
Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Information –EventID 0 –Message “JRiver sucessfully Started.”}
else
{Write-EventLog –LogName Application –Source "JRiver-monitoring" –EntryType Error –EventID 3 –Message “JRiver NOT sucessfully Started.”}
}
}
# If the TV is alive we end up here and incriment the count and wait for 10 minutes before checking a second time to see if we are streaming to it. . .
Remove-Item "D:\Sysinternals\server.cap" -ErrorAction Ignore
$count ++
sleep 600
$exit = '0'
"`$count is $count"
# After 77 times through the loop which we will reach in 13 hours if the TV is never on - set out exit variable = 1.
if ($count -ge '83') {$exit = '1'}
# OR (since we could get stuck for hours in other DO loops) for a total of 13 hours total runtime.
if ($sw.elapsed.ticks -ge '504000000000') {$exit = '1'}
} until ($exit -eq '1')
$sw.stop()To install pscap is a bit of pain - here are some notes.
==================
· Create folder PSCap under $pshome\Modules
· Place the binary (x86 or x64) for your platform into newly created folder, along with PSCap.psd1 and PSCap.format.ps1xml
import the module PSCap into your Powershell session
Edit the pscap.psd1 and set PowerShellHostVersion = '3.0'
How to load a custom module into PowerShell.
See if a custom module is blocked - switch into the directory where the module is located
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\pscap
ls . -r | get-item -Stream Zone.Identifier -ea 0
If any output is returned from the above line - its blocked.
Unblock a custom module
ls -r | unblock-file
Pscap requires VC Runtime 2013 - x86 or x64
and also this solution requires Network monitor V 3.4 - which provides a command line packet capture interface.
Finally I wrote some event logging code as well so a custom event source has to be
registered called JRiver-monitoring which is a one liner.
New-EventLog –LogName Application –Source "JRiver-monitoring"
===================
Works for my needs.