Skip to content

Commit 82bd82d

Browse files
authored
Android & iOS works with mediasoup basic demo (#3)
1 parent 4bf975c commit 82bd82d

File tree

55 files changed

+2001
-1194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2001
-1194
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
dep: "brew update && brew install cocoapods xcodegen"
3131
cmd: "./scripts/build_mac_demo.sh"
3232
- os: windows-latest
33-
cmd: ".\\scripts\\setup_windows.bat \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Tools\\MSVC\\14.43.34808\\bin\\Hostx64\\x64\\lib.exe\" && cd example\\winApp && msbuild winApp.vcxproj /t:Build /p:Configuration=Release /p:Platform=x64"
33+
cmd: ".\\scripts\\setup_windows.bat \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Tools\\MSVC\\14.44.35207\\bin\\Hostx64\\x64\\lib.exe\" && cd example\\winApp && msbuild winApp.vcxproj /t:Build /p:Configuration=Release /p:Platform=x64"
3434
- os: macos-latest
3535
cmd: "./gradlew :example:webApp:jsBrowserDistribution"
3636
- os: ubuntu-latest

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ Open the project (the repo root dir) in Android studio, and run the example.andr
7171
### iOS
7272

7373
```bash
74-
./scripts/setup_apple.sh
74+
./scripts/setup_apple_demo.sh
7575
# open example/iosApp/iosApp.xcworkspace in Xcode, and run it.
7676
```
7777

7878
### macOS
7979

8080
```bash
81-
./scripts/setup_apple.sh
81+
./scripts/setup_apple_demo.sh
8282
# open example/macApp/macApp.xcworkspace in Xcode, and run it.
8383
```
8484

@@ -91,6 +91,8 @@ Open the project (the repo root dir) in Android studio, and run the example.andr
9191

9292
### Linux
9393

94+
On Linux machine with GUI support.
95+
9496
```bash
9597
./scripts/build_linux_demo.sh
9698
./example/linuxApp/build/loopback <path to video file>
@@ -99,7 +101,7 @@ Open the project (the repo root dir) in Android studio, and run the example.andr
99101
### JS
100102

101103
```bash
102-
./gradlew :example:webApp:jsBrowserRun
104+
./gradlew :example:webApp:jsBrowserDevelopmentRun
103105
```
104106

105107
## Build WebRTC

example/androidApp/src/main/java/com/piasy/kmp/webrtc/android/CallActivity.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66
import android.os.Handler;
77
import android.os.HandlerThread;
8+
import android.text.TextUtils;
89
import android.util.DisplayMetrics;
910
import android.view.Gravity;
1011
import android.view.View;
@@ -44,14 +45,7 @@
4445

4546
import static com.piasy.kmp.webrtc.AndroidPeerConnectionClientFactoryKt.createPeerConnectionClientFactory;
4647
import static com.piasy.kmp.webrtc.AndroidPeerConnectionClientFactoryKt.initializeWebRTC;
47-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_AUDIO_ONLY;
48-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_IS_PUBLISHER;
49-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_RECORD_CALL;
50-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_VIDEO_BITRATE;
51-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_VIDEO_CODEC;
52-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_VIDEO_FPS;
53-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_VIDEO_HEIGHT;
54-
import static com.piasy.kmp.webrtc.android.HallActivity.EXTRA_VIDEO_WIDTH;
48+
import static com.piasy.kmp.webrtc.android.HallActivity.*;
5549

5650
public class CallActivity extends AppCompatActivity implements PeerConnectionClientCallback, AudioMixer.MixerCallback {
5751

@@ -61,6 +55,7 @@ public class CallActivity extends AppCompatActivity implements PeerConnectionCli
6155
private static final boolean PAUSE_STREAMING = true;
6256

6357
private boolean isPublisher;
58+
private boolean singlePc;
6459
private boolean audioOnly;
6560
private int videoWidth;
6661
private int videoHeight;
@@ -87,6 +82,7 @@ public class CallActivity extends AppCompatActivity implements PeerConnectionCli
8782
private EglBase eglBase;
8883
private PeerConnectionClientFactory pcClientFactory;
8984
private PeerConnectionClient pcClient;
85+
private PeerConnectionClient recvPcClient;
9086
private AudioMixer mixer;
9187

9288
private static int getSystemUiVisibility() {
@@ -117,6 +113,7 @@ protected void onCreate(Bundle savedInstanceState) {
117113

118114
isPublisher = intent.getBooleanExtra(EXTRA_IS_PUBLISHER, true);
119115
recording = intent.getBooleanExtra(EXTRA_RECORD_CALL, false);
116+
singlePc = intent.getBooleanExtra(EXTRA_SINGLE_PC, false);
120117
audioOnly = intent.getBooleanExtra(EXTRA_AUDIO_ONLY, false);
121118

122119
videoWidth = intent.getIntExtra(EXTRA_VIDEO_WIDTH, 640);
@@ -300,11 +297,23 @@ public Unit invoke(Integer code, String msg) {
300297

301298
// 5. create PcClient
302299
pcClient = pcClientFactory.createPeerConnectionClient(
303-
"test", PeerConnectionClient.DIR_SEND_RECV, true, videoMaxBitrate, videoFps, this
300+
"test",
301+
singlePc ? PeerConnectionClient.DIR_SEND_RECV : PeerConnectionClient.DIR_SEND_ONLY,
302+
true, videoMaxBitrate, videoFps, this
304303
);
304+
if (!singlePc) {
305+
recvPcClient = pcClientFactory.createPeerConnectionClient(
306+
"test-recv",
307+
PeerConnectionClient.DIR_RECV_ONLY,
308+
true, videoMaxBitrate, videoFps, this
309+
);
310+
}
305311

306312
// 6. create pc
307313
pcClient.createPeerConnection(Collections.emptyList());
314+
if (recvPcClient != null) {
315+
recvPcClient.createPeerConnection(Collections.emptyList());
316+
}
308317

309318
// 7. create offer
310319
pcClient.createOffer();
@@ -313,14 +322,28 @@ public Unit invoke(Integer code, String msg) {
313322
@Override
314323
public void onLocalDescription(@NotNull String peerUid, @NotNull SessionDescription sdp) {
315324
// 8. send offer to remote, get answer from remote, and set answer
316-
SessionDescription answer = new SessionDescription(SessionDescription.ANSWER, sdp.getSdpDescription());
317-
pcClient.setRemoteDescription(answer);
325+
if (singlePc || TextUtils.equals(peerUid, "test-recv")) {
326+
SessionDescription answer = new SessionDescription(SessionDescription.ANSWER, sdp.getSdpDescription());
327+
pcClient.setRemoteDescription(answer);
328+
} else if (TextUtils.equals(peerUid, "test")) {
329+
SessionDescription offer = new SessionDescription(SessionDescription.OFFER, sdp.getSdpDescription());
330+
recvPcClient.setRemoteDescription(offer);
331+
recvPcClient.createAnswer();
332+
}
333+
}
334+
335+
@Override
336+
public void onSetRemoteSdpResult(@NotNull String peerUid, boolean success) {
318337
}
319338

320339
@Override
321340
public void onIceCandidate(@NotNull String peerUid, @NotNull IceCandidate candidate) {
322341
// 9. send ice candidate to remote, get ice candidate from remote, add ice candidate
323-
pcClient.addIceCandidate(candidate);
342+
if (singlePc || TextUtils.equals(peerUid, "test-recv")) {
343+
pcClient.addIceCandidate(candidate);
344+
} else if (TextUtils.equals(peerUid, "test")) {
345+
recvPcClient.addIceCandidate(candidate);
346+
}
324347
}
325348

326349
@Override
@@ -329,7 +352,11 @@ public void onIceConnected(@NotNull String peerUid) {
329352
@Override
330353
public void run() {
331354
// 10. on ice connected, add renderer for remote stream
332-
pcClient.addRemoteTrackRenderer(remoteRenderer);
355+
if (singlePc) {
356+
pcClient.addRemoteTrackRenderer(remoteRenderer);
357+
} else {
358+
recvPcClient.addRemoteTrackRenderer(remoteRenderer);
359+
}
333360
}
334361
});
335362
}
@@ -343,6 +370,10 @@ private void disconnect() {
343370
if (pcClientFactory != null) {
344371
pcClientFactory.stopVideoCapture();
345372
pcClient.close();
373+
if (recvPcClient != null) {
374+
recvPcClient.close();
375+
recvPcClient = null;
376+
}
346377
pcClientFactory.destroyPeerConnectionFactory();
347378
pcClientFactory = null;
348379
pcClient = null;

example/androidApp/src/main/java/com/piasy/kmp/webrtc/android/HallActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
public class HallActivity extends Activity {
2020

2121
public static final String EXTRA_IS_PUBLISHER = "avconf.IS_PUBLISHER";
22+
public static final String EXTRA_SINGLE_PC = "avconf.SINGLE_PC";
2223
public static final String EXTRA_AUDIO_ONLY = "avconf.AUDIO_ONLY";
2324
public static final String EXTRA_RECORD_CALL = "avconf.RECORD_CALL";
2425
public static final String EXTRA_VIDEO_WIDTH = "avconf.VIDEO_WIDTH";
@@ -35,6 +36,7 @@ public class HallActivity extends Activity {
3536
private String keyprefVideoBitrateValue;
3637

3738
private CheckBox mRecording;
39+
private CheckBox mSinglePc;
3840
private CheckBox mAudioOnly;
3941
private CheckBox mPublisher;
4042
private CheckBox mScreenShare;
@@ -59,6 +61,7 @@ protected void onCreate(Bundle savedInstanceState) {
5961
tvVersion.setText("kmp-webrtc " + BuildConfig.VERSION_NAME);
6062

6163
mRecording = findViewById(R.id.recording);
64+
mSinglePc = findViewById(R.id.single_pc);
6265
mAudioOnly = findViewById(R.id.audio_only);
6366
mPublisher = findViewById(R.id.publisher);
6467
mScreenShare = findViewById(R.id.screen_share);
@@ -84,7 +87,8 @@ public void onRequestPermissionsResult(final int requestCode,
8487
Manifest.permission.MODIFY_AUDIO_SETTINGS,
8588
Manifest.permission.BLUETOOTH,
8689
Manifest.permission.WRITE_EXTERNAL_STORAGE,
87-
Manifest.permission.ACCESS_NETWORK_STATE
90+
Manifest.permission.ACCESS_NETWORK_STATE,
91+
Manifest.permission.BLUETOOTH_CONNECT,
8892
})
8993
void checkPermission() {
9094
mGotPermission = true;
@@ -145,6 +149,7 @@ private void startLoopback(Class activityClass) {
145149
// Start AppRTCMobile activity.
146150
Intent intent = new Intent(this, activityClass);
147151
intent.putExtra(EXTRA_IS_PUBLISHER, mPublisher.isChecked());
152+
intent.putExtra(EXTRA_SINGLE_PC, mSinglePc.isChecked());
148153
intent.putExtra(EXTRA_AUDIO_ONLY, mAudioOnly.isChecked());
149154
intent.putExtra(EXTRA_RECORD_CALL, mRecording.isChecked());
150155
intent.putExtra(EXTRA_VIDEO_WIDTH, videoWidth);

example/androidApp/src/main/res/layout/activity_hall.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@
3232

3333
<LinearLayout
3434
android:layout_width="wrap_content"
35-
android:layout_height="130dp"
35+
android:layout_height="wrap_content"
3636
android:layout_marginLeft="160dp"
3737
android:orientation="vertical"
3838
>
3939

40+
<CheckBox
41+
android:id="@+id/single_pc"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:text="single pc"
45+
/>
46+
4047
<CheckBox
4148
android:id="@+id/audio_only"
4249
android:layout_width="wrap_content"
@@ -71,7 +78,8 @@
7178
android:id="@+id/tvVersion"
7279
android:layout_width="match_parent"
7380
android:layout_height="20dp"
74-
android:layout_marginTop="130dp"
81+
android:layout_marginBottom="20dp"
82+
android:layout_gravity="bottom"
7583
android:gravity="center"
7684
tools:text="ver 1.0.0"
7785
/>

example/iosApp/Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ target 'iosApp' do
55
platform :ios, '14.0'
66
pod 'kmp_webrtc', :path => '../../kmp-webrtc/kmp_webrtc.podspec'
77
pod 'WebRTC', :path => '../../libs/apple/WebRTC.podspec'
8-
pod 'kmp_xlog', '~> 1.3.4'
98
end

example/iosApp/iosApp/ARDAppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
// The main application class of the AppRTCMobile iOS app demonstrating
1414
// interoperability between the Objective C implementation of PeerConnection
1515
// and the appr.tc demo webapp.
16-
@interface ARDAppDelegate : NSObject<UIApplicationDelegate>
16+
@interface ARDAppDelegate : UIResponder<UIApplicationDelegate>
1717
@end

example/iosApp/iosApp/ARDAppDelegate.m

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@
1010

1111
#import "ARDAppDelegate.h"
1212

13-
#import "HallViewController.h"
13+
#import "SceneDelegate.h"
1414

15-
@implementation ARDAppDelegate {
16-
UIWindow *_window;
17-
}
15+
@implementation ARDAppDelegate
1816

1917
#pragma mark - UIApplicationDelegate methods
2018

21-
- (BOOL)application:(UIApplication *)application
22-
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
20+
// 这里可以保留你的全局初始化代码
21+
return YES;
22+
}
2323

24-
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
25-
[_window makeKeyAndVisible];
26-
HallViewController *viewController = [[HallViewController alloc] init];
24+
#pragma mark - UISceneSession lifecycle (iOS 13+)
2725

28-
UINavigationController *root =
29-
[[UINavigationController alloc] initWithRootViewController:viewController];
30-
root.navigationBar.translucent = NO;
31-
_window.rootViewController = root;
26+
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
27+
// 当创建新场景时调用,返回一个场景配置对象
28+
UISceneConfiguration *configuration = [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
29+
configuration.delegateClass = [SceneDelegate class];
30+
return configuration;
31+
}
3232

33-
return YES;
33+
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
34+
// 当用户丢弃场景会话时调用
3435
}
3536

3637
@end

example/iosApp/iosApp/CallViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
3232
@interface CallViewController : UIViewController
3333

3434
- (instancetype)initWithAudioOnly:(bool)audioOnly
35+
singlePc:(bool)singlePc
3536
isLandscape:(bool)isLandscape;
3637

3738
@end

0 commit comments

Comments
 (0)