-
Notifications
You must be signed in to change notification settings - Fork 10
/
UAVTransportService.pas
260 lines (232 loc) · 6.97 KB
/
UAVTransportService.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
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
unit UAVTransportService;
interface
uses
SysUtils, Variants, Classes, ExtCtrls, UPNPLib_TLB;
type
TAVTransportService = class
private
AVTransportService: IUPnPService;
FstateOK:boolean;
Timer:TTimer;
Log:TStrings;
RelativeTimePosition,TransportStatus,TransportState:string;
procedure OnTimer(Sender:TObject);
function IsPlaying:boolean;
function IsPaused:boolean;
procedure GetPositionInfo(var CurrentTRack, CurrentTrackDuration,
CurrentTrackMetaData, CurrentTrackURI, RElativeTimePosition,
AbsoluteTimePosition, RelativeCounterPosition,
AbsoluteCounterPosition: string);
procedure GetTransportInfo(var TransportState, TransportStatus,
TransportPlaySpeed: string);
public
procedure SetAVTransportURI(InstanceId:integer;CurrentUri:string;CurrentURIMetaData:string);
procedure Play;
function GetState:string;
constructor Create(Log:TStrings);
procedure SetAVtransportservice(AVtransportservice:IUPnPService);
destructor Destroy; override;
procedure Pause;
function GetPosition:string;
function SetPosition(position:string):boolean;
procedure Stop;
function PlayStatus:integer; // type TPlayStatus = (PSPlay,PSPaused,PSStop);
end;
implementation
{ TAVTransportService }
uses WinSock;
constructor TAVTransportService.Create(Log:TStrings);
begin
AVTransportService:=NIL;
self.Log:=Log;
Timer:=TTimer.Create(NIL);
Timer.Interval:=2000;
Timer.OnTimer:=OnTimer;
Timer.Enabled:=true;
FStateOK:=false;
end;
procedure TAVTransportService.SetAVTransportService(AVtransportservice: IUPnPService);
begin
self.AVTransportService:=AVtransportservice;
end;
destructor TAVTransportService.Destroy;
begin
Timer.Enabled:=false;
if IsPlaying or IsPaused then Stop;
end;
function TAVTransportService.GetPosition: string;
begin
result:=RelativeTimePosition;
end;
function TAVTransportService.GetState: string;
begin
result:= RelativeTimePosition+' ' +TransportStatus + ' ' + TransportState;
end;
function TAVTransportService.IsPaused: boolean;
begin
result:= (TransportState = 'PAUSED_PLAYBACK') and (TransportStatus = 'OK');
end;
function TAVTransportService.IsPlaying: boolean;
begin
result:= ((TransportState = 'PLAYING') or (TransportState = 'TRANSITIONING')) and (TransportStatus = 'OK');
end;
procedure TAVTransportService.OnTimer(Sender: TObject);
VAR dummy:string;
begin
TransportStatus:='Not OK';
if FStateOK then
(* try
RelativeTimePosition:=AVTransportService.QueryStateVariable('RelativeTimePosition');
TransportStatus:=AVTransportService.QueryStateVariable('TransportStatus');
TransportState:=AVTransportService.QueryStateVariable('TransportState');
except end *)
begin
GetTransportInfo(TransportState,TransportStatus,dummy);
GetPositionInfo(dummy,dummy,dummy,dummy,RelativeTimePosition,dummy,dummy,dummy);
end;
end;
procedure TAVTransportService.GetTransportInfo(VAR TransportState,TransportStatus, TransportPlaySpeed:string);
VAR ovIn,ovOut : OleVariant;
begin
ovIn:=VarArrayCreate([0, 0], varVariant);
ovin[0]:=0;
ovOut:=VarArrayCreate([0, 2], varVariant);
try
AVTransportService.InvokeAction('GetTransportInfo',ovIn,ovOut);
except
exit;
end;
TransportState:=ovOut[0];
TransportStatus:=ovOut[1];
TransportPlaySpeed:=ovOut[2];
end;
procedure TAVTransportService.GetPositionInfo(VAR CurrentTRack, CurrentTrackDuration, CurrentTrackMetaData, CurrentTrackURI, RElativeTimePosition, AbsoluteTimePosition, RelativeCounterPosition, AbsoluteCounterPosition:string);
VAR ovIn,ovOut : OleVariant;
begin
ovIn:=VarArrayCreate([0, 0], varVariant);
ovin[0]:=0;
ovOut:=VarArrayCreate([0, 7], varVariant);
try
AVTransportService.InvokeAction('GetPositionInfo',ovIn,ovOut);
except
exit;
end;
CurrentTRack:=ovOut[0];
CurrentTrackDuration:=ovOut[1];
CurrentTrackMetaData:=ovOut[2];
CurrentTrackURI:=ovOut[3];
RElativeTimePosition:=ovOut[4];
AbsoluteTimePosition:=ovOut[5];
RelativeCounterPosition:=ovOut[6];
AbsoluteCounterPosition:=ovOut[7];
end;
procedure TAVTransportService.Pause;
VAR ovIn,ovOut : OleVariant;
begin
if IsPlaying then
begin
ovIn:=VarArrayCreate([0, 0], varVariant);
ovin[0]:=0;
ovOut:=null;
try
AVTransportService.InvokeAction('Pause',ovIn,ovOut);
except end;
end
else if IsPaused then Play;
end;
function TAVTransportService.SetPosition(position:string):boolean;
VAR ovIn,ovOut : OleVariant;
begin
result:=false;
if isPaused then Play;
if not IsPlaying then exit;
ovIn:=VarArrayCreate([0, 2], varVariant);
ovin[0]:=0;
ovin[1]:='REL_TIME';
ovin[2]:=position;
ovOut:=null;
try
AVTransportService.InvokeAction('Seek',ovIn,ovOut);
result:=true;
except end;
end;
procedure TAVTransportService.Stop;
VAR ovIn,ovOut : OleVariant;
begin
ovIn:=VarArrayCreate([0, 0], varVariant);
ovin[0]:=0;
ovOut:=null;
try
AVTransportService.InvokeAction('Stop',ovIn,ovOut);
except end;
end;
procedure TAVTransportService.Play;
VAR ovIn,ovOut : OleVariant;
begin
ovIn := VarArrayCreate([0, 1], varVariant);
ovIn[0]:= 0;
ovIn[1]:= '1';
ovOut := null ;
try
AVTransportService.InvokeAction('Play',ovIn,ovOut);
Log.Add('Play: OK');
FStateOK:=true;
except
Log.Add('Error in Play');
FStateOK:=false;
end;
end;
function TAVTransportService.PlayStatus: integer; // type TPlayStatus = (PSPlay,PSPaused,PSStop);
begin
if TransportStatus='Not OK' then result:=3
else result:=2;
if IsPaused then result:=1;
if IsPlaying then result:=0;
end;
// ip code code copied from http://www.delphigeist.com/2010/03/how-to-get-ip-address.html
function GetIPAddress: Integer;
var
Buffer: array[0..255] of AnsiChar;
RemoteHost: PHostEnt;
begin
Winsock.GetHostName(@Buffer, 255);
RemoteHost := Winsock.GetHostByName(Buffer);
if RemoteHost = nil then
Result := winsock.htonl($07000001) { 127.0.0.1 }
else
Result := longint(pointer(RemoteHost^.h_addr_list^)^);
Result := Winsock.ntohl(Result);
end;// function GetIPAddress: Integer;
function GetIPAddressAsString: String;
var
tempAddress: Integer;
Buffer: array[0..3] of Byte absolute tempAddress;
begin
tempAddress := GetIPAddress;
Result := Format('%d.%d.%d.%d', [Buffer[3], Buffer[2], Buffer[1], Buffer[0]]);
end;// function GetIPAddressAsString: String;
procedure TAVTransportService.SetAVTransportURI(InstanceId: integer; CurrentUri, CurrentURIMetaData: string);
VAR ovIn,ovOut : OleVariant;
procedure replacetext(from,too:string;VAR what:string);
VAR s:string;
p:integer;
begin
p:=pos(from,what);
s:=Copy(what,1,p-1)+too+Copy(what,p+length(from));
what:=s;
end;
begin
replaceText('127.0.0.1',GetIPAddressAsString,CurrentURI);
ovIn := VarArrayCreate([0, 2], varVariant);
ovIn[0]:= 0;
ovIn[1]:= CurrentURI;
ovIn[2]:= CurrentURIMetaData;
ovOut := null;
try
AVTransportService.InvokeAction('SetAVTransportURI',ovIn,ovOut);
Log.Add('SetAVTransportURI: OK');
except
Log.Add('Error in SetAVTransportURI');
end;
end;
end.