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 local function stripslashes(s)
31 return string.gsub(s,"\\(.)","%1")
34 --Round the number to the specified precision
35 function round(what, precision)
36 if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
39 --split text where it matches the delimiter
40 function strsplit(text, delimiter)
41 local strfind = string.find
42 local strsub = string.sub
43 local tinsert = table.insert
46 if strfind("", delimiter, 1) then -- this would result in endless loops
47 error("delimiter matches empty string!")
52 local first, last = strfind(text, delimiter, pos)
53 if first then -- found?
54 tinsert(list,i, strsub(text, pos, first-1))
57 tinsert(list,i, strsub(text, pos))
64 function round(what, precision)
65 if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
68 --main function to process commands sent with the request
70 processcommands = function ()
72 local input = _GET['input']
73 local command = _GET['command']
74 local id = tonumber(_GET['id'] or -1)
75 local val = _GET['val']
76 local options = _GET['option']
77 local band = _GET['band']
78 if type(options) ~= "table" then -- Deal with the 0 or 1 option case
82 if command == "in_play" then
84 vlc.msg.err( "<options>" )
85 for a,b in ipairs(options) do
88 vlc.msg.err( "</options>" )
90 vlc.playlist.add({{path=stripslashes(input),options=options}})
91 elseif command == "in_enqueue" then
92 vlc.playlist.enqueue({{path=stripslashes(input),options=options}})
93 elseif command == "pl_play" then
99 elseif command == "pl_pause" then
100 if vlc.playlist.status() == "stopped" then
104 vlc.playlist.goto(id)
109 elseif command == "pl_forcepause" then
110 if vlc.playlist.status() == "playing" then
113 elseif command == "pl_forceresume" then
114 if vlc.playlist.status() == "paused" then
117 elseif command == "pl_stop" then
119 elseif command == "pl_next" then
121 elseif command == "pl_previous" then
123 elseif command == "pl_delete" then
124 vlc.playlist.delete(id)
125 elseif command == "pl_empty" then
127 elseif command == "pl_sort" then
128 vlc.playlist.sort( val, id > 0 )
129 elseif command == "pl_random" then
130 vlc.playlist.random()
131 elseif command == "pl_loop" then
133 elseif command == "pl_repeat" then
134 vlc.playlist.repeat_()
135 elseif command == "pl_sd" then
136 if vlc.sd.is_loaded(val) then
141 elseif command == "fullscreen" then
142 vlc.video.fullscreen()
143 elseif command == "snapshot" then
145 elseif command == "volume" then
147 elseif command == "seek" then
149 elseif command == "key" then
150 common.hotkey("key-"..val)
151 elseif command == "audiodelay" then
152 if vlc.object.input() and val then
153 vlc.var.set(vlc.object.input(),"audio-delay",val)
155 elseif command == "rate" then
156 if vlc.object.input() and tonumber(val) >= 0 then
157 vlc.var.set(vlc.object.input(),"rate",val)
159 elseif command == "subdelay" then
160 if vlc.object.input() then
161 vlc.var.set(vlc.object.input(),"spu-delay",val)
163 elseif command == "aspectratio" then
164 if vlc.object.vout() then
165 vlc.var.set(vlc.object.vout(),"aspect-ratio",val)
167 elseif command == "preamp" then
168 vlc.equalizer.preampset(val)
169 elseif command == "equalizer" then
170 vlc.equalizer.equalizerset(band,val)
171 elseif command == "enableeq" then
172 if val == '0' then vlc.equalizer.enable(false) else vlc.equalizer.enable(true) end
182 --utilities for formatting output
184 local function xmlString(s)
185 if (type(s)=="string") then
186 return vlc.strings.convert_xml_special_chars(s)
192 local printJsonKeyValue = function (k,v,indent)
194 for i=1,indent do print(" ") end
196 print("\""..k.."\":")
199 if (type(v)=="number") then
201 elseif (type(v)=="table") then
202 if (v._array==NULL) then
204 printTableAsJson(v,indent+2)
208 printArrayAsJson(v._array,indent+2)
212 print("\""..xmlString(v).."\"")
217 printArrayAsJson = function(array,indent)
219 for i,v in ipairs(array) do
220 if not first then print(",") end
221 printJsonKeyValue(NULL,v,indent)
226 printTableAsJson = function (dict,indent)
228 for k,v in pairs(dict) do
229 if not first then print(",") end
230 printJsonKeyValue(k,v,indent)
235 local printXmlKeyValue = function (k,v,indent)
237 for i=1,indent do print(" ") end
242 if (type(v)=="table") then
243 printTableAsXml(v,indent+2)
253 printTableAsXml = function (dict,indent)
254 for k,v in pairs(dict) do
255 printXmlKeyValue(k,v,indent)
260 function logTable(t,pre)
261 local pre = pre or ""
262 for k,v in pairs(t) do
263 vlc.msg.err(pre..tostring(k).." : "..tostring(v))
264 if type(v) == "table" then
273 getplaylist = function ()
276 if _GET["search"] then
277 if _GET["search"] ~= "" then
278 _G.search_key = _GET["search"]
282 local key = vlc.strings.decode_uri(_GET["search"])
283 p = vlc.playlist.search(key)
285 p = vlc.playlist.get()
288 --logTable(p) --Uncomment to debug
293 parseplaylist = function (item)
294 if item.flags.disabled then return end
296 if (item.children) then
298 local name = vlc.strings.convert_xml_special_chars(item.name or "")
300 result["type"]="node"
301 result.id=tostring(item.id)
302 result.name=tostring(name)
303 result.ro=item.flags.ro and "ro" or "rw"
305 --store children in an array
306 --we use _array as a proxy for arrays
308 result.children._array={}
310 for _, child in ipairs(item.children) do
311 local nextChild=parseplaylist(child)
312 table.insert(result.children._array,nextChild)
318 local name, path = vlc.strings.convert_xml_special_chars(item.name or "", item.path or "")
319 local current_item = vlc.input.item()
321 -- Is the item the one currently played
322 if(current_item ~= nil) then
323 if(vlc.input.item().uri(current_item) == path) then
324 result.current = "current"
328 result["type"]="leaf"
329 result.id=tostring(item.id)
330 result.uri=tostring(path)
332 result.ro=item.flags.ro and "ro" or "rw"
333 result.duration=math.floor(item.duration)
340 playlisttable = function ()
342 local basePlaylist=getplaylist()
344 return parseplaylist(basePlaylist)
348 getstatus = function (includecategories)
351 local input = vlc.object.input()
352 local item = vlc.input.item()
353 local playlist = vlc.object.playlist()
354 local vout = vlc.object.vout()
355 local aout = vlc.object.aout()
359 --update api version when new data/commands added
361 s.version=vlc.misc.version()
362 s.volume=vlc.volume.get()
365 s.length=math.floor(vlc.var.get(input,"length"))
366 s.time=math.floor(vlc.var.get(input,"time"))
367 s.position=vlc.var.get(input,"position")
368 s.audiodelay=vlc.var.get(input,"audio-delay")
369 s.rate=vlc.var.get(input,"rate")
370 s.subtitledelay=vlc.var.get(input,"spu-delay")
381 s.fullscreen=vlc.var.get(vout,"fullscreen")
382 s.aspectratio=vlc.var.get(vout,"aspect-ratio");
383 if s.aspectratio=="" then s.aspectratio = "default" end
389 local filters=vlc.var.get(aout,"audio-filter")
390 local temp=strsplit(filters,":")
393 for i,j in pairs(temp) do
394 s.audiofilters['filter_'..id]=j
400 s.videoeffects.hue=round(vlc.config.get("hue"),2)
401 s.videoeffects.brightness=round(vlc.config.get("brightness"),2)
402 s.videoeffects.contrast=round(vlc.config.get("contrast"),2)
403 s.videoeffects.saturation=round(vlc.config.get("saturation"),2)
404 s.videoeffects.gamma=round(vlc.config.get("gamma"),2)
406 s.state=vlc.playlist.status()
407 s.random=vlc.var.get(playlist,"random")
408 s.loop=vlc.var.get(playlist,"loop")
409 s["repeat"]=vlc.var.get(playlist,"repeat")
412 s.equalizer.preamp=round(vlc.equalizer.preampget(),2)
413 s.equalizer.bands=vlc.equalizer.equalizerget()
414 if s.equalizer.bands ~= null then
415 for k,i in pairs(s.equalizer.bands) do s.equalizer.bands[k]=round(i,2) end
416 s.equalizer.presets=vlc.equalizer.presets()
419 if (includecategories and item) then
421 s.information.category={}
422 s.information.category.meta=item:metas()
424 local info = item:info()
425 for k, v in pairs(info) do
427 for k2, v2 in pairs(v) do
428 local tag = string.gsub(k2," ","_")
429 streamTable[xmlString(tag)]=xmlString(v2)
432 s.information.category[xmlString(k)]=streamTable
437 local statsdata = item:stats()
438 for k,v in pairs(statsdata) do
439 local tag = string.gsub(k,"_","")
440 s.stats[tag]=xmlString(v)