steveseguin / vdon_flutter

Mobile app designed to publish in legacy mode to VDO.Ninja; based on the flutter sample app
GNU General Public License v3.0
19 stars 13 forks source link

Uvc Feature #2

Open aloproducao opened 1 year ago

aloproducao commented 1 year ago

Hello,

I would like to propose adding support for UVC cameras in this application. This feature would allow users to stream their video through a UVC camera connected to their device. To implement this, we can use the "flutter_webrtc" and "camera" packages from Flutter, along with a custom "CameraManager" class to handle available cameras and control the selected camera.

Example code for the "CameraManager" class:

class CameraManager {
  static const _cameraNames = {
    'back': 'Back camera',
    'front': 'Front camera',
    'uvc': 'UVC camera'
  };

  final _cameras = <String, WebRTCVideoCapturer>{};
  String _selectedCamera = 'back';

  CameraManager() {
    _init();
  }

  void _init() async {
    final availableCameras = await WebRTC.getCameras();
    for (final camera in availableCameras) {
      _cameras[camera.label] = camera;
    }
  }

  void selectCamera(String camera) {
    _selectedCamera = camera;
  }

  Future<WebRTCVideoTrack> getUVCVideoTrack() async {
    if (_selectedCamera != 'uvc') {
      throw Exception("UVC camera is not selected");
    }
    final capturer = _cameras[_selectedCamera];
    return capturer.getTrack();
  }
}
Example code for adding the "open camera" option in the main screen:

Copy code
class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  CameraManager _cameraManager = CameraManager();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          ListTile(
            title: Text('Open camera'),
            onTap: () => _openCamera(),
          ),
        ],
      ),
    );
  }

  void _openCamera() {
    final track = _cameraManager.getUVCVideoTrack();
    // Use the track to start streaming
  }
}

Please let me know your thoughts on this idea and if you have any suggestions for improvements.

Thank you.

steveseguin commented 1 year ago

I'm just seeing this issue now, sorry.

If someone is able to create a custom camera class that supports UVC, I'd welcome it. I'm able to load a uvc camera into Java + Android app, but getting that code integrated and the frames transmitted to the webRTC pipeline in this Flutter app is tricky for me. It's definitely possible, and others have done it, but I need help to have it done anytime soon.

Pull requests and code contributions very welcomed

aloproducao commented 10 months ago

Hi Steave, I managed to implement UVC, you could continue the project, finishing the implementation, it should work.

I had to modify a lot but everything is commented on. Maybe opening a new branch for us to test would be good https://drive.google.com/file/d/1ZDR667kLc0VZ__uC54zwh0OLb8wTsMzY/view?usp=sharing

steveseguin commented 10 months ago

Thank you, aloproducao, I'll be trying it out tonight. If we can get this working for even just Android or iOS, I'll be ecstatic.

steveseguin commented 10 months ago

I pushed your code to the branch 'uvc'; https://github.com/steveseguin/vdon_flutter/commit/386a68c211409675bda73310083495f23260746b

Did you get this code working and building though? It looks like just something ChatGPT might generate. What's provided doesn't seem like valid code, but I might not understand what's been presented either.

Anyways, I'm trying to build what you've provided, but there are numerous errors that I don't know how to address. What am I doing wrong?

image

I'm a bit confused really as to how this is supposed to work, as from what I can see it's there's no official flutter plugin that has UVC support yet. https://github.com/flutter/flutter/issues/42413.

My own attempt at trying to get uvc support added so far has required many additional files, like libuvc, and I've only managed to get it partially working with Android. I'm still stuck on transferring the frames from one buffer to another.

aloproducao commented 10 months ago

I'm examining the code, I managed to start the camera here, in fact I used gpt chat to help as I'm not the best flutter.

I am committed to solving UVC cameras, this will open up new possibilities. Have you seen this repository?

https://github.com/dreamCN/wl_uvc_camera

steveseguin commented 10 months ago

Thank you for the link.

I can already load a uvc camera into Android; the issue for me at the moment is getting that video into the WebRTC's encoding pipeline. It seems like it's possible, but may require moving buffers around, and I'm not entirely sure how to do that with Flutter/Java/Kotlin.

Providing complete and working code as a pull request would be appreciated, along with a demo video of it working perhaps. Most code that ChatGPT has generated for me in the past does not work and often makes up a lot of functions and features that don't exist, so submitting it to me without it working first can end up wasting a lot of my time.

steveseguin commented 3 months ago

I'm going to retire this flutter-based version of the mobile app I think, as I've started a new version that better works with UVC cameras.

If looking for UVC functionality for Android, you can try the preview version of this new mobile version that has UVC added: https://drive.google.com/file/d/1L8meslXPEzivocH3wz48abNtJ926hQUr/view?usp=drive_link

It will replace the version in the Google app store when completed, and shortly after i will depreciate this repository.

As for iOS devices, I believe iPhone 15 Pros with USB 3.0 ports have some support for USB cameras now, but I've not confirmed.

aloproducao commented 3 months ago

Wow, Steve, this work is amazing! I tested it on an Infinix and a Redmi 9 Prime, and it worked really well. I'd like to see the code to learn from it.

I had issues with the Redmi when using Rullz USB 3.0 capture cards. I believe it's because of the formats it outputs.