@microsoft/live-share-media 0.5.0-beta.1 → 0.5.0-beta.2

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.
@@ -1,193 +1,193 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { EphemeralEvent, IRuntimeSignaler, TimeInterval } from '@microsoft/live-share';
7
- import { GroupTransportState } from './GroupTransportState';
8
- import { GroupPlaybackTrackEvents } from './GroupPlaybackTrack';
9
- import { CoordinationWaitPoint, ExtendedMediaSessionPlaybackState } from '../MediaSessionExtensions';
10
-
11
- /**
12
- *Per client position
13
- * @hidden
14
- */
15
- export interface ICurrentPlaybackPosition {
16
- playbackState: ExtendedMediaSessionPlaybackState;
17
- waitPoint?: CoordinationWaitPoint
18
- position: number;
19
- duration?: number;
20
- timestamp: number;
21
- clientId: string;
22
- }
23
-
24
- /**
25
- * @hidden
26
- */
27
- export class GroupPlaybackPosition {
28
- private _transportState: GroupTransportState;
29
- private _runtime: IRuntimeSignaler;
30
- private _updateInterval: TimeInterval;
31
- private _positions: Map<string, ICurrentPlaybackPosition>;
32
-
33
-
34
- constructor(transportState: GroupTransportState, runtime: IRuntimeSignaler, updateInterval: TimeInterval) {
35
- this._transportState = transportState;
36
- this._runtime = runtime;
37
- this._updateInterval = updateInterval;
38
- this._positions = new Map();
39
-
40
- // Listen for track change
41
- this._transportState.track.on(GroupPlaybackTrackEvents.trackChange, _ => {
42
- // Reset position tracking and duration
43
- this._positions = new Map();
44
- this.mediaDuration = undefined;
45
- });
46
- }
47
-
48
- /**
49
- * Returns the latest position info for the local client.
50
- */
51
- public get localPosition(): ICurrentPlaybackPosition|undefined {
52
- return this._positions.get(this._runtime.clientId!);
53
- }
54
-
55
- /**
56
- * Returns the total number of clients actively being tracked.
57
- */
58
- public get totalClients(): number {
59
- let cnt = 0;
60
- this.forEach(_ => cnt++);
61
- return cnt;
62
- }
63
-
64
- /**
65
- * Returns true if all clients being tracked are reporting that they have
66
- * either ended or haven't started.
67
- */
68
- public get trackEnded(): boolean {
69
- let playing = 0;
70
- this.forEach((position, projectedPosition) => {
71
- switch (position.playbackState) {
72
- case 'none':
73
- case 'ended':
74
- // not playing.
75
- break;
76
- default:
77
- playing++;
78
- break;
79
- }
80
- });
81
-
82
- return playing == 0;
83
- }
84
-
85
- /**
86
- * Returns the number of clients we're waiting for before we can stop waiting.
87
- */
88
- public get clientsWaiting(): number {
89
- let cnt = 0;
90
- const waitPoint = this.localPosition?.waitPoint;
91
- if (waitPoint && (waitPoint.maxClients == undefined || this.totalClients <= waitPoint.maxClients)) {
92
- this.forEach((position, projectedPosition) => {
93
- if (position.playbackState == 'suspended' && position.waitPoint) {
94
- cnt++;
95
- } else if (position.position < waitPoint.position) {
96
- cnt++;
97
- }
98
- });
99
- }
100
-
101
- return cnt;
102
- }
103
-
104
- /**
105
- * Returns the max playback position relative to the start position.
106
- *
107
- * @remarks
108
- * This is called when calculating the current seekTo position.
109
- */
110
- public get maxPosition(): number {
111
- if (this._transportState.playbackState == 'playing') {
112
- const now = EphemeralEvent.getTimestamp();
113
- const projected = this._transportState.startPosition + ((now - this._transportState.timestamp) / 1000);
114
- return this.limitProjectedPosition(projected);
115
- } else {
116
- return this._transportState.startPosition;
117
- }
118
- }
119
-
120
- /**
121
- * Optional media duration if known.
122
- */
123
- public mediaDuration?: number;
124
-
125
- public get targetPosition(): number {
126
- if (this._transportState.playbackState == 'playing') {
127
- return this.getMostProgressedPosition();
128
- } else {
129
- return this._transportState.startPosition;
130
- }
131
- }
132
-
133
-
134
- /**
135
- * Enumerates every reported playback position.
136
- * @param callbackFn Function applied to each position,
137
- */
138
- public forEach(callbackFn: (position: ICurrentPlaybackPosition, projectedPosition: number) => void): void {
139
- const now = EphemeralEvent.getTimestamp();
140
- const ignoreBefore = now - (this._updateInterval.milliseconds * 2);
141
- const shouldProject = !this._transportState.track.metadata?.liveStream;
142
- this._positions.forEach((value, key) => {
143
- const position = this._positions.get(key)!;
144
-
145
- // Ignore any old updates
146
- if (position.timestamp > ignoreBefore) {
147
- // Compute projected playback position
148
- // - This computation does not take into account future wait points.
149
- const projected = position.playbackState == 'playing' && shouldProject ? position.position + ((now - position.timestamp) / 1000) : position.position;
150
- callbackFn(position, this.limitProjectedPosition(projected));
151
- }
152
- });
153
- }
154
-
155
- public UpdatePlaybackPosition(position: ICurrentPlaybackPosition): void {
156
- // Update duration
157
- if (position.duration != undefined) {
158
- this.mediaDuration = position.duration;
159
- }
160
-
161
- // Save last position
162
- if (this._positions.has(position.clientId)) {
163
- // Only update if newer position
164
- const current = this._positions.get(position.clientId)!;
165
- if (position.timestamp >= current.timestamp) {
166
- this._positions.set(position.clientId, position);
167
- }
168
- } else {
169
- this._positions.set(position.clientId, position);
170
- }
171
- }
172
-
173
- private getMostProgressedPosition(): number {
174
- // Compute the max possible position for the current transport state.
175
- // - This is needed to properly handle seeking backwards in time. Some playback heads may
176
- // not have performed their seek yet and will therefore be ahead of the local player.
177
- const maxPosition = this.maxPosition;
178
-
179
- // Compute max progress
180
- let progress = -1;
181
- this.forEach((position, projectedPosition) => {
182
- if (projectedPosition <= maxPosition && projectedPosition > progress) {
183
- progress = projectedPosition;
184
- }
185
- });
186
-
187
- return progress;
188
- }
189
-
190
- private limitProjectedPosition(position: number): number {
191
- return this.mediaDuration != undefined ? Math.min(position, this.mediaDuration) : position;
192
- }
193
- }
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { EphemeralEvent, IRuntimeSignaler, TimeInterval } from '@microsoft/live-share';
7
+ import { GroupTransportState } from './GroupTransportState';
8
+ import { GroupPlaybackTrackEvents } from './GroupPlaybackTrack';
9
+ import { CoordinationWaitPoint, ExtendedMediaSessionPlaybackState } from '../MediaSessionExtensions';
10
+
11
+ /**
12
+ *Per client position
13
+ * @hidden
14
+ */
15
+ export interface ICurrentPlaybackPosition {
16
+ playbackState: ExtendedMediaSessionPlaybackState;
17
+ waitPoint?: CoordinationWaitPoint
18
+ position: number;
19
+ duration?: number;
20
+ timestamp: number;
21
+ clientId: string;
22
+ }
23
+
24
+ /**
25
+ * @hidden
26
+ */
27
+ export class GroupPlaybackPosition {
28
+ private _transportState: GroupTransportState;
29
+ private _runtime: IRuntimeSignaler;
30
+ private _updateInterval: TimeInterval;
31
+ private _positions: Map<string, ICurrentPlaybackPosition>;
32
+
33
+
34
+ constructor(transportState: GroupTransportState, runtime: IRuntimeSignaler, updateInterval: TimeInterval) {
35
+ this._transportState = transportState;
36
+ this._runtime = runtime;
37
+ this._updateInterval = updateInterval;
38
+ this._positions = new Map();
39
+
40
+ // Listen for track change
41
+ this._transportState.track.on(GroupPlaybackTrackEvents.trackChange, _ => {
42
+ // Reset position tracking and duration
43
+ this._positions = new Map();
44
+ this.mediaDuration = undefined;
45
+ });
46
+ }
47
+
48
+ /**
49
+ * Returns the latest position info for the local client.
50
+ */
51
+ public get localPosition(): ICurrentPlaybackPosition|undefined {
52
+ return this._positions.get(this._runtime.clientId!);
53
+ }
54
+
55
+ /**
56
+ * Returns the total number of clients actively being tracked.
57
+ */
58
+ public get totalClients(): number {
59
+ let cnt = 0;
60
+ this.forEach(_ => cnt++);
61
+ return cnt;
62
+ }
63
+
64
+ /**
65
+ * Returns true if all clients being tracked are reporting that they have
66
+ * either ended or haven't started.
67
+ */
68
+ public get trackEnded(): boolean {
69
+ let playing = 0;
70
+ this.forEach((position, projectedPosition) => {
71
+ switch (position.playbackState) {
72
+ case 'none':
73
+ case 'ended':
74
+ // not playing.
75
+ break;
76
+ default:
77
+ playing++;
78
+ break;
79
+ }
80
+ });
81
+
82
+ return playing == 0;
83
+ }
84
+
85
+ /**
86
+ * Returns the number of clients we're waiting for before we can stop waiting.
87
+ */
88
+ public get clientsWaiting(): number {
89
+ let cnt = 0;
90
+ const waitPoint = this.localPosition?.waitPoint;
91
+ if (waitPoint && (waitPoint.maxClients == undefined || this.totalClients <= waitPoint.maxClients)) {
92
+ this.forEach((position, projectedPosition) => {
93
+ if (position.playbackState == 'suspended' && position.waitPoint) {
94
+ cnt++;
95
+ } else if (position.position < waitPoint.position) {
96
+ cnt++;
97
+ }
98
+ });
99
+ }
100
+
101
+ return cnt;
102
+ }
103
+
104
+ /**
105
+ * Returns the max playback position relative to the start position.
106
+ *
107
+ * @remarks
108
+ * This is called when calculating the current seekTo position.
109
+ */
110
+ public get maxPosition(): number {
111
+ if (this._transportState.playbackState == 'playing') {
112
+ const now = EphemeralEvent.getTimestamp();
113
+ const projected = this._transportState.startPosition + ((now - this._transportState.timestamp) / 1000);
114
+ return this.limitProjectedPosition(projected);
115
+ } else {
116
+ return this._transportState.startPosition;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Optional media duration if known.
122
+ */
123
+ public mediaDuration?: number;
124
+
125
+ public get targetPosition(): number {
126
+ if (this._transportState.playbackState == 'playing') {
127
+ return this.getMostProgressedPosition();
128
+ } else {
129
+ return this._transportState.startPosition;
130
+ }
131
+ }
132
+
133
+
134
+ /**
135
+ * Enumerates every reported playback position.
136
+ * @param callbackFn Function applied to each position,
137
+ */
138
+ public forEach(callbackFn: (position: ICurrentPlaybackPosition, projectedPosition: number) => void): void {
139
+ const now = EphemeralEvent.getTimestamp();
140
+ const ignoreBefore = now - (this._updateInterval.milliseconds * 2);
141
+ const shouldProject = !this._transportState.track.metadata?.liveStream;
142
+ this._positions.forEach((value, key) => {
143
+ const position = this._positions.get(key)!;
144
+
145
+ // Ignore any old updates
146
+ if (position.timestamp > ignoreBefore) {
147
+ // Compute projected playback position
148
+ // - This computation does not take into account future wait points.
149
+ const projected = position.playbackState == 'playing' && shouldProject ? position.position + ((now - position.timestamp) / 1000) : position.position;
150
+ callbackFn(position, this.limitProjectedPosition(projected));
151
+ }
152
+ });
153
+ }
154
+
155
+ public UpdatePlaybackPosition(position: ICurrentPlaybackPosition): void {
156
+ // Update duration
157
+ if (position.duration != undefined) {
158
+ this.mediaDuration = position.duration;
159
+ }
160
+
161
+ // Save last position
162
+ if (this._positions.has(position.clientId)) {
163
+ // Only update if newer position
164
+ const current = this._positions.get(position.clientId)!;
165
+ if (position.timestamp >= current.timestamp) {
166
+ this._positions.set(position.clientId, position);
167
+ }
168
+ } else {
169
+ this._positions.set(position.clientId, position);
170
+ }
171
+ }
172
+
173
+ private getMostProgressedPosition(): number {
174
+ // Compute the max possible position for the current transport state.
175
+ // - This is needed to properly handle seeking backwards in time. Some playback heads may
176
+ // not have performed their seek yet and will therefore be ahead of the local player.
177
+ const maxPosition = this.maxPosition;
178
+
179
+ // Compute max progress
180
+ let progress = -1;
181
+ this.forEach((position, projectedPosition) => {
182
+ if (projectedPosition <= maxPosition && projectedPosition > progress) {
183
+ progress = projectedPosition;
184
+ }
185
+ });
186
+
187
+ return progress;
188
+ }
189
+
190
+ private limitProjectedPosition(position: number): number {
191
+ return this.mediaDuration != undefined ? Math.min(position, this.mediaDuration) : position;
192
+ }
193
+ }