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 --main function to process commands sent with the request
36 processcommands = function ()
38 local input = _GET['input']
39 local command = _GET['command']
40 local id = tonumber(_GET['id'] or -1)
41 local val = _GET['val']
42 local options = _GET['option']
43 if type(options) ~= "table" then -- Deal with the 0 or 1 option case
47 if command == "in_play" then
49 vlc.msg.err( "<options>" )
50 for a,b in ipairs(options) do
53 vlc.msg.err( "</options>" )
55 vlc.playlist.add({{path=stripslashes(input),options=options}})
56 elseif command == "in_enqueue" then
57 vlc.playlist.enqueue({{path=stripslashes(input),options=options}})
58 elseif command == "pl_play" then
64 elseif command == "pl_pause" then
65 if vlc.playlist.status() == "stopped" then
74 elseif command == "pl_forcepause" then
75 if vlc.playlist.status() == "playing" then
78 elseif command == "pl_forceresume" then
79 if vlc.playlist.status() == "paused" then
82 elseif command == "pl_stop" then
84 elseif command == "pl_next" then
86 elseif command == "pl_previous" then
88 elseif command == "pl_delete" then
89 vlc.playlist.delete(id)
90 elseif command == "pl_empty" then
92 elseif command == "pl_sort" then
93 vlc.playlist.sort( val, id > 0 )
94 elseif command == "pl_random" then
96 elseif command == "pl_loop" then
98 elseif command == "pl_repeat" then
99 vlc.playlist.repeat_()
100 elseif command == "pl_sd" then
101 if vlc.sd.is_loaded(val) then
106 elseif command == "fullscreen" then
107 vlc.video.fullscreen()
108 elseif command == "snapshot" then
110 elseif command == "volume" then
112 elseif command == "seek" then
114 elseif command == "key" then
115 common.hotkey("key-"..val)
116 elseif command == "audiodelay" then
117 if vlc.object.input() and val then
118 vlc.var.set(vlc.object.input(),"audio-delay",val)
120 elseif command == "rate" then
121 if vlc.object.input() and tonumber(val) >= 0 then
122 vlc.var.set(vlc.object.input(),"rate",val)
124 elseif command == "subdelay" then
125 if vlc.object.input() then
126 vlc.var.set(vlc.object.input(),"spu-delay",val)
137 --utilities for formatting output
139 local function xmlString(s)
140 if (type(s)=="string") then
141 return vlc.strings.convert_xml_special_chars(s)
147 local printJsonKeyValue = function (k,v,indent)
149 for i=1,indent do print(" ") end
151 print("\""..k.."\":")
154 if (type(v)=="number") then
156 elseif (type(v)=="table") then
157 if (v._array==NULL) then
159 printTableAsJson(v,indent+2)
163 printArrayAsJson(v._array,indent+2)
167 print("\""..xmlString(v).."\"")
172 printArrayAsJson = function(array,indent)
174 for i,v in ipairs(array) do
175 if not first then print(",") end
176 printJsonKeyValue(NULL,v,indent)
181 printTableAsJson = function (dict,indent)
183 for k,v in pairs(dict) do
184 if not first then print(",") end
185 printJsonKeyValue(k,v,indent)
190 local printXmlKeyValue = function (k,v,indent)
192 for i=1,indent do print(" ") end
197 if (type(v)=="table") then
198 printTableAsXml(v,indent+2)
208 printTableAsXml = function (dict,indent)
209 for k,v in pairs(dict) do
210 printXmlKeyValue(k,v,indent)
215 function logTable(t,pre)
216 local pre = pre or ""
217 for k,v in pairs(t) do
218 vlc.msg.err(pre..tostring(k).." : "..tostring(v))
219 if type(v) == "table" then
228 getplaylist = function ()
231 if _GET["search"] then
232 if _GET["search"] ~= "" then
233 _G.search_key = _GET["search"]
237 local key = vlc.strings.decode_uri(_GET["search"])
238 p = vlc.playlist.search(key)
240 p = vlc.playlist.get()
243 --logTable(p) --Uncomment to debug
248 parseplaylist = function (item)
249 if item.flags.disabled then return end
251 if (item.children) then
253 local name = vlc.strings.convert_xml_special_chars(item.name or "")
255 result["type"]="node"
256 result.id=tostring(item.id)
257 result.name=tostring(name)
258 result.ro=item.flags.ro and "ro" or "rw"
260 --store children in an array
261 --we use _array as a proxy for arrays
263 result.children._array={}
265 for _, child in ipairs(item.children) do
266 local nextChild=parseplaylist(child)
267 table.insert(result.children._array,nextChild)
273 local name, path = vlc.strings.convert_xml_special_chars(item.name or "", item.path or "")
274 local current_item = vlc.input.item()
276 -- Is the item the one currently played
277 if(current_item ~= nil) then
278 if(vlc.input.item().uri(current_item) == path) then
279 result.current = "current"
283 result["type"]="leaf"
284 result.id=tostring(item.id)
285 result.uri=tostring(path)
287 result.ro=item.flags.ro and "ro" or "rw"
288 result.duration=math.floor(item.duration)
295 playlisttable = function ()
297 local basePlaylist=getplaylist()
299 return parseplaylist(basePlaylist)
303 getstatus = function (includecategories)
306 local input = vlc.object.input()
307 local item = vlc.input.item()
308 local playlist = vlc.object.playlist()
309 local vout = input and vlc.object.find(input,'vout','child')
313 --update api version when new data/commands added
315 s.version=vlc.misc.version()
316 s.volume=vlc.volume.get()
319 s.length=math.floor(vlc.var.get(input,"length"))
320 s.time=math.floor(vlc.var.get(input,"time"))
321 s.position=vlc.var.get(input,"position")
322 s.audiodelay=vlc.var.get(input,"audio-delay")
323 s.rate=vlc.var.get(input,"rate")
324 s.subtitledelay=vlc.var.get(input,"spu-delay")
335 s.fullscreen=vlc.var.get(vout,"fullscreen")
340 s.state=vlc.playlist.status()
341 s.random=vlc.var.get(playlist,"random")
342 s.loop=vlc.var.get(playlist,"loop")
343 s["repeat"]=vlc.var.get(playlist,"repeat")
345 if (includecategories and item) then
347 s.information.category={}
348 s.information.category.meta=item:metas()
350 local info = item:info()
351 for k, v in pairs(info) do
353 for k2, v2 in pairs(v) do
354 local tag = string.gsub(k2," ","_")
355 streamTable[xmlString(tag)]=xmlString(v2)
358 s.information.category[xmlString(k)]=streamTable
363 local statsdata = item:stats()
364 for k,v in pairs(statsdata) do
365 local tag = string.gsub(k,"_","")
366 s.stats[tag]=xmlString(v)