--!movie
--!encoding=utf-8
global $
global gChromium
global gRect
----------------------------------------
-- encryption keys
----------------------------------------
global pwd -- used to decrypt scripts encrypted with AES-256 encryption
global hls_key -- used to decrypt the AES-128 encrypted HLS video
----------------------------------------
--
----------------------------------------
on startMovie
_player.debugPlaybackEnabled = 1
----------------------------------------
-- CONFIG
----------------------------------------
pwd = "(!O0S9QiMfL$_*ltV(0M841JCxm*J6qV"
hls_key = [52,86,163,155,168,231,251,164,29,102,17,136,22,28,237,3]
-- libs
$.import("aes")
$.import("base64")
$.import("file")
$.import("string")
$.import("ui")
-- window settings
_movie.stage.title = "Chromium Xtra Demo: Encrypted Video"
_movie.stage.titlebarOptions.visible = 1
_movie.stage.rect = rect(0,0,640,400)
_movie.stage.bgcolor = rgb(0,0,0)
_movie.centerStage = 1
_movie.stage.resizable = 0
_movie.puppetTempo(30)
-- add button
$.ui.buttonCreate("Load Encrypted Video", #slotLoadVideo)
_movie.updateStage()
_movie.stage.visible = 1
cefInit(["log_severity":99]) -- disable creation of debug.log
-- create browser window in stage window that shows the video player
r = rect(0, 40, _movie.stage.rect.width, _movie.stage.rect.height)
u = "file:///"&$.PATH&"resources/video/video.htm"
gChromium = xtra("Chromium").new(u, r, 1)
-- for debugging only
--gChromium.browserShowDevTools()
end
----------------------------------------
-- @destructor
----------------------------------------
on stopMovie (me)
if objectP(gChromium) then
gChromium.browserClose()
-- give browser some time to shutdown
Sleep(1000)
gChromium = VOID
end if
end
----------------------------------------
-- Press any key to return from (pseudo)fullscreen mode
----------------------------------------
on keyDown
if not voidP(gRect) then
_movie.stage.rect = gRect
r = rect(0, 40, _movie.stage.rect.width, _movie.stage.rect.height)
gChromium.browserSetRect(r)
gRect = VOID
end if
end
----------------------------------------
-- Loads JS script synchronously
----------------------------------------
on loadEncryptedScriptSync (fn)
data = $.file.getBytes(fn)
data = $.aes.decryptByteArray(data, pwd)
js = ""
put "var scr = document.createElement('script');" after js
put "var txt = document.createTextNode(atob('"&$.base64.encode(data)&"'));" after js
put "scr.appendChild(txt);" after js
put "document.getElementsByTagName('head')[0].appendChild(scr);" after js
gChromium.browserExecuteJavaScript(js, "js")
end
----------------------------------------
--
----------------------------------------
on loadEncryptedVideo (aUrl)
-- send dummy key event, since autoplay only works after user interaction
gChromium.browserSendKeyEvent(32, 0)
js = ""
put "hls.loadSource('"&aUrl&"');" after js
put "hls.attachMedia(video);" after js
gChromium.browserExecuteJavaScript(js, "js")
end
----------------------------------------
--
----------------------------------------
on slotLoadVideo
p = $.string.replace("\", "/", $.PATH)
u = "file:///"&p&"media/bbb_360p_60sec.m3u8"
loadEncryptedVideo(u)
$.ui.reset()
$.ui.buttonCreate("Play Video", #slotPlay)
$.ui.buttonCreate("Pause Video", #slotPause)
$.ui.buttonCreate("FullScreen", #slotFullScreen)
end
----------------------------------------
-- @callback
----------------------------------------
on slotPlay
gChromium.browserExecuteJavaScript("video.play();", "js")
end
----------------------------------------
-- @callback
----------------------------------------
on slotPause
gChromium.browserExecuteJavaScript("video.pause();", "js")
end
----------------------------------------
-- @callback
----------------------------------------
on slotFullScreen
gRect = _movie.stage.rect
_movie.stage.rect = _system.desktopRectList[1]
gChromium.browserResizeToWindow()
end
--**************************************
-- RAW CALLBACKS - NOT THREAD-SAFE!
--**************************************
----------------------------------------
-- @callback
----------------------------------------
on OnLoadEnd (aHttpStatusCode)
-- load the encrypted (and customized) hls.js
loadEncryptedScriptSync($.PATH&"resources\video\hls.light.min.js.aes")
js = ""
-- pass the 16-byte password used for the HLS encryption (as array of integers)
put "var config = {__key:"&string(hls_key)&"};" after js
-- uncomment the following line to show hls.js debug infos in message window
-- put "config['debug'] = true;" after js
-- initialize HLS
put "var video = document.getElementById('video');" after js
put "var hls = new Hls(config);" after js
gChromium.browserExecuteJavaScript(js, "js")
end
----------------------------------------
-- @callback
----------------------------------------
on OnConsoleMessage (aLevel, aMsg)
put ["[DEBUG] ","[INFO] ","[WARNING] ","[ERROR] "][aLevel] & aMsg
end
----------------------------------------
-- @callback
----------------------------------------
on OnBeforeContextMenu
return TRUE -- deactivate context menu
end
----------------------------------------
-- If browser has keyboard focus, forward to Director key handler
-- @callback
----------------------------------------
on OnKeyEvent
keyDown()
end