@microsoft/live-share-media 0.5.1 → 1.0.0-preview.3

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.
Files changed (100) hide show
  1. package/.nycrc.json +10 -10
  2. package/.vscode/launch.json +29 -0
  3. package/.vscode/settings.json +2 -0
  4. package/LICENSE +86 -86
  5. package/README.md +138 -137
  6. package/bin/IMediaPlayer.d.ts.map +1 -1
  7. package/bin/{EphemeralMediaSession.d.ts → LiveMediaSession.d.ts} +14 -21
  8. package/bin/LiveMediaSession.d.ts.map +1 -0
  9. package/bin/{EphemeralMediaSession.js → LiveMediaSession.js} +39 -37
  10. package/bin/LiveMediaSession.js.map +1 -0
  11. package/bin/{EphemeralMediaSessionCoordinator.d.ts → LiveMediaSessionCoordinator.d.ts} +5 -5
  12. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -0
  13. package/bin/{EphemeralMediaSessionCoordinator.js → LiveMediaSessionCoordinator.js} +60 -48
  14. package/bin/LiveMediaSessionCoordinator.js.map +1 -0
  15. package/bin/{EphemeralMediaSessionCoordinatorSuspension.d.ts → LiveMediaSessionCoordinatorSuspension.d.ts} +3 -3
  16. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts.map +1 -0
  17. package/bin/{EphemeralMediaSessionCoordinatorSuspension.js → LiveMediaSessionCoordinatorSuspension.js} +4 -4
  18. package/bin/LiveMediaSessionCoordinatorSuspension.js.map +1 -0
  19. package/bin/MediaPlayerSynchronizer.d.ts +11 -11
  20. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  21. package/bin/MediaPlayerSynchronizer.js +132 -75
  22. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  23. package/bin/MediaSessionActionThrottler.d.ts +1 -1
  24. package/bin/MediaSessionActionThrottler.d.ts.map +1 -1
  25. package/bin/MediaSessionActionThrottler.js.map +1 -1
  26. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  27. package/bin/RepeatedActionThrottler.d.ts +2 -2
  28. package/bin/RepeatedActionThrottler.d.ts.map +1 -1
  29. package/bin/RepeatedActionThrottler.js +9 -3
  30. package/bin/RepeatedActionThrottler.js.map +1 -1
  31. package/bin/VolumeManager.d.ts +84 -0
  32. package/bin/VolumeManager.d.ts.map +1 -0
  33. package/bin/VolumeManager.js +175 -0
  34. package/bin/VolumeManager.js.map +1 -0
  35. package/bin/index.d.ts +9 -9
  36. package/bin/index.d.ts.map +1 -1
  37. package/bin/index.js +4 -4
  38. package/bin/index.js.map +1 -1
  39. package/bin/internals/GroupCoordinatorState.d.ts +12 -12
  40. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  41. package/bin/internals/GroupCoordinatorState.js +94 -56
  42. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  43. package/bin/internals/GroupPlaybackPosition.d.ts +3 -3
  44. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  45. package/bin/internals/GroupPlaybackPosition.js +24 -15
  46. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  47. package/bin/internals/GroupPlaybackTrack.d.ts +4 -4
  48. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  49. package/bin/internals/GroupPlaybackTrack.js +14 -4
  50. package/bin/internals/GroupPlaybackTrack.js.map +1 -1
  51. package/bin/internals/GroupPlaybackTrackData.d.ts +3 -3
  52. package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
  53. package/bin/internals/GroupPlaybackTrackData.js +8 -4
  54. package/bin/internals/GroupPlaybackTrackData.js.map +1 -1
  55. package/bin/internals/GroupTransportState.d.ts +5 -5
  56. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  57. package/bin/internals/GroupTransportState.js +28 -13
  58. package/bin/internals/GroupTransportState.js.map +1 -1
  59. package/bin/internals/consts.d.ts.map +1 -1
  60. package/bin/internals/consts.js +54 -54
  61. package/bin/internals/consts.js.map +1 -1
  62. package/bin/internals/index.d.ts +6 -6
  63. package/package.json +38 -38
  64. package/src/IMediaPlayer.ts +29 -21
  65. package/src/LiveMediaSession.ts +300 -0
  66. package/src/LiveMediaSessionCoordinator.ts +621 -0
  67. package/src/{EphemeralMediaSessionCoordinatorSuspension.ts → LiveMediaSessionCoordinatorSuspension.ts} +39 -31
  68. package/src/MediaPlayerSynchronizer.ts +627 -450
  69. package/src/MediaSessionActionThrottler.ts +16 -14
  70. package/src/MediaSessionExtensions.ts +52 -42
  71. package/src/RepeatedActionThrottler.ts +74 -56
  72. package/src/VolumeManager.ts +197 -0
  73. package/src/index.ts +14 -15
  74. package/src/internals/GroupCoordinatorState.ts +606 -446
  75. package/src/internals/GroupPlaybackPosition.ts +227 -193
  76. package/src/internals/GroupPlaybackTrack.ts +176 -147
  77. package/src/internals/GroupPlaybackTrackData.ts +98 -90
  78. package/src/internals/GroupTransportState.ts +157 -127
  79. package/src/internals/consts.ts +98 -80
  80. package/src/internals/index.ts +11 -11
  81. package/src/test/GroupPlaybackPosition.spec.ts +600 -340
  82. package/src/test/GroupPlaybackTrack.spec.ts +122 -66
  83. package/src/test/SharedMediaSession.spec.ts +149 -113
  84. package/src/test/SharedMediaSessionCoordinator.spec.ts +271 -231
  85. package/src/test/VolumeManager.spec.ts +219 -0
  86. package/tsconfig.build.json +8 -8
  87. package/tsconfig.json +76 -75
  88. package/bin/EphemeralMediaSession.d.ts.map +0 -1
  89. package/bin/EphemeralMediaSession.js.map +0 -1
  90. package/bin/EphemeralMediaSessionCoordinator.d.ts.map +0 -1
  91. package/bin/EphemeralMediaSessionCoordinator.js.map +0 -1
  92. package/bin/EphemeralMediaSessionCoordinatorSuspension.d.ts.map +0 -1
  93. package/bin/EphemeralMediaSessionCoordinatorSuspension.js.map +0 -1
  94. package/bin/VolumeLimiter.d.ts +0 -70
  95. package/bin/VolumeLimiter.d.ts.map +0 -1
  96. package/bin/VolumeLimiter.js +0 -170
  97. package/bin/VolumeLimiter.js.map +0 -1
  98. package/src/EphemeralMediaSession.ts +0 -248
  99. package/src/EphemeralMediaSessionCoordinator.ts +0 -461
  100. package/src/VolumeLimiter.ts +0 -182
