@vindral/web-sdk 3.4.4 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cast-sender.js ADDED
@@ -0,0 +1,230 @@
1
+ var u = Object.defineProperty;
2
+ var l = (n, i, e) => i in n ? u(n, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[i] = e;
3
+ var s = (n, i, e) => l(n, typeof i != "symbol" ? i + "" : i, e);
4
+ var h = (n, i, e) => new Promise((t, a) => {
5
+ var o = (r) => {
6
+ try {
7
+ c(e.next(r));
8
+ } catch (S) {
9
+ a(S);
10
+ }
11
+ }, d = (r) => {
12
+ try {
13
+ c(e.throw(r));
14
+ } catch (S) {
15
+ a(S);
16
+ }
17
+ }, c = (r) => r.done ? t(r.value) : Promise.resolve(r.value).then(o, d);
18
+ c((e = e.apply(n, i)).next());
19
+ });
20
+ import { E as g } from "./BTxJOjm9.js";
21
+ class p extends g {
22
+ constructor(e) {
23
+ super();
24
+ s(this, "state", "not casting");
25
+ s(this, "config");
26
+ s(this, "unloaded", !1);
27
+ /**
28
+ * Update authentication token on an already established and authenticated connection
29
+ */
30
+ s(this, "updateAuthenticationToken", (e) => {
31
+ this.config && (this.config.options.authenticationToken = e, this.send({
32
+ type: "updateAuthToken",
33
+ token: e
34
+ }));
35
+ });
36
+ /**
37
+ * Fully unloads the instance. This disconnects the current listener but lets the
38
+ * cast session continue on the receiving device
39
+ */
40
+ s(this, "unload", () => {
41
+ var e;
42
+ this.unloaded = !0, (e = this.getSession()) == null || e.removeMessageListener("urn:x-cast:com.vindral.castdata", this.onMessage);
43
+ });
44
+ /**
45
+ * Initiates the CastSender.
46
+ * Will reject if Cast is not available on the device or the network.
47
+ */
48
+ s(this, "init", () => h(this, null, function* () {
49
+ return new Promise((e, t) => {
50
+ let a = !1;
51
+ const o = setTimeout(() => {
52
+ a = !0, t();
53
+ }, 1e4);
54
+ window.__onGCastApiAvailable = (d) => {
55
+ if (a) {
56
+ a = !1;
57
+ return;
58
+ }
59
+ if (clearTimeout(o), !d)
60
+ return t();
61
+ setTimeout(() => {
62
+ try {
63
+ this.onGCastApiAvailable(), e();
64
+ } catch (c) {
65
+ t();
66
+ }
67
+ }, 1e3);
68
+ }, this.castLibrariesAdded() && window.cast ? window.__onGCastApiAvailable(!0) : this.verifyCastLibraries();
69
+ });
70
+ }));
71
+ /**
72
+ * Requests a session. It will open the native cast receiver chooser dialog
73
+ */
74
+ s(this, "start", () => h(this, null, function* () {
75
+ var e;
76
+ yield (e = this.getInstance()) == null ? void 0 : e.requestSession();
77
+ }));
78
+ /**
79
+ * Stops a session. It will stop playback on device as well.
80
+ */
81
+ s(this, "stop", () => {
82
+ var e;
83
+ (e = this.getSession()) == null || e.endSession(!0);
84
+ });
85
+ /**
86
+ * Returns a string representing the name of the Cast receiver device or undefined if no receiver exists
87
+ */
88
+ s(this, "getReceiverName", () => {
89
+ var e, t;
90
+ return ((t = (e = this.getSession()) == null ? void 0 : e.getCastDevice()) == null ? void 0 : t.friendlyName) || void 0;
91
+ });
92
+ s(this, "onGCastApiAvailable", () => {
93
+ var o;
94
+ const e = this.getInstance();
95
+ if (!e)
96
+ throw "cast context should exist";
97
+ const t = e.getSessionState(), a = ((o = this.config) == null ? void 0 : o.receiverApplicationId) || "A5452297";
98
+ e.setOptions({
99
+ receiverApplicationId: a,
100
+ autoJoinPolicy: chrome.cast.AutoJoinPolicy.TAB_AND_ORIGIN_SCOPED,
101
+ resumeSavedSession: !0
102
+ }), e.addEventListener(cast.framework.CastContextEventType.SESSION_STATE_CHANGED, this.onSessionStateChanged), t === cast.framework.SessionState.SESSION_STARTED && setTimeout(() => {
103
+ this.state = "casting", this.emit("resumed");
104
+ });
105
+ });
106
+ s(this, "send", (e) => {
107
+ var t;
108
+ (t = this.getSession()) == null || t.sendMessage("urn:x-cast:com.vindral.castdata", e);
109
+ });
110
+ s(this, "onMessage", (e, t) => {
111
+ if (e === "urn:x-cast:com.vindral.castdata")
112
+ try {
113
+ const a = JSON.parse(t);
114
+ switch (a.type) {
115
+ case "metadata":
116
+ this.emit("metadata", a.metadata);
117
+ break;
118
+ case "serverWallclockTime":
119
+ this.emit("server wallclock time", a.serverWallclockTime);
120
+ break;
121
+ default:
122
+ break;
123
+ }
124
+ } catch (a) {
125
+ }
126
+ });
127
+ s(this, "onSessionStarted", () => {
128
+ var e;
129
+ (e = this.getSession()) == null || e.addMessageListener("urn:x-cast:com.vindral.castdata", this.onMessage), this.send({
130
+ type: "start",
131
+ config: this.config
132
+ });
133
+ });
134
+ s(this, "onSessionStateChanged", (e) => {
135
+ if (!this.unloaded)
136
+ switch (e.sessionState) {
137
+ case cast.framework.SessionState.SESSION_START_FAILED:
138
+ this.state = "not casting", this.emit("failed");
139
+ break;
140
+ case cast.framework.SessionState.SESSION_ENDED:
141
+ this.state = "not casting", this.emit("disconnected");
142
+ break;
143
+ case cast.framework.SessionState.SESSION_ENDING:
144
+ break;
145
+ case cast.framework.SessionState.SESSION_RESUMED:
146
+ this.onSessionStarted(), this.state = "casting", this.emit("resumed");
147
+ break;
148
+ case cast.framework.SessionState.SESSION_STARTED:
149
+ this.onSessionStarted(), this.state = "casting", this.emit("connected");
150
+ break;
151
+ case cast.framework.SessionState.SESSION_STARTING:
152
+ break;
153
+ }
154
+ });
155
+ s(this, "getInstance", () => {
156
+ var e;
157
+ return (e = window.cast) == null ? void 0 : e.framework.CastContext.getInstance();
158
+ });
159
+ s(this, "getSession", () => {
160
+ var e;
161
+ return (e = this.getInstance()) == null ? void 0 : e.getCurrentSession();
162
+ });
163
+ // check if cast libraries are already added
164
+ s(this, "castLibrariesAdded", () => !!(document.querySelector("#vindralCastFrameworkLib") && document.querySelector("#vindralCastSenderLib")));
165
+ s(this, "verifyCastLibraries", () => {
166
+ if (this.castLibrariesAdded())
167
+ return;
168
+ const e = document.createElement("script");
169
+ e.type = "text/javascript", e.id = "vindralCastFrameworkLib", e.src = "//www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js", document.head.appendChild(e);
170
+ const t = document.createElement("script");
171
+ t.type = "text/javascript", t.id = "vindralCastSenderLib", t.src = "//www.gstatic.com/cv/js/sender/v1/cast_sender.js", document.head.appendChild(t);
172
+ });
173
+ this.config = e;
174
+ }
175
+ /**
176
+ * True if the instance is casting right now
177
+ */
178
+ get casting() {
179
+ return this.state === "casting";
180
+ }
181
+ /**
182
+ * The current volume
183
+ */
184
+ get volume() {
185
+ var e, t;
186
+ return (t = (e = this.getSession()) == null ? void 0 : e.getVolume()) != null ? t : 0;
187
+ }
188
+ /**
189
+ * Set the current volume. Setting this to zero is equivalent to muting the video
190
+ */
191
+ set volume(e) {
192
+ var t;
193
+ (t = this.getSession()) == null || t.setVolume(e);
194
+ }
195
+ /**
196
+ * The current language
197
+ */
198
+ get language() {
199
+ var e, t;
200
+ return (t = (e = this.config) == null ? void 0 : e.options) == null ? void 0 : t.language;
201
+ }
202
+ /**
203
+ * Set the current language
204
+ */
205
+ set language(e) {
206
+ this.config && (this.config.options.language = e, this.send({
207
+ type: "setLanguage",
208
+ language: e
209
+ }));
210
+ }
211
+ /**
212
+ * The current channelId
213
+ */
214
+ get channelId() {
215
+ var e;
216
+ return ((e = this.config) == null ? void 0 : e.options.channelId) || "";
217
+ }
218
+ /**
219
+ * Set the current channelId
220
+ */
221
+ set channelId(e) {
222
+ this.config && (this.config.options.channelId = e, this.send({
223
+ type: "setChannelId",
224
+ channelId: e
225
+ }));
226
+ }
227
+ }
228
+ export {
229
+ p as CastSender
230
+ };