Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 1dae878

Browse files
authored
Update Kotlin namaspaces and split JS logic into smaller chunks (#163)
* Update namespace * Split index.tsx into smaller files
1 parent 3d148c7 commit 1dae878

25 files changed

+906
-819
lines changed

android/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'maven-publish'
44

5-
group = 'com.reactnativemembrane'
5+
group = 'org.membraneframework.reactnative'
66
version = '0.1.0'
77

88
buildscript {
@@ -51,7 +51,7 @@ afterEvaluate {
5151
}
5252

5353
android {
54-
namespace = "com.reactnativemembrane"
54+
namespace = "org.membraneframework.reactnative"
5555
compileSdk(31)
5656

5757
defaultConfig {

android/src/main/java/com/reactnativemembrane/AudioDeviceKind.kt android/src/main/java/org/membraneframework/reactnative/AudioDeviceKind.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import com.twilio.audioswitch.AudioDevice
44

android/src/main/java/com/reactnativemembrane/AudioSwitchManager.kt android/src/main/java/org/membraneframework/reactnative/AudioSwitchManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import android.content.Context
44
import android.os.Handler

android/src/main/java/com/reactnativemembrane/EmitableEvents.kt android/src/main/java/org/membraneframework/reactnative/EmitableEvents.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
object EmitableEvents {
44
const val IsCameraOn = "IsCameraOn"

android/src/main/java/com/reactnativemembrane/MembraneWebRTC.kt android/src/main/java/org/membraneframework/reactnative/MembraneWebRTC.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import android.app.Activity
44
import android.content.ActivityNotFoundException

android/src/main/java/com/reactnativemembrane/MembraneWebRTCModule.kt android/src/main/java/org/membraneframework/reactnative/MembraneWebRTCModule.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import expo.modules.kotlin.Promise
44
import expo.modules.kotlin.functions.Coroutine

android/src/main/java/com/reactnativemembrane/RNEndpoint.kt android/src/main/java/org/membraneframework/reactnative/RNEndpoint.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import org.membraneframework.rtc.SimulcastConfig
44
import org.membraneframework.rtc.media.AudioTrack

android/src/main/java/com/reactnativemembrane/Utils.kt android/src/main/java/org/membraneframework/reactnative/Utils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import org.membraneframework.rtc.TrackEncoding
44

android/src/main/java/com/reactnativemembrane/VideoPreviewView.kt android/src/main/java/org/membraneframework/reactnative/VideoPreviewView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import android.content.Context
44
import expo.modules.kotlin.AppContext

android/src/main/java/com/reactnativemembrane/VideoPreviewViewModule.kt android/src/main/java/org/membraneframework/reactnative/VideoPreviewViewModule.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import expo.modules.kotlin.modules.Module
44
import expo.modules.kotlin.modules.ModuleDefinition

android/src/main/java/com/reactnativemembrane/VideoRendererView.kt android/src/main/java/org/membraneframework/reactnative/VideoRendererView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import android.content.Context
44
import expo.modules.kotlin.AppContext

android/src/main/java/com/reactnativemembrane/VideoRendererViewModule.kt android/src/main/java/org/membraneframework/reactnative/VideoRendererViewModule.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactnativemembrane
1+
package org.membraneframework.reactnative
22

33
import expo.modules.kotlin.modules.Module
44
import expo.modules.kotlin.modules.ModuleDefinition

expo-module.config.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"platforms": ["ios", "android", "web"],
2+
"platforms": [
3+
"ios",
4+
"android",
5+
"web"
6+
],
37
"ios": {
48
"modules": [
59
"MembraneWebRTCModule",
@@ -9,9 +13,9 @@
913
},
1014
"android": {
1115
"modules": [
12-
"com.reactnativemembrane.MembraneWebRTCModule",
13-
"com.reactnativemembrane.VideoPreviewViewModule",
14-
"com.reactnativemembrane.VideoRendererViewModule"
16+
"org.membraneframework.reactnative.MembraneWebRTCModule",
17+
"org.membraneframework.reactnative.VideoPreviewViewModule",
18+
"org.membraneframework.reactnative.VideoRendererViewModule"
1519
]
1620
}
17-
}
21+
}

src/common/eventEmitter.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NativeModulesProxy, EventEmitter } from 'expo-modules-core';
2+
3+
import MembraneWebRTCModule from '../MembraneWebRTCModule';
4+
5+
export const ReceivableEvents = {
6+
IsCameraOn: 'IsCameraOn',
7+
IsMicrophoneOn: 'IsMicrophoneOn',
8+
IsScreencastOn: 'IsScreencastOn',
9+
SimulcastConfigUpdate: 'SimulcastConfigUpdate',
10+
EndpointsUpdate: 'EndpointsUpdate',
11+
AudioDeviceUpdate: 'AudioDeviceUpdate',
12+
SendMediaEvent: 'SendMediaEvent',
13+
BandwidthEstimation: 'BandwidthEstimation',
14+
} as const;
15+
16+
export const eventEmitter = new EventEmitter(
17+
MembraneWebRTCModule ?? NativeModulesProxy.MembraneWebRTC
18+
);

src/common/metadata.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Metadata } from '../MembraneWebRTC.types';
2+
import MembraneWebRTCModule from '../MembraneWebRTCModule';
3+
4+
/**
5+
/**
6+
* a function that updates endpoints's metadata on the server
7+
* @param metadata a map `string -> any` containing user's track metadata to be sent to the server
8+
*/
9+
export async function updateEndpointMetadata<
10+
EndpointMetadataType extends Metadata,
11+
>(metadata: EndpointMetadataType) {
12+
await MembraneWebRTCModule.updateEndpointMetadata(metadata);
13+
}
14+
15+
/**
16+
* a function that updates video metadata on the server.
17+
* @param metadata a map string -> any containing video track metadata to be sent to the server
18+
*/
19+
export async function updateVideoTrackMetadata<
20+
VideoTrackMetadataType extends Metadata,
21+
>(metadata: VideoTrackMetadataType) {
22+
await MembraneWebRTCModule.updateVideoTrackMetadata(metadata);
23+
}
24+
/**
25+
* a function that updates audio metadata on the server
26+
* @param metadata a map `string -> any` containing audio track metadata to be sent to the server
27+
*/
28+
export async function updateAudioTrackMetadata<
29+
AudioTrackMetadataType extends Metadata,
30+
>(metadata: AudioTrackMetadataType) {
31+
await MembraneWebRTCModule.updateAudioTrackMetadata(metadata);
32+
}

src/common/webRTC.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { LoggingSeverity, TrackEncoding } from '../MembraneWebRTC.types';
2+
import MembraneWebRTCModule from '../MembraneWebRTCModule';
3+
4+
/**
5+
* This function initialize necessary native objects to properly handle sound and video.
6+
* Call it only once in your app before any other functionality, otherwise package will not work as intended.
7+
*/
8+
export async function initializeWebRTC() {
9+
await MembraneWebRTCModule.create();
10+
}
11+
12+
/**
13+
* sets track encoding that server should send to the client library.
14+
* The encoding will be sent whenever it is available. If chooses encoding is
15+
* temporarily unavailable, some other encoding will be sent until choose encoding
16+
* becomes active again.
17+
*
18+
* @param trackId id of a track which encoding you want to select
19+
* @param encoding encoding to select
20+
*/
21+
export async function setTargetTrackEncoding(
22+
trackId: string,
23+
encoding: TrackEncoding
24+
) {
25+
await MembraneWebRTCModule.setTargetTrackEncoding(trackId, encoding);
26+
}
27+
28+
/**
29+
* Function that changes level of debugging logs in WebRTC.
30+
* @param severity to use when displaying logs
31+
* @returns a promise that is resolved when debug severity is changed
32+
*/
33+
export function changeWebRTCLoggingSeverity(
34+
severity: LoggingSeverity
35+
): Promise<void> {
36+
return MembraneWebRTCModule.changeWebRTCLoggingSeverity(severity);
37+
}

src/hooks/useAudioSettings.ts

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { useCallback, useEffect, useState } from 'react';
2+
import { Platform } from 'react-native';
3+
4+
import {
5+
AudioOutputDevice,
6+
AudioOutputDeviceType,
7+
AudioSessionMode,
8+
} from '../MembraneWebRTC.types';
9+
import MembraneWebRTCModule from '../MembraneWebRTCModule';
10+
import { ReceivableEvents, eventEmitter } from '../common/eventEmitter';
11+
12+
/**
13+
* This hook manages audio settings.
14+
*/
15+
export function useAudioSettings() {
16+
const [selectedAudioOutputDevice, setSelectedAudioOutputDevice] =
17+
useState<AudioOutputDevice | null>(null);
18+
const [availableDevices, setAvailableDevices] = useState<AudioOutputDevice[]>(
19+
[]
20+
);
21+
22+
type onAudioDeviceEvent = {
23+
AudioDeviceUpdate: {
24+
selectedDevice: AudioOutputDevice;
25+
availableDevices: AudioOutputDevice[];
26+
};
27+
};
28+
29+
const onAudioDevice = useCallback((event: onAudioDeviceEvent) => {
30+
setSelectedAudioOutputDevice(event.AudioDeviceUpdate.selectedDevice);
31+
setAvailableDevices(event.AudioDeviceUpdate.availableDevices);
32+
}, []);
33+
34+
useEffect(() => {
35+
const eventListener = eventEmitter.addListener(
36+
ReceivableEvents.AudioDeviceUpdate,
37+
onAudioDevice
38+
);
39+
MembraneWebRTCModule.startAudioSwitcher();
40+
return () => {
41+
eventListener.remove();
42+
if (Platform.OS === 'android') {
43+
MembraneWebRTCModule.stopAudioSwitcher();
44+
}
45+
};
46+
}, []);
47+
48+
/**
49+
* [Android only] selects output audio device.
50+
* For detecting and selecting bluettoth devices make sure you have the BLUETOOTH_CONNECT permission.
51+
*/
52+
const selectOutputAudioDevice = useCallback(
53+
async (device: AudioOutputDeviceType) => {
54+
if (Platform.OS === 'ios') {
55+
throw Error(
56+
'selectOutputAudioDevice function is supported only on Android. ' +
57+
'To select an output audio device on iOS use selectAudioSessionMode or showAudioRoutePicker functions'
58+
);
59+
}
60+
await MembraneWebRTCModule.setOutputAudioDevice(device);
61+
},
62+
[]
63+
);
64+
65+
/**
66+
* [iOS only] selects audio session mode. For more information refer to Apple's documentation:
67+
* https://developer.apple.com/documentation/avfaudio/avaudiosession/mode/
68+
*
69+
*/
70+
const selectAudioSessionMode = useCallback(
71+
async (audioSessionMode: AudioSessionMode) => {
72+
if (Platform.OS === 'android') {
73+
throw Error('selectAudioSessionMode function is supported only on iOS');
74+
}
75+
await MembraneWebRTCModule.selectAudioSessionMode(audioSessionMode);
76+
},
77+
[]
78+
);
79+
80+
/**
81+
* [iOS only] Shows a picker modal that allows user to select output audio device. For more
82+
* information refer to Apple's documentation: https://developer.apple.com/documentation/avkit/avroutepickerview
83+
*/
84+
const showAudioRoutePicker = useCallback(async () => {
85+
if (Platform.OS === 'android') {
86+
throw Error(
87+
'showAudioRoutePicker function is supported only on iOS. ' +
88+
'To select an output audio device on Android use selectOutputAudioDevice function'
89+
);
90+
}
91+
await MembraneWebRTCModule.showAudioRoutePicker();
92+
}, []);
93+
94+
return {
95+
/**
96+
* currently selected output audio device
97+
*/
98+
selectedAudioOutputDevice,
99+
/**
100+
* [Android only] available audio output devices to be set
101+
*/
102+
availableDevices,
103+
selectOutputAudioDevice,
104+
selectAudioSessionMode,
105+
showAudioRoutePicker,
106+
};
107+
}

src/hooks/useBandwidthEstimation.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect, useState } from 'react';
2+
3+
import { BandwidthEstimationEvent } from '../MembraneWebRTC.types';
4+
import { ReceivableEvents, eventEmitter } from '../common/eventEmitter';
5+
6+
/**
7+
* This hook provides current bandwidth estimation
8+
* estimation - client's available incoming bitrate estimated
9+
* by the server. It's measured in bits per second.
10+
*/
11+
export function useBandwidthEstimation() {
12+
const [estimation, setEstimation] = useState<number | null>(null);
13+
14+
useEffect(() => {
15+
const eventListener = eventEmitter.addListener<BandwidthEstimationEvent>(
16+
ReceivableEvents.BandwidthEstimation,
17+
(event) => setEstimation(event.BandwidthEstimation)
18+
);
19+
return () => eventListener.remove();
20+
}, []);
21+
22+
return { estimation };
23+
}

0 commit comments

Comments
 (0)