flutter-webrtc-demo

repository·master·Indexed 23 days ago

https://github.com/flutter-webrtc/flutter-webrtc-demo

A demonstration project for the flutter-webrtc plugin showcasing WebRTC capabilities in Flutter. It includes samples for P2P video calling (CallSample) and peer-to-peer data transfer (DataChannelSample), along with a Signaling class for managing session lifecycles, peer discovery, and media streams. The project also features a ScreenSelectDialog for selecting screens or windows to share on desktop platforms.

Tokens
2.5K
Snippets
4
Records
18
Agent score
78%

What's inside flutter-webrtc-demo

  1. Customize the iOS launch screen assets

    master

    To change the image displayed during the app's launch on iOS, you can use one of two methods:

    1. Direct File Replacement: Replace the existing image files located in the ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.
    2. Xcode Interface:
      • Open the iOS project in Xcode using the command: open ios/Runner.xcworkspace.
      • In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
      • Drag and drop your desired images directly into the asset catalog within the Xcode interface.
    open ios/Runner.xcworkspace
  2. Run the flutter-webrtc-demo project

    master

    To run the demo locally, clone the repository, navigate to the project directory, fetch the necessary Flutter packages, and execute the application.

    git clone https://github.com/cloudwebrtc/flutter-webrtc-demo
    cd flutter-webrtc-demo
    flutter packages get
    flutter run
  3. Use the Signaling class for WebRTC session management

    master

    The Signaling class manages WebRTC connections, signaling via WebSockets, and media streams. It handles the lifecycle of calls (inviting, accepting, rejecting, and ending) and manages multiple concurrent Session objects.

    To use it, you typically instantiate Signaling with a host and context, call connect() to establish the WebSocket connection, and then use methods like invite() to start a call or accept() to respond to one.

  4. Manage WebRTC signaling states and call events

    master

    When using the Signaling class in a data channel context, you must subscribe to several event handlers to manage the connection lifecycle:

    • onSignalingStateChange: Monitors the connection status to the signaling server (ConnectionClosed, ConnectionError, ConnectionOpen).
    • onCallStateChange: Handles the WebRTC session lifecycle. Key states include:
      • CallStateNew: A new session is created.
      • CallStateInvite: An incoming invitation is received.
      • CallStateRinging: The remote peer is ringing; requires an accept() or reject() action.
      • CallStateConnected: The peer connection is established.
      • CallStateBye: The call has ended.
    • onPeersUpdate: Provides a list of available peers and the local user's ID.
    • onDataChannel: Triggered when a data channel is opened by the remote peer.
  5. Navigate to P2P Call or Data Channel samples

    master

    The application provides two primary sample modes via the RouteItem menu:

    1. P2P Call Sample: Demonstrates standard WebRTC media streaming (audio/video).
    2. Data Channel Sample: Demonstrates WebRTC Data Channels for peer-to-peer data transfer.

    Before entering a sample, the application prompts the user to enter a server address via _showAddressDialog. The server address is persisted using SharedPreferences under the key 'server'. The default value is demo.cloudwebrtc.com.

  6. Run the Flutter-WebRTC demo application

    master

    The main.dart file serves as the entrypoint for the flutter-webrtc-demo application. It initializes the application state, loads the default WebRTC server address from SharedPreferences, and provides a navigation menu to different WebRTC samples.

    To run the application, ensure you have the Flutter SDK installed and execute:

    flutter run
    void main() => runApp(new MyApp());
  7. Use CallSample for P2P video calling

    master

    The CallSample widget provides a complete implementation of a peer-to-peer video calling interface. It requires a host parameter which specifies the signaling server address.

    Key features included in this sample:

    • Peer Discovery: Automatically lists available peers via the signaling server.
    • Video/Screen Sharing: Initiates video calls or screen sharing sessions with specific peers.
    • Call Controls: Includes UI for hanging up, muting the microphone, and switching cameras.
    • Call Lifecycle Management: Handles incoming invites, ringing states, and connection changes.
    • Screen Sharing: Supports desktop screen capture selection on desktop platforms and standard display media requests on Web.
  8. Implement a WebRTC Data Channel sample

    master

    The DataChannelSample widget demonstrates how to establish a WebRTC connection specifically for sending and receiving data (text or binary) via an RTCDataChannel. It uses a Signaling instance to manage peer discovery, invitations, and call states.

    Key Workflows

    1. Initialization: Connect to a signaling server using Signaling(host, context)..connect().
    2. Handling Messages: Listen to onDataChannelMessage to receive RTCDataChannelMessage objects. You can check data.isBinary to distinguish between text and binary payloads.
    3. Receiving Data Channels: The onDataChannel callback provides the RTCDataChannel instance once the connection is established.
    4. Sending Data: Use _dataChannel?.send() with either a text string or an RTCDataChannelMessage.fromBinary(Uint8List) for binary data.
    5. Call Lifecycle: The sample manages states like CallStateNew, CallStateInvite, CallStateRinging, CallStateConnected, and CallStateBye to control the UI and connection logic.
    // Example of handling incoming data channel messages
    _signaling?.onDataChannelMessage = (_, dc, RTCDataChannelMessage data) {
      setState(() {
        if (data.isBinary) {
          print('Got binary [' + data.binary.toString() + ']');
        } else {
          _text = data.text;
        }
      });
    };
    
    // Example of sending text and binary data
    _dataChannel?.send(RTCDataChannelMessage(text));
    _dataChannel?.send(RTCDataChannelMessage.fromBinary(Uint8List(bytes)));
  9. Use ScreenSelectDialog to pick a screen or window for sharing

    master

    The ScreenSelectDialog is a pre-built Flutter Dialog used to allow users to select a specific screen or application window to share via WebRTC.

    It provides a UI with two tabs:

    1. Entire Screen: Displays available screens.
    2. Window: Displays available application windows.

    When the user selects a source and clicks 'Share', the dialog returns the selected DesktopCapturerSource. If the user cancels, it returns null.

    To use it, show the dialog using Flutter's showDialog method and await the result.