-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsender.js
915 lines (813 loc) · 27.3 KB
/
sender.js
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* global variables
*/
var applicationID = 'YOUR_APP_ID';
var MESSAGE_NAMESPACE = 'urn:x-cast:com.google.cast.sample.mediaplayer';
var currentMedia = null;
var currentVolume = 0.5;
var progressFlag = 1;
var mediaCurrentTime = 0;
var session = null;
var mediaURLs = [
'http://commondatastorage.googleapis.com/gtv-videos-bucket/dash/BigBuckBunny/bunny_10s/BigBuckBunny_10s_isoffmain_url_relative_DIS_23009_1_v_2_1c2_2011_08_30.mpd',
'http://yt-dash-mse-test.commondatastorage.googleapis.com/media/car-20120827-manifest.mpd',
'http://playertest.longtailvideo.com/adaptive/bbbfull/bbbfull.m3u8',
'http://stream.gravlab.net/003119/sparse/v1d30/posts/2014/barcelona/barcelona.m3u8',
'https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8',
'http://playready.directtaps.net/smoothstreaming/SSWSS720H264/SuperSpeedway_720.ism/Manifest',
'http://playready.directtaps.net/smoothstreaming/SSWSS720H264PR/SuperSpeedway_720.ism/Manifest'];
var mediaTitles = [
'Big Buck Bunny',
'Public DASH: CAR',
'Big Buck Bunny HLS',
'Gravlab HLS',
'Apple HLS',
'Super Speedway SS',
'Super Speedway SS PlayReady'];
var mediaTypes = [
'video/mp4',
'video/mp4',
'video/mp4',
'application/vnd.apple.mpegurl',
'application/vnd.apple.mpegurl',
'application/vnd.apple.mpegurl',
'text/xml',
'text/xml'];
var currentMediaURL = mediaURLs[0];
var currentMediaIndex = 0;
var initialTimeIndexSeconds = 0;
var timer = null;
/**
* Call initialization
*/
if (!chrome.cast || !chrome.cast.isAvailable) {
setTimeout(initializeCastApi, 1000);
}
/**
* initialization
*/
function initializeCastApi() {
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
sessionListener,
receiverListener);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
code = ' var sessionRequest = new chrome.cast.SessionRequest(appID);\n\n' +
' var apiConfig = new chrome.cast.ApiConfig(sessionRequest,\n\n' +
' sessionListener,\n\n' +
' receiverListener);\n\n' +
' chrome.cast.initialize(apiConfig, onInitSuccess, onError);';
showCodeSnippet(code, 'sender');
};
/**
* initialization success callback
*/
function onInitSuccess() {
appendMessage("init success");
}
/**
* initialization error callback
*/
function onError() {
console.log("error");
appendMessage("error");
}
/**
* generic success callback
*/
function onSuccess(message) {
console.log(message);
}
/**
* callback on success for stopping app
*/
function onStopAppSuccess() {
console.log('Session stopped');
appendMessage('Session stopped');
}
/**
* session listener during initialization
*/
function sessionListener(e) {
console.log('New session ID: ' + e.sessionId);
appendMessage('New session ID:' + e.sessionId);
session = e;
if (session.media.length != 0) {
appendMessage(
'Found ' + session.media.length + ' existing media sessions.');
onMediaDiscovered('sessionListener', session.media[0]);
}
session.addMediaListener(
onMediaDiscovered.bind(this, 'addMediaListener'));
session.addUpdateListener(sessionUpdateListener.bind(this));
session.addMessageListener(MESSAGE_NAMESPACE, onReceiverMessage.bind(this));
}
/**
* handle message from receiver app
* @param {string} namespace A message string
* @param {string} message A message string
*/
function onReceiverMessage(namespace, message) {
var messageJSON = JSON.parse(message);
console.log(namespace + ':' + message);
var captions_div = document.getElementById('captions');
if( messageJSON['captions'] ) {
var captions = messageJSON['captions'];
captions_div.innerHTML = '';
for (var i in captions) {
var input = document.createElement('input');
input.type = 'submit';
input.onclick = (function() {
var currentI = i;
return function() {
setCaptions(currentI);
};
})();
input.value = captions[i];
captions_div.appendChild(input);
}
document.getElementById('instream_caption_div').style.display = 'block';
}
}
/**
* @param {boolean} isAlive A boolean
* true for session being live
*/
function sessionUpdateListener(isAlive) {
var message = isAlive ? 'Session Updated' : 'Session Removed';
message += ': ' + session.sessionId;
appendMessage(message);
if (!isAlive) {
session = null;
var playpauseresume = document.getElementById("playpauseresume");
playpauseresume.innerHTML = 'Play Media';
if( timer ) {
clearInterval(timer);
}
}
};
/**
* receiver listener during initialization
* @param {string} e A message string
*/
function receiverListener(e) {
if( e === 'available' ) {
console.log("receiver found");
appendMessage("receiver found");
}
else {
console.log("receiver list empty");
appendMessage("receiver list empty");
}
}
/**
* select a media URL
* @param {string} m An index for media URL
*/
function selectMedia(m) {
console.log("media selected" + m);
appendMessage("media selected" + m);
currentMediaIndex = m;
currentMediaURL = mediaURLs[m];
var alertmessage = document.getElementById('alertmessage');
document.getElementById('license').style.display = 'none';
if( m == 0 ) {
alertmessage.value = "MPEG-DASH stream\nRead about api: Host, Player and protocol";
}
else if( m == 1 ) {
alertmessage.value = "MPEG-DASH stream\nLearn more about MSE, EME and DRM";
}
else if( m == 2 ) {
alertmessage.value = "This HLS stream requires CORS e.g. a permissive crossdomain.xml file on your server\nRead more at https://developers.google.com/cast/docs/player";
}
else if( m == 3 ) {
alertmessage.value = "This HLS stream requires CORS that has to be enabled on your content servers or you may set up a proxy during development";
}
else if( m == 4 ) {
alertmessage.value = "This requires CORS and has 8 in-streams captions in 4 languages that you can choose to enable";
}
else if( m == 5 ) {
alertmessage.value = "SS stream with no PlayReady DRM\nPlay with external captions in WebVTT or TTML";
}
else if( m == 6 ) {
alertmessage.value = "PlayReady DRM with license server:\n http://playready.directtaps.net/pr/svc/rightsmanager.asmx";
document.getElementById('license').style.display = 'block';
}
else {
}
}
/**
* enter a media URL
* @param {string} m An media URL
*/
function setMyMediaURL(e) {
if( e.value ) {
currentMediaURL = e.value;
}
}
/**
* launch app
*/
function launchApp() {
console.log("launching app...");
appendMessage("launching app...");
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
code = ' chrome.cast.requestSession(onRequestSessionSuccess, ...);\n\n' +
' onRequestSessionSuccess() {\n' +
' session = e;\n' +
' session.addMessageListener(MESSAGE_NAMESPACE,\n' +
' onReceiverMessage.bind(this));\n' +
' }';
showCodeSnippet(code, 'sender');
code = ' mediaManager = new cast.receiver.MediaManager(mediaElement);\n\n' +
' var appConfig = new cast.receiver.CastReceiverManager.Config();\n' +
' castReceiverManager = cast.receiver.CastReceiverManager.getInstance();\n' +
' castReceiverManager.start(appConfig);\n\n';
showCodeSnippet(code, 'receiver');
}
/**
* callback on success for requestSession call
* @param {Object} e A non-null new session.
*/
function onRequestSessionSuccess(e) {
console.log("session success: " + e.sessionId);
appendMessage("session success: " + e.sessionId);
session = e;
session.addMessageListener(MESSAGE_NAMESPACE, onReceiverMessage.bind(this));
}
/**
* callback on launch error
*/
function onLaunchError() {
console.log("launch error");
appendMessage("launch error");
}
/**
* stop app/session
*/
function stopApp() {
session.stop(onStopAppSuccess, onError);
if( timer ) {
clearInterval(timer);
}
code = ' session.stop(success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onStop\n' +
' mediaManager.onStop = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onStopOrig\'](event);\n' +
' }\n';
showCodeSnippet(code, 'receiver');
}
/**
* load media specified by custom URL
*/
function loadCustomMedia() {
var customMediaURL = document.getElementById('customMediaURL').value;
if( customMediaURL.length > 0 ) {
loadMedia(customMediaURL);
}
}
/**
* load media
* @param {string} i An index for media
*/
function loadMedia(mediaURL) {
if (!session) {
console.log("no session");
appendMessage("no session");
return;
}
if( mediaURL ) {
var mediaInfo = new chrome.cast.media.MediaInfo(mediaURL);
}
else {
console.log("loading..." + currentMediaURL);
appendMessage("loading..." + currentMediaURL);
var mediaInfo = new chrome.cast.media.MediaInfo(currentMediaURL);
}
mediaInfo.contentType = mediaTypes[currentMediaIndex];
var request = new chrome.cast.media.LoadRequest(mediaInfo);
request.currentTime = initialTimeIndexSeconds;
request.autoplay = true;
var payload = {
'title' : mediaTitles[currentMediaIndex],
};
var json = {
"payload" : payload,
};
request.customData = json;
var captions_div = document.getElementById('captions');
captions_div.innerHTML = '';
document.getElementById('instream_caption_div').style.display = 'none';
session.loadMedia(request,
onMediaDiscovered.bind(this, 'loadMedia'),
onMediaError);
document.getElementById('caption_ttml').disabled = undefined;
document.getElementById('caption_webvtt').disabled = undefined;
code = ' session.loadMedia(request,onMediaDiscovered,onMediaError);\n\n' +
' onMediaDiscovered() {\n' +
' media.addUpdateListener(onMediaStatusUpdate);\n' +
' timer = setInterval(updateCurrentTime.bind(this), 1000);' +
' }\n';
showCodeSnippet(code, 'sender');
code = ' mediaManager.onLoad = function(event) {\n' +
' mediaHost = new cast.player.api.Host({\'mediaElement\': media, \'url\': url});\n' +
' if (url.lastIndexOf(\'.m3u8\') >= 0) {\n' +
' protocol = cast.player.api.CreateHlsStreamingProtocol(mediaHost);\n' +
' } else if (url.lastIndexOf(\'.mpd\') >= 0) {\n' +
' protocol = cast.player.api.CreateDashStreamingProtocol(mediaHost);\n' +
' } else if (url.lastIndexOf(\'.ism/\') >= 0) {\n' +
' protocol = cast.player.api.CreateSmoothStreamingProtocol(mediaHost);\n' +
' }\n' +
' mediaPlayer = new cast.player.api.Player(mediaHost);\n' +
' mediaPlayer.load(protocol, initialTimeInSeconds);\n' +
' }\n';
showCodeSnippet(code, 'receiver');
}
/**
* callback on success for loading media
* @param {Object} e A non-null media object
*/
function onMediaDiscovered(how, mediaSession) {
console.log("new media session ID:" + mediaSession.mediaSessionId);
appendMessage("new media session ID:" + mediaSession.mediaSessionId + ' (' + how + ')');
currentMedia = mediaSession;
mediaSession.addUpdateListener(onMediaStatusUpdate);
mediaCurrentTime = currentMedia.currentTime;
playpauseresume.innerHTML = 'Pause Media';
if( !timer ) {
timer = setInterval(updateCurrentTime.bind(this), 1000);
}
}
/**
* callback on media loading error
* @param {Object} e A non-null media object
*/
function onMediaError(e) {
console.log("media error");
appendMessage("media error");
}
/**
* callback for media status event
* @param {Object} e A non-null media object
*/
function onMediaStatusUpdate(isAlive) {
if( progressFlag ) {
document.getElementById("progress").value = parseInt(100 * currentMedia.currentTime / currentMedia.media.duration);
document.getElementById("progress_tick").innerHTML = currentMedia.currentTime;
document.getElementById("duration").innerHTML = currentMedia.media.duration;
}
document.getElementById("playerstate").innerHTML = currentMedia.playerState;
}
/**
* play media
*/
function playMedia() {
if( !currentMedia )
return;
if( timer ) {
clearInterval(timer);
}
var playpauseresume = document.getElementById("playpauseresume");
if( playpauseresume.innerHTML == 'Play Media' ) {
currentMedia.play(null,
mediaCommandSuccessCallback.bind(this,"playing started for " + currentMedia.sessionId),
onError);
playpauseresume.innerHTML = 'Pause Media';
currentMedia.addUpdateListener(onMediaStatusUpdate);
appendMessage("play started");
timer = setInterval(updateCurrentTime.bind(this), 1000);
code = ' currentMedia.play(null, success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onPlay\n' +
' mediaManager.onPlay = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onPlayOrig\'](event);\n' +
' }\n';
showCodeSnippet(code, 'receiver');
}
else {
if( playpauseresume.innerHTML == 'Pause Media' ) {
currentMedia.pause(null,
mediaCommandSuccessCallback.bind(this,"paused " + currentMedia.sessionId),
onError);
playpauseresume.innerHTML = 'Resume Media';
appendMessage("paused");
code = ' currentMedia.pause(null, success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onPause\n' +
' mediaManager.onPause = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onPauseOrig\'](event);\n' +
' }\n' +
' // Watch \'Media Element State\'';
showCodeSnippet(code, 'receiver');
}
else {
if( playpauseresume.innerHTML == 'Resume Media' ) {
currentMedia.play(null,
mediaCommandSuccessCallback.bind(this,"resumed " + currentMedia.sessionId),
onError);
playpauseresume.innerHTML = 'Pause Media';
appendMessage("resumed");
timer = setInterval(updateCurrentTime.bind(this), 1000);
}
code = ' currentMedia.play(null, success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onPlay\n' +
' mediaManager.onPlay = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onPlayOrig\'](event);\n' +
' }\n';
showCodeSnippet(code, 'receiver');
}
}
}
/**
* stop media
*/
function stopMedia() {
if( !currentMedia )
return;
currentMedia.stop(null,
mediaCommandSuccessCallback.bind(this,"stopped " + currentMedia.sessionId),
onError);
var playpauseresume = document.getElementById("playpauseresume");
playpauseresume.innerHTML = 'Play Media';
appendMessage("media stopped");
if( timer ) {
clearInterval(timer);
}
code = ' currentMedia.stop(null, success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onStop\n' +
' mediaManager.onStop = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onStopOrig\'](event);\n' +
' }\n';
showCodeSnippet(code, 'receiver');
}
/**
* set receiver volume
* @param {Number} level A number for volume level
* @param {Boolean} mute A true/false for mute/unmute
*/
function setReceiverVolume(level, mute) {
if( !session )
return;
if( !mute ) {
session.setReceiverVolumeLevel(level,
mediaCommandSuccessCallback.bind(this, 'media set-volume done'),
onError);
currentVolume = level;
}
else {
session.setReceiverMuted(true,
mediaCommandSuccessCallback.bind(this, 'media set-volume done'),
onError);
}
code = ' session.setReceiverVolumeLevel(level, success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onSetVolume\n' +
' mediaManager.onSetVolume = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onSetVolumeOrig\'](event);\n' +
' }\n' +
' // Watch \'Volume State\' in receiver debug message';
showCodeSnippet(code, 'receiver');
}
/**
* set media volume
* @param {Number} level A number for volume level
* @param {Boolean} mute A true/false for mute/unmute
*/
function setMediaVolume(level, mute) {
if( !currentMedia )
return;
var volume = new chrome.cast.Volume();
volume.level = level;
currentVolume = volume.level;
volume.muted = mute;
var request = new chrome.cast.media.VolumeRequest();
request.volume = volume;
currentMedia.setVolume(request,
mediaCommandSuccessCallback.bind(this, 'media set-volume done'),
onError);
}
/**
* enable/disable live streaming
* @param {DOM Object} cb A checkbox element
*/
function liveStreaming(cb) {
if( cb.checked == true ) {
document.getElementById('initialTimeIndexSeconds').innerHTML = 'initialTimeIndexSeconds: Infinity';
sendMessage({'type':'live','value':true});
}
else {
document.getElementById('initialTimeIndexSeconds').innerHTML = 'initialTimeIndexSeconds: 0';
sendMessage({'type':'live','value':false});
}
code = ' // send custom message to set initialTimeIndexSeconds to Infinity\n' +
' sendMessage({\'type\':\'live\',\'value\':true});\n' +
' // receiver sets liveStreaming flag to true';
showCodeSnippet(code, 'sender');
code = ' if( liveStreaming ) {\n' +
' mediaPlayer.load(protocol, Infinity);\n' +
' }\n' +
' else {\n' +
' mediaPlayer.load(protocol, initialTimeIndexSeconds);\n' +
' }';
showCodeSnippet(code, 'receiver');
}
/**
* mute media
* @param {DOM Object} cb A checkbox element
*/
function muteMedia(cb) {
if( cb.checked == true ) {
document.getElementById('muteText').innerHTML = 'Unmute media';
//setMediaVolume(currentVolume, true);
setReceiverVolume(currentVolume, true);
appendMessage("media muted");
}
else {
document.getElementById('muteText').innerHTML = 'Mute media';
//setMediaVolume(currentVolume, false);
setReceiverVolume(currentVolume, false);
appendMessage("media unmuted");
}
}
/**
* seek media position
* @param {Number} pos A number to indicate percent
*/
function seekMedia(pos) {
console.log('Seeking ' + currentMedia.sessionId + ':' +
currentMedia.mediaSessionId + ' to ' + pos + "%");
progressFlag = 0;
var request = new chrome.cast.media.SeekRequest();
request.currentTime = pos * currentMedia.media.duration / 100;
request.resumeState = chrome.cast.media.PlayerState.PAUSED;
currentMedia.seek(request,
onSeekSuccess.bind(this, 'media seek done'),
onError);
code = ' currentMedia.seek(SeekRequest, success, error);\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override onSeek\n' +
' mediaManager.onSeek = function(event) {\n' +
' // your custom code\n' +
' mediaManager[\'onSeekOrig\'](event);\n' +
' }\n' +
' // Watch \'Media Player State\' in receiver debug message';
showCodeSnippet(code, 'receiver');
}
/**
* callback on success for media commands
* @param {string} info A message string
* @param {Object} e A non-null media object
*/
function onSeekSuccess(info) {
console.log(info);
appendMessage(info);
setTimeout(function(){progressFlag = 1},1500);
}
/**
* callback on success for media commands
* @param {string} info A message string
* @param {Object} e A non-null media object
*/
function mediaCommandSuccessCallback(info) {
console.log(info);
appendMessage(info);
}
/**
* Updates the progress bar shown for each media item.
*/
function updateCurrentTime() {
if (!session || !currentMedia) {
return;
}
if (currentMedia.media && currentMedia.media.duration != null) {
var cTime = currentMedia.getEstimatedTime();
document.getElementById("progress").value = parseInt(100 * cTime / currentMedia.media.duration);
document.getElementById("progress_tick").innerHTML = cTime;
}
else {
document.getElementById("progress").value = 0;
document.getElementById("progress_tick").innerHTML = 0;
if( timer ) {
clearInterval(timer);
}
}
};
/**
* set the closed captioning track
* @param {string} trackNumber the closed captioning track number
*/
function setCaptions(trackNumber) {
if (session!=null) {
if (trackNumber == undefined) {
message = {
type: 'DISABLE_CC'
}
} else {
message = {
type: 'ENABLE_CC',
trackNumber: trackNumber
}
}
session.sendMessage(MESSAGE_NAMESPACE, message, onSuccess.bind(this, appendMessage("Message sent: " + JSON.stringify(message))), onError);
} else {
alert("First connect to a Cast device.");
}
code = ' \/\/Send custom message DISABLE_CC/ENABLE_CC\n' +
' sendMessage({\'type\':\'ENABLE_CC\',\n' +
' trackNumber: trackNumber});\n';
showCodeSnippet(code, 'sender');
code = ' // loop all caption tracks\n' +
' // enable this trackNumber\n' +
' setCaption(trackNumber);\n';
showCodeSnippet(code, 'receiver');
}
/**
* set the closed captioning track
* @param {string} type the closed captioning type WebVTT or TTML
*/
function setExternalCaptions(type) {
if (session!=null) {
message = {
type: type
}
session.sendMessage(MESSAGE_NAMESPACE, message, onSuccess.bind(this, appendMessage("Message sent: " + JSON.stringify(message))), onError);
document.getElementById('caption_ttml').disabled = 'disabled';
document.getElementById('caption_webvtt').disabled = 'disabled';
}
code = ' //Send custom message WebVTT or TTML\n' +
' sendMessage({\'type\':\'WebVTT\'});\n';
' //\n' +
' sendMessage({\'type\':\'TTML\'});\n';
' }';
showCodeSnippet(code, 'sender');
code = ' // disable any prior caption\n' +
' mediaPlayer.enableCaptions(false);\n' +
' // enable captions.vtt or captions.ttml\n' +
' mediaPlayer.enableCaptions(true,\'webvtt\',\'captions.vtt\');';
showCodeSnippet(code, 'receiver');
}
/**
* set the closed captioning font size
* @param {string} size the closed captioning size index
*/
function setFont(size) {
if (session!=null) {
if (size == 0) {
message = {
type: 'NORMAL_FONT'
}
} else {
message = {
type: 'LARGE_FONT'
}
}
session.sendMessage(MESSAGE_NAMESPACE, message, onSuccess.bind(this, appendMessage("Message sent: " + JSON.stringify(message))), onError);
} else {
alert("First connect to a Cast device.");
}
};
/**
* append message to debug message window
* @param {string} message A message string
*/
function appendMessage(message) {
var dw = document.getElementById("debugmessage");
dw.innerHTML += '\n' + JSON.stringify(message);
return message;
};
/**
* append message to sendercode window
* @param {string} message A message string
* @param {string} type A type string
*/
function showCodeSnippet(code, type) {
if( type == 'sender' ) {
var dw = document.getElementById("sendercode");
}
else {
var dw = document.getElementById("receivercode");
}
dw.innerHTML = '\n' + code;
};
/**
* send a custom message to receiver to set max bandwidth
*/
function setMaxBandwidth() {
var maxBW = document.getElementById('maxBW').value;
if( maxBW.length > 0 && !isNaN(maxBW) ) {
sendMessage({'type':'maxBW','value':maxBW});
}
code = ' // send custom message to set maxBW\n' +
' sendMessage({\'type\':\'maxBW\',\'value\':maxBW});';
showCodeSnippet(code, 'sender');
code = ' // receiver calls\n' +
' qLevel = protocol.getQualityLevel(c, maxBW);\n' +
' // to get quality level for streamVideoQuality\n' +
' setDebugMessage(\'streamVideoQuality\', streamInfo.bitrates[qLevel]);\n' +
' // up to that specified by ' + maxBW;
showCodeSnippet(code, 'receiver');
}
/**
* send a custom message to receiver
* so that it will override updateLicenseRequest
* by setting license server URL
*/
function setLicenseUrl() {
var licenseUrl = document.getElementById('licenseUrl').value;
if( licenseUrl.length > 0 ) {
sendMessage({'type':'license','value':licenseUrl});
}
code = ' // send custom message to set server license URL\n' +
' sendMessage({\'type\':\'license\',\'value\':licenseUrl});\n';
showCodeSnippet(code, 'sender');
code = ' // save original and override updateLicenseRequestInfo\n' +
' mediaHost[\'Orig\'] = mediaHost.updateLicenseRequestInfo;\n' +
' mediaHost.updateLicenseRequestInfo = function(requestInfo) {\n' +
' mediaHost.licenseUrl = licenseUrl;\n' +
' mediaHost[\'Orig\'](requestInfo);\n' +
' }\n' +
' // Watch \'Media Host State\' for error if incorrect license URL';
showCodeSnippet(code, 'receiver');
}
/**
* send a custom message to receiver so that
* it will display debug message on TV
*/
function showReceiverDebugMessage() {
sendMessage({'type':'show','target':'debug'});
code = ' //Send custom message \'show\'\n' +
' sendMessage({\'type\':\'show\',\'target\':\'debug\'});';
showCodeSnippet(code, 'sender');
code = ' // onMessage\n' +
' document.getElementById(\'messages\').style.display = \'block\';\n';
showCodeSnippet(code, 'receiver');
}
/**
* send a custom message to receiver so that
* it will hide debug message on TV
*/
function hideReceiverDebugMessage() {
sendMessage({'type':'hide','target':'debug'});
code = ' //Send custom message \'hide\'\n' +
' sendMessage({\'type\':\'hide\',\'target\':\'debug\'});';
showCodeSnippet(code, 'sender');
code = ' // onMessage\n' +
' document.getElementById(\'messages\').style.display = \'none\';\n';
showCodeSnippet(code, 'receiver');
}
/**
* send a custom message to receiver so that
* it will show video element on TV
*/
function showVideoOnTV() {
sendMessage({'type':'show','target':'video'});
code = ' //Send custom message \'show\'\n' +
' sendMessage({\'type\':\'show\',\'target\':\'video\'});';
showCodeSnippet(code, 'sender');
code = ' // onMessage\n' +
' document.getElementById(\'receiverVideoElement\').style.display = \'block\';\n';
showCodeSnippet(code, 'receiver');
}
/**
* send a custom message to receiver so that
* it will hide video element on TV
*/
function hideVideoOnTV() {
sendMessage({'type':'hide','target':'video'});
code = ' //Send custom message \'hide\'\n' +
' sendMessage({\'type\':\'hide\',\'target\':\'video\'});';
showCodeSnippet(code, 'sender');
code = ' // onMessage\n' +
' document.getElementById(\'receiverVideoElement\').style.display = \'none\';\n';
showCodeSnippet(code, 'receiver');
}
/**
* @param {string} message A message string
*/
function sendMessage(message) {
if (session!=null) {
session.sendMessage(MESSAGE_NAMESPACE, message, onSuccess.bind(this, "Message sent: " + message), onError);
}
};