Skip to content

Add MAUI example app to csharp samples #4078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ tests/fuzz/*.o
cov-int/
getversion.mak
configure.out
*/Platforms/iOS/pjsua2/*
*/Platforms/iOS/lib/*
40 changes: 39 additions & 1 deletion pjsip-apps/src/swig/csharp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@ SWIG_FLAGS += -w312
PROJ_NAME=pjsua2xamarin
OUT_DIR=$(PROJ_NAME)/$(PROJ_NAME)/pjsua2
NAMESPACE=$(PROJ_NAME).pjsua2
MAUI_PROJ_NAME=pjsua2maui
MAUI_NAMESPACE=libpjsua2.maui

ARCH=$(TARGET_ARCH)

ifeq ($(OS),android)
LIBPJSUA2_DIR=$(PROJ_NAME)/$(PROJ_NAME).Android/lib/$(ARCH)
LIBPJSUA2=$(LIBPJSUA2_DIR)/libpjsua2.so
SUP_CLASS_DIR=$(PROJ_NAME)/$(PROJ_NAME).Android/org/pjsip
MAUI_LIBPJSUA2_DIR=$(MAUI_PROJ_NAME)/$(MAUI_PROJ_NAME)/Platforms/Android/lib/$(ARCH)
MAUI_LIBPJSUA2=$(MAUI_LIBPJSUA2_DIR)/libpjsua2.so
MAUI_SUP_CLASS_DIR=$(MAUI_PROJ_NAME)/$(MAUI_PROJ_NAME)/Platforms/Android/org/pjsip
MAUI_OUT_DIR=$(MAUI_PROJ_NAME)/$(MAUI_PROJ_NAME)/Platforms/Android/pjsua2
else
ifeq ($(OS),ios)
LIBPJSUA2_DIR=$(PROJ_NAME)/$(PROJ_NAME).iOS/lib/$(ARCH)
MAUI_LIBPJSUA2_DIR=$(MAUI_PROJ_NAME)/$(MAUI_PROJ_NAME)/Platforms/iOS/lib/$(ARCH)
SWIG_FLAGS += -dllimport "__Internal"
MAUI_OUT_DIR=$(MAUI_PROJ_NAME)/$(MAUI_PROJ_NAME)/Platforms/iOS/pjsua2
else
LIBPJSUA2_DIR=$(PROJ_NAME)/lib

endif
LIBPJSUA2=$(LIBPJSUA2_DIR)/libpjsua2.a
MAUI_LIBPJSUA2=$(MAUI_LIBPJSUA2_DIR)/libpjsua2.a
endif

# Build settings
Expand All @@ -44,7 +54,7 @@ MY_LDFLAGS := $(PJ_LDXXFLAGS) $(PJ_LDXXLIBS) $(LDFLAGS)

.PHONY: all install uninstall

all: $(LIBPJSUA2) sample
all: $(LIBPJSUA2) $(MAUI_LIBPJSUA2) sample

$(LIBPJSUA2): $(OUT_DIR)/pjsua2_wrap.o
mkdir -p $(LIBPJSUA2_DIR)
Expand Down Expand Up @@ -74,6 +84,34 @@ $(OUT_DIR)/pjsua2_wrap.cpp: ../pjsua2.i ../symbols.i Makefile $(SRCS)
mkdir -p $(OUT_DIR)
swig $(SWIG_FLAGS) -namespace $(NAMESPACE) -csharp -o $(OUT_DIR)/pjsua2_wrap.cpp ../pjsua2.i

$(MAUI_LIBPJSUA2): $(MAUI_OUT_DIR)/pjsua2_wrap.o
mkdir -p $(MAUI_LIBPJSUA2_DIR)
ifeq ($(OS),android)
mkdir -p $(MAUI_SUP_CLASS_DIR)
$(PJ_CXX) -shared -o $(MAUI_LIBPJSUA2) $(MAUI_OUT_DIR)/pjsua2_wrap.o \
$(MY_CFLAGS) $(MY_LDFLAGS)
# copy libc++_shared.so manually
cp -f ${STD_CPP_LIB} $(MAUI_LIBPJSUA2_DIR)
else
$(AR) $(MAUI_LIBPJSUA2) $(AR_FLAGS) $(MAUI_OUT_DIR)/pjsua2_wrap.o $(PJ_LIBXX_FILES)
endif
ifneq (,$(findstring PJMEDIA_VIDEO_DEV_HAS_ANDROID=1,$(ANDROID_CFLAGS)))
@echo "Copying Android camera helper components..."
cp $(PJDIR)/pjmedia/src/pjmedia-videodev/android/PjCamera*.java $(MAUI_SUP_CLASS_DIR)/
endif
ifneq (,$(findstring PJMEDIA_AUDIO_DEV_HAS_OBOE=1,$(OBOE_CFLAGS)))
@echo "Copying Android Oboe audio device helper components..."
cp $(PJDIR)/pjmedia/src/pjmedia-audiodev/android/PjAudioDevInfo.java $(MAUI_SUP_CLASS_DIR)/
endif

$(MAUI_OUT_DIR)/pjsua2_wrap.o: $(MAUI_OUT_DIR)/pjsua2_wrap.cpp
$(PJ_CXX) -c $(MAUI_OUT_DIR)/pjsua2_wrap.cpp -o $(MAUI_OUT_DIR)/pjsua2_wrap.o \
$(MY_CFLAGS)

$(MAUI_OUT_DIR)/pjsua2_wrap.cpp: ../pjsua2.i ../symbols.i Makefile $(SRCS)
mkdir -p $(MAUI_OUT_DIR)
swig $(SWIG_FLAGS) -namespace $(MAUI_NAMESPACE) -csharp -o $(MAUI_OUT_DIR)/pjsua2_wrap.cpp ../pjsua2.i

sample: sample.cs
@echo "Copying sample code..."
cp sample.cs $(PROJ_NAME)/$(PROJ_NAME)
Expand Down
6 changes: 6 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.dll
.java
.class
.jar
.so
/*/obj/
147 changes: 147 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# pjsua2maui - pjsip on .net maui

