rwscode / dart_castscreen

A dart package can cast video streams to UPnP & DLNA devices.
Apache License 2.0
2 stars 0 forks source link

Can't find any devices #2

Open tal32123 opened 1 week ago

tal32123 commented 1 week ago

I get this log:

flutter: INFO: 2024-06-28 23:05:50.281586: Starting device discovery...
flutter: INFO: 2024-06-28 23:05:53.314311: Device discovery completed. Found 0 devices. 

With this code:

import 'package:logging/logging.dart';

class CastService {
  final Logger log = Logger('CastService');
  List<Device> _devices = [];
  Device? _selectedDevice;

  Future<List<Device>> discoverDevices() async {
    try {
      log.info('Starting device discovery...');
      _devices = await CastScreen.discoverDevice(
        ipv4: true,
        ipv6: true,
        timeout: const Duration(seconds: 3),
        onError: (Exception e) => log.severe('Discovery error: $e'),
      );
      log.info('Device discovery completed. Found ${_devices.length} devices.');
      for (var device in _devices) {
        log.info('Found device: ${device.spec.friendlyName}');
      }
      return _devices;
    } catch (e) {
      log.severe('Error discovering devices: $e');
      return [];
    }
  }

  List<Device> get devices => _devices;

  void selectDevice(Device device) {
    _selectedDevice = device;
  }

  Future<void> loadMedia(String url) async {
    if (_selectedDevice != null) {
      try {
        final alive = await _selectedDevice!.alive();
        if (!alive) {
          log.severe('Selected device is not alive.');
          return;
        }
        final output = await _selectedDevice!.setAVTransportURI(SetAVTransportURIInput(url));
        log.info('Loaded media: $output');
      } catch (e) {
        log.severe('Error loading media: $e');
      }
    }
  }

  Future<void> play() async {
    if (_selectedDevice != null) {
      try {
        final alive = await _selectedDevice!.alive();
        if (!alive) {
          log.severe('Selected device is not alive.');
          return;
        }
        final output = await _selectedDevice!.play(PlayInput());
        log.info('Playing media: $output');
      } catch (e) {
        log.severe('Error playing media: $e');
      }
    }
  }

  Future<void> stop() async {
    if (_selectedDevice != null) {
      try {
        final alive = await _selectedDevice!.alive();
        if (!alive) {
          log.severe('Selected device is not alive.');
          return;
        }
        final output = await _selectedDevice!.stop(const StopInput());
        log.info('Stopped media: $output');
      } catch (e) {
        log.severe('Error stopping media: $e');
      }
    }
  }
}
billcoding commented 5 days ago

Thanks for opening the issue. Please ensure that only devices in the local LAN and supporting UPnP or DLNA can be discovered.

VendaGamer commented 4 days ago

Hi, I have the same problem on windows. However, when I try to debug on android it finds the devices.