@@ -1,170 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Microsoft Corporation. All rights reserved.
4
- * Licensed under the Microsoft Live Share SDK License.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.VolumeLimiter = exports.LevelType = void 0;
8
- const live_share_1 = require("@microsoft/live-share");
9
- var LevelType;
10
- (function (LevelType) {
11
- LevelType[LevelType["fixed"] = 0] = "fixed";
12
- LevelType[LevelType["percentage"] = 1] = "percentage";
13
- })(LevelType = exports.LevelType || (exports.LevelType = {}));
14
- class VolumeLimiter {
15
- constructor(player) {
16
- // Settings
17
- this._duration = new live_share_1.TimeInterval(2000);
18
- this._rampDownDuration = new live_share_1.TimeInterval(500);
19
- this._rampUpDuration = new live_share_1.TimeInterval(500);
20
- this._level = 0.1;
21
- this._levelType = LevelType.fixed;
22
- // Running state
23
- this._running = false;
24
- this._endTime = 0;
25
- this._startTime = 0;
26
- this._startVolume = 0;
27
- this._inRamp = false;
28
- this._player = player;
29
- }
30
- /**
31
- * Time, in seconds, that the volume should be lowered.
32
- *
33
- * @remarks
34
- * Applications should periodically call `lowerVolume()` to keep the volume lowered.
35
- */
36
- get duration() {
37
- return this._duration.seconds;
38
- }
39
- set duration(value) {
40
- this._duration.seconds = Math.abs(value);
41
- }
42
- /**
43
- * Target level to lower volume to.
44
- *
45
- * @remarks
46
- * Expressed as a value between 0.0 and 1.0. The value is applied based upon the configured
47
- * `levelType`. The default value is 0.2.
48
- *
49
- * For a level type of `LevelType.fixed` the value is the exact level the volume will be
50
- * lowered to. The default value of 0.2 would cause the volume to be lowered to 0.2.
51
- *
52
- * For a level type of `LevelType.percentage` the value is the percentage by which the volume
53
- * level should be lowered to. The default value of 0.2 would cause the volume to be lowered
54
- * to 20% of its starting value.
55
- */
56
- get level() {
57
- return this._level;
58
- }
59
- set level(value) {
60
- if (value < 0 || value > 1.0) {
61
- throw new Error(`VolumeLimiter: cannot set level to ${value}. Level must be between 0.0 and 1.0.`);
62
- }
63
- this._level = value;
64
- }
65
- /**
66
- * The type of level represented by the `level` property.
67
- */
68
- get levelType() {
69
- return this._levelType;
70
- }
71
- set levelType(value) {
72
- this._levelType = value;
73
- }
74
- /**
75
- * Amount of time, in seconds, it should take to ramp the volume down to the desired level.
76
- */
77
- get rampDownDuration() {
78
- return this._rampDownDuration.seconds;
79
- }
80
- set rampDownDuration(value) {
81
- this._rampDownDuration.seconds = Math.abs(value);
82
- }
83
- /**
84
- * Amount of time, in seconds, it should take to ramp the volume back up to the original level.
85
- */
86
- get rampUpDuration() {
87
- return this._rampUpDuration.seconds;
88
- }
89
- set rampUpDuration(value) {
90
- this._rampUpDuration.seconds = Math.abs(value);
91
- }
92
- /**
93
- * Temporarily lowers an audio sources volume.
94
- */
95
- lowerVolume() {
96
- this._endTime = new Date().getTime() + this._duration.milliseconds;
97
- if (!this._running) {
98
- this.startLimiter();
99
- }
100
- }
101
- startLimiter() {
102
- const adjustVolume = () => {
103
- const now = new Date().getTime();
104
- if (now < this._endTime) {
105
- // Make no adjustment if muted
106
- if (!this._player.muted) {
107
- // Compute adjusted volume level
108
- let volume;
109
- const targetVolume = this.computeTargetVolume();
110
- const rampRange = this._startVolume - targetVolume;
111
- const rampUpStart = this._endTime - this._rampUpDuration.milliseconds;
112
- const rampDownEnd = this._startTime + this._rampDownDuration.milliseconds;
113
- if (now > rampUpStart) {
114
- // We're about to end so ramping up
115
- const progress = (now - rampUpStart) / this._rampUpDuration.milliseconds;
116
- volume = targetVolume + (rampRange * progress);
117
- this._player.volume = volume;
118
- this._inRamp = true;
119
- }
120
- else if (now < rampDownEnd) {
121
- // We just begun so ramping down
122
- const remaining = (rampDownEnd - now) / this._rampUpDuration.milliseconds;
123
- volume = targetVolume + (rampRange * remaining);
124
- this._player.volume = volume;
125
- this._inRamp = true;
126
- }
127
- else if (this._inRamp) {
128
- // We're at the end of a ramp
129
- volume = targetVolume;
130
- this._player.volume = volume;
131
- this._inRamp = false;
132
- }
133
- }
134
- // Schedule next animation frame
135
- this.scheduleAnimationFrame(adjustVolume);
136
- }
137
- else {
138
- // Reset volume to original level
139
- this._player.volume = this._startVolume;
140
- this._running = false;
141
- this._endTime = 0;
142
- this._startTime = 0;
143
- this._startVolume = 0;
144
- }
145
- };
146
- this._running = true;
147
- this._startTime = new Date().getTime();
148
- this._startVolume = this._player.volume;
149
- this.scheduleAnimationFrame(adjustVolume);
150
- }
151
- scheduleAnimationFrame(callback) {
152
- if (requestAnimationFrame) {
153
- requestAnimationFrame(callback);
154
- }
155
- else {
156
- setTimeout(callback, 20);
157
- }
158
- }
159
- computeTargetVolume() {
160
- switch (this._levelType) {
161
- case LevelType.percentage:
162
- return this._startVolume * this._level;
163
- case LevelType.fixed:
164
- default:
165
- return this._level;
166
- }
167
- }
168
- }
169
- exports.VolumeLimiter = VolumeLimiter;
170
- //# sourceMappingURL=VolumeLimiter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"VolumeLimiter.js","sourceRoot":"","sources":["../src/VolumeLimiter.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEF,sDAAqD;AAGtD,IAAY,SAA+B;AAA3C,WAAY,SAAS;IAAG,2CAAK,CAAA;IAAE,qDAAU,CAAA;AAAC,CAAC,EAA/B,SAAS,GAAT,iBAAS,KAAT,iBAAS,QAAsB;AAE3C,MAAa,aAAa;IAiBtB,YAAY,MAAoB;QAdhC,WAAW;QACM,cAAS,GAAG,IAAI,yBAAY,CAAC,IAAI,CAAC,CAAC;QACnC,sBAAiB,GAAG,IAAI,yBAAY,CAAC,GAAG,CAAC,CAAC;QAC1C,oBAAe,GAAG,IAAI,yBAAY,CAAC,GAAG,CAAC,CAAC;QACjD,WAAM,GAAW,GAAG,CAAA;QACpB,eAAU,GAAc,SAAS,CAAC,KAAK,CAAC;QAEhD,gBAAgB;QACR,aAAQ,GAAG,KAAK,CAAC;QACjB,aAAQ,GAAG,CAAC,CAAC;QACb,eAAU,GAAG,CAAC,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mB;QACvB,QAAQ,IAAI,CAAC,UAAU,EAAE;YACrB,KAAK,SAAS,CAAC,UAAU;gBACrB,OAAO,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC;YAC3C,KAAK,SAAS,CAAC,KAAK,CAAC;YACrB;gBACI,OAAO,IAAI,CAAC,MAAM,CAAC;SAC1B;IACL,CAAC;CACJ;AA3KD,sCA2KC"}
@@ -1,248 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { DataObject, DataObjectFactory } from '@fluidframework/aqueduct';
7
- import { EphemeralTelemetryLogger, UserMeetingRole } from '@microsoft/live-share';
8
- import { MediaPlayerSynchronizer } from './MediaPlayerSynchronizer';
9
- import { ITriggerActionEvent, TelemetryEvents } from './internals';
10
- import { MediaSessionCoordinatorEvents, ExtendedMediaSessionAction, ExtendedMediaSessionActionDetails } from './MediaSessionExtensions';
11
- import { EphemeralMediaSessionCoordinator, IMediaPlayerState } from './EphemeralMediaSessionCoordinator';
12
- import { MediaSessionActionThrottler } from './MediaSessionActionThrottler';
13
- import { RepeatedActionThrottler } from './RepeatedActionThrottler';
14
- import { IMediaPlayer } from './IMediaPlayer';
15
-
16
- /**
17
- * Ephemeral fluid object that synchronizes media playback across multiple clients.
18
- */
19
- export class EphemeralMediaSession extends DataObject {
20
- private _actionThrottler: MediaSessionActionThrottler = new RepeatedActionThrottler();
21
- private _logger?: EphemeralTelemetryLogger;
22
- private _requestPlayerStateHandler?: () => IMediaPlayerState;
23
- private _coordinator?: EphemeralMediaSessionCoordinator;
24
- private readonly _actionHandlers: Map<string, MediaSessionActionHandler> = new Map();
25
- private _synchronizing?: MediaPlayerSynchronizer;
26
-
27
- // Position update timer
28
- private _updateTimer: any;
29
- private _lastUpdateTime: number = 0;
30
-
31
- /**
32
- * The objects fluid type name.
33
- */
34
- public static readonly TypeName = `@microsoft/fluid-media:SharedMediaSession`;
35
-
36
- /**
37
- * The objects fluid type factory.
38
- */
39
- public static readonly factory = new DataObjectFactory(
40
- EphemeralMediaSession.TypeName,
41
- EphemeralMediaSession,
42
- [],
43
- {}
44
- );
45
-
46
- /**
47
- * Extension point that lets applications replace the default logic for throttling the sessions
48
- * local sync behavior.
49
- *
50
- * @remarks
51
- * The `EphemeralMediaCoordinator` is fairly aggressive at wanting to keep the local media player
52
- * in sync with the rest of the group. This aggressiveness can result in the coordinator sending
53
- * new sync actions before the local player has finished responding to the previous sync action.
54
- * The `ActionThrottler` gives apps fine grain control over how aggressive they want sync to be.
55
- *
56
- * By default, a `RepeatedAction` throttler is used which prevents the same sync action from
57
- * being sent within an adjustable time period.
58
- */
59
- public get actionThrottler(): MediaSessionActionThrottler {
60
- return this._actionThrottler;
61
- }
62
-
63
- public set actionThrottler(value: MediaSessionActionThrottler) {
64
- this._actionThrottler = value;
65
- }
66
-
67
- /**
68
- * Returns true if the object has been initialized.
69
- */
70
- public get isInitialized(): boolean {
71
- return this.coordinator.isInitialized;
72
- }
73
-
74
- /**
75
- * @deprecated isInitialized should be used instead
76
- * Returns true if the object has been initialized.
77
- */
78
- public get isStarted(): boolean {
79
- return this.isInitialized
80
- }
81
-
82
- /**
83
- * The group coordinator for the session.
84
- */
85
- public get coordinator(): EphemeralMediaSessionCoordinator {
86
- return this._coordinator!;
87
- }
88
-
89
- /**
90
- * Returns the logger used by the session and coordinator.
91
- *
92
- * @remarks
93
- * This is used by the `MediaPlayerSynchronizer` to log events.
94
- */
95
- public get logger(): EphemeralTelemetryLogger {
96
- return this._logger!;
97
- }
98
-
99
- /**
100
- * initialize the object.
101
- * @param acceptTransportChangesFrom Optional. List of roles allowed to group transport
102
- * operations like play/pause/seek/setTrack.
103
- */
104
- public initialize(acceptTransportChangesFrom?: UserMeetingRole[]): Promise<void> {
105
- return this.coordinator.initialize(acceptTransportChangesFrom);
106
- }
107
-
108
- /**
109
- * @deprecated initialize should be used instead
110
- * initialize the object.
111
- * @param acceptTransportChangesFrom Optional. List of roles allowed to group transport
112
- * operations like play/pause/seek/setTrack.
113
- */
114
- public start(acceptTransportChangesFrom?: UserMeetingRole[]): Promise<void> {
115
- return this.initialize(acceptTransportChangesFrom)
116
- }
117
-
118
- /**
119
- * Registers an action handler with the session.
120
- * @param action Name of the action to register a handler for.
121
- * @param handler Function called when the action is triggered.
122
- */
123
- public setActionHandler(action: ExtendedMediaSessionAction, handler: MediaSessionActionHandler | null): void {
124
- if (handler) {
125
- // add handler
126
- this._actionHandlers.set(action, handler);
127
-
128
- } else if (this._actionHandlers.has(action)) {
129
- // remove handler
130
- this._actionHandlers.delete(action);
131
- }
132
- }
133
-
134
- /**
135
- * Registers a handler that will be queried anytime the group coordinate needs to know the
136
- * local players transport state and position.
137
- */
138
- public setRequestPlayerStateHandler(handler: () => IMediaPlayerState) {
139
- this._requestPlayerStateHandler = handler;
140
- }
141
-
142
- /**
143
- * Begins synchronizing the playback of a media element.
144
- * @param player Something that "looks like" and HTML Media Element.
145
- * @returns A new synchronizer instance. Call `synchronizer.end()` to stop synchronizing the elements playback.
146
- */
147
- public synchronize(player: IMediaPlayer): MediaPlayerSynchronizer {
148
- // A session can only synchronize one player at a time
149
- if (this._synchronizing) {
150
- this._synchronizing.end();
151
- }
152
-
153
- // Start position update timer
154
- if (!this._updateTimer) {
155
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.BeginPositionUpdateTimer);
156
- this._updateTimer = setInterval(() => {
157
- // Get current state
158
- const state = this.getCurrentPlayerState();
159
-
160
- // Check for hit wait point
161
- this.checkWaitPointHit(state);
162
-
163
- // Send position update if interval hit
164
- const now = new Date().getTime();
165
- const delta = (now - this._lastUpdateTime) / 1000;
166
- if (this.coordinator.isInitialized && delta >= this.coordinator.positionUpdateInterval) {
167
- this._lastUpdateTime = now;
168
- this.coordinator.sendPositionUpdate(state);
169
- }
170
- }, 500);
171
- }
172
-
173
- return new MediaPlayerSynchronizer(player, this, () => {
174
- this._synchronizing = undefined;
175
- });
176
- }
177
-
178
- protected async hasInitialized(): Promise<void> {
179
- this. _logger = new EphemeralTelemetryLogger(this.runtime);
180
-
181
- // Create coordinator and listen for triggered actions
182
- this._coordinator = new EphemeralMediaSessionCoordinator(this.runtime, () => this.getCurrentPlayerState());
183
- this._coordinator.on(MediaSessionCoordinatorEvents.triggeraction, (event: ITriggerActionEvent) => {
184
- // Pre-process actions
185
- const details = event.details;
186
- const seekTime = typeof details.seekTime == 'number' ? details.seekTime : -1;
187
- switch (details.action) {
188
- case 'play':
189
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.PlayAction, null, {position: seekTime});
190
- break;
191
- case 'pause':
192
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.PauseAction, null, {position: seekTime});
193
- break;
194
- case 'seekto':
195
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.SeekToAction, null, {position: seekTime});
196
- break;
197
- case 'settrack':
198
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.SetTrackAction);
199
- break;
200
- case 'datachange':
201
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.DataChangeAction);
202
- break;
203
- case 'catchup':
204
- // Default to seekto if catchup not implemented
205
- this.logger.sendTelemetryEvent(TelemetryEvents.MediaSession.CatchUpAction, null, {position: seekTime});
206
- if (!this._actionHandlers.has('catchup')) {
207
- details.action = 'seekto';
208
- }
209
- break;
210
- }
211
-
212
- // Dispatch action
213
- this.dispatchAction(details);
214
- });
215
- }
216
-
217
- private checkWaitPointHit(state: IMediaPlayerState): void {
218
- // Was a wait point hit?
219
- if (state.positionState && this.coordinator.isInitialized) {
220
- const waitPoint = this.coordinator.findNextWaitPoint();
221
- if (waitPoint && state.positionState.position != undefined && state.positionState.position >= waitPoint.position) {
222
- // Ensure handler registered
223
- if (!this._actionHandlers.has('wait')) {
224
- throw new Error(`SharedMediaSession: wait point hit but no 'wait' action registered.`);
225
- }
226
-
227
- // Begin suspension for wait point
228
- const suspension = this.coordinator.beginSuspension(waitPoint);
229
- this.dispatchAction({action: 'wait', suspension: suspension});
230
- }
231
- }
232
- }
233
-
234
- private getCurrentPlayerState(): IMediaPlayerState {
235
- if (!this._requestPlayerStateHandler) {
236
- throw new Error(`SharedMediaSession: no getPlayerState callback configured.`);
237
- }
238
-
239
- return this._requestPlayerStateHandler();
240
- }
241
-
242
- private dispatchAction(details: MediaSessionActionDetails|ExtendedMediaSessionActionDetails): void {
243
- if (this._actionHandlers.has(details.action)) {
244
- const handler = this._actionHandlers.get(details.action);
245
- this._actionThrottler.throttled(details, handler);
246
- }
247
- }
248
- }