27

I am trying to build a mobile app which streams video from the camera of the device and sends it live to a server. Also, the mobile device should be able to play live video received from the server.

I am building the app in Flutter, but can't seem to find a well documented library/package in Flutter which uses HLS/RTSL/WebRTC/etc.

Should I use the byte stream and make a custom solution or is there a official package I can use to do the work?

Thank you in advance!

Nikolay Nikolov
  • 473
  • 1
  • 6
  • 8
  • Possible duplicate of [Is it possible to stream a video with flutter camera plugin?](https://stackoverflow.com/questions/50873010/is-it-possible-to-stream-a-video-with-flutter-camera-plugin) – Lakhwinder Singh Mar 05 '19 at 12:27
  • 2
    Hey, did you find any solution to this? – Saurabh Gour Mar 25 '19 at 16:32
  • I found a package for that purpose, dart wrapper for android native code https://github.com/espresso3389/flutter_rtmp_publisher. Here is the native android library https://github.com/TakuSemba/RtmpPublisher If you already found a solution, please drop it here just to weigh options. – Aliu Wuyi Adepoju Aug 11 '19 at 15:42
  • I think this can help: https://youtu.be/tPcuBo5QAW4 – devDeejay Jun 02 '20 at 05:22
  • It doesn't seem like it helps @devDeejay, as it shows how to play video from a network stream (unless there's something else to the end of it). – marcelocra Jul 27 '20 at 18:34
  • I am looking solution of Stream Screen Recording in flutter, is there any suggestion?? Thanks in advance. – Kamlesh Nov 13 '20 at 09:27
  • has anyone tried https://docs.agora.io/en/Video/start_live_flutter?platform=Flutter ? – Krishna Shetty Dec 21 '21 at 23:36

1 Answers1

7

For WebRTC Please try this package flutter_webrtc
https://github.com/cloudwebrtc/flutter-webrtc
and more example link
https://github.com/cloudwebrtc/flutter-webrtc-demo/

import 'package:flutter/material.dart';
import 'package:flutter_webrtc/webrtc.dart';
import 'dart:core';

/**
 * getUserMedia sample
 */
class GetUserMediaSample extends StatefulWidget {
  static String tag = 'get_usermedia_sample';

  @override
  _GetUserMediaSampleState createState() => new _GetUserMediaSampleState();
}

class _GetUserMediaSampleState extends State<GetUserMediaSample> {
  MediaStream _localStream;
  final _localRenderer = new RTCVideoRenderer();
  bool _inCalling = false;

  @override
  initState() {
    super.initState();
    initRenderers();
  }

  @override
  deactivate() {
    super.deactivate();
    if (_inCalling) {
      _hangUp();
    }
  }

  initRenderers() async {
    await _localRenderer.initialize();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  _makeCall() async {
    final Map<String, dynamic> mediaConstraints = {
      "audio": true,
      "video": {
        "mandatory": {
          "minWidth":'640', // Provide your own width, height and frame rate here
          "minHeight": '480',
          "minFrameRate": '30',
        },
        "facingMode": "user",
        "optional": [],
      }
    };

    try {
      var stream = await navigator.getUserMedia(mediaConstraints);
      _localStream = stream;
      _localRenderer.srcObject = _localStream;
    } catch (e) {
      print(e.toString());
    }
    if (!mounted) return;

    setState(() {
      _inCalling = true;
    });
  }

  _hangUp() async {
    try {
      await _localStream.dispose();
      _localRenderer.srcObject = null;
    } catch (e) {
      print(e.toString());
    }
    setState(() {
      _inCalling = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('GetUserMedia API Test'),
      ),
      body: new OrientationBuilder(
        builder: (context, orientation) {
          return new Center(
            child: new Container(
              margin: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              child: RTCVideoView(_localRenderer),
              decoration: new BoxDecoration(color: Colors.black54),
            ),
          );
        },
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _inCalling ? _hangUp : _makeCall,
        tooltip: _inCalling ? 'Hangup' : 'Call',
        child: new Icon(_inCalling ? Icons.call_end : Icons.phone),
      ),
    );
  }
}
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • Can we do the same for audio..like for songs I mean?? – Jalakam Kiran Aug 20 '20 at 18:22
  • @JalakamKiran, Are you looking for audio stream? please reference this https://stackoverflow.com/a/59405990/2179571 – chunhunghan Aug 21 '20 at 00:42
  • 1
    @chunhunghan I am looking solution of Stream Screen Recording in flutter, is there any suggestion?? Thanks in advance. – Kamlesh Nov 13 '20 at 09:28
  • @chunhunghan Please share your suggestion on my last query. Thanks. – Kamlesh Jan 22 '21 at 06:52
  • @Kamlesh, may be you can try this https://pub.dev/packages/camera_with_rtmp – chunhunghan Jan 22 '21 at 09:18
  • @Kamlesh, and this https://pub.dev/packages/flutter_rtmp_publisher – chunhunghan Jan 22 '21 at 09:20
  • @chunhunghan Thanks for sharing your solutions, I have already tried them. My requirement is to capture video by camera then add an overlay at bottom of screen (something like fixed text/message) then want to stream screen output (capturing video + overlay text) on RTMP. Could you please suggest me how can I do this? Thanks a lot. – Kamlesh Jan 22 '21 at 13:00
  • @Kamlesh, sorry, I am not major in this area. – chunhunghan Jan 25 '21 at 00:36
  • @chunhunghan Ok, thanks to confirm. I am still searching, will post the solution here when I will get it. Thanks again :) – Kamlesh Jan 25 '21 at 10:27