Skip to content

Commit

Permalink
Decoupled transformers and NS
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalocostamendes committed Jun 25, 2024
1 parent ef9a46f commit 716323d
Show file tree
Hide file tree
Showing 38 changed files with 313 additions and 126 deletions.
30 changes: 21 additions & 9 deletions Media-Transformers-Java/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@

All notable changes to this project will be documented in this file.

## 2.25.2
## 2.27.2

### Added

- Support pre-built transformers in the Vonage Media Processor library or create your own custom video transformer to apply to published video.
- Support Vonage Media Processor library as a opt-in library.
- Supports Noise Suppression

### Fixed

- NA
- Decoupled Media Processor library

### Notes

For versions with Android Gradle Plugin prior to 7.4, on merging native builds, it raises a error due to "2 files found with path 'lib/arm64-v8a/libmltransformers.so' from inputs".

The issue can be solved by adding to Media-Transformers-Java build.gradle the following option.

### Enhancements
```c
packagingOptions {
pickFirst "**/libmltransformers.so"
pickFirst "**/libmltransformersaudionoisesuppression.so"
}
```
- NA
### Changed
- NA
## 2.25.2
### Added
- Support pre-built transformers in the Vonage Media Processor library or create your own custom video transformer to apply to published video.
### Deprecated
- NA
5 changes: 0 additions & 5 deletions Media-Transformers-Java/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
pickFirst "**/libmltransformers.so"
pickFirst "**/libmltransformersaudionoisesuppression.so"
}
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions Media-Transformers-Java/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tokbox.sample.videotransformers" >
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class OpenTokConfig {
*/

// Replace with a API key
public static final String API_KEY = "47521351";
public static final String API_KEY = "";

// Replace with a generated Session ID
public static final String SESSION_ID = "1_MX40NzUyMTM1MX5-MTcxODM1OTEyMzUyNH5oaUhWbUx4TXJQdldMY2tmRGJ1bFB3a2Z-fn4";
public static final String SESSION_ID = "";

// Replace with a generated token (from the dashboard or using an OpenTok server SDK)
public static final String TOKEN = "T1==cGFydG5lcl9pZD00NzUyMTM1MSZzaWc9OWU0YTYyZWVlMDk1YjY4YmFlNjhmZDRjZjkyYjU3ODU4MjcxZjQyNzpzZXNzaW9uX2lkPTFfTVg0ME56VXlNVE0xTVg1LU1UY3hPRE0xT1RFeU16VXlOSDVvYVVoV2JVeDRUWEpRZGxkTVkydG1SR0oxYkZCM2EyWi1mbjQmY3JlYXRlX3RpbWU9MTcxODM1OTEyOCZub25jZT0wLjAxMTc0MTEzNTQ5NDAxOTExNyZyb2xlPXB1Ymxpc2hlciZleHBpcmVfdGltZT0xNzIwOTUxMTI3JmluaXRpYWxfbGF5b3V0X2NsYXNzX2xpc3Q9";
public static final String TOKEN = "";

// *** The code below is to validate this configuration file. You do not need to modify it ***

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ServerConfig {
Note that this application will ignore credentials in the `OpenTokConfig` file when `CHAT_SERVER_URL` contains a
valid URL.
*/
public static final String CHAT_SERVER_URL = "";
public static final String CHAT_SERVER_URL = "https://YOURAPPNAME.herokuapp.com";

// *** The code below is to validate this configuration file. You do not need to modify it ***

Expand Down
4 changes: 2 additions & 2 deletions Media-Transformers-Java/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
}

task clean(type: Delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
File renamed without changes.
37 changes: 37 additions & 0 deletions Media-Transformers-Kotlin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Video Transformers Changelog

All notable changes to this project will be documented in this file.

## 2.27.2

### Added

- Support Vonage Media Processor library as a opt-in library.
- Supports Noise Suppression

### Fixed

- Decoupled Media Processor library

### Notes

For versions with Android Gradle Plugin prior to 7.4, on merging native builds, it raises a error due to "2 files found with path 'lib/arm64-v8a/libmltransformers.so' from inputs".

The issue can be solved by adding to Media-Transformers-Java build.gradle the following option.

```c
packagingOptions {
pickFirst "**/libmltransformers.so"
pickFirst "**/libmltransformersaudionoisesuppression.so"
}
```
## 2.25.2
### Added
- Support pre-built transformers in the Vonage Media Processor library or create your own custom video transformer to apply to published video.
219 changes: 219 additions & 0 deletions Media-Transformers-Kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
Video Transformers
======================

The Video Transformers app is a very simple application created on top of Basic Video Chat meant to get a new developer
started using Media Processor APIs on OpenTok Android SDK. For a full description, see the [Video Transformers tutorial at the
OpenTok developer center](https://tokbox.com/developer/guides/vonage-media-processor/android).

<p class="topic-summary">
You can use pre-built transformers in the Vonage Media Processor library or create your own custom audio
or video transformer to apply to published video.
</p>

You can use the <a href="/developer/sdks/android/reference/com/opentok/android/PublisherKit.html#setAudioTransformers(java.util.ArrayList)"><code>PublisherKit.setAudioTransformers()</code></a> and
<a href="/developer/sdks/android/reference/com/opentok/android/PublisherKit.html#setVideoTransformers(java.util.ArrayList)"><code>PublisherKit.setVideoTransformers()</code></a>
methods to apply audio and video transformers to a stream.

<div class="important">
<b>Important:</b>
<p>
Media transformations are not supported on all devices. See <a href="#client-requirements">Client requirements</a>.
</p>
</div>

The Vonage Video Android SDK includes two ways to implement transformers:

* **Moderate** — For video, you can apply the background blur and background replacement video transformers included in the [Vonage Media Library](#vonage-media-library-integration). See [Applying a video transformer from the Vonage Media Library](#applying-a-video-transformer-from-the-vonage-media-library). For audio, you can apply the noise suppression audio transformer included in the [Vonage Media Library](#vonage-media-library-integration). See [Applying an audio transformer from the Vonage Media Library](#applying-an-audio-transformer-from-the-vonage-media-library).

* **Advanced** — You can create your own [custom video transformers](#creating-a-custom-video-transformer) and [custom audio transformers](#creating-a-custom-audio-transformer).

<a name="supported-devices-and-android-versions"></a>
## Client requirements

The transformers from the Vonage Media Library are supported on Android API Level of 23 or above, on the following devices:

* Samsung Galaxy S8 and above
* Google Pixel 5 and above
* OPPO A94 and above
* Android phones using Qualcomm Snapdragon 835 and above
* Android Phones using Qualcomm Snapdragon 765G and above

Test on other devices to check for support.

Transformers require adequate processor support. Even on supported devices, transformers may not be stable when background processes limit available processing resources. The same limitations may apply with custom media transformers in addition to transformers from the Vonage Media Library.

Android may throttle CPU performance to conserve energy (for example, to extend battery life) by throttling CPU performance. This may result in suboptimal transformer performance and introduce unwanted audio or video artifacts. We recommend disabling battery saver or low-power mode, if it is an option, in such cases.

Many video transformations (such as background blur) use segmentation to separate the speaker from the background. For best results, use proper lighting and a plain background. Insufficient lighting or complex backgrounds may cause video artifacts (for example, the speaker or a hat the speaker is wearing may get blurred along with the background).

You should perform benchmark tests on as many supported devices as possible, regardless of the transformation.

# Vonage Media Library integration

Due to significant increased size when integrating Vonage Media Library into SDK, from OpenTok SDK v2.27.2 the Media Transformers are available via the opt-in Vonage Media Library. This library needs to explicitly be added to the project.

The Vonage Media Library was initially embedded in OpenTok SDK. If your OpenTok SDK version is older than 2.27.2, move directly to [Applying a video transformer from the Vonage Media Library](#applying-a-video-transformer-from-the-vonage-media-library) and [Applying an audio transformer from the Vonage Media Library](#applying-an-audio-transformer-from-the-vonage-media-library).

A Maven version is available at https://central.sonatype.com/artifact/com.vonage/client-sdk-video-transformers.
The artifact ID is `"client-sdk-video-transformers"`.

Modify the app's build.gradle file and add the following code snippet to the
`dependencies` section:

```
implementation 'com.vonage:client-sdk-video-transformers:2.27.2'
```

If a call to <code>PublisherKit.VideoTransformer(String name, String properties)</code> or <code>PublisherKit.AudioTransformer(String name, String properties)</code> is made without loading the library, the transformer returned will be null. An exception will be raised with the following error code `0x0A000006 - OTC_MEDIA_TRANSFORMER_OPENTOK_TRANSFORMERS_LIBRARY_NOT_LOADED`.

## Applying a video transformer from the Vonage Media Library

Use the <a href="/developer/sdks/android/reference/com/opentok/android/PublisherKit.VideoTransformer.html#%3Cinit%3E(java.lang.String,java.lang.String)"><code>PublisherKit.VideoTransformer(String name, String properties)</code></a>
constructor to create a video transformer that uses a named transformer from the Vonage Media Library.

Two transformers are supported:

* **Background blur.** For this filter, set the `name` parameter to `"BackgroundBlur"`. Set the `properties` parameter to a JSON string defining properties for the transformer. For the background blur transformer, this JSON includes one property -- `radius` -- which can be set
to `"High"`, `"Low"`, `"None"`, or `"Custom`. If you set the `radius` property to "Custom", add a `custom_radius` property to the JSON string: `"{\"radius\":\"Custom\",\"custom_radius\":\"value\"}"` (where `custom_radius` is a positive integer defining the blur radius).

```java
Publisher publisher = new Publisher.Builder(MainActivity.this).build();
ArrayList<PublisherKit.VideoTransformer> videoTransformers = new ArrayList<>();
PublisherKit.VideoTransformer backgroundBlur = publisher.new VideoTransformer(
"BackgroundBlur",
"{\"radius\":\"High\"}"
);
videoTransformers.add(backgroundBlur);
publisher.setVideoTransformers(videoTransformers);
```

* **Background replacement.** For this filter, set the `name` parameter to `"BackgroundReplacement"`. And set a `properties` parameter to a JSON string. The format of the JSON is `"{\"image_file_path\":\"path/to/image\"}"`, where `image_file_path` is the absolute file path of a local image to use as virtual background. Supported image formats are PNG and JPEG.

```java
Publisher publisher = new Publisher.Builder(MainActivity.this).build();
ArrayList<PublisherKit.VideoTransformer> videoTransformers = new ArrayList<>();
PublisherKit.VideoTransformer backgroundReplacement = publisher.new VideoTransformer(
"BackgroundReplacement",
"{\"image_file_path\":\"path-to-image-file\"}"
);
videoTransformers.add(backgroundReplacement);
publisher.setVideoTransformers(videoTransformers);
```

## Applying an audio transformer from the Vonage Media Library

<p class="note">
<b>Note:</b> This is a beta feature.
</p>

Use the <a href="http://localhost:9778/developer/sdks/android/reference/com/opentok/android/PublisherKit.AudioTransformer.html#<init>(java.lang.String,java.lang.String)"><code>PublisherKit.AudioTransformer(String name, String properties)</code></a>
constructor to create an audio transformer that uses a named transformer from the Vonage Media Library.

One transformer is supported:

* **Noise Suppression.** For this filter, set the `name` parameter to `"NoiseSuppression"`. Set the `properties` parameter to a JSON string defining properties for the transformer. For the noise suppression transformer, this JSON does not include any property at the moment. Set it to an empty JSON string `"{}"`.

```java
Publisher publisher = new Publisher.Builder(MainActivity.this).build();
ArrayList<PublisherKit.AudioTransformer> audioTransformers = new ArrayList<>();
PublisherKit.AudioTransformer noiseSuppression = publisher.new AudioTransformer(
"NoiseSuppression",
"{}"
);
audioTransformers.add(noiseSuppression);
publisher.setAudioTransformers(audioTransformers);
```

## Creating a custom video transformer

Create a class that implements the <a href="/developer/sdks/android/reference/com/opentok/android/PublisherKit.CustomVideoTransformer.html"><code>PublisherKit.CustomVideoTransformer</code></a>
interface. Implement the `PublisherKit.CustomVideoTransformer.onTransform​(BaseVideoRenderer.Frame frame)` method. The `PublisherKit.CustomVideoTransformer.onTransform​(BaseVideoRenderer.Frame frame)` method is triggered for each video frame.
In the implementation of the method, apply a transformation to the `frame` object passed into the method:

```java
public class MyCustomTransformer implements PublisherKit.CustomVideoTransformer {
@Override
public void onTransform(BaseVideoRenderer.Frame frame) {
// Replace this with code to transform the frame:
frame.convertInPlace(frame.getYplane(), frame.getVplane(), frame.getUplane(), frame.getYstride(), frame.getUvStride());
}
}
```

Then pass the object that implements the PublisherKit.CustomVideoTransformer interface into the `PublisherKit.setVideoTransformers()` method:

```java
Publisher publisher = new Publisher.Builder(MainActivity.this).build();
MyCustomTransformer transformer = new MyCustomTransformer();
PublisherKit.VideoTransformer myCustomTransformer = publisher.new VideoTransformer("myTransformer", transformer);
ArrayList<PublisherKit.VideoTransformer> videoTransformers = new ArrayList<>();
videoTransformers.add(myCustomTransformer);
publisher.setVideoTransformers(videoTransformers);
```

You can combine the Vonage Media library transformer (see the previous section) with custom transformers or apply
multiple custom transformers by adding multiple PublisherKit.VideoTransformer objects to the ArrayList passed
into the `PublisherKit.setVideoTransformers()` method.

adding multiple OTPublisherKit.VideoTransformer objects to the array used
for the `OTPublisherKit.videoTransformers` property.

## Creating a custom audio transformer

Create a class that implements the <a href="/developer/sdks/android/reference/com/opentok/android/PublisherKit.CustomAudioTransformer.html"><code>PublisherKit.CustomAudioTransformer</code></a>
interface. Implement the `PublisherKit.CustomAudioTransformer.onTransform​(AudioData audioData)` method.
The `PublisherKit.CustomAudioTransformer.onTransform​(AudioData audioData)` method is triggered for each audio frame.
In the implementation of the method, apply a transformation to the `frame` object passed into the method.
The following example applies a simple amplitude limiter on the audio:

```java
public class MyCustomAudioTransformer implements PublisherKit.CustomAudioTransformer {
private short CROP_LIMIT = (short)(32767 * 0.05);
@Override
public void onTransform(AudioData audioData) {
int samplesPerChannel = (int)audioData.getNumberOfSamples() * (int)audioData.getNumberOfChannels();
ShortBuffer samples = audioData.getSampleBuffer().asShortBuffer();
for (int s = 0; s < samplesPerChannel; ++s) {
short sample = samples.get(s);
if (sample > CROP_LIMIT)
samples.put(s, CROP_LIMIT);
else if (sample < -CROP_LIMIT)
samples.put(s, (short)-CROP_LIMIT);
}
}
}
```

Then pass the object that implements the PublisherKit.CustomAudioTransformer interface into the `PublisherKit.setAudioTransformers()` method:

```java
Publisher publisher = new Publisher.Builder(MainActivity.this).build();
MyCustomAudioTransformer transformer = new MyCustomAudioTransformer();
PublisherKit.AudioTransformer myCustomTransformer = publisher.new AudioTransformer("myTransformer", transformer);
ArrayList<PublisherKit.VideoTransformer> audioTransformers = new ArrayList<>();
audioTransformers.add(myCustomTransformer);
publisher.setAudioTransformers(audioTransformers);
```

You can apply multiple custom transformers by adding multiple PublisherKit.AudioTransformer objects to the ArrayList
passed into the `PublisherKit.setAudioTransformers()` method.

## Clearing video transformers for a publisher

To clear video transformers for a publisher, pass an empty ArrayList into
into the `PublisherKit.setVideoTransformers()` method.

```java
videoTransformers.clear();
mPublisher.setVideoTransformers(videoTransformers);
```

## Clearing audio transformers for a publisher

To clear audio transformers for a publisher, pass an empty ArrayList into
into the `PublisherKit.setAudioTransformers()` method.

```java
audioTransformers.clear();
mPublisher.setAudioTransformers(videoTransformers);
```
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ android {

dependencies {
// Dependency versions are defined in the ../../commons.gradle file
implementation "com.opentok.android:opentok-android-sdk:${extOpentokSdkVersion}"
implementation "com.vonage:client-sdk-video:${extOpentokSdkVersion}"
implementation "com.vonage:client-sdk-video-transformers:${extOpentokSdkVersion}"
implementation "androidx.appcompat:appcompat:${extAppCompatVersion}"
implementation "pub.devrel:easypermissions:${extEasyPermissionsVersion}"
implementation "androidx.constraintlayout:constraintlayout:${extConstraintLyoutVersion}"
Expand Down
Loading

0 comments on commit 716323d

Please sign in to comment.