3
* Spring Signage Ltd - http://www.springsignage.com
4
* Copyright (C) 2016 Spring Signage Ltd
12
use Respect\Validation\Validator as v;
13
use Xibo\Exception\InvalidArgumentException;
14
use Xibo\Exception\XiboException;
15
use Xibo\Factory\ModuleFactory;
19
* @package Xibo\Widget
21
class Hls extends ModuleWidget
23
public $codeSchemaVersion = 1;
26
public function init()
28
// Initialise extra validation rules
29
v::with('Xibo\\Validation\\Rules\\');
33
* Install or Update this module
34
* @param ModuleFactory $moduleFactory
36
public function installOrUpdate($moduleFactory)
38
if ($this->module == null) {
40
$module = $moduleFactory->createEmpty();
41
$module->name = 'HLS';
42
$module->type = 'hls';
43
$module->class = 'Xibo\Widget\Hls';
44
$module->description = 'HLS Video Stream';
45
$module->imageUri = 'forms/library.gif';
47
$module->previewEnabled = 1;
48
$module->assignable = 1;
49
$module->regionSpecific = 1;
50
$module->renderAs = 'html';
51
$module->schemaVersion = $this->codeSchemaVersion;
52
$module->defaultDuration = 60;
53
$module->settings = [];
55
$this->setModule($module);
56
$this->installModule();
59
// Check we are all installed
60
$this->installFiles();
66
public function installFiles()
68
$this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/jquery-1.11.1.min.js')->save();
74
* path="/playlist/widget/hls/{playlistId}",
75
* operationId="WidgetHlsAdd",
77
* summary="Add a HLS Widget",
78
* description="Add a new HLS Widget to the specified playlist",
82
* description="The playlist ID to add a Widget to",
89
* description="Optional Widget Name",
96
* description="Edit Only - (0, 1) Select only if you will provide duration parameter as well",
103
* description="The Widget Duration",
110
* description="URL to HLS video stream",
117
* description="Flag (0, 1) Should the video be muted?",
122
* name="transparency",
124
* description="Flag (0, 1), This causes some android devices to switch to a hardware accelerated web view",
130
* description="successful operation",
131
* @SWG\Schema(ref="#/definitions/Widget"),
134
* description="Location of the new widget",
140
public function add()
142
$this->setCommonOptions();
152
public function edit()
154
$this->setCommonOptions();
163
* @throws XiboException
165
private function validate()
167
if ($this->getUseDuration() == 1 && $this->getDuration() == 0)
168
throw new InvalidArgumentException(__('Please enter a duration'), 'duration');
170
if (!v::url()->notEmpty()->validate(urldecode($this->getOption('uri'))))
171
throw new InvalidArgumentException(__('Please enter a link'), 'uri');
177
private function setCommonOptions()
179
$this->setDuration($this->getSanitizer()->getInt('duration', $this->getDuration()));
180
$this->setUseDuration($this->getSanitizer()->getCheckbox('useDuration'));
181
$this->setOption('name', $this->getSanitizer()->getString('name'));
182
$this->setOption('uri', urlencode($this->getSanitizer()->getString('uri')));
183
$this->setOption('mute', $this->getSanitizer()->getCheckbox('mute'));
185
// This causes some android devices to switch to a hardware accellerated web view
186
$this->setOption('transparency', 0);
188
// Ensure we have the necessary files linked up
189
$media = $this->mediaFactory->createModuleFile(PROJECT_ROOT . '/modules/vendor/hls/hls.min.js');
191
$this->assignMedia($media->mediaId);
193
$this->setOption('hlsId', $media->mediaId);
195
$media = $this->mediaFactory->createModuleFile(PROJECT_ROOT . '/modules/vendor/hls/hls-1px-transparent.png');
197
$this->assignMedia($media->mediaId);
199
$this->setOption('posterId', $media->mediaId);
205
public function isValid()
207
// Using the information you have in your module calculate whether it is valid or not.
216
* Return the rendered resource to be used by the client (or a preview) for displaying this content.
217
* @param integer $displayId If this comes from a real client, this will be the display id.
220
public function getResource($displayId = 0)
223
->initialiseGetResource()
224
->appendViewPortWidth($this->region->width)
225
->appendJavaScriptFile('vendor/jquery-1.11.1.min.js')
226
->appendJavaScriptFile('vendor/hls/hls.min.js')
228
$(document).ready(function() {
230
if(Hls.isSupported()) {
231
var video = document.getElementById("video");
235
capLevelToPlayerSize: false,
237
defaultAudioCodec: undefined,
240
hls.loadSource("' . urldecode($this->getOption('uri')) . '");
241
hls.attachMedia(video);
242
hls.on(Hls.Events.MANIFEST_PARSED, function() {
245
hls.on(Hls.Events.ERROR, function (event, data) {
248
case Hls.ErrorTypes.NETWORK_ERROR:
249
// try to recover network error
250
//console.log("fatal network error encountered, try to recover");
254
case Hls.ErrorTypes.MEDIA_ERROR:
255
//console.log("fatal media error encountered, try to recover");
256
hls.recoverMediaError();
269
->appendBody('<video id="video" poster="' . $this->getResourceUrl('vendor/hls/hls-1px-transparent.png') . '" ' . (($this->getOption('mute', 0) == 1) ? 'muted' : '') . '></video>')
278
return $this->finaliseGetResource();
b'\\ No newline at end of file'