@microsoft/live-share-media 1.3.0 → 1.4.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.
Files changed (78) hide show
  1. package/.nycrc.json +10 -10
  2. package/LICENSE +90 -90
  3. package/README.md +135 -135
  4. package/bin/LiveMediaSession.d.ts.map +1 -1
  5. package/bin/LiveMediaSession.js +9 -1
  6. package/bin/LiveMediaSession.js.map +1 -1
  7. package/bin/LiveMediaSessionCoordinator.d.ts +18 -2
  8. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -1
  9. package/bin/LiveMediaSessionCoordinator.js +52 -13
  10. package/bin/LiveMediaSessionCoordinator.js.map +1 -1
  11. package/bin/MediaPlayerSynchronizer.d.ts +6 -4
  12. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  13. package/bin/MediaPlayerSynchronizer.js +41 -12
  14. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  15. package/bin/MediaSessionExtensions.d.ts +17 -8
  16. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  17. package/bin/MediaSessionExtensions.js +4 -0
  18. package/bin/MediaSessionExtensions.js.map +1 -1
  19. package/bin/internals/GroupCoordinatorState.d.ts +15 -1
  20. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  21. package/bin/internals/GroupCoordinatorState.js +123 -57
  22. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  23. package/bin/internals/GroupPlaybackPosition.d.ts +3 -1
  24. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  25. package/bin/internals/GroupPlaybackPosition.js +7 -3
  26. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  27. package/bin/internals/GroupPlaybackRate.d.ts +39 -0
  28. package/bin/internals/GroupPlaybackRate.d.ts.map +1 -0
  29. package/bin/internals/GroupPlaybackRate.js +62 -0
  30. package/bin/internals/GroupPlaybackRate.js.map +1 -0
  31. package/bin/internals/GroupPlaybackTrack.d.ts +2 -2
  32. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  33. package/bin/internals/GroupPlaybackTrack.js +3 -2
  34. package/bin/internals/GroupPlaybackTrack.js.map +1 -1
  35. package/bin/internals/GroupPlaybackTrackData.d.ts +3 -3
  36. package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
  37. package/bin/internals/GroupPlaybackTrackData.js +12 -11
  38. package/bin/internals/GroupPlaybackTrackData.js.map +1 -1
  39. package/bin/internals/GroupTransportState.d.ts +6 -3
  40. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  41. package/bin/internals/GroupTransportState.js +16 -11
  42. package/bin/internals/GroupTransportState.js.map +1 -1
  43. package/bin/internals/consts.d.ts +10 -3
  44. package/bin/internals/consts.d.ts.map +1 -1
  45. package/bin/internals/consts.js +10 -3
  46. package/bin/internals/consts.js.map +1 -1
  47. package/bin/internals/index.d.ts +1 -0
  48. package/bin/internals/index.d.ts.map +1 -1
  49. package/bin/internals/index.js +1 -0
  50. package/bin/internals/index.js.map +1 -1
  51. package/bin/internals/interfaces.d.ts +10 -0
  52. package/bin/internals/interfaces.d.ts.map +1 -0
  53. package/bin/internals/interfaces.js +3 -0
  54. package/bin/internals/interfaces.js.map +1 -0
  55. package/package.json +45 -45
  56. package/src/IMediaPlayer.ts +29 -29
  57. package/src/LiveMediaSession.ts +349 -336
  58. package/src/LiveMediaSessionCoordinator.ts +760 -678
  59. package/src/LiveMediaSessionCoordinatorSuspension.ts +39 -39
  60. package/src/MediaPlayerSynchronizer.ts +754 -714
  61. package/src/MediaSessionActionThrottler.ts +19 -19
  62. package/src/MediaSessionExtensions.ts +198 -187
  63. package/src/RepeatedActionThrottler.ts +76 -76
  64. package/src/VolumeManager.ts +200 -200
  65. package/src/index.ts +14 -14
  66. package/src/internals/GroupCoordinatorState.ts +802 -685
  67. package/src/internals/GroupPlaybackPosition.ts +235 -228
  68. package/src/internals/GroupPlaybackRate.ts +90 -0
  69. package/src/internals/GroupPlaybackTrack.ts +183 -182
  70. package/src/internals/GroupPlaybackTrackData.ts +105 -104
  71. package/src/internals/GroupTransportState.ts +179 -170
  72. package/src/internals/consts.ts +115 -102
  73. package/src/internals/errors.ts +42 -42
  74. package/src/internals/index.ts +13 -12
  75. package/src/internals/interfaces.ts +10 -0
  76. package/tsconfig.build.json +8 -8
  77. package/tsconfig.json +76 -76
  78. package/typedoc.json +10 -10
