1. --!movie
  2. --!encoding=utf-8
  3.  
  4. global $
  5.  
  6. global gChromium
  7. global gRect
  8.  
  9. ----------------------------------------
  10. -- encryption keys
  11. ----------------------------------------
  12. global pwd -- used to decrypt scripts encrypted with AES-256 encryption
  13. global hls_key -- used to decrypt the AES-128 encrypted HLS video
  14.  
  15. ----------------------------------------
  16. --
  17. ----------------------------------------
  18. on startMovie
  19.     _player.debugPlaybackEnabled = 1
  20.  
  21.     ----------------------------------------
  22.     -- CONFIG
  23.     ----------------------------------------
  24.     pwd = "(!O0S9QiMfL$_*ltV(0M841JCxm*J6qV"
  25.     hls_key = [52,86,163,155,168,231,251,164,29,102,17,136,22,28,237,3]
  26.  
  27.     -- libs
  28.     $.import("aes")
  29.     $.import("base64")
  30.     $.import("file")
  31.     $.import("string")
  32.     $.import("ui")
  33.  
  34.     -- window settings
  35.     _movie.stage.title = "Chromium Xtra Demo: Encrypted Video"
  36.     _movie.stage.titlebarOptions.visible = 1
  37.     _movie.stage.rect = rect(0,0,640,400)
  38.     _movie.stage.bgcolor = rgb(0,0,0)
  39.     _movie.centerStage = 1
  40.     _movie.stage.resizable = 0
  41.     _movie.puppetTempo(30)
  42.  
  43.     -- add button
  44.     $.ui.buttonCreate("Load Encrypted Video", #slotLoadVideo)
  45.  
  46.     _movie.updateStage()
  47.     _movie.stage.visible = 1
  48.  
  49.     cefInit(["log_severity":99]) -- disable creation of debug.log
  50.  
  51.     -- create browser window in stage window that shows the video player
  52.     r = rect(0, 40, _movie.stage.rect.width, _movie.stage.rect.height)
  53.  
  54.     u = "file:///"&$.PATH&"resources/video/video.htm"
  55.     gChromium = xtra("Chromium").new(u, r, 1)
  56.  
  57.     -- for debugging only
  58.     --gChromium.browserShowDevTools()
  59. end
  60.  
  61. ----------------------------------------
  62. -- @destructor
  63. ----------------------------------------
  64. on stopMovie (me)
  65.     if objectP(gChromium) then
  66.         gChromium.browserClose()
  67.         -- give browser some time to shutdown
  68.         Sleep(1000)
  69.         gChromium = VOID
  70.     end if
  71. end
  72.  
  73. ----------------------------------------
  74. -- Press any key to return from (pseudo)fullscreen mode
  75. ----------------------------------------
  76. on keyDown
  77.     if not voidP(gRect) then
  78.         _movie.stage.rect = gRect
  79.         r = rect(0, 40, _movie.stage.rect.width, _movie.stage.rect.height)
  80.         gChromium.browserSetRect(r)
  81.         gRect = VOID
  82.     end if
  83. end
  84.  
  85. ----------------------------------------
  86. -- Loads JS script synchronously
  87. ----------------------------------------
  88. on loadEncryptedScriptSync (fn)
  89.     data = $.file.getBytes(fn)
  90.     data = $.aes.decryptByteArray(data, pwd)
  91.     js = ""
  92.     put "var scr = document.createElement('script');" after js
  93.     put "var txt = document.createTextNode(atob('"&$.base64.encode(data)&"'));" after js
  94.     put "scr.appendChild(txt);" after js
  95.     put "document.getElementsByTagName('head')[0].appendChild(scr);" after js
  96.     gChromium.browserExecuteJavaScript(js, "js")
  97. end
  98.  
  99. ----------------------------------------
  100. --
  101. ----------------------------------------
  102. on loadEncryptedVideo (aUrl)
  103.     -- send dummy key event, since autoplay only works after user interaction
  104.     gChromium.browserSendKeyEvent(32, 0)
  105.  
  106.     js = ""
  107.     put "hls.loadSource('"&aUrl&"');" after js
  108.     put "hls.attachMedia(video);" after js
  109.     gChromium.browserExecuteJavaScript(js, "js")
  110. end
  111.  
  112. ----------------------------------------
  113. --
  114. ----------------------------------------
  115. on slotLoadVideo
  116.     p = $.string.replace("\", "/", $.PATH)
  117.     u = "file:///"&p&"media/bbb_360p_60sec.m3u8"
  118.     loadEncryptedVideo(u)
  119.  
  120.     $.ui.reset()
  121.     $.ui.buttonCreate("Play Video", #slotPlay)
  122.     $.ui.buttonCreate("Pause Video", #slotPause)
  123.     $.ui.buttonCreate("FullScreen", #slotFullScreen)
  124. end
  125.  
  126. ----------------------------------------
  127. -- @callback
  128. ----------------------------------------
  129. on slotPlay
  130.     gChromium.browserExecuteJavaScript("video.play();", "js")
  131. end
  132.  
  133. ----------------------------------------
  134. -- @callback
  135. ----------------------------------------
  136. on slotPause
  137.     gChromium.browserExecuteJavaScript("video.pause();", "js")
  138. end
  139.  
  140. ----------------------------------------
  141. -- @callback
  142. ----------------------------------------
  143. on slotFullScreen
  144.     gRect = _movie.stage.rect
  145.     _movie.stage.rect = _system.desktopRectList[1]
  146.     gChromium.browserResizeToWindow()
  147. end
  148.  
  149. --**************************************
  150. -- RAW CALLBACKS - NOT THREAD-SAFE!
  151. --**************************************
  152.  
  153. ----------------------------------------
  154. -- @callback
  155. ----------------------------------------
  156. on OnLoadEnd (aHttpStatusCode)
  157.  
  158.     -- load the encrypted (and customized) hls.js
  159.     loadEncryptedScriptSync($.PATH&"resources\video\hls.light.min.js.aes")
  160.  
  161.     js = ""
  162.  
  163.     -- pass the 16-byte password used for the HLS encryption (as array of integers)
  164.     put "var config = {__key:"&string(hls_key)&"};" after js
  165.  
  166.     -- uncomment the following line to show hls.js debug infos in message window
  167.     -- put "config['debug'] = true;" after js
  168.  
  169.     -- initialize HLS
  170.     put "var video = document.getElementById('video');" after js
  171.     put "var hls = new Hls(config);" after js
  172.  
  173.     gChromium.browserExecuteJavaScript(js, "js")
  174. end
  175.  
  176. ----------------------------------------
  177. -- @callback
  178. ----------------------------------------
  179. on OnConsoleMessage (aLevel, aMsg)
  180.     put ["[DEBUG] ","[INFO] ","[WARNING] ","[ERROR] "][aLevel] & aMsg
  181. end
  182.  
  183. ----------------------------------------
  184. -- @callback
  185. ----------------------------------------
  186. on OnBeforeContextMenu
  187.     return TRUE -- deactivate context menu
  188. end
  189.  
  190. ----------------------------------------
  191. -- If browser has keyboard focus, forward to Director key handler
  192. -- @callback
  193. ----------------------------------------
  194. on OnKeyEvent
  195.     keyDown()
  196. end
  197.  
[raw code]