forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.js
executable file
·148 lines (124 loc) · 3.49 KB
/
receiver.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
/**
* @license
* Copyright 2015 Google Inc.
*
* 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.
*/
/**
* Chromecast receiver functionality.
* @class
*/
var receiver = function() {};
/**
* @const {string}
* @private
*/
receiver.MESSAGE_NAMESPACE_ = 'urn:x-cast:com.shaka.cast.messages';
/**
* @type {cast.receiver.CastReceiverManager}
* @private
*/
receiver.castReceiverManager_ = null;
/**
* @type {cast.receiver.CastMessageBus}
* @private
*/
receiver.messageBus_ = null; // custom message bus
/**
* @const {number} The ms to remain idle before killing the receiver app.
* @private
*/
receiver.IDLE_TIMEOUT_ = 1000 * 60 * 5; // 5 minutes
/**
* @type {?number} Id of the idle timer.
* @private
*/
receiver.idleTimer_ = null;
/**
* Initializes the receiver.
*/
receiver.initialize = function() {
receiver.castReceiverManager_ =
cast.receiver.CastReceiverManager.getInstance();
receiver.castReceiverManager_.onSenderDisconnected = function(event) {
if (receiver.castReceiverManager_.getSenders().length == 0) {
if (event.reason ==
cast.receiver.system.DisconnectReason.REQUESTED_BY_SENDER) {
window.close();
}
}
};
receiver.messageBus_ = receiver.castReceiverManager_.getCastMessageBus(
receiver.MESSAGE_NAMESPACE_);
/**
* Handles messages from the sender. event.data will be objects with a
* type and value.
* @param {Event} event The message event.
*/
receiver.messageBus_.onMessage = function(event) {
var message = JSON.parse(event['data']);
switch (message['type']) {
case 'loadStream':
receiverApp.loadDashStream(message['value']);
break;
case 'play':
receiverApp.play();
break;
case 'pause':
receiverApp.pause();
break;
case 'setCurrentTime':
receiverApp.setCurrentTime(message['value']);
break;
case 'mute':
receiverApp.mute(message['value']);
break;
case 'setVolume':
receiverApp.setVolume(message['value']);
}
};
receiver.castReceiverManager_.start();
receiver.setIdleTimeout(receiver.IDLE_TIMEOUT_);
};
/**
* Sets an idle timeout.
* @param {number} timeout Time in milliseconds before the idle player
* terminates.
*/
receiver.setIdleTimeout = function(timeout) {
// If starts playing before timeout, the timer will be cancelled.
if (receiver.idleTimer) {
window.clearTimeout(receiver.idleTimer);
}
receiver.idleTimer = window.setTimeout(function() {
window.close();
}, timeout);
};
/**
* Cancels idle timeout.
*/
receiver.cancelIdleTimeout = function() {
if (receiver.idleTimer) {
window.clearTimeout(receiver.idleTimer);
}
receiver.IdleTimeout = null;
};
/**
* Broadcast message to all senders via custom message channel.
* @param {string} type The type of message.
* @param {*} value The value of the message.
*/
receiver.broadcast = function(type, value) {
receiver.messageBus_.broadcast(
JSON.stringify({'type': type, 'value': value}));
};