1 --[==========================================================================[
2 httprequests.lua: code for processing httprequests commands and output
3 --[==========================================================================[
4 Copyright (C) 2007 the VideoLAN team
7 Authors: Antoine Cellerier <dionoea at videolan dot org>
8 Rob Jonson <rob at hobbyistsoftware.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 --]==========================================================================]
25 module("httprequests",package.seeall)
30 --Round the number to the specified precision
31 function round(what, precision)
32 if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
35 --split text where it matches the delimiter
36 function strsplit(text, delimiter)
37 local strfind = string.find
38 local strsub = string.sub
39 local tinsert = table.insert
42 if strfind("", delimiter, 1) then -- this would result in endless loops
43 error("delimiter matches empty string!")
48 local first, last = strfind(text, delimiter, pos)
49 if first then -- found?
50 tinsert(list,i, strsub(text, pos, first-1))
53 tinsert(list,i, strsub(text, pos))
60 function round(what, precision)
61 if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
64 --main function to process commands sent with the request
66 processcommands = function ()
68 local input = _GET['input']
69 local command = _GET['command']
70 local id = tonumber(_GET['id'] or -1)
71 local val = _GET['val']
72 local options = _GET['option']
73 local band = _GET['band']
74 if type(options) ~= "table" then -- Deal with the 0 or 1 option case
78 if command == "in_play" then
80 vlc.msg.err( "<options>" )
81 for a,b in ipairs(options) do
84 vlc.msg.err( "</options>" )
86 vlc.playlist.add({{path=vlc.strings.make_uri(input),options=options}})
87 elseif command == "addsubtitle" then
88 vlc.input.add_subtitle (vlc.strings.make_uri(val))
89 elseif command == "in_enqueue" then
90 vlc.playlist.enqueue({{path=vlc.strings.make_uri(input),options=options}})
91 elseif command == "pl_play" then
97 elseif command == "pl_pause" then
98 if vlc.playlist.status() == "stopped" then
102 vlc.playlist.goto(id)
107 elseif command == "pl_forcepause" then
108 if vlc.playlist.status() == "playing" then
111 elseif command == "pl_forceresume" then
112 if vlc.playlist.status() == "paused" then
115 elseif command == "pl_stop" then
117 elseif command == "pl_next" then
119 elseif command == "pl_previous" then
121 elseif command == "pl_delete" then
122 vlc.playlist.delete(id)
123 elseif command == "pl_empty" then
125 elseif command == "pl_sort" then
126 vlc.playlist.sort( val, id > 0 )
127 elseif command == "pl_random" then
128 vlc.playlist.random()
129 elseif command == "pl_loop" then
130 --if loop is set true, then repeat needs to be set false
131 if vlc.playlist.loop() then
132 vlc.playlist.repeat_("off")
134 elseif command == "pl_repeat" then
135 --if repeat is set true, then loop needs to be set false
136 if vlc.playlist.repeat_() then
137 vlc.playlist.loop("off")
139 elseif command == "pl_sd" then
140 if vlc.sd.is_loaded(val) then
145 elseif command == "fullscreen" then
146 vlc.video.fullscreen()
147 elseif command == "snapshot" then
149 elseif command == "volume" then
151 elseif command == "seek" then
153 elseif command == "key" then
154 common.hotkey("key-"..val)
155 elseif command == "audiodelay" then
156 if vlc.object.input() and val then
157 vlc.var.set(vlc.object.input(),"audio-delay",val)
159 elseif command == "rate" then
160 if vlc.object.input() and tonumber(val) >= 0 then
161 vlc.var.set(vlc.object.input(),"rate",val)
163 elseif command == "subdelay" then
164 if vlc.object.input() then
165 vlc.var.set(vlc.object.input(),"spu-delay",val)
167 elseif command == "aspectratio" then
168 if vlc.object.vout() then
169 vlc.var.set(vlc.object.vout(),"aspect-ratio",val)
171 elseif command == "preamp" then
172 vlc.equalizer.preampset(val)
173 elseif command == "equalizer" then
174 vlc.equalizer.equalizerset(band,val)
175 elseif command == "enableeq" then
176 if val == '0' then vlc.equalizer.enable(false) else vlc.equalizer.enable(true) end
177 elseif command == "setpreset" then
178 vlc.equalizer.setpreset(val)
188 --utilities for formatting output
190 function xmlString(s)
191 if (type(s)=="string") then
192 return vlc.strings.convert_xml_special_chars(s)
198 local printJsonKeyValue = function (k,v,indent)
200 for i=1,indent do print(" ") end
202 print("\""..k.."\":")
205 if (type(v)=="number") then
207 elseif (type(v)=="table") then
208 if (v._array==NULL) then
210 printTableAsJson(v,indent+2)
214 printArrayAsJson(v._array,indent+2)
218 print("\""..xmlString(v).."\"")
223 printArrayAsJson = function(array,indent)
225 for i,v in ipairs(array) do
226 if not first then print(",") end
227 printJsonKeyValue(NULL,v,indent)
232 printTableAsJson = function (dict,indent)
234 for k,v in pairs(dict) do
235 if not first then print(",") end
236 printJsonKeyValue(k,v,indent)
241 local printXmlKeyValue = function (k,v,indent)
243 for i=1,indent do print(" ") end
248 if (type(v)=="table") then
249 printTableAsXml(v,indent+2)
255 print("</"..xmlString(k)..">")
259 printTableAsXml = function (dict,indent)
260 for k,v in pairs(dict) do
261 printXmlKeyValue(k,v,indent)
266 function logTable(t,pre)
267 local pre = pre or ""
268 for k,v in pairs(t) do
269 vlc.msg.err(pre..tostring(k).." : "..tostring(v))
270 if type(v) == "table" then
279 getplaylist = function ()
282 if _GET["search"] then
283 if _GET["search"] ~= "" then
284 _G.search_key = _GET["search"]
288 local key = vlc.strings.decode_uri(_GET["search"])
289 p = vlc.playlist.search(key)
291 p = vlc.playlist.get()
294 --logTable(p) --Uncomment to debug
299 parseplaylist = function (item)
300 if item.flags.disabled then return end
302 if (item.children) then
304 local name = (item.name or "")
306 result["type"]="node"
307 result.id=tostring(item.id)
308 result.name=tostring(name)
309 result.ro=item.flags.ro and "ro" or "rw"
311 --store children in an array
312 --we use _array as a proxy for arrays
314 result.children._array={}
316 for _, child in ipairs(item.children) do
317 local nextChild=parseplaylist(child)
318 table.insert(result.children._array,nextChild)
324 local name, path = item.name or ""
325 local path = item.path or ""
326 local current_item = vlc.input.item()
328 -- Is the item the one currently played
329 if(current_item ~= nil) then
330 if(vlc.input.item().uri(current_item) == path) then
331 result.current = "current"
335 result["type"]="leaf"
336 result.id=tostring(item.id)
337 result.uri=tostring(path)
339 result.ro=item.flags.ro and "ro" or "rw"
340 result.duration=math.floor(item.duration)
347 playlisttable = function ()
349 local basePlaylist=getplaylist()
351 return parseplaylist(basePlaylist)
354 getbrowsetable = function ()
357 local uri = _GET["uri"]
358 --uri takes precedence, but fall back to dir
360 dir = vlc.strings.make_path(uri)
366 --paths are returned as an array of elements
368 result.element._array={}
371 if dir == "~" then dir = vlc.misc.homedir() end
372 dir = common.realpath(dir.."/")
373 local d = vlc.net.opendir(dir)
376 for _,f in pairs(d) do
377 if f == ".." or not string.match(f,"^%.") then
378 local df = common.realpath(dir..f)
379 local s = vlc.net.stat(df)
380 local path, name = df, f
383 for k,v in pairs(s) do
389 local uri=vlc.strings.make_uri(df)
390 --windows paths are returned with / separators, but make_uri expects \ for windows and returns nil
392 --convert failed path to windows format and try again
393 path=string.gsub(path,"/","\\")
394 uri=vlc.strings.make_uri(df)
398 table.insert(result.element._array,element)
408 getstatus = function (includecategories)
411 local input = vlc.object.input()
412 local item = vlc.input.item()
413 local playlist = vlc.object.playlist()
414 local vout = vlc.object.vout()
415 local aout = vlc.object.aout()
419 --update api version when new data/commands added
421 s.version=vlc.misc.version()
422 s.volume=vlc.volume.get()
425 s.length=math.floor(vlc.var.get(input,"length"))
426 s.time=math.floor(vlc.var.get(input,"time"))
427 s.position=vlc.var.get(input,"position")
428 s.audiodelay=vlc.var.get(input,"audio-delay")
429 s.rate=vlc.var.get(input,"rate")
430 s.subtitledelay=vlc.var.get(input,"spu-delay")
441 s.fullscreen=vlc.var.get(vout,"fullscreen")
442 s.aspectratio=vlc.var.get(vout,"aspect-ratio");
443 if s.aspectratio=="" then s.aspectratio = "default" end
449 local filters=vlc.var.get(aout,"audio-filter")
450 local temp=strsplit(filters,":")
453 for i,j in pairs(temp) do
454 s.audiofilters['filter_'..id]=j
460 s.videoeffects.hue=round(vlc.config.get("hue"),2)
461 s.videoeffects.brightness=round(vlc.config.get("brightness"),2)
462 s.videoeffects.contrast=round(vlc.config.get("contrast"),2)
463 s.videoeffects.saturation=round(vlc.config.get("saturation"),2)
464 s.videoeffects.gamma=round(vlc.config.get("gamma"),2)
466 s.state=vlc.playlist.status()
467 s.random=vlc.var.get(playlist,"random")
468 s.loop=vlc.var.get(playlist,"loop")
469 s["repeat"]=vlc.var.get(playlist,"repeat")
472 s.equalizer.preamp=round(vlc.equalizer.preampget(),2)
473 s.equalizer.bands=vlc.equalizer.equalizerget()
474 if s.equalizer.bands ~= null then
475 for k,i in pairs(s.equalizer.bands) do s.equalizer.bands[k]=round(i,2) end
476 s.equalizer.presets=vlc.equalizer.presets()
479 if (includecategories and item) then
481 s.information.category={}
482 s.information.category.meta=item:metas()
484 local info = item:info()
485 for k, v in pairs(info) do
487 for k2, v2 in pairs(v) do
488 local tag = string.gsub(k2," ","_")
492 s.information.category[k]=streamTable
497 local statsdata = item:stats()
498 for k,v in pairs(statsdata) do
499 local tag = string.gsub(k,"_","")