forked from konvikkor/TFFMPEG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uMediaReader.pas
159 lines (147 loc) · 5.35 KB
/
uMediaReader.pas
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
unit uMediaReader;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
(* Media modules *)
uMediaDisplay, uMediaConstant, uVideoThread, uVideoMain,
Winapi.GDIPOBJ, Winapi.GDIPAPI, OpenGL,
Vcl.ExtCtrls,Math,System.SyncObjs,System.Generics.Collections,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FFTypes, FFUtils, System.DateUtils,
libavcodec, libavcodec_avfft, libavdevice, libavfilter, libavfilter_avcodec,
libavfilter_buffersink, libavfilter_buffersrc, libavfilter_formats,
libavformat, libavformat_avio, libavformat_url, libavutil,
libavutil_audio_fifo, libavutil_avstring, libavutil_bprint, libavutil_buffer,
libavutil_channel_layout, libavutil_common, libavutil_cpu, libavutil_dict,
libavutil_display, libavutil_error, libavutil_eval, libavutil_fifo,
libavutil_file, libavutil_frame, libavutil_imgutils, libavutil_log,
libavutil_mathematics, libavutil_md5, libavutil_mem, libavutil_motion_vector,
libavutil_opt, libavutil_parseutils, libavutil_pixdesc, libavutil_pixfmt,
libavutil_rational, libavutil_samplefmt, libavutil_time, libavutil_timestamp,
libswresample, libswscale, sdl2, {SDL2_ttf,{sdl, {uResourcePaths,} System.Threading;
type
TMediaReader = class (TThread)
private
FOnIsPlayed: TOnIsPlayed;
FOnError: TOnError;
FOnReadVideoPacked: TOnReadVideoPacked;
FOnDecodeFrame: TOnDecodeVideo;
FSyncTime: TOnSyncTime;
FOnRenderVideoFrame: TOnRenderVideoFrame;
protected
FVideo:Array [0..0] of TVideoThread;
FMediaDecoder:TMediaDecoder;
procedure Execute; override;
published
constructor Create(MediaDecoder:TMediaDecoder);
Property OnSyncTime:TOnSyncTime read FSyncTime Write FSyncTime;
Property OnDecodeFrame:TOnDecodeVideo read FOnDecodeFrame Write FOnDecodeFrame;
property OnReadVideoPacked:TOnReadVideoPacked read FOnReadVideoPacked Write FOnReadVideoPacked;
property OnRenderVideoFrame:TOnRenderVideoFrame read FOnRenderVideoFrame Write FOnRenderVideoFrame;
property OnIsPlayed:TOnIsPlayed read FOnIsPlayed write FOnIsPlayed;
property OnError:TOnError Read FOnError Write FOnError;
end;
implementation
{ TMediaReader }
constructor TMediaReader.Create(MediaDecoder: TMediaDecoder);
begin
inherited Create(true);
FMediaDecoder:=MediaDecoder;
end;
procedure TMediaReader.Execute;
var Play:Boolean;
pack:PAVPacket;
V,i:Integer;
VideoBuffer:TMediaBuffer;
GlobalCS:TCriticalSection;
BRepeat:Boolean;
VideoEvent:TEvent;
begin
NameThreadForDebugging('TMediaReader');
v:=0;
VideoBuffer:=TMediaBuffer.Create; //VideoBuffer
VideoEvent:=TEvent.Create(nil,True,False,'');
GlobalCS:=TCriticalSection.Create;
for I := 0 to High(FVideo) do begin
if not Assigned(FVideo[i]) then FVideo[i]:=TVideoThread.Create(FMediaDecoder.GetVideoStrem,VideoEvent,GlobalCS);
FVideo[i].SetSDL(FMediaDecoder.Display.GetSDLInfo);
FVideo[i].FBuffer:=VideoBuffer;
FVideo[i].SetBitmap(FMediaDecoder.Display.GetCanvas);
FVideo[i].SetMediaDisplay(FMediaDecoder.Display);
FVideo[i].OnDecodeFrame:=FOnDecodeFrame;
//FVideo[i].OnRenderVideoFrame:=FOnRenderVideoFrame;
FVideo[i].OnReadVideoPacked:=FOnReadVideoPacked;
FVideo[i].OnIsPlayed:=FOnIsPlayed;
FVideo[i].OnSyncTime:=FSyncTime;
FVideo[i].tag := i;
repeat
Sleep(1);
until (Terminated) or (FVideo[i].FBuffer <> nil);
end;
VideoEvent.SetEvent;
try
repeat
Sleep(1);
//if Assigned(Application) then Application.ProcessMessages;
try
if (Assigned(FOnIsPlayed))and(Assigned(FMediaDecoder)) then begin
if Assigned(FOnIsPlayed) then FOnIsPlayed(self,Play) else Play:=False;
if Play then begin
//if FMediaDecoder.FVideoBuffer.GetSize < High(FMediaDecoder.FVideoBuffer.FBuffer)-1 then begin
if FMediaDecoder.ReadPacked(Pack) = 0 then begin
if pack.stream_index = FMediaDecoder.VideoIdx then begin
repeat
Sleep(1);
//Application.ProcessMessages;
if not Assigned(FVideo[v]) then Break;
if (Terminated) or (not Play) then Break;
if (FVideo[v].Suspended) then FVideo[v].Start;
GlobalCS.Enter;
try
BRepeat := (FVideo[v].FBuffer.GetCount < FVideo[v].FBuffer.GetSize);
finally
GlobalCS.Leave;
end;
until BRepeat;
GlobalCS.Enter;
try
if Assigned(FVideo[v]) then FVideo[v].FBuffer.WriteData(Pointer(pack));
finally
GlobalCS.Leave;
end;
Inc(V);
if v > High(FVideo) then v:=0;
//FMediaDecoder.FVideoBuffer.WriteData(Pointer(pack));
pack:=nil;
end else begin
av_packet_unref(pack);
av_packet_free(@pack);
pack:=nil;
end;
end else begin
Break;
end;
end;
end;
except
on E:Exception do begin
OutputDebugString(PWideChar('ERROR [TMediaReader]:'+E.Message));
end;
end;
until (Terminated);
finally
for I := 0 to High(FVideo) do begin
try
if FVideo[i] <> nil then begin
FVideo[i].Terminate;
//FVideo[i].WaitFor;
//FVideo[i].Destroy;
end;
except
FVideo[i]:=nil;
end;
FreeAndNil(FVideo[i]);
end;
end;
end;
end.