@@ -11,7 +11,7 @@ export default class RemoteTrackPublication extends TrackPublication {
11
11
track ?: RemoteTrack ;
12
12
13
13
/** @internal */
14
- _allowed = true ;
14
+ protected allowed = true ;
15
15
16
16
// keeps track of client's desire to subscribe to a track
17
17
protected subscribed ?: boolean ;
@@ -28,6 +28,9 @@ export default class RemoteTrackPublication extends TrackPublication {
28
28
*/
29
29
setSubscribed ( subscribed : boolean ) {
30
30
this . subscribed = subscribed ;
31
+ // reset allowed status when desired subscription state changes
32
+ // server will notify client via signal message if it's not allowed
33
+ this . allowed = true ;
31
34
32
35
const sub : UpdateSubscription = {
33
36
trackSids : [ this . trackSid ] ,
@@ -46,11 +49,11 @@ export default class RemoteTrackPublication extends TrackPublication {
46
49
47
50
get subscriptionStatus ( ) : TrackPublication . SubscriptionStatus {
48
51
if ( this . subscribed === false || ! super . isSubscribed ) {
52
+ if ( ! this . allowed ) {
53
+ return TrackPublication . SubscriptionStatus . NotAllowed ;
54
+ }
49
55
return TrackPublication . SubscriptionStatus . Unsubscribed ;
50
56
}
51
- if ( ! this . _allowed ) {
52
- return TrackPublication . SubscriptionStatus . NotAllowed ;
53
- }
54
57
return TrackPublication . SubscriptionStatus . Subscribed ;
55
58
}
56
59
@@ -61,9 +64,6 @@ export default class RemoteTrackPublication extends TrackPublication {
61
64
if ( this . subscribed === false ) {
62
65
return false ;
63
66
}
64
- if ( ! this . _allowed ) {
65
- return false ;
66
- }
67
67
return super . isSubscribed ;
68
68
}
69
69
@@ -127,11 +127,13 @@ export default class RemoteTrackPublication extends TrackPublication {
127
127
128
128
/** @internal */
129
129
setTrack ( track ?: Track ) {
130
- if ( this . track ) {
130
+ const prevStatus = this . subscriptionStatus ;
131
+ const prevTrack = this . track ;
132
+ if ( prevTrack ) {
131
133
// unregister listener
132
- this . track . off ( TrackEvent . VideoDimensionsChanged , this . handleVideoDimensionsChange ) ;
133
- this . track . off ( TrackEvent . VisibilityChanged , this . handleVisibilityChange ) ;
134
- this . track . off ( TrackEvent . Ended , this . handleEnded ) ;
134
+ prevTrack . off ( TrackEvent . VideoDimensionsChanged , this . handleVideoDimensionsChange ) ;
135
+ prevTrack . off ( TrackEvent . VisibilityChanged , this . handleVisibilityChange ) ;
136
+ prevTrack . off ( TrackEvent . Ended , this . handleEnded ) ;
135
137
}
136
138
super . setTrack ( track ) ;
137
139
if ( track ) {
@@ -140,6 +142,22 @@ export default class RemoteTrackPublication extends TrackPublication {
140
142
track . on ( TrackEvent . VisibilityChanged , this . handleVisibilityChange ) ;
141
143
track . on ( TrackEvent . Ended , this . handleEnded ) ;
142
144
}
145
+ this . emitSubscriptionUpdateIfChanged ( prevStatus ) ;
146
+ if ( ! ! track !== ! ! prevTrack ) {
147
+ // when undefined status changes, there's a subscription changed event
148
+ if ( track ) {
149
+ this . emit ( TrackEvent . Subscribed , track ) ;
150
+ } else {
151
+ this . emit ( TrackEvent . Unsubscribed , prevTrack ) ;
152
+ }
153
+ }
154
+ }
155
+
156
+ /** @internal */
157
+ setAllowed ( allowed : boolean ) {
158
+ const prevStatus = this . subscriptionStatus ;
159
+ this . allowed = allowed ;
160
+ this . emitSubscriptionUpdateIfChanged ( prevStatus ) ;
143
161
}
144
162
145
163
/** @internal */
@@ -149,6 +167,14 @@ export default class RemoteTrackPublication extends TrackPublication {
149
167
this . track ?. setMuted ( info . muted ) ;
150
168
}
151
169
170
+ private emitSubscriptionUpdateIfChanged ( previousStatus : TrackPublication . SubscriptionStatus ) {
171
+ const currentStatus = this . subscriptionStatus ;
172
+ if ( previousStatus === currentStatus ) {
173
+ return ;
174
+ }
175
+ this . emit ( TrackEvent . SubscriptionPermissionChanged , currentStatus , previousStatus ) ;
176
+ }
177
+
152
178
private isManualOperationAllowed ( ) : boolean {
153
179
if ( this . isAdaptiveStream ) {
154
180
log . warn ( 'adaptive stream is enabled, cannot change track settings' , {
0 commit comments