/
WAbstractMedia.C
440 lines (394 loc) · 12.1 KB
/
WAbstractMedia.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2010 Emweb bvba, Leuven, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WAbstractMedia"
#include "Wt/WApplication"
#include "Wt/WEnvironment"
#include "Wt/WException"
#include "Wt/WResource"
#include "DomElement.h"
#include "WebUtils.h"
#ifndef WT_DEBUG_JS
#include "js/WAbstractMedia.min.js"
#endif
#include <boost/lexical_cast.hpp>
namespace {
Wt::WAbstractMedia::ReadyState intToReadyState(int i)
{
switch (i) {
case 0:
return Wt::WAbstractMedia::HaveNothing;
case 1:
return Wt::WAbstractMedia::HaveMetaData;
case 2:
return Wt::WAbstractMedia::HaveCurrentData;
case 3:
return Wt::WAbstractMedia::HaveFutureData;
case 4:
return Wt::WAbstractMedia::HaveEnoughData;
default:
throw Wt::WException("Invalid readystate");
}
}
}
using namespace Wt;
const char *WAbstractMedia::PLAYBACKSTARTED_SIGNAL = "play";
const char *WAbstractMedia::PLAYBACKPAUSED_SIGNAL = "pause";
const char *WAbstractMedia::ENDED_SIGNAL = "ended";
const char *WAbstractMedia::TIMEUPDATED_SIGNAL = "timeupdate";
const char *WAbstractMedia::VOLUMECHANGED_SIGNAL = "volumechange";
WAbstractMedia::WAbstractMedia(WContainerWidget *parent)
: WInteractWidget(parent),
sourcesRendered_(0),
flags_(0),
preloadMode_(PreloadAuto),
alternative_(0),
flagsChanged_(false),
preloadChanged_(false),
sourcesChanged_(false),
playing_(false),
volume_(-1),
current_(-1),
duration_(-1),
ended_(false),
readyState_(HaveNothing)
{
setInline(false);
setFormObject(true);
WApplication *app = wApp;
LOAD_JAVASCRIPT(app, "js/WAbstractMedia.js", "WAbstractMedia", wtjs1);
setJavaScriptMember(" WAbstractMedia",
"new " WT_CLASS ".WAbstractMedia("
+ app->javaScriptClass() + "," + jsRef() + ");");
#ifndef WT_TARGET_JAVA
implementStateless(&WAbstractMedia::play, &WAbstractMedia::play);
implementStateless(&WAbstractMedia::pause, &WAbstractMedia::pause);
#endif //WT_TARGET_JAVA
}
WAbstractMedia::~WAbstractMedia()
{
for (std::size_t i = 0; i < sources_.size(); ++i)
delete sources_[i];
}
EventSignal<>& WAbstractMedia::playbackStarted()
{
return *voidEventSignal(PLAYBACKSTARTED_SIGNAL, true);
}
EventSignal<>& WAbstractMedia::playbackPaused()
{
return *voidEventSignal(PLAYBACKPAUSED_SIGNAL, true);
}
EventSignal<>& WAbstractMedia::ended()
{
return *voidEventSignal(ENDED_SIGNAL, true);
}
EventSignal<>& WAbstractMedia::timeUpdated()
{
return *voidEventSignal(TIMEUPDATED_SIGNAL, true);
}
EventSignal<>& WAbstractMedia::volumeChanged()
{
return *voidEventSignal(VOLUMECHANGED_SIGNAL, true);
}
void WAbstractMedia::setFormData(const FormData& formData)
{
if (!Utils::isEmpty(formData.values)) {
std::vector<std::string> attributes;
boost::split(attributes, formData.values[0], boost::is_any_of(";"));
if (attributes.size() == 6) {
try {
volume_ = boost::lexical_cast<double>(attributes[0]);
} catch (const std::exception& e) {
volume_ = -1;
}
try {
current_ = boost::lexical_cast<double>(attributes[1]);
} catch (const std::exception& e) {
current_ = -1;
}
try {
duration_ = boost::lexical_cast<double>(attributes[2]);
} catch (const std::exception& e) {
duration_ = -1;
}
playing_ = (attributes[3] == "0");
ended_ = (attributes[4] == "1");
try {
readyState_ = intToReadyState(boost::lexical_cast<int>(attributes[5]));
} catch (const std::exception& e) {
throw WException("WAbstractMedia: error parsing: " + formData.values[0]
+ ": " + e.what());
}
} else
throw WException("WAbstractMedia: error parsing: " + formData.values[0]);
}
}
void WAbstractMedia::play()
{
doJavaScript("jQuery.data(" + jsRef() + ", 'obj').play();");
}
void WAbstractMedia::pause()
{
doJavaScript("jQuery.data(" + jsRef() + ", 'obj').pause();}");
}
void WAbstractMedia::renderSource(DomElement* element,
WAbstractMedia::Source &source, bool isLast)
{
// src is mandatory
element->setAttribute("src", resolveRelativeUrl(source.link.url()));
if (source.type != "")
element->setAttribute("type", source.type);
if (source.media != "")
element->setAttribute("media", source.media);
if (isLast && alternative_) {
// Last element -> add error handler for unsupported content
element->setAttribute("onerror",
"""var media = this.parentNode;"
"""if(media){"
"" "while (media && media.children.length)"
"" "if (" WT_CLASS ".hasTag(media.firstChild,'SOURCE')){"
"" "media.removeChild(media.firstChild);"
"" "}else{"
"" "media.parentNode.insertBefore(media.firstChild, media);"
"" "}"
"" "media.style.display= 'none';"
"""}"
);
} else {
element->setAttribute("onerror", "");
}
}
void WAbstractMedia::updateMediaDom(DomElement& element, bool all)
{
// Only if not IE
if (all && alternative_) {
element.setAttribute("onerror",
"""if(event.target.error && event.target.error.code=="
"" "event.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED){"
"" "while (this.hasChildNodes())"
"" "if (" WT_CLASS ".hasTag(this.firstChild,'SOURCE')){"
"" "this.removeChild(this.firstChild);"
"" "}else{"
"" "this.parentNode.insertBefore(this.firstChild, this);"
"" "}"
"" "this.style.display= 'none';"
"""}"
);
}
if (all || flagsChanged_) {
if ((!all) || flags_ & Controls)
element.setAttribute("controls", flags_ & Controls ? "controls" : "");
if ((!all) || flags_ & Autoplay)
element.setAttribute("autoplay", flags_ & Autoplay ? "autoplay" : "");
if ((!all) || flags_ & Loop)
element.setAttribute("loop", flags_ & Loop ? "loop" : "");
}
if (all || preloadChanged_) {
switch (preloadMode_) {
case PreloadNone:
element.setAttribute("preload", "none");
break;
default:
case PreloadAuto:
element.setAttribute("preload", "auto");
break;
case PreloadMetadata:
element.setAttribute("preload", "metadata");
break;
}
}
updateEventSignals(element, all);
if (all)
if (alternative_) {
element.addChild(alternative_->createSDomElement(wApp));
}
flagsChanged_ = preloadChanged_ = false;
}
DomElement *WAbstractMedia::createDomElement(WApplication *app)
{
DomElement *result = 0;
if (isInLayout()) {
// It's easier to set WT_RESIZE_JS after the following code,
// but if it's not set, the alternative content will think that
// it is not included in a layout manager. Set some phony function
// now, which will be overwritten later in this method.
setJavaScriptMember(WT_RESIZE_JS, "function() {}");
}
if (app->environment().agentIsIElt(9) ||
app->environment().agent() == WEnvironment::MobileWebKitAndroid) {
// Shortcut: IE misbehaves when it encounters a media element
result = DomElement::createNew(DomElement_DIV);
if (alternative_)
result->addChild(alternative_->createSDomElement(app));
} else {
DomElement *media = createMediaDomElement();
DomElement *wrap = 0;
if (isInLayout()) {
media->setProperty(PropertyStylePosition, "absolute");
media->setProperty(PropertyStyleLeft, "0");
media->setProperty(PropertyStyleRight, "0");
wrap = DomElement::createNew(DomElement_DIV);
wrap->setProperty(PropertyStylePosition, "relative");
}
result = wrap ? wrap : media;
if (wrap) {
mediaId_ = id() + "_media";
media->setId(mediaId_);
} else {
mediaId_ = id();
}
updateMediaDom(*media, true);
// Create the 'source' elements
for (std::size_t i = 0; i < sources_.size(); ++i) {
DomElement *src = DomElement::createNew(DomElement_SOURCE);
src->setId(mediaId_ + "s" + boost::lexical_cast<std::string>(i));
renderSource(src, *sources_[i], i + 1 >= sources_.size());
media->addChild(src);
}
sourcesRendered_ = sources_.size();
sourcesChanged_ = false;
if (wrap) {
wrap->addChild(media);
}
}
if (isInLayout()) {
std::stringstream ss;
ss <<
"""function(self, w, h) {";
if (!mediaId_.empty()) {
ss <<
"" "v=" + jsMediaRef() + ";"
"" "if(v){"
"" "v.setAttribute('width', w);"
"" "v.setAttribute('height', h);"
"" "}";
}
if (alternative_) {
ss <<
"""a=" + alternative_->jsRef() + ";"
"" "if(a && a." << WT_RESIZE_JS <<")"
"" "a." << WT_RESIZE_JS << "(a, w, h);";
}
ss
<<"}";
setJavaScriptMember(WT_RESIZE_JS, ss.str());
}
setId(result, app);
updateDom(*result, true);
if (isInLayout()) {
result->setEvent(PLAYBACKSTARTED_SIGNAL, std::string());
result->setEvent(PLAYBACKPAUSED_SIGNAL, std::string());
result->setEvent(ENDED_SIGNAL, std::string());
result->setEvent(TIMEUPDATED_SIGNAL, std::string());
result->setEvent(VOLUMECHANGED_SIGNAL, std::string());
}
setJavaScriptMember("mediaId", "'" + mediaId_ + "'");
return result;
}
std::string WAbstractMedia::jsMediaRef() const
{
if (mediaId_.empty()) {
return "null";
} else {
return WT_CLASS ".getElement('" + mediaId_ + "')";
}
}
void WAbstractMedia::getDomChanges(std::vector<DomElement *>& result,
WApplication *app)
{
if (!mediaId_.empty()) {
DomElement *media = DomElement::getForUpdate(mediaId_, DomElement_DIV);
updateMediaDom(*media, false);
if (sourcesChanged_) {
// Updating source elements seems to be ill-supported in at least FF,
// so we delete them all and reinsert them.
// Delete source elements that are no longer required
for (std::size_t i = 0; i < sourcesRendered_; ++i)
media->callJavaScript
(WT_CLASS ".remove('" + mediaId_ + "s"
+ boost::lexical_cast<std::string>(i) + "');",
true);
sourcesRendered_ = 0;
for (std::size_t i = 0; i < sources_.size(); ++i) {
DomElement *src = DomElement::createNew(DomElement_SOURCE);
src->setId(mediaId_ + "s" + boost::lexical_cast<std::string>(i));
renderSource(src, *sources_[i], i + 1 >= sources_.size());
media->addChild(src);
}
sourcesRendered_ = sources_.size();
sourcesChanged_ = false;
// Explicitly request rerun of media selection algorithm
// 4.8.9.2 says it should happen automatically, but FF doesn't
media->callJavaScript(jsMediaRef() + ".load();");
}
result.push_back(media);
}
WInteractWidget::getDomChanges(result, app);
}
void WAbstractMedia::setOptions(const WFlags<Options> &flags)
{
flags_ = flags;
flagsChanged_ = true;
this->repaint(Wt::RepaintPropertyAttribute);
}
WFlags<WAbstractMedia::Options> WAbstractMedia::getOptions() const
{
return flags_;
}
void WAbstractMedia::setPreloadMode(PreloadMode mode)
{
preloadMode_ = mode;
preloadChanged_ = true;
repaint(Wt::RepaintPropertyAttribute);
}
WAbstractMedia::PreloadMode WAbstractMedia::preloadMode() const
{
return preloadMode_;
}
void WAbstractMedia::clearSources()
{
for (std::size_t i = 0; i < sources_.size(); ++i) {
delete sources_[i];
}
sources_.clear();
repaint(Wt::RepaintPropertyAttribute);
}
void WAbstractMedia::addSource(const WLink& link, const std::string &type,
const std::string& media)
{
sources_.push_back(new Source(this, link, type, media));
sourcesChanged_ = true;
repaint(Wt::RepaintPropertyAttribute);
}
void WAbstractMedia::setAlternativeContent(WWidget *alternative)
{
if (alternative_)
delete alternative_;
alternative_ = alternative;
if (alternative_)
addChild(alternative_);
}
WAbstractMedia::Source::Source(WAbstractMedia *parent,
const WLink& link, const std::string &type,
const std::string &media)
: parent(parent),
type(type),
media(media),
link(link)
{
if (link.type() == WLink::Resource)
connection = link.resource()->dataChanged().connect
(this, &Source::resourceChanged);
}
WAbstractMedia::Source::~Source()
{
connection.disconnect();
}
void WAbstractMedia::Source::resourceChanged()
{
parent->sourcesChanged_ = true;
parent->repaint(Wt::RepaintPropertyAttribute);
}