-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdr-streamdev-client.lua
More file actions
2588 lines (2407 loc) · 82.2 KB
/
vdr-streamdev-client.lua
File metadata and controls
2588 lines (2407 loc) · 82.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright 2017 Martin Wache
-- VDR Streamdev Client is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as published
-- by the Free Software Foundation Version 2 of the License.
-- You can obtain a copy of the License from
-- https://www.gnu.org/licenses/gpl2.html
--
-- VDR Streamdev Client
-- Version 0.3.4
--
-- A script which turns mpv into a client for VDR with the Streamdev-Plugin
--
-- Features:
-- * runs on Windows, Linux and Mac Os. (needs bash and netcat installed)
-- * easy channel switching a la vdr (with channel group support)
-- * show current and next epg event if available
-- * create timers from epg, disable/enable and remove timers
-- * watch recordings
--
-- Short instructions:
-- 1. Enable the streamdev-server-plugin in vdr
-- 2. Modify streamdevhosts.conf to contain the clients IP
-- 3. If you want to have channel names and epg info,
-- modify svdrphosts.conf to contain the clients IP.
-- Also netcat ('nc') and bash needs to be installed and in the path.
-- 4. Place this file in one of mpvs script folders
-- ( ~/.config/mpv/scripts/) or call mpv with the --script
-- command line option.
-- For now --script vdr-streamdev-client.lua is prefered.
-- 5. start mpv
-- mpv vdrstream://[vdr-host][:streamdev-port][/channel] [--script vdr-streamdev-client.lua]
--
-- When mpv is running in Streamdev client mode, you can use
-- the keys UP,DOWN, 0-9 to select channels.
-- ENTER will bring up the channel info display.
-- The key 'm' will show the menu.
--
-- Many thanks to:
-- - wolfi.m@vdr-portal.de for fixing the dimensions of the time-box
-- in the channel-info, the progress bar position and pointing
-- out that I forgot to remove my startup channel.
-- - jrie@vdr-portal.de for fixing the bash path for windows
--
local config = {
host="192.168.55.4",
svdrp_port="6419",
--svdrp_port="2004",
streamdev_port="3000",
-- default startup channel to show
startup_channel=1,
-- time after which the '0' key returns to this channel
previous_channel_time=10,
-- for how long to show playback/channel info
show_info_timeout=7,
-- timeout after which channel entry is assumed to be finished
channel_switch_timeout=5,
--media_dir="/Users/wache/Downloads/mps/",
media_dir="/Volumes/video",
-- all media extensions in upper case please!
media_extensions={".AVI",".MPG",".OGG",".M4A",".M3U",".MP4",".WEBM",},
-- if you don't want to use streamdev-streaming for recordings
-- you can provide the path to VDRs video directory here (mounted locally)
vdr_video_dir="",
-- recording MB/minute to estimate remaining recording time
mb_per_minute=25,
-- how often the current/next epg events are loaded from the server.
-- In seconds
epg_nownext_update_time=300,
-- time after which a schedule for a channel is considered out of date
-- and updated from the server. In seconds.
epg_channel_update_time=3600,
-- how long after the event ended it is still shown. In seconds.
epg_old_events_linger_time=120,
-- how much time before an event the timer starts. In minutes.
timer_margin_start=5,
-- how much time after an event the timer stops. In minutes.
timer_margin_stop=10,
-- the default lifetime of a recording (see VDRs manual)
timer_default_lifetime=99,
-- the default priority of a recording (see VDRs manual)
timer_default_priority=50,
osd_font_pixel_per_char=8,
-- osd colors
osd_background_color="000000",
osd_background_alpha="70",
osd_header_color="000090",
osd_header_alpha="70",
osd_highlight_color="000090",
osd_highlight_alpha="70",
osd_progressbar_fg_color="00F000",
osd_progressbar_fg_alpha="10",
osd_progressbar_bg_color="000090",
osd_progressbar_bg_alpha="10",
osd_red="0000F0",
osd_reda="70",
osd_green="00F000",
osd_greena="70",
osd_yellow="00F0F0",
osd_yellowa="70",
osd_blue="F00000",
osd_bluea="70",
osd_message_color="007700",
osd_message_alpha="10",
osd_confirm_color="007777",
osd_confirm_alpha="10",
osd_top_menu=20,
osd_left_menu=20,
osd_width_menu=430,
osd_height_menu=242,
osd_menu_max_items=9,
osd_menu_item_height=20,
osd_max_rows = 14,
osd_message_left=20,
osd_message_top=230,
osd_message_width=430,
osd_message_height=20,
osd_info_left=20,
osd_info_top=180,
osd_info_width=430,
osd_info_height=80,
}
require 'mp.options'
read_options(config,'vdr-streamdev-client')
local assdraw = require "mp.assdraw"
local channels = { }
local chno_to_idx = {}
local chid_to_idx = {}
local startup = 1
local has_svdrp = 0
local vdruri
local utils = require 'mp.utils'
local channel_idx=nil
local next_channel=0
local channel_timer
local last_channel=1
local next_last_channel=1
local update_last_channel_timeout
local disk_space_available=nil
local disk_space_free=nil
local disk_space_percent=nil
local epginfo = {}
local epginfo_time = {}
local epg_timer -- refreshes the epg info regulary
local timerinfo = {}
local vw=495
local vh=275
-- ************************* misc ***********************
function ends_with(str,str_end)
local str_len=str:len()
return str:sub(1+str:len()-str_end:len(),str_len)==str_end
end
function strip_end(str,str_end)
local str_len=str:len()
return str:sub(1,str:len()-str_end:len()+1)
end
function toArray(i)
local array={}
for v in i do
array[#array+1]=v
end
return array
end
function slice(tbl,first,last)
local s={}
for i = first or 1, last or #tbl do
s[#s+1] = tbl[i]
end
return s
end
-- ************************* state machine stuff **********************
local state={}
local state_livetv
local state_playback
local state_channel_info
local main_menu_items
function update_state()
local cstate=curr_state()
if (cstate.update_state) then
cstate:update_state()
end
end
function update_osd()
local cstate=curr_state()
mp.log("info","update_osd "..cstate.name)
local ass
if (cstate.update_osd) then
ass = cstate:update_osd()
else
ass = assdraw.ass_new()
end
if cstate.message ~= nil or cstate.confirm ~= nil then
-- message box
ass:new_event()
ass:pos(config.osd_message_left, config.osd_message_top)
if cstate.confirm ~= nil then
ass_color(ass,config.osd_confirm_color)
ass_alpha(ass,config.osd_confirm_alpha)
else
ass_color(ass,config.osd_message_color)
ass_alpha(ass,config.osd_message_alpha)
end
ass:draw_start()
ass:rect_ccw(0,0,config.osd_message_width,config.osd_message_height)
ass:draw_stop()
-- print message
ass:new_event()
ass:pos(config.osd_message_left, config.osd_message_top)
if cstate.confirm ~= nil then
ass:append(cstate.confirm)
else
ass:append(cstate.message)
end
end
mp.set_osd_ass(0, 0, ass.text)
end
function state_update_timeout()
local cstate=curr_state()
if cstate.timeout then
if cstate.timer == nil then
cstate.timer=mp.add_timeout(cstate.timeout,state_back)
else
cstate.timer:kill()
cstate.timer:resume()
end
end
if cstate.update_osd_timeout then
if cstate.osd_timer == nil then
cstate.osd_timer=mp.add_periodic_timer(cstate.update_osd_timeout,
function()
update_state()
update_osd()
end)
else
cstate.osd_timer:kill()
cstate.osd_timer:resume()
end
end
end
function state_remove_timeouts()
local cstate=curr_state()
if cstate.timer then
cstate.timer:kill()
end
if cstate.osd_timer then
cstate.osd_timer:kill()
end
end
function update_hide_osd_timeout()
local cstate=curr_state()
if cstate.hide_osd_timeout then
if cstate.hide_osd_timer ~= nil then
cstate.hide_osd_timer:kill()
end
cstate.hide_osd_timer=mp.add_timeout(cstate.hide_osd_timeout,
function()
cstate.update_osd=nil
update_osd()
end)
end
cstate.hide_osd_timeout=nil
end
function new_state(nstate)
table.insert(state,nstate)
mp.log("info","state_new "..nstate.name)
state_update_timeout()
update_state()
update_hide_osd_timeout()
update_osd()
end
function state_remove_including(name)
local cstate=curr_state()
while #state>1 and name ~= cstate.name do
state_back()
cstate=curr_state()
end
-- remove the state the name
state_back()
end
function state_back_to(name)
local cstate=curr_state()
while #state>1 and name ~= cstate.name do
state_back()
cstate=curr_state()
end
end
function state_back()
local cstate=curr_state()
state_remove_timeouts()
if #state>1 then
table.remove(state)
end
cstate=curr_state()
mp.log("info","state_back, new "..cstate.name)
update_state()
update_osd()
end
function curr_state()
return state[#state]
end
-- *********************** OSD stuff *******************************
function ass_color(ass,bgr)
ass:append("{\\1c&H"..bgr.."&}")
end
function ass_bdcolor(ass,bgr)
ass:append("{\\3c&H"..bgr.."&}")
end
function ass_alpha(ass,alpha)
ass:append("{\\1a&H"..alpha.."}")
end
function ass_bdalpha(ass,alpha)
ass:append("{\\3a&H"..alpha.."}")
end
function ass_scale_font(ass,scale)
ass:append("{\\fscx"..scale.."\\fscy"..scale.."}")
end
function ass_clip(ass,x1,y1,x2,y2)
ass:append("{\\clip("..x1..","..y1..","..x2..","..y2.."}")
end
local function vdrtime2str(t)
return t:sub(1,2)..":"..t:sub(3,5)
end
local function print_time(t)
if (t == nil) then
return " "
end
return os.date('%H:%M',t)
end
local function print_date(t)
if (t == nil) then
return " "
end
return os.date('%a %d.%m',t)
end
-- local fake time stamp
local function lts(options)
if options.hour == nil then options.hour=0 end
if options.min == nil then options.min=0 end
return ((((options.year-1970)*366+options.month)*31+options.day)*24+
options.hour)*60+options.min
end
local function to_lts(date, time)
local year=tonumber(date:sub(1,4))
local month=tonumber(date:sub(6,7))
local day=tonumber(date:sub(9,10))
local min=tonumber(time:sub(3,4))
local hour=tonumber(time:sub(1,2))
--return os.time{year=year,month=month,day=day,hour=hour,minute=min}
return lts{year=year,month=month,day=day,
hour=hour,min=min}
end
local function format_epg(epg_info)
if epg_info == nil then return "" end
local msg
msg=print_time(epg_info['start'])
if (epg_info.timer_status =="T") then
msg= msg.." REC: "
end
if (epg_info['title'] ~= nil) then
msg = msg.." "..epg_info['title']
end
return msg
end
local function date_format_epg(epg_info)
local msg=format_epg(epg_info)
msg = print_date(epg_info.start) .. " " .. msg
return msg
end
local function print_duration(t)
if t==nil then
return "xx:xx:xx"
end
local h=math.floor(t/3600)
local m=math.floor(t%3600/60)
local s=t%60
return string.format("%02d:%02d:%02d",h,m,s)
end
function curr_channel_id()
local cinfo = channels[channel_idx]
return cinfo and cinfo['id'] or nil
end
function channel_id_to_idx(cid)
return chid_to_idx[cid]
end
function channel_info_from_cid(cid)
return channels[chid_to_idx[cid]]
end
function draw_progressbar(ass,left,top, width, height, part)
ass:new_event()
ass:pos(left, top)
ass_color(ass,config.osd_progressbar_bg_color)
ass_alpha(ass,config.osd_progressbar_bg_alpha)
ass_bdcolor(ass,config.osd_progressbar_bg_color)
ass_bdalpha(ass,config.osd_progressbar_bg_alpha)
ass:draw_start()
ass:rect_ccw(0,0,width,height)
ass:draw_stop()
ass:new_event()
if part <0 then return end
ass:pos(left, top)
ass_color(ass,config.osd_progressbar_fg_color)
ass_alpha(ass,config.osd_progressbar_fg_alpha)
ass_bdcolor(ass,config.osd_progressbar_fg_color)
ass_bdalpha(ass,config.osd_progressbar_fg_alpha)
ass:draw_start()
ass:rect_ccw(0,0,part*width,height)
ass:draw_stop()
ass:new_event()
end
function show_playback_info(self)
--local time_pos = mp.get_property_native("time-pos")
local time_pos = mp.get_property_native("playback-time")
local max_time = mp.get_property_native("duration")
local ass = assdraw.ass_new()
-- channel info box
ass:new_event()
ass:pos(config.osd_info_left, config.osd_info_top)
ass_color(ass,config.osd_background_color)
ass_alpha(ass,config.osd_background_alpha)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_info_width,config.osd_info_height)
ass:draw_stop()
-- print current playback time
ass:new_event()
ass:pos(config.osd_info_left, config.osd_info_top)
ass:append(print_duration(time_pos))
-- print playback length
ass:new_event()
ass:pos(config.osd_info_width-80, config.osd_info_top)
ass:append(print_duration(max_time))
-- print recording name
ass:new_event()
ass:pos(config.osd_info_left+80, config.osd_info_top)
ass_clip(ass,config.osd_info_left+80,config.osd_info_top,
config.osd_info_left+config.osd_info_width-100,config.osd_info_top+20)
ass_scale_font(ass,80)
ass:append(self.rinfo['name'])
if time_pos~= nil and max_time~= nil then
draw_progressbar(ass,config.osd_info_left+10,config.osd_info_top+30,
config.osd_info_width-config.osd_info_left-10,20,time_pos/max_time)
end
return ass
end
function show_channel_info(self)
local ass = assdraw.ass_new()
local cidx = next_channel==0 and channel_idx or chno_to_idx[next_channel]
local cinfo = cidx and channels[cidx] or nil
local chno = cinfo and cinfo.no or cidx
if next_channel ~= 0 then
-- channel switching by entering a number
chno = next_channel
cidx = chno_to_idx[chno]
cinfo = cidx and channels[cidx] or nil
else
cidx = channel_idx
cinfo = channels[cidx]
chno = cinfo and cinfo.no or cidx
end
-- channel info box
ass:pos(config.osd_info_left, config.osd_info_top)
ass_color(ass,config.osd_background_color)
ass_alpha(ass,config.osd_background_alpha)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_info_width,config.osd_info_height)
ass:draw_stop()
-- info time box
ass:new_event()
ass:pos(config.osd_info_left, config.osd_info_top)
ass_color(ass,config.osd_header_color)
ass_alpha(ass,config.osd_header_alpha)
ass:draw_start()
ass:rect_ccw(0,0,55,23)
ass:draw_stop()
-- print time
ass:new_event()
ass:pos(config.osd_info_left, config.osd_info_top)
ass:append(os.date("%H:%M"))
-- channel name
ass:new_event()
ass:pos(config.osd_info_left+70, config.osd_info_top)
if cinfo and not cinfo.is_group_separator then
ass:append(chno)
if next_channel~=0 then
ass:append("_")
end
end
if cinfo then ass:append(" "..tostring(cinfo['name'])) end
ass:new_event()
local cid = cinfo and cinfo['id'] or nil
local einfo=get_epgnow(cid)
if einfo then
-- epg progress bar
local dwidth=200
local dheight=5
if einfo['start'] ~= nil and einfo['duration'] ~= nil then
local part = (os.time()-tonumber(einfo['start']))/
tonumber(einfo['duration'])
draw_progressbar(ass,config.osd_info_left+1, config.osd_info_top+24,
dwidth,dheight,part)
end
-- epg now info
ass:pos(config.osd_info_left+2, config.osd_info_top+35)
ass_clip(ass,config.osd_info_left,config.osd_info_top+35,
config.osd_info_left+config.osd_info_width,
config.osd_info_top+55)
ass_scale_font(ass,80)
ass:append(format_epg(einfo))
ass:new_event()
end
einfo = get_epgnext(cid)
if einfo ~= nil then
-- epg next info
ass:pos(config.osd_info_left+2, config.osd_info_top+55)
ass_clip(ass,config.osd_info_left,config.osd_info_top+55,
config.osd_info_left+config.osd_info_width,
config.osd_info_top+75)
ass_scale_font(ass,80)
ass:append(format_epg(einfo))
ass:new_event()
end
return ass
end
function create_menu_base(options)
local ass = assdraw.ass_new()
local bg = config.osd_background_color
local bga = config.osd_background_alpha
local hd = config.osd_header_color
local hda = config.osd_header_alpha
local red = config.osd_red
local reda = config.osd_reda
local green = config.osd_green
local greena = config.osd_greena
local yellow = config.osd_yellow
local yellowa = config.osd_yellowa
local blue = config.osd_blue
local bluea = config.osd_bluea
-- menu box
ass:pos(config.osd_left_menu, config.osd_top_menu)
ass_color(ass,bg)
ass_alpha(ass,bga)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_width_menu,config.osd_height_menu)
ass:draw_stop()
ass:new_event()
-- header box
ass:pos(config.osd_left_menu, config.osd_top_menu)
ass_color(ass,hd)
ass_alpha(ass,hda)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_width_menu,20)
ass:draw_stop()
ass:new_event()
-- header
ass:pos(config.osd_left_menu, config.osd_top_menu)
ass:append(os.date("%H:%M"))
if options and options.name then ass:append(" "..options.name) end
ass:new_event()
-- footer
ass:pos(config.osd_left_menu,config.osd_top_menu+config.osd_height_menu-20)
ass_color(ass,red)
ass_alpha(ass,reda)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_width_menu/4,20)
ass:draw_stop()
ass:new_event()
if options and options.red then
ass:pos(config.osd_left_menu+2,config.osd_top_menu+config.osd_height_menu-20)
ass_scale_font(ass,80)
ass:append(options.red)
ass:new_event()
end
ass:pos(config.osd_left_menu+config.osd_width_menu/4,config.osd_top_menu+config.osd_height_menu-20)
ass_color(ass,green)
ass_alpha(ass,greena)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_width_menu/4,20)
ass:draw_stop()
ass:new_event()
if options and options.green then
ass:pos(config.osd_left_menu+config.osd_width_menu/4+2,config.osd_top_menu+config.osd_height_menu-20)
ass_scale_font(ass,80)
ass:append(options.green)
ass:new_event()
end
ass:pos(config.osd_left_menu+config.osd_width_menu/4*2,config.osd_top_menu+config.osd_height_menu-20)
ass_color(ass,yellow)
ass_alpha(ass,yellowa)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_width_menu/4,20)
ass:draw_stop()
ass:new_event()
if options and options.yellow then
ass:pos(config.osd_left_menu+config.osd_width_menu/4*2+2,config.osd_top_menu+config.osd_height_menu-20)
ass_scale_font(ass,80)
ass:append(options.yellow)
ass:new_event()
end
ass:pos(config.osd_left_menu+config.osd_width_menu/4*3,config.osd_top_menu+config.osd_height_menu-20)
ass_color(ass,blue)
ass_alpha(ass,bluea)
ass:draw_start()
ass:rect_ccw(0,0,config.osd_width_menu/4,20)
ass:draw_stop()
ass:new_event()
if options and options.blue then
ass:pos(config.osd_left_menu+config.osd_width_menu/4*3+2,config.osd_top_menu+config.osd_height_menu-20)
ass_scale_font(ass,80)
ass:append(options.blue)
ass:new_event()
end
return ass
end
function draw_scrollbar(ass,left,top,width,height,pos,max)
local part=height/max
local bheight=part
if part<10 then bheight=10 end
if pos<0 or max < 1 then return end
part = (pos-1)*(height-bheight)/max
ass:pos(left,top)
ass_color(ass,config.osd_highlight_color)
ass_alpha(ass,config.osd_highlight_alpha)
ass:draw_start()
ass:rect_ccw(0,part,width,part+bheight)
ass:draw_stop()
ass:new_event()
end
local item_w=config.osd_width_menu - 11
function show_menu(self)
local ass = create_menu_base{name=self.header,
red=self.red_name,green=self.green_name,
yellow=self.yellow_name,blue=self.blue_name}
if self.selected_item == nil then self.selected_item = 1 end
if self.items == nil or #self.items == 0 then
return ass
end
if self.start_pos == nil or self.start_pos < 1 then self.start_pos = 1 end
if self.selected_item>self.start_pos+config.osd_menu_max_items then
self.start_pos=self.selected_item - config.osd_menu_max_items
end
if self.selected_item<self.start_pos then
self.start_pos=self.selected_item
end
local maxi = #self.items>self.start_pos+config.osd_menu_max_items
and self.start_pos+config.osd_menu_max_items or #self.items
local draw_item=self.draw_item and self.draw_item or
draw_column_item(nil,self.column_width)
for i = self.start_pos,maxi do
local v = self.items[i]
local itop= config.osd_top_menu+1+(i-self.start_pos+1)*config.osd_menu_item_height
if self.selected_item == i then
ass:pos(config.osd_left_menu, itop)
ass_color(ass,config.osd_highlight_color)
ass_alpha(ass,config.osd_highlight_alpha)
ass:draw_start()
ass:rect_ccw(0,0,item_w,config.osd_menu_item_height)
ass:draw_stop()
ass:new_event()
end
if v.draw == nil then
draw_item(v,ass,config.osd_left_menu+2,itop,item_w,config.osd_menu_item_height)
else
v:draw(ass,config.osd_left_menu+2,itop,item_w,config.osd_menu_item_height)
end
ass:new_event()
end
if #self.items>config.osd_menu_max_items then
draw_scrollbar(ass,config.osd_left_menu+config.osd_width_menu-10,
config.osd_top_menu+21,9,config.osd_height_menu-42,
self.start_pos,#self.items-config.osd_menu_max_items)
end
return ass
end
function menu_handle_key(self,k)
if k=="UP" then
self.selected_item = self.selected_item - 1
elseif k=="DOWN" then
self.selected_item = self.selected_item + 1
elseif k=="LEFT" then
self.selected_item = self.selected_item - config.osd_menu_max_items
elseif k=="RIGHT" then
self.selected_item = self.selected_item + config.osd_menu_max_items
elseif k=="BS" then
state_back()
elseif k=="RED" and self.red_action then
self:red_action()
elseif k=="GREEN" and self.green_action then
self:green_action()
elseif k=="YELLOW" and self.yellow_action then
self:yellow_action()
elseif k=="BLUE" and self.blue_action then
self:blue_action()
elseif k=="ENTER" then
local item = self.items[self.selected_item]
if item and item.action then
item:action()
end
elseif k=="MENU" then
state_remove_including("main_menu")
elseif type(k) == "number" and k>=0 and k<=9 then
self.selected_item = k
end
if self.selected_item then
if self.items ~= nil and self.selected_item > #self.items then
self.selected_item = #self.items
end
if self.selected_item < 1 then
self.selected_item = 1
end
end
update_osd()
end
local margin=20
function split_text(max_len,text)
local pos = 1
return function()
local npos = text:find("\n",pos)
if npos == nil then npos=text:len()+1 end
if npos-pos>max_len then
-- find space to split the string
npos=text:find("[ -.+]",pos+max_len-margin>0 and pos+max_len-margin or 0)
if npos == nil then npos=text:len()+1 end
end
if pos>=text:len() then
return nil
end
local ret = text:sub(pos,npos)
pos = npos + 1
return ret
end
end
-- shows text in self.text
-- uses self.header, self.title, self.subtitle
function show_text(self)
local ass = create_menu_base{name=self.header,
red=self.red_name,green=self.green_name,
yellow=self.yellow_name,blue=self.blue_name}
local i = 0
local t = config.osd_top_menu + 23
local l = config.osd_left_menu + 2
local max_rows=config.osd_max_rows
local text = self.text and toArray(split_text(config.osd_width_menu/config.osd_font_pixel_per_char/0.8,self.text)) or {}
if self.title then
-- time, title
ass:pos(l, t)
ass_clip(ass,l,t,l+config.osd_width_menu-10,t+25)
ass:append(tostring(self.title))
ass:new_event()
t = t + 25
max_rows = max_rows - 2
end
-- subtitle
if self.subtitle then
ass:pos(l, t)
ass_clip(ass,l,t,l+config.osd_width_menu-10,t+20)
ass_scale_font(ass,80)
ass:append(tostring(self.subtitle))
ass:new_event()
t = t + 20
max_rows = max_rows - 2
end
if self.start_pos == nil then self.start_pos = 1 end
if self.start_pos > #text-max_rows then self.start_pos = #text-max_rows end
if self.start_pos < 1 then self.start_pos = 1 end
local sp=self.start_pos
if self.text then
for j=0,max_rows do
local v = text[j+sp]
if v then
ass:pos(l, t + j*13)
ass_scale_font(ass,70)
ass:append(v)
ass:new_event()
end
end
end
if #text > max_rows+1 then
draw_scrollbar(ass,config.osd_left_menu+config.osd_width_menu-10,
config.osd_top_menu+21,9,config.osd_height_menu-42,
self.start_pos,#text-max_rows-1)
end
return ass
end
function key(k)
return function()
local cstate=curr_state()
mp.log("info","state name "..cstate.name)
mp.log("info","key "..k)
if cstate.confirm ~= nil then
local action=cstate.confirm_action
cstate.confirm=nil
cstate.confirm_action=nil
if k=="ENTER" and action ~= nil then
action(cstate)
end
update_osd()
return
end
if cstate and cstate.handle_key then
cstate:handle_key(k)
end
end
end
local function send_webrequest(path)
ret = utils.subprocess({
args= {'bash', '-c', '( printf "GET /'..path
..' HTTP/1.0\n\n"; sleep 1)|nc '..config.host..' '
..config.streamdev_port},
-- args= {'/bin/bash', '-c', 'echo "'..command
-- ..'" >/dev/tcp/'..config.host..'/'..config.svdrp_port},
cancellable=false,
})
return ret.stdout
end
local function parse_ext3mu(stdout)
mp.log("info","Parsing ext3mu")
local state='http_header'
local ret={}
local title={}
for l in string.gmatch(stdout,"[^\r\n]+") do
if state=='http_header' then
if l~="HTTP/1.0 200 OK" then
state="error_http_header"
break
end
state="http_header_content"
elseif state=="http_header_content" then
if l~="Content-type: audio/x-mpegurl; charset=UTF-8" then
state="error_http_content"
break
end
state="content_header"
elseif state=="content_header" then
if l~="#EXTM3U" then
state="error_content_header"
break
end
state="content_line_header"
elseif state=="content_line_header" then
if l:sub(1,11)~="#EXTINF:-1," then
state="error_content_line_header"
mp.log("info","'"..l:sub(1,11).."'")
break
end
local info=toArray(string.gmatch(l:sub(12),"[^ ]+"))
title['idx']=info[1]
title['day']=info[2]
title['time']=info[3]
title['name']=table.concat(slice(info,4)," ")
state = "content_line"
elseif state=="content_line" then
title['url']=l
table.insert(ret,title)
title = {}
state = "content_line_header"
end
end
mp.log("info",state)
return ret
end
local function get_info_svdrp(self)
mp.log("info","get_info_svdrp "..tostring(self.name))
local info=parse_lste(send_svdrp(string.format("LSTR %d",self.idx)))
for i,v in pairs(info) do
mp.log("info",tostring(i)..":"..tostring(v))
for j,event in pairs(v) do
mp.log("info",tostring(j)..":"..tostring(event))
return event.description,format_epg(event),event.subtitle
end
end
end
local function parse_lstr(stdout)
mp.log("info","Parsing lstr")
local ret={}
for i in string.gmatch(stdout,"[^\r\n]+") do
local code = i:sub(1,3)
if code == "250" then
local c = i:sub(5,5)
local line = i:sub(5)
local idx,date,time,length,new,name = line:match("(%d+) (%d%d.%d%d.%d%d) (%d%d:%d%d) (%d?%d:%d%d)(%*?) +(.*)")
local title={
idx=idx,
day=date,
time=time,
length=length,
new=new,
name=name,
url=string.format("http://%s:%d/%d.rec.ts",config.host,config.streamdev_port,idx),
info=get_info_svdrp,
}
table.insert(ret,title)
else
mp.log("info","parse_lstr unknown "..i)
end
end
return ret
end
local function collect_directories(recordings)
mp.log("info","collect recordings")
local ret={}
local cache_tree={
entries={},
child={},
}
for i,v in pairs(recordings) do
local spath=toArray(string.gmatch(v['name'],"[^~]+"))
local j=1
local dir=ret
local cache=cache_tree
while (j~=#spath) do
local name=spath[j]