-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVLC.m
305 lines (261 loc) · 7.64 KB
/
VLC.m
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
%
% VLC.m
%
% Created by Léa Strobino.
% Copyright 2018. All rights reserved.
%
classdef VLC < matlab.mixin.SetGet
properties (Constant)
Port = 4212;
end
properties (SetAccess = private)
Version
Status
Current
Playlist
end
properties
Loop
Repeat
Random
Fullscreen
Rate
Volume
end
properties (Access = private)
requestURL
password = 'QvGkByH97AOxRvhP';
end
methods
function this = VLC()
persistent retry %#ok<*NASGU>
this.requestURL = sprintf('http://127.0.0.1:%d/request.json',this.Port);
try
this.set('Loop','off','Repeat','off','Random','off','Fullscreen','off','Rate',1);
catch e
if isempty(retry) && ~isempty([strfind(e.message,'java.net.ConnectException') strfind(e.message,'java.net.SocketTimeoutException')])
args = sprintf('--extraintf http --http-host 127.0.0.1 --http-port %d --http-password "%s" --http-src "%s"',...
this.Port,this.password,fileparts(mfilename('fullpath')));
if ispc
if exist('C:\Program Files (x86)\VideoLAN\VLC\vlc.exe','file')
[~,~] = dos(['"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" ' args ' &']);
else
[~,~] = dos(['"C:\Program Files\VideoLAN\VLC\vlc.exe" ' args ' &']);
end
elseif ismac
[~,~] = unix(['open -a VLC --args ' args ' 2> /dev/null']);
end
pause(3);
retry = true;
this = VLC();
else
retry = [];
error('VLC:CommunicationError','Unable to communicate with VLC.');
end
end
retry = [];
end
% add 'file' to the playlist
function add(this,file)
this.request(['c=enqueue&v=' this.urlencode(this.getFile(file))]);
end
% resume playback or play 'file'
function play(this,file)
if nargin > 1
this.request(['c=add&v=' this.urlencode(this.getFile(file))]);
else
this.request('c=play');
end
end
% pause playback
function pause(this)
this.request('c=pause');
end
% stop playback
function stop(this)
this.request('c=stop');
end
% play next track
function next(this)
this.request('c=next');
end
% play previous track
function prev(this)
this.request('c=prev');
end
% seek to position (in seconds)
function seek(this,position)
this.request(sprintf('c=seek&v=%.0f',position));
end
% move item ID 'x' in the playlist after item ID 'y'
function move(this,x,y)
id = [this.Playlist.Content.ID];
if any(x == id) && any(y == id)
this.request(sprintf('c=move&v=%.0f,%.0f',x,y));
else
error('VLC:InvalidTrackID','Invalid track ID.');
end
end
% remove item ID 'x' from the playlist
function remove(this,x)
if any(x == [this.Playlist.Content.ID])
this.request(sprintf('c=delete&v=%.0f',x));
else
error('VLC:InvalidTrackID','Invalid track ID.');
end
end
% empty the playlist
function clear(this)
this.request('c=clear');
end
% quit VLC and delete object
function quit(this)
this.request('c=quit');
delete(this);
end
function v = get.Version(this)
v = this.getStatus().version;
end
function s = get.Status(this)
s = this.getStatus().status;
end
function c = get.Current(this)
r = this.getStatus();
if isfield(r,'current')
c = r.current;
c.Meta = struct();
m = fieldnames(r.current.Meta);
for i = 1:length(m)
n = lower(m{i});
j = find(n == ' ' | n == '_');
try %#ok<TRYNC>
n([1 j+1]) = upper(n([1 j+1]));
end
n(j) = [];
c.Meta.(n) = r.current.Meta.(m{i});
end
else
c = [];
end
end
function p = get.Playlist(this)
p = this.getStatus().playlist;
end
function v = get.Loop(this)
v = this.getValue('loop');
end
function set.Loop(this,loop)
this.setValue('loop',loop);
end
function v = get.Repeat(this)
v = this.getValue('repeat');
end
function set.Repeat(this,repeat)
this.setValue('repeat',repeat);
end
function v = get.Random(this)
v = this.getValue('random');
end
function set.Random(this,random)
this.setValue('random',random);
end
function v = get.Fullscreen(this)
v = this.getValue('fullscreen');
end
function set.Fullscreen(this,fullscreen)
this.setValue('fullscreen',fullscreen);
end
function v = get.Rate(this)
v = this.getStatus().rate;
end
function set.Rate(this,rate)
this.request(sprintf('c=rate&v=%.6f',rate));
end
function v = get.Volume(this)
v = this.getStatus().volume;
end
function set.Volume(this,volume)
this.request(sprintf('c=volume&v=%.0f',volume));
end
end
methods (Access = private)
function f = getFile(~,f)
[p,n,e] = fileparts(f);
if isempty(p)
p = cd();
end
f = fullfile(p,[n e]);
h = fopen(f);
if h > 0
fclose(h);
else
error('VLC:FileNotFound','"%s": no such file.',f);
end
end
function s = getStatus(this)
persistent t json
if isempty(t) || toc(t) > .5
json = json_decode(this.request());
t = tic();
end
s = json;
end
function v = getValue(this,c)
if this.getStatus().(c)
v = 'on';
else
v = 'off';
end
end
function setValue(this,c,v)
if strcmpi(v,'on')
this.request(['c=' c '&v=on']);
elseif strcmpi(v,'off')
this.request(['c=' c '&v=off']);
else
throwAsCaller(MException('MATLAB:datatypes:InvalidEnumValueFor',...
'Invalid enum value. Use one of these values: ''on'' | ''off''.'));
end
end
function s = request(this,r)
persistent authorization isc
if isempty(authorization)
authorization = ['Basic ' org.apache.commons.codec.binary.Base64.encodeBase64(uint8([':' this.password]))'];
isc = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier();
end
try
if nargin == 2
url = java.net.URL([this.requestURL '?' r]);
else
url = java.net.URL(this.requestURL);
end
c = url.openConnection();
c.setConnectTimeout(50);
c.setReadTimeout(500);
c.setRequestProperty('Authorization',authorization);
if nargin == 2
c.connect();
c.getContentLength();
else
i = c.getInputStream();
o = java.io.ByteArrayOutputStream();
isc.copyStream(i,o);
i.close();
o.close();
s = native2unicode(typecast(o.toByteArray()','uint8'),'UTF-8');
end
catch e
if strcmp(e.identifier,'MATLAB:Java:GenericException')
r = regexp(e.message,'\n([^\n]*)\n','tokens','once');
error('VLC:SocketException',r{1});
else
rethrow(e);
end
end
end
function s = urlencode(~,s)
s = char(java.net.URLEncoder.encode(s,'UTF-8'));
s = strrep(s,'+','%20');
end
end
end