@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,14 +1,14 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { ExtendedMediaSessionActionDetails } from './MediaSessionExtensions';
7
-
8
- /**
9
- * Base class for action throttlers.
10
- */
11
- export abstract class MediaSessionActionThrottler {
12
- public abstract throttled(details: ExtendedMediaSessionActionDetails, handler?: MediaSessionActionHandler): void;
13
- }
14
-
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { ExtendedMediaSessionActionDetails } from './MediaSessionExtensions';
7
+
8
+ /**
9
+ * Base class for action throttlers.
10
+ */
11
+ export abstract class MediaSessionActionThrottler {
12
+ public abstract throttled(details: ExtendedMediaSessionActionDetails, handler?: MediaSessionActionHandler): void;
13
+ }
14
+
@@ -1,42 +1,42 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- export interface MediaSessionCoordinatorSuspension {
7
- waitPoint?: CoordinationWaitPoint;
8
- end(seekTo?: number): void;
9
- }
10
-
11
- export type MediaSessionCoordinatorState = "closed" | "waiting" | "joined";
12
-
13
- export enum MediaSessionCoordinatorEvents {
14
- coordinatorstatechange = 'coordinatorstatechange',
15
- triggeraction = 'triggeraction'
16
- }
17
-
18
- export type ExtendedMediaSessionAction = MediaSessionAction | "settrack" | "wait" | "catchup" | "datachange" | "blocked";
19
-
20
- export type ExtendedMediaSessionPlaybackState = MediaSessionPlaybackState | "suspended" | "waiting" | "ended";
21
-
22
- export interface ExtendedMediaMetadata extends MediaMetadata {
23
- trackIdentifier: string;
24
- liveStream: boolean;
25
- }
26
-
27
- export interface CoordinationWaitPoint {
28
- position: number;
29
- reason?: string;
30
- maxClients?: number;
31
- }
32
-
33
- export interface ExtendedMediaSessionActionDetails {
34
- action: ExtendedMediaSessionAction;
35
- fastSeek?: boolean | null;
36
- seekOffset?: number | null;
37
- seekTime?: number | null;
38
- metadata?: ExtendedMediaMetadata | null;
39
- suspension?: MediaSessionCoordinatorSuspension | null;
40
- data?: object | null;
41
- blocked?: ExtendedMediaSessionAction | null;
42
- }
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ export interface MediaSessionCoordinatorSuspension {
7
+ waitPoint?: CoordinationWaitPoint;
8
+ end(seekTo?: number): void;
9
+ }
10
+
11
+ export type MediaSessionCoordinatorState = "closed" | "waiting" | "joined";
12
+
13
+ export enum MediaSessionCoordinatorEvents {
14
+ coordinatorstatechange = 'coordinatorstatechange',
15
+ triggeraction = 'triggeraction'
16
+ }
17
+
18
+ export type ExtendedMediaSessionAction = MediaSessionAction | "settrack" | "wait" | "catchup" | "datachange" | "blocked";
19
+
20
+ export type ExtendedMediaSessionPlaybackState = MediaSessionPlaybackState | "suspended" | "waiting" | "ended";
21
+
22
+ export interface ExtendedMediaMetadata extends MediaMetadata {
23
+ trackIdentifier: string;
24
+ liveStream: boolean;
25
+ }
26
+
27
+ export interface CoordinationWaitPoint {
28
+ position: number;
29
+ reason?: string;
30
+ maxClients?: number;
31
+ }
32
+
33
+ export interface ExtendedMediaSessionActionDetails {
34
+ action: ExtendedMediaSessionAction;
35
+ fastSeek?: boolean | null;
36
+ seekOffset?: number | null;
37
+ seekTime?: number | null;
38
+ metadata?: ExtendedMediaMetadata | null;
39
+ suspension?: MediaSessionCoordinatorSuspension | null;
40
+ data?: object | null;
41
+ blocked?: ExtendedMediaSessionAction | null;
42
+ }
@@ -1,56 +1,56 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { TimeInterval } from '@microsoft/live-share';
7
- import { MediaSessionActionThrottler } from './MediaSessionActionThrottler';
8
- import { ExtendedMediaSessionActionDetails, ExtendedMediaSessionAction } from './MediaSessionExtensions';
9
-
10
- /**
11
- * A Throttler that passes through all actions but will only let an action be repeated once every 2 seconds.
12
- */
13
- export class RepeatedActionThrottler extends MediaSessionActionThrottler {
14
- private static FILTERED_ACTIONS: ExtendedMediaSessionAction[] = ['play', 'pause', 'seekto', 'catchup'];
15
- private _repeatInterval = new TimeInterval(2000);
16
- private _lastActionSentTime?: number;
17
- private _lastActionSent?: string;
18
-
19
- public get repeatInterval(): number {
20
- return this._repeatInterval.seconds;
21
- }
22
-
23
- public set repeatInterval(value: number) {
24
- this._repeatInterval.seconds = value;
25
- }
26
-
27
- public throttled(details: ExtendedMediaSessionActionDetails, handler?: MediaSessionActionHandler): void {
28
- if (handler) {
29
- if (RepeatedActionThrottler.FILTERED_ACTIONS.indexOf(details.action) >= 0) {
30
- const changeKey = this.getChangeKey(details);
31
- const now = new Date().getTime();
32
- if (this._lastActionSent && changeKey == this._lastActionSent) {
33
- if (now - this._lastActionSentTime! >= this._repeatInterval.milliseconds) {
34
- handler(details as MediaSessionActionDetails);
35
- this._lastActionSentTime = now;
36
- }
37
- } else {
38
- handler(details as MediaSessionActionDetails);
39
- this._lastActionSent = changeKey;
40
- this._lastActionSentTime = now;
41
- }
42
- } else {
43
- handler(details as MediaSessionActionDetails);
44
- }
45
- }
46
- }
47
-
48
- private getChangeKey(details: ExtendedMediaSessionActionDetails): string {
49
- switch (details.action) {
50
- case 'seekto':
51
- return `seekto:${details.seekTime}`;
52
- default:
53
- return details.action;
54
- }
55
- }
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { TimeInterval } from '@microsoft/live-share';
7
+ import { MediaSessionActionThrottler } from './MediaSessionActionThrottler';
8
+ import { ExtendedMediaSessionActionDetails, ExtendedMediaSessionAction } from './MediaSessionExtensions';
9
+
10
+ /**
11
+ * A Throttler that passes through all actions but will only let an action be repeated once every 2 seconds.
12
+ */
13
+ export class RepeatedActionThrottler extends MediaSessionActionThrottler {
14
+ private static FILTERED_ACTIONS: ExtendedMediaSessionAction[] = ['play', 'pause', 'seekto', 'catchup'];
15
+ private _repeatInterval = new TimeInterval(2000);
16
+ private _lastActionSentTime?: number;
17
+ private _lastActionSent?: string;
18
+
19
+ public get repeatInterval(): number {
20
+ return this._repeatInterval.seconds;
21
+ }
22
+
23
+ public set repeatInterval(value: number) {
24
+ this._repeatInterval.seconds = value;
25
+ }
26
+
27
+ public throttled(details: ExtendedMediaSessionActionDetails, handler?: MediaSessionActionHandler): void {
28
+ if (handler) {
29
+ if (RepeatedActionThrottler.FILTERED_ACTIONS.indexOf(details.action) >= 0) {
30
+ const changeKey = this.getChangeKey(details);
31
+ const now = new Date().getTime();
32
+ if (this._lastActionSent && changeKey == this._lastActionSent) {
33
+ if (now - this._lastActionSentTime! >= this._repeatInterval.milliseconds) {
34
+ handler(details as MediaSessionActionDetails);
35
+ this._lastActionSentTime = now;
36
+ }
37
+ } else {
38
+ handler(details as MediaSessionActionDetails);
39
+ this._lastActionSent = changeKey;
40
+ this._lastActionSentTime = now;
41
+ }
42
+ } else {
43
+ handler(details as MediaSessionActionDetails);
44
+ }
45
+ }
46
+ }
47
+
48
+ private getChangeKey(details: ExtendedMediaSessionActionDetails): string {
49
+ switch (details.action) {
50
+ case 'seekto':
51
+ return `seekto:${details.seekTime}`;
52
+ default:
53
+ return details.action;
54
+ }
55
+ }
56
56
  }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { TimeInterval } from '@microsoft/live-share';
7
+ import { IMediaPlayer } from './IMediaPlayer';
8
+
9
+ export enum LimitLevelType { fixed, percentage }
10
+
11
+ /**
12
+ * Smooth audio level changes when volume is modified, or if volume limiting has started/ended.
13
+ */
14
+ export class VolumeManager {
15
+ private readonly _player: IMediaPlayer;
16
+ private readonly _volumeChangeDuration = new TimeInterval(500);
17
+
18
+ // defaults to player volume
19
+ private _volume = 0.0;
20
+ private _isLimiting = false;
21
+ private _limitLevel = 0.1
22
+ private _limitLevelType: LimitLevelType = LimitLevelType.fixed;
23
+ private _startTime = 0;
24
+ private _startVolume = 0;
25
+ private _running = false;
26
+
27
+ constructor(player: IMediaPlayer) {
28
+ this._player = player;
29
+ this._volume = this._player.volume;
30
+ }
31
+
32
+ /**
33
+ * The selected volume.
34
+ *
35
+ * @remarks
36
+ * Expressed as a value between 0.0 and 1.0. The default value is 1.0.
37
+ * Can be used for things like volume sliders.
38
+ *
39
+ * Does not return the current volume if limiting is enabled, but the desired volume.
40
+ */
41
+ public get volume(): number {
42
+ return this._volume;
43
+ }
44
+
45
+ public set volume(value: number) {
46
+ if (value < 0 || value > 1.0) {
47
+ throw new Error(`VolumeManager: cannot set volume to ${value}. Level must be between 0.0 and 1.0.`);
48
+ }
49
+
50
+ this._volume = value;
51
+ this.startAdjusting();
52
+ }
53
+
54
+ /**
55
+ * Target level to lower volume to.
56
+ *
57
+ * @remarks
58
+ * Expressed as a value between 0.0 and 1.0. The value is applied based upon the configured
59
+ * `levelType`. The default value is 0.1.
60
+ *
61
+ * For a level type of `LevelType.fixed` the value is the exact level the volume will be
62
+ * lowered to. The default value of 0.1 would cause the volume to be lowered to 0.1.
63
+ *
64
+ * For a level type of `LevelType.percentage` the value is the percentage by which the volume
65
+ * level should be lowered to. The default value of 0.1 would cause the volume to be lowered
66
+ * to 10% of its starting value.
67
+ */
68
+ public get limitLevel(): number {
69
+ return this._limitLevel;
70
+ }
71
+
72
+ public set limitLevel(value: number) {
73
+ if (value < 0 || value > 1.0) {
74
+ throw new Error(`VolumeManager: cannot set level to ${value}. Level must be between 0.0 and 1.0.`);
75
+ }
76
+
77
+ this._limitLevel = value;
78
+ }
79
+
80
+ /**
81
+ * The type of level represented by the `limitLevel` property.
82
+ */
83
+ public get limitLevelType(): LimitLevelType {
84
+ return this._limitLevelType;
85
+ }
86
+
87
+ public set limitLevelType(value: LimitLevelType) {
88
+ this._limitLevelType = value;
89
+ }
90
+
91
+ /**
92
+ * Amount of time, in seconds, it should take to change the volume up or down to the desired level.
93
+ *
94
+ * @remarks
95
+ * Default `volumeChangeDuration` is 0.5 seconds.
96
+ */
97
+ public get volumeChangeDuration(): number {
98
+ return this._volumeChangeDuration.seconds;
99
+ }
100
+
101
+ public set volumeChangeDuration(value: number) {
102
+ this._volumeChangeDuration.seconds = Math.abs(value);
103
+ }
104
+
105
+ /**
106
+ * Whether or not volume limiting is currently enabled.
107
+ */
108
+ public get isLimiting(): boolean {
109
+ return this._isLimiting;
110
+ }
111
+
112
+ /**
113
+ * Limits volume based on `limitLevel` and `limitLevelType` properties.
114
+ * @see `limitLevel`
115
+ * @see `limitLevelType`
116
+ */
117
+ public startLimiting(): void {
118
+ this._isLimiting = true;
119
+ this.startAdjusting();
120
+ }
121
+
122
+ /**
123
+ * Disables volume limit.
124
+ */
125
+ public stopLimiting(): void {
126
+ this._isLimiting = false;
127
+ this.startAdjusting();
128
+ }
129
+
130
+ private startAdjusting() {
131
+ const adjustVolume = () => {
132
+ // Schedule next animation frame if volume change not finished
133
+ if (this.millisSinceVolumeChangeStart() <= this._volumeChangeDuration.milliseconds) {
134
+ this._player.volume = this.computeInterpolatedVolume();
135
+ this.scheduleAnimationFrame(adjustVolume);
136
+ } else {
137
+ this._player.volume = this.computeTargetVolume();
138
+ this._running = false;
139
+ }
140
+ }
141
+
142
+ this._startTime = new Date().getTime();
143
+ this._startVolume = this._player.volume;
144
+ if (!this._running) {
145
+ this._running = true
146
+ this.scheduleAnimationFrame(adjustVolume);
147
+ }
148
+ }
149
+
150
+ private scheduleAnimationFrame(callback: FrameRequestCallback): void {
151
+ if (typeof requestAnimationFrame == "function") {
152
+ requestAnimationFrame(callback);
153
+ } else {
154
+ setTimeout(callback, 20);
155
+ }
156
+ }
157
+
158
+ private computeInterpolatedVolume(): number {
159
+ const volumeChangeMillis = this._volumeChangeDuration.milliseconds
160
+ const volumeDifference = this.computeTargetVolume() - this._startVolume
161
+ const adjustmentFromStart = volumeDifference / volumeChangeMillis * this.millisSinceVolumeChangeStart()
162
+ return this._startVolume + adjustmentFromStart;
163
+ }
164
+
165
+ private computeTargetVolume(): number {
166
+ if (this._limitLevelType == LimitLevelType.percentage) {
167
+ if (this._isLimiting) {
168
+ return this._volume * this._limitLevel;
169
+ } else {
170
+ return this._volume;
171
+ }
172
+ } else {
173
+ if (this._isLimiting && this._volume > this._limitLevel) {
174
+ return this._limitLevel;
175
+ } else {
176
+ return this._volume;
177
+ }
178
+ }
179
+ }
180
+
181
+ private millisSinceVolumeChangeStart(): number {
182
+ const now = new Date().getTime();
183
+ return now - this._startTime;
184
+ }
185
+ }
package/src/index.ts CHANGED
@@ -1,15 +1,15 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
-
7
- export * from './EphemeralMediaSession';
8
- export * from './EphemeralMediaSessionCoordinator';
9
- export * from './EphemeralMediaSessionCoordinatorSuspension';
10
- export * from './IMediaPlayer';
11
- export * from './MediaPlayerSynchronizer';
12
- export * from './MediaSessionActionThrottler';
13
- export * from './MediaSessionExtensions';
14
- export * from './RepeatedActionThrottler';
15
- export * from './VolumeLimiter';
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+
7
+ export * from './LiveMediaSession';
8
+ export * from './LiveMediaSessionCoordinator';
9
+ export * from './LiveMediaSessionCoordinatorSuspension';
10
+ export * from './IMediaPlayer';
11
+ export * from './MediaPlayerSynchronizer';
12
+ export * from './MediaSessionActionThrottler';
13
+ export * from './MediaSessionExtensions';
14
+ export * from './RepeatedActionThrottler';
15
+ export * from './VolumeManager';