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

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 (68) 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/LiveMediaSession.d.ts +95 -0
  7. package/bin/LiveMediaSession.d.ts.map +1 -0
  8. package/bin/LiveMediaSession.js +217 -0
  9. package/bin/LiveMediaSession.js.map +1 -0
  10. package/bin/LiveMediaSessionCoordinator.d.ts +228 -0
  11. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -0
  12. package/bin/LiveMediaSessionCoordinator.js +396 -0
  13. package/bin/LiveMediaSessionCoordinator.js.map +1 -0
  14. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts +20 -0
  15. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts.map +1 -0
  16. package/bin/LiveMediaSessionCoordinatorSuspension.js +28 -0
  17. package/bin/LiveMediaSessionCoordinatorSuspension.js.map +1 -0
  18. package/bin/MediaPlayerSynchronizer.d.ts +6 -6
  19. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  20. package/bin/MediaPlayerSynchronizer.js +12 -7
  21. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  22. package/bin/VolumeManager.d.ts +84 -0
  23. package/bin/VolumeManager.d.ts.map +1 -0
  24. package/bin/VolumeManager.js +173 -0
  25. package/bin/VolumeManager.js.map +1 -0
  26. package/bin/index.d.ts +4 -4
  27. package/bin/index.d.ts.map +1 -1
  28. package/bin/index.js +4 -4
  29. package/bin/index.js.map +1 -1
  30. package/bin/internals/GroupCoordinatorState.d.ts +6 -6
  31. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  32. package/bin/internals/GroupCoordinatorState.js +6 -6
  33. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  34. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  35. package/bin/internals/GroupPlaybackPosition.js +2 -2
  36. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  37. package/bin/internals/GroupPlaybackTrack.d.ts +1 -1
  38. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  39. package/bin/internals/GroupTransportState.d.ts +1 -1
  40. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  41. package/bin/internals/GroupTransportState.js +1 -1
  42. package/bin/internals/GroupTransportState.js.map +1 -1
  43. package/package.json +38 -38
  44. package/src/IMediaPlayer.ts +21 -21
  45. package/src/{EphemeralMediaSession.ts → LiveMediaSession.ts} +238 -248
  46. package/src/{EphemeralMediaSessionCoordinator.ts → LiveMediaSessionCoordinator.ts} +461 -461
  47. package/src/{EphemeralMediaSessionCoordinatorSuspension.ts → LiveMediaSessionCoordinatorSuspension.ts} +30 -30
  48. package/src/MediaPlayerSynchronizer.ts +455 -449
  49. package/src/MediaSessionActionThrottler.ts +14 -14
  50. package/src/MediaSessionExtensions.ts +42 -42
  51. package/src/RepeatedActionThrottler.ts +55 -55
  52. package/src/VolumeManager.ts +185 -0
  53. package/src/index.ts +15 -15
  54. package/src/internals/GroupCoordinatorState.ts +445 -445
  55. package/src/internals/GroupPlaybackPosition.ts +193 -193
  56. package/src/internals/GroupPlaybackTrack.ts +147 -147
  57. package/src/internals/GroupPlaybackTrackData.ts +90 -90
  58. package/src/internals/GroupTransportState.ts +127 -127
  59. package/src/internals/consts.ts +80 -80
  60. package/src/internals/index.ts +10 -10
  61. package/src/test/GroupPlaybackPosition.spec.ts +339 -339
  62. package/src/test/GroupPlaybackTrack.spec.ts +65 -65
  63. package/src/test/SharedMediaSession.spec.ts +113 -113
  64. package/src/test/SharedMediaSessionCoordinator.spec.ts +230 -230
  65. package/src/test/VolumeManager.spec.ts +212 -0
  66. package/tsconfig.build.json +8 -8
  67. package/tsconfig.json +75 -75
  68. package/src/VolumeLimiter.ts +0 -182
