Skip to main content

utd_media_client 2.12.0 copy "utd_media_client: ^2.12.0" to clipboard
utd_media_client: ^2.12.0 copied to clipboard

UTD Media Client — real-time audio and video for Flutter (iOS, Android, Web).

pub package

UTD Media Client — Flutter SDK #

Use this SDK to add realtime video, audio and data features to your Flutter app. By connecting to the UTD Media Engine, you can quickly build applications such as live streaming, voice rooms, video calls, and multi-modal experiences with just a few lines of code.

This package is published to pub.dev as utd_media_client.

Docs #

More docs and guides are available at https://utdsoftware.com.

Supported platforms #

The UTD Media Client for Flutter is designed to work across all platforms supported by Flutter:

  • Android
  • iOS
  • Web
  • macOS
  • Windows
  • Linux

Installation #

Include this package in your pubspec.yaml:

dependencies:
  utd_media_client: ^2.7.0

iOS #

Camera and microphone usage need to be declared in your Info.plist file.

<dict>
  ...
  <key>NSCameraUsageDescription</key>
  <string>$(PRODUCT_NAME) uses your camera</string>
  <key>NSMicrophoneUsageDescription</key>
  <string>$(PRODUCT_NAME) uses your microphone</string>

Your application can still run the voice call when it is switched to the background if the background mode is enabled. Select the app target in Xcode, click the Capabilities tab, enable Background Modes, and check Audio, AirPlay, and Picture in Picture.

Your Info.plist should have the following entries.

<dict>
  ...
  <key>UIBackgroundModes</key>
  <array>
    <string>audio</string>
  </array>

For iOS, the minimum supported deployment target is 12.1. You will need to add the following to your Podfile.

platform :ios, '12.1'

You may need to delete Podfile.lock and re-run pod install after updating the deployment target.

Android #

The UTD audio library (2.12.0+). The plugin adds the repository https://maven.udt-stream.com/ (group io.utdstream only) and replaces the stock WebRTC AAR with ours (io.utdstream:rtc); in a normal Flutter app there is nothing to do. Flutter's own Gradle plugin adds a repository to the project, so an app cannot use RepositoriesMode.FAIL_ON_PROJECT_REPOS (it fails before it reaches us, with "repository 'maven' was added by plugin 'dev.flutter.flutter-gradle-plugin'"). If your settings.gradle sets a repositories mode that ignores project repositories (RepositoriesMode.PREFER_SETTINGS), add these two lines to dependencyResolutionManagement { repositories { ... } } (checked on a new Flutter app with AGP 9.0.1; without the first one flutter_webrtc fails with "Could not find com.github.davidliu:audioswitch", without the second one with "Could not find io.utdstream:rtc"): maven { url = uri("https://jitpack.io") } and exclusiveContent { forRepository { maven { url = uri("https://maven.udt-stream.com/") } }; filter { includeGroup("io.utdstream") } } (exclusiveContent makes sure that group is answered by our repository only). The first build of a machine needs network access to maven.udt-stream.com (the audio library is about 53 MB for the four ABIs; the APK keeps only the ones it targets). See the CHANGELOG, 2.12.0.

Foreground service manifest entries (2.10.0+). This package's manifest is merged into your app automatically: it adds FOREGROUND_SERVICE, FOREGROUND_SERVICE_MICROPHONE, FOREGROUND_SERVICE_MEDIA_PLAYBACK, POST_NOTIFICATIONS, WAKE_LOCK and a microphone|mediaPlayback service, even if you never call UtdmForegroundService.start. An app that targets API 34 must complete the foreground-service declaration in the Play Console. To drop them, add xmlns:tools="http://schemas.android.com/tools" to your <manifest> and tools:node="remove" entries for the service (io.utd.media.plugin.UtdmForegroundService) and for each of the five permissions. See the CHANGELOG, "Android manifest merge".

We require a set of permissions that need to be declared in your AppManifest.xml.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.your.package">
  <uses-feature android:name="android.hardware.camera" />
  <uses-feature android:name="android.hardware.camera.autofocus" />
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
  <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
  ...
</manifest>

Audio Modes

By default, we use the communication audio mode on Android which works best for two-way voice communication.

If your app is media-playback oriented and does not need the device's microphone, you can use the media audio mode which provides better audio quality.

Desktop support #

In order to enable Flutter desktop development, please follow the official Flutter desktop setup instructions.

Usage #

Connecting to a room, publish video & audio #

final roomOptions = RoomOptions(
  adaptiveStream: true,
  dynacast: true,
  // ... your room options
);

final room = Room();

// you can use `prepareConnection` to speed up connection.
await room.prepareConnection(url, token);

await room.connect(url, token, roomOptions: roomOptions);

try {
  // video will fail when running in the ios simulator
  await room.localParticipant.setCameraEnabled(true);
} catch (error) {
  print('Could not publish video, error: $error');
}

await room.localParticipant.setMicrophoneEnabled(true);

Screen sharing #

Screen sharing is supported across all platforms. You can enable it with:

room.localParticipant.setScreenShareEnabled(true);

Rendering video #

Each track can be rendered separately with the provided VideoTrackRenderer widget.

VideoTrack? track;

@override
Widget build(BuildContext context) {
  if (track != null) {
    return VideoTrackRenderer(track);
  } else {
    return Container(
      color: Colors.grey,
    );
  }
}

Audio handling #

Audio tracks are played automatically as long as you are subscribed to them.

Handling changes #

The client makes it simple to build declarative UI that reacts to state changes. It notifies changes in two ways:

  • ChangeNotifier — generic notification of changes. Useful when building reactive UI that only cares about changes that may impact rendering.
  • EventsListener<Event> — listener pattern to listen to specific events.
class _RoomState extends State<RoomWidget> {
  late final EventsListener<RoomEvent> _listener = widget.room.createListener();

  @override
  void initState() {
    super.initState();
    widget.room.addListener(_onChange);

    _listener
      ..on<RoomDisconnectedEvent>((_) {
        // handle disconnect
      })
      ..on<ParticipantConnectedEvent>((e) {
        print("participant joined: ${e.participant.identity}");
      });
  }

  @override
  void dispose() {
    _listener.dispose();
    widget.room.removeListener(_onChange);
    super.dispose();
  }

  void _onChange() {
    setState(() {
      // your updates here
    });
  }
}

Mute, unmute local tracks #

On LocalTrackPublications, you can control whether the track is muted by setting its muted property. Changing the mute status emits a TrackMutedEvent or TrackUnmutedEvent.

// mute track
trackPub.muted = true;

// unmute track
trackPub.muted = false;

End-to-end encryption #

The SDK supports end-to-end encryption for audio/video data sent over the network. On native platforms E2EE works without extra setup; on Flutter web you need to compile an e2ee.worker.dart.js file.

License #

Apache License 2.0