Page 1 of 2
CasparCG countdown clock
Posted: Wed Sep 07, 2016 6:10 pm
by bern333
I'm trying to use a send command
Code: Select all
send "showTime" to me in 500 milliseconds
to control a countdown clock for video clips in CasparCG. Because I don't have a knowledge of OSC, and in any case Livecode doesn't have a library I can use (there's very old one, I know) I'm doing it by asking CasparCG for the remaining duration via it's INFO command. This returns a whole lot of information, and waiting for a reply and then decoding every few hundred milliseconds clogs up the system, so I'm asking once at the beginning and then free running the countdown.
Unfortunately it runs slow. A 7.44 clip I have is ten seconds behind. Not much use in a tv studio.
The code is below, most - as ever - kludged together from various places. I wonder if anyone has any idea why it should run slow?
thanks
Bernie
Code: Select all
-- @@@@@@@@@@@@@@@@@@@@@@@@@ showtime @@@@@@@@@@'
on showTime
-- its going to loop every 500msecs
subtract 0.5 from totalsecs
-- put the result on the screen
put totalsecs div 3600 into xhrs of card "clipper"
put (totalsecs mod 3600) div 60 into xmins of card "clipper"
put trunc((totalsecs mod 3600) mod 60) into xsecs of card "clipper"
--add leading zeros etc
if xhrs<10 then
put "0" & xhrs into field "xHr" of card "clipper"
else if xhrs =0 then
put "00" into field "xHr" of card "clipper"
else
put xhrs into field "xHr" of card "clipper"
end if
if xmins <10 then
put "0" & xmins into field "xMin" of card "clipper"
else if xmins =0 then
put "00" into field "xMin" of card "clipper"
else
put xmins into field "xMin" of card "clipper"
end if
if xsecs <10 then
put "0" & xsecs into field "xSec" of card "clipper"
else if xsecs =0 then
put "00" into field "xSec" of card "clipper"
else
put xsecs into field "xSec" of card "clipper"
end if
-- getting out of loop
if (totalsecs >1 and the cMediaOnFlag of this stack is 1) then
send "showTime" to me in 500 milliseconds
else
if showTime is in the pendingMessages then
put the pendingMessages into myMsgs
filter myMsgs with "*showTime*"
repeat for each line myMsg in myMsgs
cancel item 1 of myMsg
end repeat
end if
-- change the button setup
set the disabled of button "player1" of card "clipper" to "false"
--reset everything
set the cMediaOnFlag of this stack to 0
set the visible of field "board1" of card "clipper" to "false" -- don't show clip playing
put 00 into field "xSec" of card "clipper"
put 00 into field "xMin" of card "clipper"
put 00 into field "xHr" of card "clipper"
put 0 into xsecs of card "clipper"
put 0 into xmins of card "clipper"
put 0 into xhrs of card "clipper"
end if
end showTime
Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 6:38 pm
by dunbarx
Hi.
Before I can even start, I cannot make sense of a line such as:
Code: Select all
put totalsecs div 3600 into xhrs of card "clipper"
Is it "fld xHrs"...?
Craig Newman
Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 8:47 pm
by bern333
Code: Select all
put totalsecs div 3600 into xhrs of card "clipper"
totalsecs - a variable
div 3600 - divide by 3600 and give just the integer
xhrs of card "clipper" - a variable on card clipper
It gives the hours for the countdown clock at this particular moment. So the next two lines give the minutes and seconds
Code: Select all
put (totalsecs mod 3600) div 60 into xmins of card "clipper"
put trunc((totalsecs mod 3600) mod 60) into xsecs of card "clipper"
This stuff all works - it gives a correct countdown, just a slow one. Personally I worry about calling showTime from within showTime, but as the documentation is as ever scant I've just followed someone else.
B
Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 9:09 pm
by Klaus
Hi bern333,
bern333 wrote:...
xhrs of card "clipper" - a variable on card clipper
...
cards do NOT have variables. so this does not make any sense!
Variables can be LOCAL (to a script) or GLOBAL (to everything), but in this case simply leave out "... of cd XYZ"
an dit will still work. I wonder why this did not throw an error!?
You can also leave out the leading-zero-formatting stuff by setting "the numberformat" and make the whole script shorter anyway
Code: Select all
...
set numberformat to "xx"
put totalsecs div 3600 into fld "xhrs" of cd "clipper
put (totalsecs mod 3600) div 60 into fld "xmins" of cd "clipper"
put trunc((totalsecs mod 3600) mod 60) into fld "xsecs" of cd "clipper"
## getting out of loop
if (totalsecs >1 and the cMediaOnFlag of this stack is 1) then
send "showTime" to me in 500 milliseconds
...
Best
Klaus
Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 10:05 pm
by dunbarx
I saw it as a problem. When I ran it, the handler threw an error and halted.
How did the OP get past it??
Craig
Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 10:13 pm
by Klaus
dunbarx wrote:I saw it as a problem. When I ran it, the handler threw an error and halted.
How did the OP get past it??
Craig
Bribe?

Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 10:31 pm
by bern333
Klaus - thank you.
I've made those changes - you live and learn. Unfortunately it doesn't make any obvious difference to the speed of the counter.
Also - not that it matters in the wider context - the system didn't throw an error. I had moved the showTime code from a button, to the card, to the stack, to make sure it will still work, as eventually I need to be able to cope with a Pause button and that it keeps running when I change to another card and back. On the original card and button I'd declared the variables as global, though not on the stack script, which this is.
I don't understand the "send" construct, but I do wonder if endlessly sending a message from within the procedure just keeps things stacking up with the buffer getting bigger and bigger. If there's a proper explanation somewhere I'll read it, but as usual I did a lot of hunting before I asked and didn't find one.
Bernie
Re: CasparCG countdown clock
Posted: Wed Sep 07, 2016 10:44 pm
by dunbarx
Bernie.
I would be glad to run your handler to see what I get.
Is it so, however, that the container reference that is throwing me (though not, apparently you) is what I wrote above, that the word "field" is missing?
Craig
Re: CasparCG countdown clock
Posted: Thu Sep 08, 2016 11:33 am
by bern333
All help gratefully received - and I know that people in the future will be trawling through this thread looking for clues just as I have for the past few weeks.
First - it's fixed, but I don't know why.
The loop must just run more quickly with Klaus's corrections. I found a few mistakes along the way - the biggest being a failure to press the Apply button. When I moved, or thought I moved, the code up from the card to the stack, I commented it out on the card script. Then I didn't press Apply and didn't go back to the card script, so have been running the card script version. That's why a failure to declare the global variables and the errors pointed out above didn't show up.
You live and learn.
None of this should make a difference to the timing, other than Klaus's corrections, so I can only assume that Klaus triumphs!
Here is the showTime procedure as updated thanks to Klaus. Totalsecs needs a fudge factor at the beginning because of the time taken polling the socket. 0.5 seconds seems to be good.
Code: Select all
-- @@@@@@@@@@@@@@@@@@@@@@@@@ showtime @@@@@@@@@@'
on showTime
-- its going to loop every 500msecs
subtract 0.5 from totalsecs
-- do the time maths
set numberformat to "xx"
put totalsecs div 3600 into xhrs
put (totalsecs mod 3600) div 60 into xmins
put trunc((totalsecs mod 3600) mod 60) into xsecs
--put the numbers on the screen
put xhrs into field "xHr" of card "clipper"
put xmins into field "xMin" of card "clipper"
put xsecs into field "xSec" of card "clipper"
-- getting out of loop - is the clock >0 and is the clip running (stop button not pressed)
if (totalsecs >1 and the cMediaOnFlag of this stack is 1) then
send "showTime" to me in 500 milliseconds
else
-- otherwise do this (lifted from somewhere.....) to make it stop
if showTime is in the pendingMessages then
put the pendingMessages into myMsgs
filter myMsgs with "*showTime*"
repeat for each line myMsg in myMsgs
cancel item 1 of myMsg
end repeat
end if
-- change the button setup
set the disabled of button "player1" of card "clipper" to "false"
--reset everything
set the cMediaOnFlag of this stack to 0 --reset the clip running flag
set the visible of field "board1" of card "clipper" to "false" -- don't show clip playing
put 00 into field "xSec" of card "clipper" -- put 00s on the screen
put 00 into field "xMin" of card "clipper"
put 00 into field "xHr" of card "clipper"
put 0 into xsecs -- reset the variables
put 0 into xmins
put 0 into xhrs
end if
end showTime
The proper answer to the problem is to make changes in CasparCG - there are enough people out there having similar timing problems.
For anyone interested, I have two more threads running in the CasparCG forum and on Github. These are about trying to persuade those who write CasparCG to add a simple time info system -
http://forum.casparcg.com/viewtopic.php ... 107#p27107
https://github.com/CasparCG/Server/issu ... -245492444
Thanks to all.
Bernie
Re: CasparCG countdown clock
Posted: Thu Sep 08, 2016 5:54 pm
by bern333
New question....
I thought it would be a good idea to have the countdown timer on a floater, so that it could appear when a clip is running and count down, whatever you do with the main stack - like going off and putting on a caption, then coming back to the running clip card. So I made a substack with a single card and on the card are the clock fields which count down. The substack opens when the play button is pressed and should close when the stop button is pressed.
There must be a routine to do this but various tries keep coming up with an error message that I wrote in the main open stack code. I can't see why that should happen as I would have thought that once the main stack is open the system doesn't go back to the openStack code.
Any guidance please...?
Bernie
Re: CasparCG countdown clock
Posted: Thu Sep 08, 2016 6:34 pm
by Klaus
HI Bernie,
sorry, not sure what your problem is!?
What error do you get and what script do you use, please post it here.
Maybe just an addressing error?
Did you :
...
put xhrs into fld "xhrs" of cd X OF STACK "the extra stack for displaying the time"
...
?
Best
Klaus
Re: CasparCG countdown clock
Posted: Thu Sep 08, 2016 8:38 pm
by bern333
Deleted last version as it was wrong, and I've been trying things as I've been writing.
I've now got rid of the various errors by putting in versions of "of stack "main"" or "of stack "pop"" all over the place.
It doesn't actually count down now, but I imagine that's a similar problem.
thanks
Bernie
Re: CasparCG countdown clock
Posted: Thu Sep 08, 2016 10:46 pm
by dunbarx
Bernie.
It is a good lesson, to see that LC needs to know where the fields are (or where the handlers are) when calling them from remote locations.
It is probably common (I do it) to leave these references out when all that action is based within a single card, or where calls fall directly in the message path. But these do not cross stacks unless they are in use.
Craig
Re: CasparCG countdown clock
Posted: Fri Sep 09, 2016 11:00 am
by bern333
Do we know anything about the actual mechanics of a loop like this one?
I've added a few more "
of stack "main"" and got the thing to run again but it isn't reliable. In fact I think it might slow down over time. I have a clip with reversed timecode so that I can see one against the other. They don't start in sync at the moment but I can fix that by fudging the start totalsecs. That isn't a problem. But by around 4.00 into my 7.44 test clip I feel that I can see it slowing over time. It surely can't be the actual loop, that must execute in no time at all.
What could be making it inaccurate? I suspect an overstuffed message buffer but I don't know much about this nor do i know a way of checking
As usual any thoughts gratefully accepted, and when thing works I'll publish it under GPL like the last version. It will be stuffed with useful code for those following along behind!
Bernie

Re: CasparCG countdown clock
Posted: Fri Sep 09, 2016 2:29 pm
by Klaus
Hi Bernie,
you are using a player object in Livecode, right? If yes, you could query the player itself
and compute the neccessary values from that. Know what I mean?
Something like this (out of my head):
Code: Select all
...
put the duration of player 1 into tDuration
put the currenttime of player 1 into tNow
put the timescale of player 1 into tTimeScale
put tDuration - tNow into tRemainingtime
put tRemainingtime/tTimescale into tRemainingSeconds
## Now compute hours, minutes and seconds from that variable
## You get the picture :-)
...
This way your countdown will also be in sync with the video!
Best
Klaus