## STEPS FOR BUILDING:

### Android

- SETUP:
- Visual Studio Code & .NET MAUI - [Install steps](https://learn.microsoft.com/en-us/dotnet/maui/get-started/installation?view=net-maui-11.0&tabs=visual-studio-code)
- Java 17
- Android NDK r21d or later
- pjsip v2.14.1 or later

- Run the following commands:

```bash
export ANDROID_SDK_ROOT=/somewhere/there/is/installed/the/sdk/
sdkmanager --install "ndk;28.0.12674087" --channel=3 --sdk_root=$ANDROID_SDK_ROOT
sdkmanager --install "cmake;3.31.0" --sdk_root=$ANDROID_SDK_ROOT
sdkmanager --install "build-tools;35.0.0" "platform-tools" "platforms;android-35" --sdk_root=$ANDROID_SDK_ROOT
echo yes | sdkmanager --licenses --sdk_root=$ANDROID_SDK_ROOT
Copy link
Member

Choose a reason for hiding this comment

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

Add note:
If you are sharing the Android SDK location with Android Studio, the recommended way to install/update the SDK and accept licenses is by using Android Studio.

```
note:
If you are sharing the Android SDK location with Android Studio, the recommended way to install/update the SDK and accept licenses is by using Android Studio.

- Create the following variable:

```bash

export ANDROID_NDK_ROOT=/path/to/ndk/root/installation

```

- Then build pjsip

For device:
```bash
./configure-android
```

For simulator:
```bash

TARGET_ABI=x86_64 ./configure-android
Copy link
Member

Choose a reason for hiding this comment

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

Need instruction for device too, i.e.:
For device:
./configure-android
For simulator:
TARGET_ABI=x86_64 ./configure-android

make dep && make clean && make

```


after run make dep and make, run the make on ``` pjsip-apps/src/swig/csharp ``` folder

```bash

make

```

then start the android emulator, go to ``` pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui ``` and run:
Copy link
Member

Choose a reason for hiding this comment

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

This also needs instruction for device.

Here's what I did:
For Android device:

  • Open Android Studio and connect an Android device, such as by using "Pair Devices Using Wifi"
  • (Optional) Start Logcat in VS Code, for obtaining Android log.
    • Install Logcat: View-Extensions. Install Logcat extension.
    • Start Logcat: View-Command Pallette.


```bash

dotnet workload restore
dotnet restore
dotnet build -t:Run -c Debug -f net9.0-android

```
or, for run in device:

Open Android Studio and connect an Android device, such as by using "Pair Devices Using Wifi"
(Optional) Start Logcat in VS Code, for obtaining Android log.
Install Logcat: View-Extensions. Install Logcat extension.
Start Logcat: View-Command Pallette.

### iOS

- SETUP:
- Visual Studio Code & .NET MAUI - [Install steps](https://learn.microsoft.com/en-us/dotnet/maui/get-started/installation?view=net-maui-11.0&tabs=visual-studio-code)
- Xcode 16.1 or later
- pjsip v2.14.1 or later

- PREPARING:

- Building pj stack:

Create the following ``` config_site.h ``` :

```c

#define PJ_CONFIG_IPHONE 1

#include <pj/config_site_sample.h>

```
Create the following variable:

```bash

export DEVPATH="`xcrun -sdk iphonesimulator --show-sdk-platform-path`/Developer"

```
Run configure with the following flags to build the lib to run on simulator:

```bash

MIN_IOS="-miphoneos-version-min=12.2" ARCH="-arch x86_64" CFLAGS="-O2 -m32 -mios-simulator-version-min=12.2 -fembed-bitcode" LDFLAGS="-O2 -m32 -mios-simulator-version-min=12.2 -fembed-bitcode" ./configure-iphone
Copy link
Member

Choose a reason for hiding this comment

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

Need instruction for device as well.

For device:
./configure-iphone
For simulator:
export DEVPATH="xcrun -sdk iphonesimulator --show-sdk-platform-path/Developer"
...


```
For run on device:

```bash
./configure-iphone
```

after run make dep and make, run the make on ``` pjsip-apps/src/swig/csharp ``` folder

```bash

make

```

then go to ``` pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui ``` and run:

For simulator:

```bash
dotnet workload restore
dotnet restore
dotnet build -t:Run -c Debug -f net9.0-ios -p:_DeviceName=:v2:udid=UDID_OF_YOUR_EMULATOR
Copy link
Member

Choose a reason for hiding this comment

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

Need instruction for device as well.
With a real device, the code signing is more complicated using VSCode, so here's what I did:

  • dotnet publish -c Debug -f net9.0-ios -p:RuntimeIdentifier=ios-arm64
  • Open the resulting .ipa file in Xcode: pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/bin/Debug/net9.0-ios/ios-arm64/publish/pjsua2maui.ipa
    Drag it to your connected device under "Devices and Simulators."
    Xcode will handle the signing and deployment.

```

For device:

```bash
dotnet workload restore
dotnet restore
dotnet build -t:Run -c Debug -f net9.0-ios -p:_DeviceName=:v2:udid=UDID_OF_YOUR_DEVICE
```

For the codesign, here are the steps:

```bash
dotnet publish -c Debug -f net9.0-ios -p:RuntimeIdentifier=ios-arm64
```
Open the resulting .ipa file in Xcode: ``` pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/bin/Debug/net9.0-ios/ios-arm64/publish/pjsua2maui.ipa ```

Drag it to your connected device under "Devices and Simulators."
Xcode will handle the signing and deployment.
23 changes: 23 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pjsua2maui", "pjsua2maui\pjsua2maui.csproj", "{C9D0190E-58BC-417B-9810-335D234D5002}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C9D0190E-58BC-417B-9810-335D234D5002}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9D0190E-58BC-417B-9810-335D234D5002}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9D0190E-58BC-417B-9810-335D234D5002}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{C9D0190E-58BC-417B-9810-335D234D5002}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9D0190E-58BC-417B-9810-335D234D5002}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.dll
.java
.class
.jar
.so
/*/obj/
14 changes: 14 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:pjsua2maui"
x:Class="pjsua2maui.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
11 changes: 11 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace pjsua2maui;

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
14 changes: 14 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="pjsua2maui.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:pjsua2maui.Views"
Shell.FlyoutBehavior="Disabled">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:BuddyPage}"
Route="BuddyPage" />

</Shell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace pjsua2maui;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace pjsua2maui.Controls;

public class CallView : ContentView
{
public CallView()
{
}
}
40 changes: 40 additions & 0 deletions pjsip-apps/src/swig/csharp/pjsua2maui/pjsua2maui/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Extensions.Logging;
using pjsua2maui.Controls;
#if ANDROID
using pjsua2maui.Platforms.Android;
#endif
#if IOS
using pjsua2maui.Platforms.iOS;
#endif

namespace pjsua2maui;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

#if DEBUG
builder.Logging.AddDebug();
#endif
#if __ANDROID__
builder.ConfigureMauiHandlers((x) => {
x.AddHandler(typeof(CallView), typeof(CallPageRenderer));
});
#endif
#if __IOS__
builder.ConfigureMauiHandlers((x) => {
x.AddHandler(typeof(CallView), typeof(CallPageRenderer));
});
#endif
return builder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using CommunityToolkit.Mvvm.Messaging.Messages;
using libpjsua2.maui;

namespace pjsua2maui.Messages;

public class AddBuddyMessage : ValueChangedMessage<BuddyConfig>
{
public AddBuddyMessage(BuddyConfig buddyConfig) : base(buddyConfig)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using CommunityToolkit.Mvvm.Messaging.Messages;
using libpjsua2.maui;

namespace pjsua2maui.Messages;

public class EditBuddyMessage : ValueChangedMessage<BuddyConfig>
{
public EditBuddyMessage(BuddyConfig buddyConfig) : base(buddyConfig)
{
}
}

Loading
Loading