-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathustreamplayer.pas
More file actions
623 lines (547 loc) · 14.3 KB
/
Copy pathustreamplayer.pas
File metadata and controls
623 lines (547 loc) · 14.3 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
unit ustreamplayer;
{
Written by R.Sombrowsky info@somby.de
}
{$mode delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls, Buttons, ActnList, LazUtf8,
IniFiles,
sdl2,
uexplayer;
type
TStreamPlayerForm = class;
{ TEXStreamPlayer }
TEXStreamPlayer = class(TEXPlayerControl)
private
FParent: TStreamPlayerForm;
FError, FSelecting: boolean;
procedure SetError(AValue: boolean);
procedure SetSelecting(AValue: boolean);
protected
procedure ShowItem(renderer: PSDL_Renderer; x, y, w, h, idx: integer;
selected, // selected
current: boolean); virtual; // current
procedure ShowItemName(error: boolean); virtual;
function GetRendererFlags: longword; override;
public
procedure PaintOverlays(renderer: PSDL_Renderer; var present: boolean); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Selecting: boolean read FSelecting write SetSelecting;
property Error: boolean read FError write SetError;
end;
TStreamPlayerItem = class(TCollectionItem)
public
FName, FUrl: string;
end;
{ TStreamPlayerForm }
TStreamPlayerForm = class(TForm)
aClose: TAction;
aShowMax: TAction;
aShowWin: TAction;
aUp: TAction;
aDown: TAction;
aRight: TAction;
aLeft: TAction;
aSelect: TAction;
aMute: TAction;
aPauseResume: TAction;
ActionList1: TActionList;
tmrLoad: TTimer;
procedure aCloseExecute(Sender: TObject);
procedure aDownExecute(Sender: TObject);
procedure aLeftExecute(Sender: TObject);
procedure aShowMaxExecute(Sender: TObject);
procedure aShowWinExecute(Sender: TObject);
procedure aMuteExecute(Sender: TObject);
procedure aPauseResumeExecute(Sender: TObject);
procedure aRightExecute(Sender: TObject);
procedure aSelectExecute(Sender: TObject);
procedure aUpExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure tmrLoadTimer(Sender: TObject);
private
FPlayer: TEXStreamPlayer;
FSelCols, FSelRows: integer;
FWinFormat: boolean;
FStartIndex, FSelIndex, // selectindex
FItemIndex: integer; // current shown index
FItems: TCollection;
procedure StartPlay;
public
procedure LoadItems;
procedure AdjustStartIndex;
procedure LoadParams; // You can load some params at the begin
procedure SaveParams; // You can save some params at the end
procedure SetWinFormat(win: boolean);
end;
var
StreamPlayerForm: TStreamPlayerForm;
implementation
{$R *.lfm}
resourcestring
{
rsLiveStreamNotAvailable='Livestream nicht verfügbar';
rsNoItems='Keine Einträge verfügbar';
}
rsLiveStreamNotAvailable = 'Livestream not available';
rsNoItems = 'No entries available';
{ TEXStreamPlayer }
procedure TEXStreamPlayer.SetSelecting(AValue: boolean);
begin
if FSelecting = AValue then
Exit;
FSelecting := AValue;
Invalidate;
end;
procedure TEXStreamPlayer.SetError(AValue: boolean);
begin
if FError = AValue then
Exit;
FError := AValue;
invalidate;
end;
procedure TEXStreamPlayer.ShowItem(renderer: PSDL_Renderer; x, y, w, h, idx: integer;
selected, current: boolean);
var
r, rt: TSDL_Rect;
it: TStreamPlayerItem;
tx: PSDL_Texture;
co: TSDL_Color;
ww, hh: integer;
begin
r.x := x;
r.y := y;
r.w := w;
r.h := h;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
if selected then
begin
SDL_SetRenderDrawColor(renderer, 200, 200, 255, 200);
end
else
if current then
begin
SDL_SetRenderDrawColor(renderer, 255, 200, 200, 200);
end
else
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 200);
SDL_RenderFillRect(renderer, @r);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDrawLine(renderer, x, y, x + w - 1, y);
SDL_RenderDrawLine(renderer, x, y + h - 1, x + w - 1, y + h - 1);
SDL_RenderDrawLine(renderer, x, y, x, y + h - 1);
SDL_RenderDrawLine(renderer, x + w - 1, y, x + w - 1, y + h - 1);
if (idx >= 0) and (idx < FParent.FItems.Count) then
begin
it := TStreamPlayerItem(FParent.FItems.Items[idx]);
if current then
with co do
begin
a := 255;
r := 255;
g := 0;
b := 0;
end
else
with co do
begin
a := 255;
r := 0;
g := 0;
b := 0;
end;
tx := LoadText(it.FName, co, ww, hh);
rt.h := h div 2;
rt.w := round(ww * rt.h / hh);
rt.x := x + ((w - rt.w) div 2);
rt.y := y + ((h - rt.h) div 2);
SDL_RenderCopy(Renderer, tx, nil, @rt);
SDL_DestroyTexture(tx);
end;
end;
procedure TEXStreamPlayer.ShowItemName(error: boolean);
var
co: TSDL_Color;
tx: PSDL_Texture;
rt: TSDL_Rect;
it: TStreamPlayerItem;
ww, hh: integer;
s: string;
begin
if error then
with co do
begin
a := 255;
r := 255;
g := 0;
b := 0;
end
else
with co do
begin
a := 255;
r := 0;
g := 255;
b := 0;
end;
it := nil;
if (FParent.FItemIndex >= 0) and (FParent.FItemIndex < FParent.FItems.Count) then
begin
it := TStreamPlayerItem(FParent.FItems.Items[FParent.FItemIndex]);
tx := LoadText(it.FName, co, ww, hh);
rt.h := round(Height * 10 / 100); // 8% of height
rt.w := round(ww * rt.h / hh);
rt.x := (Width - rt.w) div 2;
rt.y := (Height div 2) - int64(rt.h) - round(Height * 0.5 / 100);
SDL_RenderCopy(Renderer, tx, nil, @rt);
SDL_DestroyTexture(tx);
end;
if error then
begin
if it = nil then
s := rsNoItems
else
s := rsLiveStreamNotAvailable;
tx := LoadText(s, co, ww, hh);
rt.h := round(Height * 6 / 100);
rt.w := round(ww * rt.h / hh);
rt.x := (Width - rt.w) div 2;
rt.y := (Height div 2) + round(Height * 0.5 / 100);
SDL_RenderCopy(Renderer, tx, nil, @rt);
SDL_DestroyTexture(tx);
end;
end;
function TEXStreamPlayer.GetRendererFlags: longword;
begin
Result := inherited GetRendererFlags;
// result:=SDL_RENDERER_ACCELERATED;
end;
procedure TEXStreamPlayer.PaintOverlays(renderer: PSDL_Renderer; var present: boolean);
var
rt: TSDL_Rect;
row, col, idx, x, y, iw, ih, ww, hh: integer;
tx: PSDL_Texture;
co: TSDL_Color;
begin
if FError or (not Playing) or Pausing then
begin
PaintImage;
present := True;
end;
if FError then
begin
ShowItemName(True);
end
else
if not VideoAvailable and AudioAvailable then
begin
ShowItemName(False);
end;
if FSelecting then
begin
iw := ((5 * Width) div 6) div FParent.FSelCols; // Itemwidth
ih := ((5 * Height) div 6) div FParent.FSelRows; // Itemheight
x := (Width - FParent.FSelCols * iw) div 2;
y := (Height - FParent.FSelRows * ih) div 2;
idx := FParent.FStartIndex;
for col := 0 to FParent.FSelCols - 1 do
begin
for row := 0 to FParent.FSelRows - 1 do
begin
ShowItem(renderer, x + col * iw, y + row * ih, iw, ih, idx, idx =
FParent.FSelIndex, idx = FParent.FItemIndex);
Inc(idx);
end;
end;
with co do
begin
a := 255;
r := 0;
g := 0;
b := 255;
end;
if FParent.FStartIndex > 0 then
begin
// set arrow left
tx := LoadText('<', co, ww, hh);
rt.h := ih div 2;
rt.w := round(ww * rt.h / hh);
rt.x := x - rt.w;
rt.y := y + FParent.FSelRows * ih - rt.h - 1;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 200);
SDL_RenderFillRect(renderer, @rt);
SDL_RenderCopy(Renderer, tx, nil, @rt);
SDL_DestroyTexture(tx);
end;
if FParent.FStartIndex < (((FParent.FItems.Count - 1) div
FParent.FSelRows) // last column
- FParent.FSelCols + 1) * FParent.FSelRows then
begin
// set arrow right
tx := LoadText('>', co, ww, hh);
rt.h := ih div 2;
rt.w := round(ww * rt.h / hh);
rt.x := x + FParent.FSelCols * iw;
rt.y := y + FParent.FSelRows * ih - rt.h - 1;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 200);
SDL_RenderFillRect(renderer, @rt);
SDL_RenderCopy(Renderer, tx, nil, @rt);
SDL_DestroyTexture(tx);
end;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
end;
end;
constructor TEXStreamPlayer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
destructor TEXStreamPlayer.Destroy;
begin
inherited Destroy;
end;
{ TStreamPlayerForm }
procedure TStreamPlayerForm.FormCreate(Sender: TObject);
begin
Color := clBlack;
FWinFormat := True;
FSelCols := 2;
FSelRows := 10;
FItemIndex := -1;
FItems := TCollection.Create(TStreamPlayerItem);
LoadItems;
LoadParams;
FPlayer := TEXStreamPlayer.Create(self);
with FPlayer do
begin
Cursor := crNone;
FParent := self;
Align := alClient;
Parent := self;
end;
SetWinFormat(FWinFormat);
end;
procedure TStreamPlayerForm.FormDestroy(Sender: TObject);
begin
SaveParams;
FItems.Free;
end;
procedure TStreamPlayerForm.FormShow(Sender: TObject);
var
it: TStreamPlayerItem;
begin
if FItems.Count > 0 then
begin
it := TStreamPlayerItem(FItems.Items[FItemIndex]);
FPlayer.Url := it.FUrl;
end;
tmrLoad.enabled := true;
end;
procedure TStreamPlayerForm.tmrLoadTimer(Sender: TObject);
begin
tmrLoad.enabled := false;
StartPlay;
end;
procedure TStreamPlayerForm.StartPlay;
begin
FPlayer.Repaint; // first black
if not FPlayer.Play then
begin
FPlayer.Error := True;
end
else
FPlayer.Error := False;
end;
procedure TStreamPlayerForm.LoadItems;
var
ini: TIniFile;
s: string;
sl: TStringList;
i: integer;
it: TStreamPlayerItem;
begin
FItems.Clear;
sl := nil;
ini := nil;
try
sl := TStringList.Create;
ini := TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini'));
ini.ReadSections(sl);
for i := 0 to sl.Count - 1 do
begin
s := ini.ReadString(sl[i], 'url', '');
if s <> '' then
begin
it := TStreamPlayerItem(FItems.Add);
it.FName := WinCPToUTF8(sl[i]);
it.FUrl := WinCPToUTF8(s);
end;
end;
finally
ini.Free;
sl.Free;
end;
end;
procedure TStreamPlayerForm.AdjustStartIndex;
var
l: integer;
begin
if FSelIndex < FStartIndex then
begin
FStartIndex := (FSelIndex div FSelRows) * FSelRows;
end
else
if FSelIndex >= FStartIndex + FSelCols * FSelRows then
begin
FStartIndex := (FSelIndex div FSelRows) * FSelRows - (FSelCols - 1) * FSelRows;
l := (((FItems.Count - 1) div FSelRows) // last column
- FSelCols + 1) * FSelRows;
if l < 0 then
l := 0;
if FStartIndex > l then
FStartIndex := l;
end;
end;
procedure TStreamPlayerForm.LoadParams;
begin
if FItems.Count > 0 then
FItemIndex := 0;
end;
procedure TStreamPlayerForm.SaveParams;
begin
end;
procedure TStreamPlayerForm.SetWinFormat(win: boolean);
var
l, t, w, h: integer;
begin
if win then
begin
BorderStyle := bsSizeable;
WindowState := wsNormal;
w := 800;
h := 450;
l := (Screen.Width - w) div 2;
t := (Screen.Height - h) div 2;
SetBounds(l, t, w, h);
FPlayer.Cursor := crDefault;
end
else
begin
BorderStyle := bsNone;
WindowState := wsMaximized;
FPlayer.Cursor := crNone;
end;
end;
procedure TStreamPlayerForm.aCloseExecute(Sender: TObject);
begin
if FPlayer.Selecting then
begin
FPlayer.Selecting := False;
end
else
begin
FPlayer.Stop;
Close;
end;
end;
procedure TStreamPlayerForm.aDownExecute(Sender: TObject);
begin
if not FPlayer.Selecting or (FItems.Count = 0) or (FSelIndex < 0) or
(FSelIndex >= FItems.Count - 1) then
exit;
Inc(FSelIndex);
if FSelIndex >= FItems.Count then
FSelIndex := FItems.Count - 1;
AdjustStartIndex;
FPlayer.invalidate;
end;
procedure TStreamPlayerForm.aLeftExecute(Sender: TObject);
begin
if not FPlayer.Selecting or (FItems.Count = 0) or (FSelIndex < 0) then
exit;
Dec(FSelIndex, FSelRows);
if FSelIndex < 0 then
FSelIndex := 0;
AdjustStartIndex;
FPlayer.invalidate;
end;
procedure TStreamPlayerForm.aShowMaxExecute(Sender: TObject);
begin
if not FWinFormat then
exit;
FWinFormat := False;
FPlayer.Stop;
SetWinFormat(FWinFormat);
end;
procedure TStreamPlayerForm.aShowWinExecute(Sender: TObject);
begin
if FWinFormat then
exit;
FWinFormat := True;
FPlayer.Stop;
SetWinFormat(FWinFormat);
end;
procedure TStreamPlayerForm.aMuteExecute(Sender: TObject);
begin
FPlayer.Mute := not FPlayer.Mute;
end;
procedure TStreamPlayerForm.aPauseResumeExecute(Sender: TObject);
begin
if FPlayer.Pausing then
FPlayer.Resume
else
FPlayer.Pause;
end;
procedure TStreamPlayerForm.aRightExecute(Sender: TObject);
begin
if (not FPlayer.Selecting) or (FItems.Count = 0) or (FSelIndex < 0) or
(FSelIndex >= FItems.Count - 1) then
exit;
Inc(FSelIndex, FSelRows);
if FSelIndex >= FItems.Count then
FSelIndex := FItems.Count - 1;
AdjustStartIndex;
FPlayer.Invalidate;
end;
procedure TStreamPlayerForm.aSelectExecute(Sender: TObject);
begin
if FPlayer.Selecting then
begin
if FItemIndex <> FSelIndex then
begin
FPlayer.Stop;
FItemIndex := FSelIndex;
FPlayer.Url := TStreamPlayerItem(FItems.Items[FItemIndex]).FUrl;
if not FPlayer.Play then
begin
FPlayer.Error := True;
end
else
FPlayer.Error := False;
end;
FPlayer.Selecting := False;
end
else
if FItems.Count > 0 then
begin
FSelIndex := FItemIndex;
AdjustStartIndex;
FPlayer.Selecting := True;
end;
end;
procedure TStreamPlayerForm.aUpExecute(Sender: TObject);
begin
if not FPlayer.Selecting or (FItems.Count = 0) or (FSelIndex < 0) then
exit;
Dec(FSelIndex);
if FSelIndex < 0 then
FSelIndex := 0;
AdjustStartIndex;
FPlayer.Invalidate;
end;
end.