@@ -1,200 +1,200 @@
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 {
10
- fixed,
11
- percentage,
12
- }
13
-
14
- /**
15
- * Smooth audio level changes when volume is modified, or if volume limiting has started/ended.
16
- */
17
- export class VolumeManager {
18
- private readonly _player: IMediaPlayer;
19
- private readonly _volumeChangeDuration = new TimeInterval(500);
20
-
21
- // defaults to player volume
22
- private _volume = 0.0;
23
- private _isLimiting = false;
24
- private _limitLevel = 0.1;
25
- private _limitLevelType: LimitLevelType = LimitLevelType.fixed;
26
- private _startTime = 0;
27
- private _startVolume = 0;
28
- private _running = false;
29
-
30
- constructor(player: IMediaPlayer) {
31
- this._player = player;
32
- this._volume = this._player.volume;
33
- }
34
-
35
- /**
36
- * The selected volume.
37
- *
38
- * @remarks
39
- * Expressed as a value between 0.0 and 1.0. The default value is 1.0.
40
- * Can be used for things like volume sliders.
41
- *
42
- * Does not return the current volume if limiting is enabled, but the desired volume.
43
- */
44
- public get volume(): number {
45
- return this._volume;
46
- }
47
-
48
- public set volume(value: number) {
49
- if (value < 0 || value > 1.0) {
50
- throw new Error(
51
- `VolumeManager: cannot set volume to ${value}. Level must be between 0.0 and 1.0.`
52
- );
53
- }
54
-
55
- this._volume = value;
56
- this.startAdjusting();
57
- }
58
-
59
- /**
60
- * Target level to lower volume to.
61
- *
62
- * @remarks
63
- * Expressed as a value between 0.0 and 1.0. The value is applied based upon the configured
64
- * `levelType`. The default value is 0.1.
65
- *
66
- * For a level type of `LevelType.fixed` the value is the exact level the volume will be
67
- * lowered to. The default value of 0.1 would cause the volume to be lowered to 0.1.
68
- *
69
- * For a level type of `LevelType.percentage` the value is the percentage by which the volume
70
- * level should be lowered to. The default value of 0.1 would cause the volume to be lowered
71
- * to 10% of its starting value.
72
- */
73
- public get limitLevel(): number {
74
- return this._limitLevel;
75
- }
76
-
77
- public set limitLevel(value: number) {
78
- if (value < 0 || value > 1.0) {
79
- throw new Error(
80
- `VolumeManager: cannot set level to ${value}. Level must be between 0.0 and 1.0.`
81
- );
82
- }
83
-
84
- this._limitLevel = value;
85
- }
86
-
87
- /**
88
- * The type of level represented by the `limitLevel` property.
89
- */
90
- public get limitLevelType(): LimitLevelType {
91
- return this._limitLevelType;
92
- }
93
-
94
- public set limitLevelType(value: LimitLevelType) {
95
- this._limitLevelType = value;
96
- }
97
-
98
- /**
99
- * Amount of time, in seconds, it should take to change the volume up or down to the desired level.
100
- *
101
- * @remarks
102
- * Default `volumeChangeDuration` is 0.5 seconds.
103
- */
104
- public get volumeChangeDuration(): number {
105
- return this._volumeChangeDuration.seconds;
106
- }
107
-
108
- public set volumeChangeDuration(value: number) {
109
- this._volumeChangeDuration.seconds = Math.abs(value);
110
- }
111
-
112
- /**
113
- * Whether or not volume limiting is currently enabled.
114
- */
115
- public get isLimiting(): boolean {
116
- return this._isLimiting;
117
- }
118
-
119
- /**
120
- * Limits volume based on `limitLevel` and `limitLevelType` properties.
121
- * @see `limitLevel`
122
- * @see `limitLevelType`
123
- */
124
- public startLimiting(): void {
125
- this._isLimiting = true;
126
- this.startAdjusting();
127
- }
128
-
129
- /**
130
- * Disables volume limit.
131
- */
132
- public stopLimiting(): void {
133
- this._isLimiting = false;
134
- this.startAdjusting();
135
- }
136
-
137
- private startAdjusting() {
138
- const adjustVolume = () => {
139
- // Schedule next animation frame if volume change not finished
140
- if (
141
- this.millisSinceVolumeChangeStart() <=
142
- this._volumeChangeDuration.milliseconds
143
- ) {
144
- this._player.volume = this.computeInterpolatedVolume();
145
- this.scheduleAnimationFrame(adjustVolume);
146
- } else {
147
- this._player.volume = this.computeTargetVolume();
148
- this._running = false;
149
- }
150
- };
151
-
152
- this._startTime = new Date().getTime();
153
- this._startVolume = this._player.volume;
154
- if (!this._running) {
155
- this._running = true;
156
- this.scheduleAnimationFrame(adjustVolume);
157
- }
158
- }
159
-
160
- private scheduleAnimationFrame(callback: FrameRequestCallback): void {
161
- if (typeof requestAnimationFrame == "function") {
162
- requestAnimationFrame(callback);
163
- } else {
164
- setTimeout(
165
- callback,
166
- Math.min(this._volumeChangeDuration.milliseconds / 25, 20)
167
- );
168
- }
169
- }
170
-
171
- private computeInterpolatedVolume(): number {
172
- const volumeChangeMillis = this._volumeChangeDuration.milliseconds;
173
- const volumeDifference = this.computeTargetVolume() - this._startVolume;
174
- const adjustmentFromStart =
175
- (volumeDifference / volumeChangeMillis) *
176
- this.millisSinceVolumeChangeStart();
177
- return this._startVolume + adjustmentFromStart;
178
- }
179
-
180
- private computeTargetVolume(): number {
181
- if (this._limitLevelType == LimitLevelType.percentage) {
182
- if (this._isLimiting) {
183
- return this._volume * this._limitLevel;
184
- } else {
185
- return this._volume;
186
- }
187
- } else {
188
- if (this._isLimiting && this._volume > this._limitLevel) {
189
- return this._limitLevel;
190
- } else {
191
- return this._volume;
192
- }
193
- }
194
- }
195
-
196
- private millisSinceVolumeChangeStart(): number {
197
- const now = new Date().getTime();
198
- return now - this._startTime;
199
- }
200
- }
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 {
10
+ fixed,
11
+ percentage,
12
+ }
13
+
14
+ /**
15
+ * Smooth audio level changes when volume is modified, or if volume limiting has started/ended.
16
+ */
17
+ export class VolumeManager {
18
+ private readonly _player: IMediaPlayer;
19
+ private readonly _volumeChangeDuration = new TimeInterval(500);
20
+
21
+ // defaults to player volume
22
+ private _volume = 0.0;
23
+ private _isLimiting = false;
24
+ private _limitLevel = 0.1;
25
+ private _limitLevelType: LimitLevelType = LimitLevelType.fixed;
26
+ private _startTime = 0;
27
+ private _startVolume = 0;
28
+ private _running = false;
29
+
30
+ constructor(player: IMediaPlayer) {
31
+ this._player = player;
32
+ this._volume = this._player.volume;
33
+ }
34
+
35
+ /**
36
+ * The selected volume.
37
+ *
38
+ * @remarks
39
+ * Expressed as a value between 0.0 and 1.0. The default value is 1.0.
40
+ * Can be used for things like volume sliders.
41
+ *
42
+ * Does not return the current volume if limiting is enabled, but the desired volume.
43
+ */
44
+ public get volume(): number {
45
+ return this._volume;
46
+ }
47
+
48
+ public set volume(value: number) {
49
+ if (value < 0 || value > 1.0) {
50
+ throw new Error(
51
+ `VolumeManager: cannot set volume to ${value}. Level must be between 0.0 and 1.0.`
52
+ );
53
+ }
54
+
55
+ this._volume = value;
56
+ this.startAdjusting();
57
+ }
58
+
59
+ /**
60
+ * Target level to lower volume to.
61
+ *
62
+ * @remarks
63
+ * Expressed as a value between 0.0 and 1.0. The value is applied based upon the configured
64
+ * `levelType`. The default value is 0.1.
65
+ *
66
+ * For a level type of `LevelType.fixed` the value is the exact level the volume will be
67
+ * lowered to. The default value of 0.1 would cause the volume to be lowered to 0.1.
68
+ *
69
+ * For a level type of `LevelType.percentage` the value is the percentage by which the volume
70
+ * level should be lowered to. The default value of 0.1 would cause the volume to be lowered
71
+ * to 10% of its starting value.
72
+ */
73
+ public get limitLevel(): number {
74
+ return this._limitLevel;
75
+ }
76
+
77
+ public set limitLevel(value: number) {
78
+ if (value < 0 || value > 1.0) {
79
+ throw new Error(
80
+ `VolumeManager: cannot set level to ${value}. Level must be between 0.0 and 1.0.`
81
+ );
82
+ }
83
+
84
+ this._limitLevel = value;
85
+ }
86
+
87
+ /**
88
+ * The type of level represented by the `limitLevel` property.
89
+ */
90
+ public get limitLevelType(): LimitLevelType {
91
+ return this._limitLevelType;
92
+ }
93
+
94
+ public set limitLevelType(value: LimitLevelType) {
95
+ this._limitLevelType = value;
96
+ }
97
+
98
+ /**
99
+ * Amount of time, in seconds, it should take to change the volume up or down to the desired level.
100
+ *
101
+ * @remarks
102
+ * Default `volumeChangeDuration` is 0.5 seconds.
103
+ */
104
+ public get volumeChangeDuration(): number {
105
+ return this._volumeChangeDuration.seconds;
106
+ }
107
+
108
+ public set volumeChangeDuration(value: number) {
109
+ this._volumeChangeDuration.seconds = Math.abs(value);
110
+ }
111
+
112
+ /**
113
+ * Whether or not volume limiting is currently enabled.
114
+ */
115
+ public get isLimiting(): boolean {
116
+ return this._isLimiting;
117
+ }
118
+
119
+ /**
120
+ * Limits volume based on `limitLevel` and `limitLevelType` properties.
121
+ * @see `limitLevel`
122
+ * @see `limitLevelType`
123
+ */
124
+ public startLimiting(): void {
125
+ this._isLimiting = true;
126
+ this.startAdjusting();
127
+ }
128
+
129
+ /**
130
+ * Disables volume limit.
131
+ */
132
+ public stopLimiting(): void {
133
+ this._isLimiting = false;
134
+ this.startAdjusting();
135
+ }
136
+
137
+ private startAdjusting() {
138
+ const adjustVolume = () => {
139
+ // Schedule next animation frame if volume change not finished
140
+ if (
141
+ this.millisSinceVolumeChangeStart() <=
142
+ this._volumeChangeDuration.milliseconds
143
+ ) {
144
+ this._player.volume = this.computeInterpolatedVolume();
145
+ this.scheduleAnimationFrame(adjustVolume);
146
+ } else {
147
+ this._player.volume = this.computeTargetVolume();
148
+ this._running = false;
149
+ }
150
+ };
151
+
152
+ this._startTime = new Date().getTime();
153
+ this._startVolume = this._player.volume;
154
+ if (!this._running) {
155
+ this._running = true;
156
+ this.scheduleAnimationFrame(adjustVolume);
157
+ }
158
+ }
159
+
160
+ private scheduleAnimationFrame(callback: FrameRequestCallback): void {
161
+ if (typeof requestAnimationFrame == "function") {
162
+ requestAnimationFrame(callback);
163
+ } else {
164
+ setTimeout(
165
+ callback,
166
+ Math.min(this._volumeChangeDuration.milliseconds / 25, 20)
167
+ );
168
+ }
169
+ }
170
+
171
+ private computeInterpolatedVolume(): number {
172
+ const volumeChangeMillis = this._volumeChangeDuration.milliseconds;
173
+ const volumeDifference = this.computeTargetVolume() - this._startVolume;
174
+ const adjustmentFromStart =
175
+ (volumeDifference / volumeChangeMillis) *
176
+ this.millisSinceVolumeChangeStart();
177
+ return this._startVolume + adjustmentFromStart;
178
+ }
179
+
180
+ private computeTargetVolume(): number {
181
+ if (this._limitLevelType == LimitLevelType.percentage) {
182
+ if (this._isLimiting) {
183
+ return this._volume * this._limitLevel;
184
+ } else {
185
+ return this._volume;
186
+ }
187
+ } else {
188
+ if (this._isLimiting && this._volume > this._limitLevel) {
189
+ return this._limitLevel;
190
+ } else {
191
+ return this._volume;
192
+ }
193
+ }
194
+ }
195
+
196
+ private millisSinceVolumeChangeStart(): number {
197
+ const now = new Date().getTime();
198
+ return now - this._startTime;
199
+ }
200
+ }
package/src/index.ts CHANGED
@@ -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
- export * from "./LiveMediaSession";
7
- export * from "./LiveMediaSessionCoordinator";
8
- export * from "./LiveMediaSessionCoordinatorSuspension";
9
- export * from "./IMediaPlayer";
10
- export * from "./MediaPlayerSynchronizer";
11
- export * from "./MediaSessionActionThrottler";
12
- export * from "./MediaSessionExtensions";
13
- export * from "./RepeatedActionThrottler";
14
- export * from "./VolumeManager";
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ export * from "./LiveMediaSession";
7
+ export * from "./LiveMediaSessionCoordinator";
8
+ export * from "./LiveMediaSessionCoordinatorSuspension";
9
+ export * from "./IMediaPlayer";
10
+ export * from "./MediaPlayerSynchronizer";
11
+ export * from "./MediaSessionActionThrottler";
12
+ export * from "./MediaSessionExtensions";
13
+ export * from "./RepeatedActionThrottler";
14
+ export * from "./VolumeManager";