forked from livekit/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: start remote track as muted and unmute on first packet receive #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
android/src/main/java/com/oney/WebRTCModule/AudioTrackAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package com.oney.WebRTCModule; | ||
|
|
||
| import android.util.Log; | ||
|
|
||
| import com.facebook.react.bridge.Arguments; | ||
| import com.facebook.react.bridge.WritableMap; | ||
|
|
||
| import org.webrtc.AudioTrack; | ||
| import org.webrtc.AudioTrackSink; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** | ||
| * Fires the W3C 'unmute' event on a remote audio track when the first | ||
| * decoded PCM buffer arrives via {@link AudioTrackSink}. | ||
| * | ||
| * IMPORTANT — only the initial muted → unmuted transition is detectable. | ||
| * Subsequent mute events (e.g. network stall mid-call) cannot be detected | ||
| * from the sink: Android's audio render path and WebRTC's NetEq synthesize | ||
| * silence / PLC frames whenever RTP stops, so {@code onData} keeps firing | ||
| * at a steady rate regardless of network state. For "remote participant | ||
| * muted their mic" UI, use the out-of-band participant state from your | ||
| * signaling layer — that is the correct source of truth, not this adapter. | ||
| * | ||
| * Only attach to remote audio tracks. {@code AudioTrackSink} callbacks | ||
| * are not delivered for local tracks. | ||
| */ | ||
| public class AudioTrackAdapter { | ||
| static final String TAG = AudioTrackAdapter.class.getCanonicalName(); | ||
|
|
||
| private final Map<String, FirstDataUnmuteSink> sinks = new HashMap<>(); | ||
| private final int peerConnectionId; | ||
| private final WebRTCModule webRTCModule; | ||
|
|
||
| public AudioTrackAdapter(WebRTCModule webRTCModule, int peerConnectionId) { | ||
| this.peerConnectionId = peerConnectionId; | ||
| this.webRTCModule = webRTCModule; | ||
| } | ||
|
|
||
| public void addAdapter(AudioTrack audioTrack) { | ||
| String trackId = audioTrack.id(); | ||
| if (sinks.containsKey(trackId)) { | ||
| Log.w(TAG, "Attempted to add adapter twice for track ID: " + trackId); | ||
| return; | ||
| } | ||
| FirstDataUnmuteSink sink = new FirstDataUnmuteSink(trackId); | ||
| sinks.put(trackId, sink); | ||
| audioTrack.addSink(sink); | ||
| Log.d(TAG, "Created adapter for " + trackId); | ||
| } | ||
|
|
||
| public void removeAdapter(AudioTrack audioTrack) { | ||
| String trackId = audioTrack.id(); | ||
| FirstDataUnmuteSink sink = sinks.remove(trackId); | ||
| if (sink == null) { | ||
| Log.w(TAG, "removeAdapter - no adapter for " + trackId); | ||
| return; | ||
| } | ||
| audioTrack.removeSink(sink); | ||
| Log.d(TAG, "Deleted adapter for " + trackId); | ||
| } | ||
|
|
||
| private class FirstDataUnmuteSink implements AudioTrackSink { | ||
| private final AtomicBoolean fired = new AtomicBoolean(false); | ||
| private final String trackId; | ||
|
|
||
| FirstDataUnmuteSink(String trackId) { | ||
| this.trackId = trackId; | ||
| } | ||
|
|
||
| @Override | ||
| public void onData(ByteBuffer audioData, | ||
| int bitsPerSample, | ||
| int sampleRate, | ||
| int numberOfChannels, | ||
| int numberOfFrames, | ||
| long absoluteCaptureTimestampMs) { | ||
| if (!fired.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
| WritableMap params = Arguments.createMap(); | ||
| params.putInt("pcId", peerConnectionId); | ||
| params.putString("trackId", trackId); | ||
| params.putBoolean("muted", false); | ||
| Log.d(TAG, "Unmute event pcId: " + peerConnectionId + " trackId: " + trackId); | ||
| webRTCModule.sendEvent("mediaStreamTrackMuteChanged", params); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
|
|
||
| #import <WebRTC/RTCAudioRenderer.h> | ||
| #import <WebRTC/RTCPeerConnection.h> | ||
| #import "WebRTCModule.h" | ||
|
|
||
| @interface RTCPeerConnection (AudioTrackAdapter) | ||
|
|
||
| @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *audioTrackAdapters; | ||
|
|
||
| - (void)addAudioTrackAdapter:(RTCAudioTrack *)track; | ||
| - (void)removeAudioTrackAdapter:(RTCAudioTrack *)track; | ||
|
santhoshvai marked this conversation as resolved.
|
||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
|
|
||
| #import <AVFoundation/AVFoundation.h> | ||
| #import <Foundation/Foundation.h> | ||
| #import <objc/runtime.h> | ||
| #import <stdatomic.h> | ||
|
|
||
| #import <React/RCTBridge.h> | ||
| #import <React/RCTEventDispatcher.h> | ||
| #import <React/RCTLog.h> | ||
|
|
||
| #import <WebRTC/RTCAudioRenderer.h> | ||
| #import <WebRTC/RTCAudioTrack.h> | ||
|
|
||
| #import "WebRTCModule+AudioTrackAdapter.h" | ||
| #import "WebRTCModule+RTCPeerConnection.h" | ||
| #import "WebRTCModule.h" | ||
|
|
||
| /* Fires the W3C 'unmute' event on a remote audio track when the first | ||
| * decoded PCM buffer arrives via RTCAudioRenderer. | ||
| * | ||
| * IMPORTANT — only the initial muted → unmuted transition is detectable. | ||
| * Subsequent mute events (network stall mid-call) cannot be detected | ||
| * from the renderer: the iOS audio render path and WebRTC's NetEq | ||
| * synthesize silence / PLC frames whenever RTP stops, so | ||
| * renderPCMBuffer: keeps firing at a steady rate regardless of network | ||
| * state. For "remote participant muted their mic" UI, use the | ||
| * out-of-band participant state from your signaling layer — that is the | ||
| * correct source of truth, not this adapter. | ||
| */ | ||
| @interface FirstBufferUnmuteRenderer : NSObject<RTCAudioRenderer> | ||
|
|
||
| @property(copy, nonatomic) NSNumber *peerConnectionId; | ||
| @property(copy, nonatomic) NSString *trackId; | ||
| @property(weak, nonatomic) WebRTCModule *module; | ||
|
|
||
| - (instancetype)initWith:(NSNumber *)peerConnectionId | ||
| trackId:(NSString *)trackId | ||
| webRTCModule:(WebRTCModule *)module; | ||
|
|
||
| @end | ||
|
|
||
| @implementation FirstBufferUnmuteRenderer { | ||
| atomic_flag _fired; | ||
| } | ||
|
|
||
| - (instancetype)initWith:(NSNumber *)peerConnectionId | ||
| trackId:(NSString *)trackId | ||
| webRTCModule:(WebRTCModule *)module { | ||
| self = [super init]; | ||
| if (self) { | ||
| self.peerConnectionId = peerConnectionId; | ||
| self.trackId = trackId; | ||
| self.module = module; | ||
| atomic_flag_clear(&_fired); | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)renderPCMBuffer:(AVAudioPCMBuffer *)pcmBuffer { | ||
| if (atomic_flag_test_and_set(&_fired)) { | ||
| return; | ||
| } | ||
| [self.module sendEventWithName:kEventMediaStreamTrackMuteChanged | ||
| body:@{ | ||
| @"pcId" : self.peerConnectionId, | ||
| @"trackId" : self.trackId, | ||
| @"muted" : @NO | ||
| }]; | ||
| RCTLog(@"[AudioTrackAdapter] Unmute event for pc %@ track %@", self.peerConnectionId, self.trackId); | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| @implementation RTCPeerConnection (AudioTrackAdapter) | ||
|
|
||
| - (NSMutableDictionary<NSString *, id> *)audioTrackAdapters { | ||
| return objc_getAssociatedObject(self, _cmd); | ||
| } | ||
|
|
||
| - (void)setAudioTrackAdapters:(NSMutableDictionary<NSString *, id> *)audioTrackAdapters { | ||
| objc_setAssociatedObject( | ||
| self, @selector(audioTrackAdapters), audioTrackAdapters, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
| } | ||
|
santhoshvai marked this conversation as resolved.
|
||
|
|
||
| - (void)addAudioTrackAdapter:(RTCAudioTrack *)track { | ||
| NSString *trackId = track.trackId; | ||
| if ([self.audioTrackAdapters objectForKey:trackId] != nil) { | ||
| RCTLogWarn(@"[AudioTrackAdapter] Adapter already exists for track %@", trackId); | ||
| return; | ||
| } | ||
|
|
||
| FirstBufferUnmuteRenderer *renderer = [[FirstBufferUnmuteRenderer alloc] initWith:self.reactTag | ||
| trackId:trackId | ||
| webRTCModule:self.webRTCModule]; | ||
| [self.audioTrackAdapters setObject:renderer forKey:trackId]; | ||
| [track addRenderer:renderer]; | ||
|
|
||
| RCTLogTrace(@"[AudioTrackAdapter] Adapter created for track %@", trackId); | ||
| } | ||
|
|
||
| - (void)removeAudioTrackAdapter:(RTCAudioTrack *)track { | ||
| NSString *trackId = track.trackId; | ||
| FirstBufferUnmuteRenderer *renderer = [self.audioTrackAdapters objectForKey:trackId]; | ||
| if (renderer == nil) { | ||
| RCTLogWarn(@"[AudioTrackAdapter] Adapter doesn't exist for track %@", trackId); | ||
| return; | ||
| } | ||
|
|
||
| [track removeRenderer:renderer]; | ||
| [self.audioTrackAdapters removeObjectForKey:trackId]; | ||
| RCTLogTrace(@"[AudioTrackAdapter] Adapter removed for track %@", trackId); | ||
| } | ||
|
santhoshvai marked this conversation as resolved.
|
||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.