Skip to content

Commit

Permalink
ios: Add helper methods to access RTCAudioSession.audioSessionDidActi…
Browse files Browse the repository at this point in the history
…vate and deactivate
  • Loading branch information
davidliu committed Aug 15, 2024
1 parent 1121257 commit 9ffdd6f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Documentation/iOSInstallation.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ Navigate to `<ProjectFolder>/ios/<ProjectName>/` and edit `Info.plist`, add the
<string>Microphone permission description</string>
```

## CallKit

If your app uses a CallKit integration to handle incoming calls, then your
CXProviderDelegate should call through to `RTCAudioSession.sharedInstance.audioSessionDidActivate/Deactivate` accordingly.

```
#import <WebRTC/RTCAudioSession.h>
- (void) provider:(CXProvider *) provider didActivateAudioSession:(AVAudioSession *) audioSession {
[[RTCAudioSession sharedInstance] audioSessionDidActivate:[AVAudioSession sharedInstance]];
}
- (void) provider:(CXProvider *) provider didDeactivateAudioSession:(AVAudioSession *) audioSession {
[[RTCAudioSession sharedInstance] audioSessionDidDeactivate:[AVAudioSession sharedInstance]];
}
```

Javascript methods are also provided to call these methods:

```
import { RTCAudioSession } from '@livekit/react-native-webrtc'
// Call as needed.
RTCAudioSession.audioSessionDidActivate();
RTCAudioSession.audioSessionDidDeactivate();
```

## Library not loaded/Code signature invalid

This is an issue with iOS 13.3.1.
Expand Down
4 changes: 4 additions & 0 deletions ios/RCTWebRTC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
4EE3A8D425B841DD00FAA24A /* ScreenCapturer.m in Sources */ = {isa = PBXBuildFile; fileRef = 4EE3A8CD25B841DD00FAA24A /* ScreenCapturer.m */; };
D3FF699919D2664B25C9D458 /* Pods_RCTWebRTC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A20F721AD842563B66292D5B /* Pods_RCTWebRTC.framework */; };
D74EF94829652169000742E1 /* TrackCapturerEventsEmitter.m in Sources */ = {isa = PBXBuildFile; fileRef = D74EF94629652169000742E1 /* TrackCapturerEventsEmitter.m */; };
D7F0711E2C6DC91F0031F594 /* WebRTCModule+RTCAudioSession.m in Sources */ = {isa = PBXBuildFile; fileRef = D7F0711D2C6DC91F0031F594 /* WebRTCModule+RTCAudioSession.m */; };
DEC96577264176C10052DB35 /* DataChannelWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = DEC96576264176C10052DB35 /* DataChannelWrapper.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -75,6 +76,7 @@
D74EF94529652148000742E1 /* CapturerEventsDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = CapturerEventsDelegate.h; path = RCTWebRTC/CapturerEventsDelegate.h; sourceTree = SOURCE_ROOT; };
D74EF94629652169000742E1 /* TrackCapturerEventsEmitter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TrackCapturerEventsEmitter.m; path = RCTWebRTC/TrackCapturerEventsEmitter.m; sourceTree = SOURCE_ROOT; };
D74EF94729652169000742E1 /* TrackCapturerEventsEmitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TrackCapturerEventsEmitter.h; path = RCTWebRTC/TrackCapturerEventsEmitter.h; sourceTree = SOURCE_ROOT; };
D7F0711D2C6DC91F0031F594 /* WebRTCModule+RTCAudioSession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "WebRTCModule+RTCAudioSession.m"; path = "RCTWebRTC/WebRTCModule+RTCAudioSession.m"; sourceTree = SOURCE_ROOT; };
D7F99C122938F4E0000A2450 /* WebRTCModule+RTCMediaStream.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "WebRTCModule+RTCMediaStream.h"; path = "RCTWebRTC/WebRTCModule+RTCMediaStream.h"; sourceTree = SOURCE_ROOT; };
DEC96576264176C10052DB35 /* DataChannelWrapper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = DataChannelWrapper.m; path = RCTWebRTC/DataChannelWrapper.m; sourceTree = "<group>"; };
DEC96579264176DF0052DB35 /* DataChannelWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DataChannelWrapper.h; path = RCTWebRTC/DataChannelWrapper.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -131,6 +133,7 @@
4EE3A8AF25B8413200FAA24A /* VideoCaptureController.h */,
0BDDA6DF20C18B6B00B38B45 /* VideoCaptureController.m */,
4EE3A8B525B8414A00FAA24A /* WebRTCModule+Permissions.m */,
D7F0711D2C6DC91F0031F594 /* WebRTCModule+RTCAudioSession.m */,
4EE3A8B825B8415900FAA24A /* WebRTCModule+RTCDataChannel.h */,
4EE3A8B925B8415900FAA24A /* WebRTCModule+RTCDataChannel.m */,
D7F99C122938F4E0000A2450 /* WebRTCModule+RTCMediaStream.h */,
Expand Down Expand Up @@ -262,6 +265,7 @@
4EC498BC25B8777F00E76218 /* ScreenCapturePickerViewManager.m in Sources */,
4EE3A8B225B8414000FAA24A /* WebRTCModule.m in Sources */,
4EE3A8D225B841DD00FAA24A /* CaptureController.m in Sources */,
D7F0711E2C6DC91F0031F594 /* WebRTCModule+RTCAudioSession.m in Sources */,
D74EF94829652169000742E1 /* TrackCapturerEventsEmitter.m in Sources */,
4EE3A8D125B841DD00FAA24A /* SocketConnection.m in Sources */,
4EE3A8BD25B8416500FAA24A /* WebRTCModule+RTCMediaStream.m in Sources */,
Expand Down
20 changes: 20 additions & 0 deletions ios/RCTWebRTC/WebRTCModule+RTCAudioSession.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#import <objc/runtime.h>

#import <React/RCTBridge.h>
#import <React/RCTBridgeModule.h>

#import "WebRTCModule.h"

@implementation WebRTCModule (RTCAudioSession)

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(audioSessionDidActivate) {
[[RTCAudioSession sharedInstance] audioSessionDidActivate:[AVAudioSession sharedInstance]];
return nil;
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(audioSessionDidDeactivate) {
[[RTCAudioSession sharedInstance] audioSessionDidDeactivate:[AVAudioSession sharedInstance]];
return nil;
}

@end
25 changes: 25 additions & 0 deletions src/RTCAudioSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NativeModules, Platform } from 'react-native';

const { WebRTCModule } = NativeModules;

export default class RTCAudioSession {
/**
* To be called when CallKit activates the audio session.
*/
static audioSessionDidActivate() {
// Only valid for iOS
if (Platform.OS === "ios") {

Check failure on line 11 in src/RTCAudioSession.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
WebRTCModule.audioSessionDidActivate()

Check failure on line 12 in src/RTCAudioSession.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}
}

/**
* To be called when CallKit deactivates the audio session.
*/
static audioSessionDidDeactivate() {
// Only valid for iOS
if (Platform.OS === "ios") {

Check failure on line 21 in src/RTCAudioSession.ts

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
WebRTCModule.audioSessionDidDeactivate()

Check failure on line 22 in src/RTCAudioSession.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import MediaStream from './MediaStream';
import MediaStreamTrack from './MediaStreamTrack';
import MediaStreamTrackEvent from './MediaStreamTrackEvent';
import permissions from './Permissions';
import RTCAudioSession from './RTCAudioSession';
import RTCErrorEvent from './RTCErrorEvent';
import RTCIceCandidate from './RTCIceCandidate';
import RTCPeerConnection from './RTCPeerConnection';
Expand All @@ -40,6 +41,7 @@ export {
RTCRtpReceiver,
RTCRtpSender,
RTCErrorEvent,
RTCAudioSession,
MediaStream,
MediaStreamTrack,
mediaDevices,
Expand Down

0 comments on commit 9ffdd6f

Please sign in to comment.