@@ -1,231 +1,231 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import 'mocha';
7
- import { strict as assert } from 'assert';
8
- import { SimulatedBroadcastHub, SimulatedCollaborationSpace, IParticipant, ParticipantRole, Deferred } from '@microsoft/teams-collaboration';
9
- import { ExtendedMediaMetadata } from './MediaSessionExtensions';
10
- import { ICurrentPlaybackPosition } from './coordinatorState';
11
- import { TestMediaSession } from './SharedMediaSession.spec';
12
-
13
- describe('SharedMediaSessionCoordinator', () => {
14
- const user1: IParticipant = { participantId: 'user1', role: ParticipantRole.organizer };
15
- const user2: IParticipant = { participantId: 'user2', role: ParticipantRole.participant };
16
- const testTrack: ExtendedMediaMetadata = {
17
- trackIdentifier: 'https://example.org/test.mp3',
18
- title: 'Test Title',
19
- album: 'Test Album',
20
- artist: 'The Testers',
21
- artwork: []
22
- };
23
- const leaderPosition: Partial<ICurrentPlaybackPosition> = {
24
- playbackState: 'playing',
25
- position: 25.0,
26
- duration: 600.0
27
- };
28
-
29
- const laggerPosition: Partial<ICurrentPlaybackPosition> = {
30
- playbackState: 'playing',
31
- position: 20.0,
32
- duration: 600.0
33
- };
34
-
35
- it('should send "joined" event when joined.', async () => {
36
- const hub = new SimulatedBroadcastHub();
37
- const space1 = new SimulatedCollaborationSpace(user1, hub);
38
- const space2 = new SimulatedCollaborationSpace(user2, hub);
39
-
40
- try {
41
- let cnt = 0;
42
- const session = new TestMediaSession(space1);
43
- await space2.join();
44
- space2.socket.addBroadcastListener(session.name, 'joined', async (event) => {
45
- cnt++;
46
- });
47
-
48
- await space1.join();
49
- assert(cnt == 1, `joined event not sent`);
50
- } finally {
51
- await space1.leave();
52
- await space2.leave();
53
- }
54
- });
55
-
56
- it('should send "positionUpdate" event regularly.', async () => {
57
- const hub = new SimulatedBroadcastHub();
58
- const space1 = new SimulatedCollaborationSpace(user1, hub);
59
- const space2 = new SimulatedCollaborationSpace(user2, hub);
60
-
61
- try {
62
- let cnt = 0;
63
- const done = new Deferred();
64
- const session = new TestMediaSession(space1, testTrack);
65
- session.coordinator.positionUpdateInterval = 0.02;
66
- await space2.join();
67
- space2.socket.addBroadcastListener(session.name, 'positionUpdate', async (event) => {
68
- cnt++;
69
- if (cnt > 1) {
70
- done.resolve();
71
- }
72
- });
73
-
74
- await session.coordinator.join();
75
- await session.coordinator.play();
76
- await done.promise;
77
- } finally {
78
- await space1.leave();
79
- await space2.leave();
80
- }
81
- });
82
-
83
- it('should broadcast "play" transport command.', async () => {
84
- const hub = new SimulatedBroadcastHub();
85
- const space1 = new SimulatedCollaborationSpace(user1, hub);
86
- const space2 = new SimulatedCollaborationSpace(user2, hub);
87
-
88
- try {
89
- const session1 = new TestMediaSession(space1, testTrack);
90
- const session2 = new TestMediaSession(space2, testTrack);
91
-
92
- await space1.join();
93
- await space2.join();
94
-
95
- // Listen for action
96
- const done = new Deferred();
97
-
98
- // Start playback
99
- await session1.coordinator.play();
100
-
101
- // Wait for action to be broadcast
102
- await session2.waitForAction(async (details) => {
103
- assert(details.action == 'play', `wrong action received`);
104
- });
105
- } finally {
106
- await space1.leave();
107
- await space2.leave();
108
- }
109
- });
110
-
111
- it('should broadcast "pause" transport command.', async () => {
112
- const hub = new SimulatedBroadcastHub();
113
- const space1 = new SimulatedCollaborationSpace(user1, hub);
114
- const space2 = new SimulatedCollaborationSpace(user2, hub);
115
-
116
- try {
117
- const session1 = new TestMediaSession(space1, testTrack);
118
- const session2 = new TestMediaSession(space2, testTrack);
119
-
120
- await session1.coordinator.join();
121
- await space2.join();
122
-
123
- // Start playback
124
- await session1.coordinator.play();
125
-
126
- // Wait for action to be broadcast
127
- await session2.waitForAction(async (details) => {
128
- assert(details.action == 'play', `didn't start playback`);
129
-
130
- // Pause playback
131
- await session1.coordinator.pause();
132
- await session2.waitForAction(async (details) => {
133
- assert(details.action == 'pause', `didn't pause playback`);
134
- });
135
- });
136
- } finally {
137
- await space1.leave();
138
- await space2.leave();
139
- }
140
- });
141
-
142
- it('should broadcast "seekTo" transport command.', async () => {
143
- const hub = new SimulatedBroadcastHub();
144
- const space1 = new SimulatedCollaborationSpace(user1, hub);
145
- const space2 = new SimulatedCollaborationSpace(user2, hub);
146
-
147
- try {
148
- const session1 = new TestMediaSession(space1, testTrack);
149
- const session2 = new TestMediaSession(space2, testTrack);
150
-
151
- await session1.coordinator.join();
152
- await space2.join();
153
-
154
- // Start playback
155
- await session1.coordinator.play();
156
-
157
- // Wait for action to be broadcast
158
- await session2.waitForAction(async (details) => {
159
- assert(details.action == 'play', `didn't start playback`);
160
-
161
- // Seek to position
162
- await session1.coordinator.seekTo(2.3);
163
- await session2.waitForAction(async (details) => {
164
- assert(details.action == 'seekto', `wong action received "${details.action}"`);
165
- assert(details.seekTime >= 2.3, `wrong seek position "${details.seekTime}"`)
166
- });
167
- });
168
- } finally {
169
- await space1.leave();
170
- await space2.leave();
171
- }
172
- });
173
-
174
- it('should broadcast "setTrack" transport command.', async () => {
175
- const hub = new SimulatedBroadcastHub();
176
- const space1 = new SimulatedCollaborationSpace(user1, hub);
177
- const space2 = new SimulatedCollaborationSpace(user2, hub);
178
-
179
- try {
180
- const session1 = new TestMediaSession(space1);
181
- const session2 = new TestMediaSession(space2);
182
-
183
- await space1.join();
184
- await space2.join();
185
-
186
- // Change tracks
187
- await session1.coordinator.setTrack(testTrack);
188
-
189
- // Wait for action to be broadcast
190
- await session2.waitForAction(async (details) => {
191
- assert(details.action == 'settrack', `wrong action received "${details.action}"`);
192
- assert(JSON.stringify(session1.metadata) == JSON.stringify(testTrack), `session1 has wrong track: ${JSON.stringify(session1.metadata)}`);
193
- assert(JSON.stringify(session2.metadata) == JSON.stringify(testTrack), `session2 has wrong track: ${JSON.stringify(session2.metadata)}`);
194
- });
195
- } finally {
196
- await space1.leave();
197
- await space2.leave();
198
- }
199
- });
200
-
201
- // it('should trigger "catchup" action.', async () => {
202
- // const hub = new SimulatedBroadcastHub();
203
- // const space1 = new SimulatedCollaborationSpace(user1, hub);
204
- // const space2 = new SimulatedCollaborationSpace(user2, hub);
205
-
206
- // try {
207
- // const session1 = new TestMediaSession(testTrack, leaderPosition);
208
- // const session1.coordinator = new SharedMediaSessionCoordinator(space1, session1, session1.name);
209
- // session1.coordinator.positionUpdateInterval = 0.02;
210
- // const session2 = new TestMediaSession(testTrack, laggerPosition);
211
- // const coordinator2 = new SharedMediaSessionCoordinator(space2, session2, session2.name);
212
- // coordinator2.positionUpdateInterval = 0.02;
213
-
214
- // await session1.coordinator.join();
215
- // await coordinator2.join();
216
-
217
- // await session1.coordinator.play();
218
-
219
- // // Wait for action to be broadcast
220
- // session2.reset();
221
- // await session2.waitForAction(async () => {
222
- // assert(session2.isOutOfSync, `session2 should be out of sync`);
223
- // assert(session2.targetPosition >= leaderPosition.position, `session2 returned target position of ${session2.targetPosition}`);
224
- // });
225
- // } finally {
226
- // await space1.leave();
227
- // await space2.leave();
228
- // }
229
- // });
230
-
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import 'mocha';
7
+ import { strict as assert } from 'assert';
8
+ import { SimulatedBroadcastHub, SimulatedCollaborationSpace, IParticipant, ParticipantRole, Deferred } from '@microsoft/teams-collaboration';
9
+ import { ExtendedMediaMetadata } from './MediaSessionExtensions';
10
+ import { ICurrentPlaybackPosition } from './coordinatorState';
11
+ import { TestMediaSession } from './SharedMediaSession.spec';
12
+
13
+ describe('SharedMediaSessionCoordinator', () => {
14
+ const user1: IParticipant = { participantId: 'user1', role: ParticipantRole.organizer };
15
+ const user2: IParticipant = { participantId: 'user2', role: ParticipantRole.participant };
16
+ const testTrack: ExtendedMediaMetadata = {
17
+ trackIdentifier: 'https://example.org/test.mp3',
18
+ title: 'Test Title',
19
+ album: 'Test Album',
20
+ artist: 'The Testers',
21
+ artwork: []
22
+ };
23
+ const leaderPosition: Partial<ICurrentPlaybackPosition> = {
24
+ playbackState: 'playing',
25
+ position: 25.0,
26
+ duration: 600.0
27
+ };
28
+
29
+ const laggerPosition: Partial<ICurrentPlaybackPosition> = {
30
+ playbackState: 'playing',
31
+ position: 20.0,
32
+ duration: 600.0
33
+ };
34
+
35
+ it('should send "joined" event when joined.', async () => {
36
+ const hub = new SimulatedBroadcastHub();
37
+ const space1 = new SimulatedCollaborationSpace(user1, hub);
38
+ const space2 = new SimulatedCollaborationSpace(user2, hub);
39
+
40
+ try {
41
+ let cnt = 0;
42
+ const session = new TestMediaSession(space1);
43
+ await space2.join();
44
+ space2.socket.addBroadcastListener(session.name, 'joined', async (event) => {
45
+ cnt++;
46
+ });
47
+
48
+ await space1.join();
49
+ assert(cnt == 1, `joined event not sent`);
50
+ } finally {
51
+ await space1.leave();
52
+ await space2.leave();
53
+ }
54
+ });
55
+
56
+ it('should send "positionUpdate" event regularly.', async () => {
57
+ const hub = new SimulatedBroadcastHub();
58
+ const space1 = new SimulatedCollaborationSpace(user1, hub);
59
+ const space2 = new SimulatedCollaborationSpace(user2, hub);
60
+
61
+ try {
62
+ let cnt = 0;
63
+ const done = new Deferred();
64
+ const session = new TestMediaSession(space1, testTrack);
65
+ session.coordinator.positionUpdateInterval = 0.02;
66
+ await space2.join();
67
+ space2.socket.addBroadcastListener(session.name, 'positionUpdate', async (event) => {
68
+ cnt++;
69
+ if (cnt > 1) {
70
+ done.resolve();
71
+ }
72
+ });
73
+
74
+ await session.coordinator.join();
75
+ await session.coordinator.play();
76
+ await done.promise;
77
+ } finally {
78
+ await space1.leave();
79
+ await space2.leave();
80
+ }
81
+ });
82
+
83
+ it('should broadcast "play" transport command.', async () => {
84
+ const hub = new SimulatedBroadcastHub();
85
+ const space1 = new SimulatedCollaborationSpace(user1, hub);
86
+ const space2 = new SimulatedCollaborationSpace(user2, hub);
87
+
88
+ try {
89
+ const session1 = new TestMediaSession(space1, testTrack);
90
+ const session2 = new TestMediaSession(space2, testTrack);
91
+
92
+ await space1.join();
93
+ await space2.join();
94
+
95
+ // Listen for action
96
+ const done = new Deferred();
97
+
98
+ // Start playback
99
+ await session1.coordinator.play();
100
+
101
+ // Wait for action to be broadcast
102
+ await session2.waitForAction(async (details) => {
103
+ assert(details.action == 'play', `wrong action received`);
104
+ });
105
+ } finally {
106
+ await space1.leave();
107
+ await space2.leave();
108
+ }
109
+ });
110
+
111
+ it('should broadcast "pause" transport command.', async () => {
112
+ const hub = new SimulatedBroadcastHub();
113
+ const space1 = new SimulatedCollaborationSpace(user1, hub);
114
+ const space2 = new SimulatedCollaborationSpace(user2, hub);
115
+
116
+ try {
117
+ const session1 = new TestMediaSession(space1, testTrack);
118
+ const session2 = new TestMediaSession(space2, testTrack);
119
+
120
+ await session1.coordinator.join();
121
+ await space2.join();
122
+
123
+ // Start playback
124
+ await session1.coordinator.play();
125
+
126
+ // Wait for action to be broadcast
127
+ await session2.waitForAction(async (details) => {
128
+ assert(details.action == 'play', `didn't start playback`);
129
+
130
+ // Pause playback
131
+ await session1.coordinator.pause();
132
+ await session2.waitForAction(async (details) => {
133
+ assert(details.action == 'pause', `didn't pause playback`);
134
+ });
135
+ });
136
+ } finally {
137
+ await space1.leave();
138
+ await space2.leave();
139
+ }
140
+ });
141
+
142
+ it('should broadcast "seekTo" transport command.', async () => {
143
+ const hub = new SimulatedBroadcastHub();
144
+ const space1 = new SimulatedCollaborationSpace(user1, hub);
145
+ const space2 = new SimulatedCollaborationSpace(user2, hub);
146
+
147
+ try {
148
+ const session1 = new TestMediaSession(space1, testTrack);
149
+ const session2 = new TestMediaSession(space2, testTrack);
150
+
151
+ await session1.coordinator.join();
152
+ await space2.join();
153
+
154
+ // Start playback
155
+ await session1.coordinator.play();
156
+
157
+ // Wait for action to be broadcast
158
+ await session2.waitForAction(async (details) => {
159
+ assert(details.action == 'play', `didn't start playback`);
160
+
161
+ // Seek to position
162
+ await session1.coordinator.seekTo(2.3);
163
+ await session2.waitForAction(async (details) => {
164
+ assert(details.action == 'seekto', `wong action received "${details.action}"`);
165
+ assert(details.seekTime >= 2.3, `wrong seek position "${details.seekTime}"`)
166
+ });
167
+ });
168
+ } finally {
169
+ await space1.leave();
170
+ await space2.leave();
171
+ }
172
+ });
173
+
174
+ it('should broadcast "setTrack" transport command.', async () => {
175
+ const hub = new SimulatedBroadcastHub();
176
+ const space1 = new SimulatedCollaborationSpace(user1, hub);
177
+ const space2 = new SimulatedCollaborationSpace(user2, hub);
178
+
179
+ try {
180
+ const session1 = new TestMediaSession(space1);
181
+ const session2 = new TestMediaSession(space2);
182
+
183
+ await space1.join();
184
+ await space2.join();
185
+
186
+ // Change tracks
187
+ await session1.coordinator.setTrack(testTrack);
188
+
189
+ // Wait for action to be broadcast
190
+ await session2.waitForAction(async (details) => {
191
+ assert(details.action == 'settrack', `wrong action received "${details.action}"`);
192
+ assert(JSON.stringify(session1.metadata) == JSON.stringify(testTrack), `session1 has wrong track: ${JSON.stringify(session1.metadata)}`);
193
+ assert(JSON.stringify(session2.metadata) == JSON.stringify(testTrack), `session2 has wrong track: ${JSON.stringify(session2.metadata)}`);
194
+ });
195
+ } finally {
196
+ await space1.leave();
197
+ await space2.leave();
198
+ }
199
+ });
200
+
201
+ // it('should trigger "catchup" action.', async () => {
202
+ // const hub = new SimulatedBroadcastHub();
203
+ // const space1 = new SimulatedCollaborationSpace(user1, hub);
204
+ // const space2 = new SimulatedCollaborationSpace(user2, hub);
205
+
206
+ // try {
207
+ // const session1 = new TestMediaSession(testTrack, leaderPosition);
208
+ // const session1.coordinator = new SharedMediaSessionCoordinator(space1, session1, session1.name);
209
+ // session1.coordinator.positionUpdateInterval = 0.02;
210
+ // const session2 = new TestMediaSession(testTrack, laggerPosition);
211
+ // const coordinator2 = new SharedMediaSessionCoordinator(space2, session2, session2.name);
212
+ // coordinator2.positionUpdateInterval = 0.02;
213
+
214
+ // await session1.coordinator.join();
215
+ // await coordinator2.join();
216
+
217
+ // await session1.coordinator.play();
218
+
219
+ // // Wait for action to be broadcast
220
+ // session2.reset();
221
+ // await session2.waitForAction(async () => {
222
+ // assert(session2.isOutOfSync, `session2 should be out of sync`);
223
+ // assert(session2.targetPosition >= leaderPosition.position, `session2 returned target position of ${session2.targetPosition}`);
224
+ // });
225
+ // } finally {
226
+ // await space1.leave();
227
+ // await space2.leave();
228
+ // }
229
+ // });
230
+
231
231
  });
@@ -0,0 +1,212 @@
1
+ import { strict as assert } from "assert";
2
+ import { IMediaPlayer } from "../IMediaPlayer"
3
+ import { LimitLevelType, VolumeManager } from "../VolumeManager"
4
+ import { Deferred } from '@microsoft/live-share/src/test/Deferred';
5
+
6
+ describe('VolumeManager', () => {
7
+ it('should ramp down volume', async () => {
8
+ const testAwait = new Deferred();
9
+ const player = new TestMediaPlayer();
10
+ player.volume = 1.0
11
+ const volumeManager = new VolumeManager(player);
12
+
13
+ assert(player.volume == 1.0);
14
+ volumeManager.startLimiting();
15
+ setTimeout(() => {
16
+ // check volume at halfway point
17
+ assert(player.volume > 0.4);
18
+ assert(player.volume < 0.6);
19
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
20
+
21
+ setTimeout(() => {
22
+ // check volume at end with 20ms of leeway
23
+ assert(player.volume == volumeManager.limitLevel);
24
+ testAwait.resolve();
25
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
26
+
27
+ await testAwait.promise;
28
+ });
29
+
30
+ it('should ramp up volume', async () => {
31
+ const testAwait = new Deferred();
32
+ const player = new TestMediaPlayer();
33
+ player.volume = 1.0
34
+ const volumeManager = new VolumeManager(player);
35
+
36
+ // limit at start
37
+ volumeManager.startLimiting();
38
+
39
+ // when limited all the way, test ramp up
40
+ setTimeout(() => {
41
+ assert(player.volume == volumeManager.limitLevel);
42
+ volumeManager.stopLimiting();
43
+
44
+ setTimeout(() => {
45
+ // check volume at halfway point
46
+ assert(player.volume > 0.4);
47
+ assert(player.volume < 0.6);
48
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
49
+
50
+ setTimeout(() => {
51
+ // check volume at end with 20ms of leeway
52
+ assert(player.volume == volumeManager.volume);
53
+ testAwait.resolve();
54
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
55
+
56
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
57
+
58
+ await testAwait.promise;
59
+ });
60
+
61
+ it('should ramp down halfway, then ramp up', async () => {
62
+ const testAwait = new Deferred();
63
+ const player = new TestMediaPlayer();
64
+ player.volume = 1.0
65
+
66
+ const volumeManager = new VolumeManager(player);
67
+
68
+ volumeManager.startLimiting();
69
+ setTimeout(() => {
70
+ // check volume at halfway point, begin ramping other direction
71
+ assert(player.volume > 0.4);
72
+ assert(player.volume < 0.6);
73
+ volumeManager.stopLimiting();
74
+
75
+ setTimeout(() => {
76
+ // check volume at end with 20ms of leeway
77
+ assert(player.volume == volumeManager.volume);
78
+ testAwait.resolve();
79
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
80
+
81
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
82
+
83
+ await testAwait.promise;
84
+ });
85
+
86
+ it('ramp using selected volume', async () => {
87
+ const testAwait = new Deferred();
88
+ const player = new TestMediaPlayer();
89
+ player.volume = 1.0
90
+
91
+ const volumeManager = new VolumeManager(player);
92
+
93
+ volumeManager.volume = 0.3;
94
+ setTimeout(() => {
95
+ // check volume at halfway point
96
+ assert(player.volume > 0.6);
97
+ assert(player.volume < 0.7);
98
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
99
+
100
+ setTimeout(() => {
101
+ // check volume at end with 20ms of leeway
102
+ assert(player.volume == volumeManager.volume);
103
+ testAwait.resolve();
104
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
105
+
106
+ await testAwait.promise;
107
+ });
108
+
109
+ it('enable limit, change selected volume, then disable limit', async () => {
110
+ const testAwait = new Deferred();
111
+ const player = new TestMediaPlayer();
112
+ player.volume = 1.0
113
+ const volumeManager = new VolumeManager(player);
114
+
115
+ volumeManager.startLimiting();
116
+ setTimeout(() => {
117
+ // check volume at halfway point, begin ramping other direction with lower selected volume
118
+ assert(player.volume > 0.4);
119
+ assert(player.volume < 0.6);
120
+
121
+ volumeManager.volume = 0.7;
122
+ volumeManager.stopLimiting();
123
+
124
+ setTimeout(() => {
125
+ // check volume at end with 20ms of leeway
126
+ assert(player.volume === volumeManager.volume);
127
+ testAwait.resolve();
128
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
129
+
130
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
131
+
132
+ await testAwait.promise;
133
+ });
134
+
135
+ it('ramp down with selected volume to 0.3, then up to 0.8', async () => {
136
+ const testAwait = new Deferred();
137
+ const player = new TestMediaPlayer();
138
+ player.volume = 1.0
139
+
140
+ const volumeManager = new VolumeManager(player);
141
+ volumeManager.volume = 0.3;
142
+
143
+ setTimeout(() => {
144
+ // check volume at halfway point
145
+ assert(player.volume > 0.6);
146
+ assert(player.volume < 0.7);
147
+ volumeManager.volume = 0.8;
148
+
149
+ setTimeout(() => {
150
+ // check volume at end with 20ms of leeway
151
+ assert(player.volume == volumeManager.volume);
152
+ testAwait.resolve();
153
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
154
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
155
+
156
+ await testAwait.promise;
157
+ });
158
+
159
+ it('test 50% limiting', async () => {
160
+ const testAwait = new Deferred();
161
+ const player = new TestMediaPlayer();
162
+ player.volume = 1.0
163
+
164
+ const volumeManager = new VolumeManager(player);
165
+ volumeManager.limitLevelType = LimitLevelType.percentage;
166
+ volumeManager.limitLevel = 0.5;
167
+
168
+ volumeManager.startLimiting();
169
+
170
+ setTimeout(() => {
171
+ // check volume at halfway point
172
+ assert(player.volume > 0.7);
173
+ assert(player.volume < 0.8);
174
+ }, volumeManager.volumeChangeDuration * 1000 / 2);
175
+
176
+ setTimeout(() => {
177
+ // check volume at end with 20ms of leeway
178
+ assert(player.volume == volumeManager.limitLevel * volumeManager.volume);
179
+ testAwait.resolve();
180
+ }, volumeManager.volumeChangeDuration * 1000 + 20);
181
+
182
+ await testAwait.promise;
183
+ });
184
+ });
185
+
186
+ // test implementation
187
+ class TestMediaPlayer implements IMediaPlayer {
188
+ currentSrc: string;
189
+ currentTime: number;
190
+ duration: number;
191
+ ended: boolean;
192
+ muted: boolean;
193
+ paused: boolean;
194
+ playbackRate: number;
195
+ src: string;
196
+ volume: number;
197
+ load(): void {
198
+ throw new Error("Method not implemented.");
199
+ }
200
+ pause(): void {
201
+ throw new Error("Method not implemented.");
202
+ }
203
+ play(): Promise<void> {
204
+ throw new Error("Method not implemented.");
205
+ }
206
+ addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void {
207
+ throw new Error("Method not implemented.");
208
+ }
209
+ removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions | undefined): void {
210
+ throw new Error("Method not implemented.");
211
+ }
212
+ }