~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Widget/Hls.php

  • Committer: Dan Garner
  • Date: 2016-02-15 17:54:45 UTC
  • mto: (454.4.130)
  • mto: This revision was merged to the branch mainline in revision 484.
  • Revision ID: git-v1:dd226a6f84464ff28758a249e1fd52ca4a35d911
Correction to Layout Duration ToolTip
xibosignage/xibo#721

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2016 Spring Signage Ltd
5
 
 * (GoogleTraffic.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Widget;
10
 
 
11
 
 
12
 
use Respect\Validation\Validator as v;
13
 
use Xibo\Exception\InvalidArgumentException;
14
 
use Xibo\Exception\XiboException;
15
 
use Xibo\Factory\ModuleFactory;
16
 
 
17
 
/**
18
 
 * Class Hls
19
 
 * @package Xibo\Widget
20
 
 */
21
 
class Hls extends ModuleWidget
22
 
{
23
 
    public $codeSchemaVersion = 1;
24
 
 
25
 
    /** @inheritdoc */
26
 
    public function init()
27
 
    {
28
 
        // Initialise extra validation rules
29
 
        v::with('Xibo\\Validation\\Rules\\');
30
 
    }
31
 
 
32
 
    /**
33
 
     * Install or Update this module
34
 
     * @param ModuleFactory $moduleFactory
35
 
     */
36
 
    public function installOrUpdate($moduleFactory)
37
 
    {
38
 
        if ($this->module == null) {
39
 
            // Install
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';
46
 
            $module->enabled = 1;
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 = [];
54
 
 
55
 
            $this->setModule($module);
56
 
            $this->installModule();
57
 
        }
58
 
 
59
 
        // Check we are all installed
60
 
        $this->installFiles();
61
 
    }
62
 
 
63
 
    /**
64
 
     * Install Files
65
 
     */
66
 
    public function installFiles()
67
 
    {
68
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/jquery-1.11.1.min.js')->save();
69
 
    }
70
 
 
71
 
    /**
72
 
     * Adds a HLS Widget
73
 
     * @SWG\Post(
74
 
     *  path="/playlist/widget/hls/{playlistId}",
75
 
     *  operationId="WidgetHlsAdd",
76
 
     *  tags={"widget"},
77
 
     *  summary="Add a HLS Widget",
78
 
     *  description="Add a new HLS Widget to the specified playlist",
79
 
     *  @SWG\Parameter(
80
 
     *      name="playlistId",
81
 
     *      in="path",
82
 
     *      description="The playlist ID to add a Widget to",
83
 
     *      type="integer",
84
 
     *      required=true
85
 
     *   ),
86
 
     *  @SWG\Parameter(
87
 
     *      name="name",
88
 
     *      in="formData",
89
 
     *      description="Optional Widget Name",
90
 
     *      type="string",
91
 
     *      required=false
92
 
     *  ),
93
 
     *  @SWG\Parameter(
94
 
     *      name="useDuration",
95
 
     *      in="formData",
96
 
     *      description="Edit Only - (0, 1) Select only if you will provide duration parameter as well",
97
 
     *      type="integer",
98
 
     *      required=false
99
 
     *  ),
100
 
     *  @SWG\Parameter(
101
 
     *      name="duration",
102
 
     *      in="formData",
103
 
     *      description="The Widget Duration",
104
 
     *      type="integer",
105
 
     *      required=false
106
 
     *  ),
107
 
     *  @SWG\Parameter(
108
 
     *      name="uri",
109
 
     *      in="formData",
110
 
     *      description="URL to HLS video stream",
111
 
     *      type="string",
112
 
     *      required=true
113
 
     *   ),
114
 
     *  @SWG\Parameter(
115
 
     *      name="mute",
116
 
     *      in="formData",
117
 
     *      description="Flag (0, 1) Should the video be muted?",
118
 
     *      type="integer",
119
 
     *      required=false
120
 
     *   ),
121
 
     *  @SWG\Parameter(
122
 
     *      name="transparency",
123
 
     *      in="formData",
124
 
     *      description="Flag (0, 1), This causes some android devices to switch to a hardware accelerated web view",
125
 
     *      type="integer",
126
 
     *      required=false
127
 
     *   ),
128
 
     *  @SWG\Response(
129
 
     *      response=201,
130
 
     *      description="successful operation",
131
 
     *      @SWG\Schema(ref="#/definitions/Widget"),
132
 
     *      @SWG\Header(
133
 
     *          header="Location",
134
 
     *          description="Location of the new widget",
135
 
     *          type="string"
136
 
     *      )
137
 
     *  )
138
 
     * )
139
 
     */
140
 
    public function add()
141
 
    {
142
 
        $this->setCommonOptions();
143
 
        $this->validate();
144
 
 
145
 
        // Save the widget
146
 
        $this->saveWidget();
147
 
    }
148
 
 
149
 
    /**
150
 
     * Edit Media
151
 
     */
152
 
    public function edit()
153
 
    {
154
 
        $this->setCommonOptions();
155
 
        $this->validate();
156
 
 
157
 
        // Save the widget
158
 
        $this->saveWidget();
159
 
    }
160
 
 
161
 
    /**
162
 
     * Validate
163
 
     * @throws XiboException
164
 
     */
165
 
    private function validate()
166
 
    {
167
 
        if ($this->getUseDuration() == 1 && $this->getDuration() == 0)
168
 
            throw new InvalidArgumentException(__('Please enter a duration'), 'duration');
169
 
 
170
 
        if (!v::url()->notEmpty()->validate(urldecode($this->getOption('uri'))))
171
 
            throw new InvalidArgumentException(__('Please enter a link'), 'uri');
172
 
    }
173
 
 
174
 
    /**
175
 
     * Set common options
176
 
     */
177
 
    private function setCommonOptions()
178
 
    {
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'));
184
 
 
185
 
        // This causes some android devices to switch to a hardware accellerated web view
186
 
        $this->setOption('transparency', 0);
187
 
 
188
 
        // Ensure we have the necessary files linked up
189
 
        $media = $this->mediaFactory->createModuleFile(PROJECT_ROOT . '/modules/vendor/hls/hls.min.js');
190
 
        $media->save();
191
 
        $this->assignMedia($media->mediaId);
192
 
 
193
 
        $this->setOption('hlsId', $media->mediaId);
194
 
 
195
 
        $media = $this->mediaFactory->createModuleFile(PROJECT_ROOT . '/modules/vendor/hls/hls-1px-transparent.png');
196
 
        $media->save();
197
 
        $this->assignMedia($media->mediaId);
198
 
 
199
 
        $this->setOption('posterId', $media->mediaId);
200
 
    }
201
 
 
202
 
    /**
203
 
     * @inheritdoc
204
 
     */
205
 
    public function isValid()
206
 
    {
207
 
        // Using the information you have in your module calculate whether it is valid or not.
208
 
        // 0 = Invalid
209
 
        // 1 = Valid
210
 
        // 2 = Unknown
211
 
        return 1;
212
 
    }
213
 
 
214
 
    /**
215
 
     * GetResource
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.
218
 
     * @return mixed
219
 
     */
220
 
    public function getResource($displayId = 0)
221
 
    {
222
 
        $this
223
 
            ->initialiseGetResource()
224
 
            ->appendViewPortWidth($this->region->width)
225
 
            ->appendJavaScriptFile('vendor/jquery-1.11.1.min.js')
226
 
            ->appendJavaScriptFile('vendor/hls/hls.min.js')
227
 
            ->appendJavaScript('
228
 
                $(document).ready(function() {
229
 
            
230
 
                    if(Hls.isSupported()) {
231
 
                        var video = document.getElementById("video");
232
 
                        var hls = new Hls({
233
 
                            autoStartLoad: true,
234
 
                            startPosition : -1,
235
 
                            capLevelToPlayerSize: false,
236
 
                            debug: false,
237
 
                            defaultAudioCodec: undefined,
238
 
                            enableWorker: true
239
 
                        });
240
 
                        hls.loadSource("' . urldecode($this->getOption('uri')) . '");
241
 
                        hls.attachMedia(video);
242
 
                        hls.on(Hls.Events.MANIFEST_PARSED, function() {
243
 
                          video.play();
244
 
                        });
245
 
                        hls.on(Hls.Events.ERROR, function (event, data) {
246
 
                            if (data.fatal) {
247
 
                                switch(data.type) {
248
 
                                    case Hls.ErrorTypes.NETWORK_ERROR:
249
 
                                        // try to recover network error
250
 
                                        //console.log("fatal network error encountered, try to recover");
251
 
                                        hls.startLoad();
252
 
                                        break;
253
 
                                    
254
 
                                    case Hls.ErrorTypes.MEDIA_ERROR:
255
 
                                        //console.log("fatal media error encountered, try to recover");
256
 
                                        hls.recoverMediaError();
257
 
                                        break;
258
 
                                        
259
 
                                    default:
260
 
                                        // cannot recover
261
 
                                        hls.destroy();
262
 
                                        break;
263
 
                                }
264
 
                            }
265
 
                        });
266
 
                     }
267
 
                });
268
 
            ')
269
 
            ->appendBody('<video id="video" poster="' . $this->getResourceUrl('vendor/hls/hls-1px-transparent.png') . '" ' . (($this->getOption('mute', 0) == 1) ? 'muted' : '') . '></video>')
270
 
            ->appendCss('
271
 
                video {
272
 
                    width: 100%; 
273
 
                    height: 100%;
274
 
                }
275
 
            ')
276
 
        ;
277
 
 
278
 
        return $this->finaliseGetResource();
279
 
    }
280
 
}
 
 
b'\\ No newline at end of file'