Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
66 changes: 64 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,55 @@ 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")) {
// Handle different types of deviceId constraints:
// 1. String: { deviceId: "audio-1" }
// 2. Object: { deviceId: { exact: "audio-1" } }
try {
ReadableType deviceIdType = audioConstraintsMap.getType("deviceId");

if (deviceIdType == ReadableType.String) {
// Simple string deviceId
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) {
// Object/Map with constraints like { exact: "audio-1" }
ReadableMap deviceIdMap = audioConstraintsMap.getMap("deviceId");

// Try to get from "exact" constraint first
if (deviceIdMap.hasKey("exact")) {
String exactDeviceId = deviceIdMap.getString("exact");
if (exactDeviceId != null && !exactDeviceId.isEmpty()) {
Log.d(TAG, "Using exact audio device ID: " + exactDeviceId);
this.audioDeviceId = exactDeviceId;
}
}
// If no exact, try "ideal" constraint
else if (deviceIdMap.hasKey("ideal")) {
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, "Unsupported deviceId type: " + deviceIdType);
}
} catch (Exception e) {
Log.e(TAG, "Error processing audio deviceId constraint: " + e.getMessage());
}
}

String id = UUID.randomUUID().toString();
PeerConnectionFactory pcFactory = webRTCModule.mFactory;
MediaConstraints peerConstraints = webRTCModule.constraintsForOptions(audioConstraintsMap);
Expand Down Expand Up @@ -157,7 +207,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 +394,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 +483,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-0";

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-0";

@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