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)
27 local common = require ("common")
28 local dkjson = require ("dkjson")
32 --Round the number to the specified precision
33 function round(what, precision)
34 if type(what) == "string" then
35 what = common.us_tonumber(what)
37 if type(what) == "number" then
38 return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
43 --split text where it matches the delimiter
44 function strsplit(text, delimiter)
45 local strfind = string.find
46 local strsub = string.sub
47 local tinsert = table.insert
50 if strfind("", delimiter, 1) then -- this would result in endless loops
51 error("delimiter matches empty string!")
55 local first, last = strfind(text, delimiter, pos)
56 if first then -- found?
57 tinsert(list,i, strsub(text, pos, first-1))
60 tinsert(list,i, strsub(text, pos))
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 = tonumber(_GET['band'])
78 local name = _GET['name']
79 local duration = tonumber(_GET['duration'])
80 if type(options) ~= "table" then -- Deal with the 0 or 1 option case
84 if command == "in_play" then
86 vlc.msg.err( "<options>" )
87 for a,b in ipairs(options) do
90 vlc.msg.err( "</options>" )
92 vlc.playlist.add({{path=vlc.strings.make_uri(input),options=options,name=name,duration=duration}})
93 elseif command == "addsubtitle" then
94 vlc.player.add_subtitle(val)
95 elseif command == "in_enqueue" then
96 vlc.playlist.enqueue({{path=vlc.strings.make_uri(input),options=options,name=name,duration=duration}})
97 elseif command == "pl_play" then
101 vlc.playlist.gotoitem(id)
103 elseif command == "pl_pause" then
104 if vlc.playlist.status() == "stopped" then
108 vlc.playlist.gotoitem(id)
113 elseif command == "pl_forcepause" then
114 if vlc.playlist.status() == "playing" then
117 elseif command == "pl_forceresume" then
118 if vlc.playlist.status() == "paused" then
121 elseif command == "pl_stop" then
123 elseif command == "pl_next" then
125 elseif command == "pl_previous" then
127 elseif command == "pl_delete" then
128 vlc.playlist.delete(id)
129 elseif command == "pl_empty" then
131 elseif command == "pl_sort" then
132 vlc.playlist.sort( val, id > 0 )
133 elseif command == "pl_random" then
134 vlc.playlist.random()
135 elseif command == "pl_loop" then
137 elseif command == "pl_repeat" then
138 vlc.playlist.repeat_()
139 elseif command == "fullscreen" then
140 if vlc.object.vout() then
141 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 val = common.us_tonumber(val)
153 vlc.player.set_audio_delay(val)
154 elseif command == "rate" then
155 val = common.us_tonumber(val)
157 vlc.player.set_rate(val)
159 elseif command == "subdelay" then
160 val = common.us_tonumber(val)
161 vlc.player.set_subtitle_delay(val)
162 elseif command == "aspectratio" then
163 if vlc.object.vout() then
164 vlc.var.set(vlc.object.vout(),"aspect-ratio",val)
166 elseif command == "preamp" then
167 val = common.us_tonumber(val)
168 vlc.equalizer.preampset(val)
169 elseif command == "equalizer" then
170 val = common.us_tonumber(val)
171 vlc.equalizer.equalizerset(band,val)
172 elseif command == "enableeq" then
173 if val == '0' then vlc.equalizer.enable(false) else vlc.equalizer.enable(true) end
174 elseif command == "setpreset" then
175 vlc.equalizer.setpreset(val)
176 elseif command == "title" then
177 vlc.player.title_goto(val)
178 elseif command == "chapter" then
179 vlc.player.chapter_goto(val)
180 elseif command == "audio_track" then
181 vlc.player.toggle_audio_track(val)
182 elseif command == "video_track" then
183 vlc.player.toggle_video_track(val)
184 elseif command == "subtitle_track" then
185 vlc.player.toggle_spu_track(val)
186 elseif command == "set_renderer" then
187 local rd = get_renderer_discovery()
192 elseif command == "unset_renderer" then
203 --utilities for formatting output
205 function xmlString(s)
206 if (type(s)=="string") then
207 return vlc.strings.convert_xml_special_chars(s)
208 elseif (type(s)=="number") then
209 return common.us_tostring(s)
215 --dkjson outputs numbered tables as arrays
216 --so we don't need the array indicators
217 function removeArrayIndicators(dict)
220 for k,v in pairs(dict) do
221 if (type(v)=="table") then
222 local arrayEntry=v._array
227 dict[k]=removeArrayIndicators(v)
234 printTableAsJson = function (dict)
235 dict=removeArrayIndicators(dict)
237 local output=dkjson.encode (dict, { indent = true })
241 local printXmlKeyValue = function (k,v,indent)
243 for i=1,indent do print(" ") end
245 --XML element naming rules: this is a bit more conservative
246 --than it strictly needs to be
247 if not string.match(k, "^[a-zA-Z_][a-zA-Z0-9_%-%.]*$")
248 or string.match(k, "^[xX][mM][lL]") then
249 print('<element name="'..xmlString(k)..'">')
256 if (type(v)=="table") then
257 printTableAsXml(v,indent+2)
267 printTableAsXml = function (dict,indent)
268 for k,v in pairs(dict) do
269 printXmlKeyValue(k,v,indent)
274 function logTable(t,pre)
275 local pre = pre or ""
276 for k,v in pairs(t) do
277 vlc.msg.err(pre..tostring(k).." : "..tostring(v))
278 if type(v) == "table" then
287 getplaylist = function ()
289 if _GET["search"] then
290 if _GET["search"] ~= "" then
291 _G.search_key = _GET["search"]
295 local key = vlc.strings.decode_uri(_GET["search"])
296 p = vlc.playlist.search(key)
298 p = vlc.playlist.list()
301 --logTable(p) --Uncomment to debug
306 parseplaylist = function (list)
308 local current_item_id = vlc.playlist.current()
309 for i, item in ipairs(list) do
312 local name, path = item.name or ""
313 local path = item.path or ""
315 -- Is the item the one currently played
316 if(current_item_id ~= nil) then
317 if(current_item_id == item.id) then
318 result.current = "current"
321 result["type"]="leaf"
322 result.id=tostring(item.id)
323 result.uri=tostring(path)
325 result.duration=math.floor(item.duration)
332 playlisttable = function ()
334 local basePlaylist=getplaylist()
336 return parseplaylist(basePlaylist)
339 getbrowsetable = function ()
341 --paths are returned as an array of elements
342 local result = { element = { _array = {} } }
345 --uri takes precedence, but fall back to dir
347 if _GET["uri"] == "file://~" then
350 local uri = string.gsub(_GET["uri"], "[?#].*$", "")
351 if not string.match(uri, "/$") then
354 dir = vlc.strings.make_path(common.realpath(uri))
356 elseif _GET["dir"] then
359 -- "" dir means listing all drive letters e.g. "A:\", "C:\"...
360 --however the opendir() API won't resolve "X:\.." to that behavior,
361 --so we offer this resolution as "backwards compatibility"
362 if string.match(dir, '^[a-zA-Z]:[\\/]*%.%.$') or
363 string.match(dir, '^[a-zA-Z]:[\\/]*%.%.[\\/]') then
367 if dir ~= "" and dir ~= "~" then
368 dir = dir.."/" --luckily Windows accepts '/' as '\'
376 dir = vlc.config.homedir().."/"
379 local d = vlc.net.opendir(dir)
382 --FIXME: this is the wrong place to do this, but this still offers
383 --some useful mitigation: see #25021
384 if #d == 0 and dir ~= "" then
385 table.insert(d, "..")
388 for _,f in pairs(d) do
389 if f == ".." or not string.match(f,"^%.") then
393 local s = vlc.net.stat(path)
395 for k,v in pairs(s) do
399 element["type"]="unknown"
404 local uri=vlc.strings.make_uri(path)
406 element["uri"]=common.realpath(uri)
409 table.insert(result.element._array,element)
417 getstatus = function (includecategories)
420 local item = vlc.player.item()
421 local playlist = vlc.object.playlist()
422 local vout = vlc.object.vout()
423 local aout = vlc.object.aout()
427 --update api version when new data/commands added
429 s.version=vlc.misc.version()
430 s.volume=vlc.volume.get()
432 s.time = vlc.player.get_time() / 1000000
433 s.position = vlc.player.get_position()
434 s.currentplid = vlc.playlist.current()
435 s.audiodelay = vlc.player.get_audio_delay()
436 s.rate = vlc.player.get_rate()
437 s.subtitledelay = vlc.player.get_subtitle_delay()
440 s.length=math.floor(item:duration())
446 s.fullscreen=vlc.var.get(vout,"fullscreen")
447 s.aspectratio=vlc.var.get(vout,"aspect-ratio");
448 if s.aspectratio=="" then s.aspectratio = "default" end
454 local filters=vlc.var.get(aout,"audio-filter")
455 local temp=strsplit(filters,":")
458 for i,j in pairs(temp) do
459 s.audiofilters['filter_'..id]=j
465 s.videoeffects.hue=round(vlc.config.get("hue"),2)
466 s.videoeffects.brightness=round(vlc.config.get("brightness"),2)
467 s.videoeffects.contrast=round(vlc.config.get("contrast"),2)
468 s.videoeffects.saturation=round(vlc.config.get("saturation"),2)
469 s.videoeffects.gamma=round(vlc.config.get("gamma"),2)
471 s.state=vlc.playlist.status()
472 s.random = vlc.playlist.get_random()
473 s.loop = vlc.playlist.get_loop()
474 s["repeat"] = vlc.playlist.get_repeat()
477 s.equalizer.preamp=round(vlc.equalizer.preampget(),2)
478 s.equalizer.bands=vlc.equalizer.equalizerget()
479 if s.equalizer.bands ~= null then
480 for k,i in pairs(s.equalizer.bands) do s.equalizer.bands[k]=round(i,2) end
481 s.equalizer.presets=vlc.equalizer.presets()
484 if (includecategories and item) then
486 s.information.category={}
487 s.information.category.meta=item:metas()
489 local info = item:info()
490 for k, v in pairs(info) do
492 for k2, v2 in pairs(v) do
493 local tag = string.gsub(k2," ","_")
497 s.information.category[k]=streamTable
502 local statsdata = item:stats()
503 for k,v in pairs(statsdata) do
504 local tag = string.gsub(k,"_","")
508 s.information.chapter = vlc.player.get_chapter_index()
509 s.information.title = vlc.player.get_title_index()
511 s.information.chapters_count = vlc.player.get_chapters_count()
512 s.information.titles_count = vlc.player.get_titles_count()
518 get_renderers = function()
519 local rd = get_renderer_discovery()