Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
Expand Down Expand Up @@ -52,13 +53,15 @@ class GetUserMediaImpl {
private final Map<String, TrackPrivate> tracks = new HashMap<>();

private final WebRTCModule webRTCModule;
private String audioDeviceId;

private Promise displayMediaPromise;
private Intent mediaProjectionPermissionResultData;

GetUserMediaImpl(WebRTCModule webRTCModule, ReactApplicationContext reactContext) {
this.webRTCModule = webRTCModule;
this.reactContext = reactContext;
this.audioDeviceId = WebRTCModuleOptions.getInstance().defaultAudioDeviceId;

reactContext.addActivityEventListener(new BaseActivityEventListener() {
@Override
Expand All @@ -85,8 +88,80 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
private AudioTrack createAudioTrack(ReadableMap constraints) {
ReadableMap audioConstraintsMap = constraints.getMap("audio");

Log.d(TAG, "==========================================");
Log.d(TAG, "JONATHAN'S FORK: USING CUSTOM AUDIO DEVICE CODE");
Log.d(TAG, "==========================================");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will delete this kind of thing after i know what i have works

Log.d(TAG, "getUserMedia(audio): " + audioConstraintsMap);

// Check if a specific audio device ID was requested
if (audioConstraintsMap != null && audioConstraintsMap.hasKey("deviceId")) {
try {
// IMPORTANT: Check the type of deviceId to avoid casting errors
ReadableType deviceIdType = audioConstraintsMap.getType("deviceId");
Log.d(TAG, "Audio deviceId type: " + deviceIdType);

if (deviceIdType == ReadableType.String) {
// Case 1: deviceId is a simple string
String deviceId = audioConstraintsMap.getString("deviceId");
if (deviceId != null && !deviceId.isEmpty()) {
Log.d(TAG, "Using specific audio device ID (string): " + deviceId);
this.audioDeviceId = deviceId;
}
}
else if (deviceIdType == ReadableType.Map) {
// Case 2: deviceId is an object with exact/ideal constraints
ReadableMap deviceIdMap = audioConstraintsMap.getMap("deviceId");
Log.d(TAG, "Audio deviceId is a map: " + deviceIdMap);

// Try to get the "exact" constraint first
if (deviceIdMap.hasKey("exact")) {
ReadableType exactType = deviceIdMap.getType("exact");
if (exactType == ReadableType.String) {
String exactDeviceId = deviceIdMap.getString("exact");
if (exactDeviceId != null && !exactDeviceId.isEmpty()) {
Log.d(TAG, "Using exact audio device ID: " + exactDeviceId);
this.audioDeviceId = exactDeviceId;
}
} else {
Log.w(TAG, "Exact deviceId is not a string but: " + exactType);
}
}
// If no exact value, try the "ideal" constraint
else if (deviceIdMap.hasKey("ideal")) {
ReadableType idealType = deviceIdMap.getType("ideal");
if (idealType == ReadableType.String) {
String idealDeviceId = deviceIdMap.getString("ideal");
if (idealDeviceId != null && !idealDeviceId.isEmpty()) {
Log.d(TAG, "Using ideal audio device ID: " + idealDeviceId);
this.audioDeviceId = idealDeviceId;
}
} else {
Log.w(TAG, "Ideal deviceId is not a string but: " + idealType);
}
}
else {
// Fallback: Just use whatever is in the map as a last resort
Log.d(TAG, "No exact/ideal keys in deviceId map, using default");
}
}
else if (deviceIdType == ReadableType.Array) {
// Case 3: deviceId is an array
Log.w(TAG, "Audio deviceId is an array, not currently supported");
}
else {
// Case 4: deviceId is some other type
Log.w(TAG, "Unsupported deviceId type: " + deviceIdType);
}
} catch (Exception e) {
// Log the full stack trace to help with debugging
Log.e(TAG, "Error processing audio deviceId constraint", e);
}
}

// Log the final chosen audio device ID
Log.d(TAG, "Final audio device ID being used: " + this.audioDeviceId);

String id = UUID.randomUUID().toString();
PeerConnectionFactory pcFactory = webRTCModule.mFactory;
MediaConstraints peerConstraints = webRTCModule.constraintsForOptions(audioConstraintsMap);
Expand Down Expand Up @@ -157,7 +232,7 @@ ReadableArray enumerateDevices() {
}

WritableMap audio = Arguments.createMap();
audio.putString("deviceId", "audio-1");
audio.putString("deviceId", audioDeviceId); // Use the configured/current audio device ID
audio.putString("groupId", "");
audio.putString("label", "Audio");
audio.putString("kind", "audioinput");
Expand Down Expand Up @@ -344,7 +419,7 @@ void createStream(MediaStreamTrack[] tracks, BiConsumer<String, ArrayList<Writab

if (track instanceof AudioTrack) {
WritableMap settings = Arguments.createMap();
settings.putString("deviceId", "audio-1");
settings.putString("deviceId", audioDeviceId); // Use the configured/current audio device ID
settings.putString("groupId", "");
trackInfo.putMap("settings", settings);
}
Expand Down Expand Up @@ -433,6 +508,18 @@ void setVideoEffect(String trackId, String name) {
}
}

/**
* Set the audio device ID to use for future getUserMedia calls.
*
* @param deviceId The device ID to use
*/
void setAudioDeviceId(String deviceId) {
if (deviceId != null && !deviceId.isEmpty()) {
Log.d(TAG, "Setting audio device ID to: " + deviceId);
this.audioDeviceId = deviceId;
}
}

/**
* Application/library-specific private members of local
* {@code MediaStreamTrack}s created by {@code GetUserMediaImpl}.
Expand Down
50 changes: 50 additions & 0 deletions android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.oney.WebRTCModule;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
Expand Down Expand Up @@ -64,6 +71,9 @@
public class WebRTCModule extends ReactContextBaseJavaModule {
static final String TAG = WebRTCModule.class.getCanonicalName();

// Define fork version constant - increment this when making changes to the fork
public static final String FORK_VERSION = "fork-version-1";

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will delete this when i know my code works. sorry about that

PeerConnectionFactory mFactory;
VideoEncoderFactory mVideoEncoderFactory;
VideoDecoderFactory mVideoDecoderFactory;
Expand Down Expand Up @@ -1544,4 +1554,44 @@ public void addListener(String eventName) {
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
}

/**
* Set the default audio device ID to use when no specific device is requested.
* This allows applications to control which audio device is used by default.
*
* @param deviceId The device ID to use as default (e.g., "audio-1", "expo-av-audio", etc.)
*/
@ReactMethod
public void setDefaultAudioDeviceId(String deviceId) {
if (deviceId != null && !deviceId.isEmpty()) {
Log.d(TAG, "Setting default audio device ID to: " + deviceId);
WebRTCModuleOptions.getInstance().defaultAudioDeviceId = deviceId;

// Update current instance
if (getUserMediaImpl != null) {
getUserMediaImpl.setAudioDeviceId(deviceId);
}
}
}

/**
* Get the current default audio device ID.
*
* @param promise Promise to resolve with the current default audio device ID
*/
@ReactMethod
public void getDefaultAudioDeviceId(Promise promise) {
String deviceId = WebRTCModuleOptions.getInstance().defaultAudioDeviceId;
promise.resolve(deviceId);
}

/**
* Get the version of this custom fork
*
* @param promise Promise to resolve with the fork version
*/
@ReactMethod
public void getForkVersion(Promise promise) {
promise.resolve(FORK_VERSION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class WebRTCModuleOptions {
public AudioDeviceModule audioDeviceModule;
public Callable<AudioProcessingFactory> audioProcessingFactoryFactory;

// Default audio device identifier used when no specific device is requested
public String defaultAudioDeviceId = "audio-1";

public Loggable injectableLogger;
public Logging.Severity loggingSeverity;
public String fieldTrials;
Expand Down
21 changes: 21 additions & 0 deletions ios/RCTWebRTC/WebRTCModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#import "WebRTCModule+RTCPeerConnection.h"
#import "WebRTCModule.h"
#import "WebRTCModuleOptions.h"
#import <WebRTC/RTCSSLAdapter.h>

// Define fork version constant - increment this when making changes to the fork
static NSString * const FORK_VERSION = @"fork-version-1";

@interface WebRTCModule ()
@end
Expand Down Expand Up @@ -141,4 +145,21 @@ - (dispatch_queue_t)methodQueue {
];
}

/**
* Get the default audio device ID
*/
RCT_EXPORT_METHOD(getDefaultAudioDeviceId:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
NSString *deviceId = [WebRTCModuleOptions sharedInstance].defaultAudioDeviceId ?: @"";
resolve(deviceId);
}

/**
* Get the version of this custom fork
*/
RCT_EXPORT_METHOD(getForkVersion:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
resolve(FORK_VERSION);
}

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

Check failure on line 1 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
const { WebRTCModule } = NativeModules

Check failure on line 2 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon

/**
* Set the default audio device ID to use when no specific device is requested.
* This allows applications to control which audio device is used by default.
*
* @param deviceId - The device ID to use as default (e.g., "audio-1", "expo-av-audio", etc.)
*/
export function setDefaultAudioDeviceId(deviceId: string): void {
if (typeof deviceId !== 'string' || !deviceId.trim()) {

Check failure on line 11 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 2
throw new TypeError('deviceId must be a non-empty string')

Check failure on line 12 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 4

Check failure on line 12 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}

Check failure on line 13 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 2

WebRTCModule.setDefaultAudioDeviceId(deviceId)

Check failure on line 15 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 2

Check failure on line 15 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}

/**
* Get the current default audio device ID.
*
* @returns A promise that resolves to the current default audio device ID
*/
export function getDefaultAudioDeviceId(): Promise<string> {
return WebRTCModule.getDefaultAudioDeviceId()

Check failure on line 24 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 2

Check failure on line 24 in src/AudioDeviceModule.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}
Loading
Loading