faithoflifedev / easy_onvif_workspace

This package works with a variety of ONVIF compatible devices allowing for IP Cameras and NVRs (network video recorders) to be integrated into Dart and Flutter applications.
32 stars 18 forks source link

DioException [receive timeout]: The request #56

Closed bubblesdev closed 9 months ago

bubblesdev commented 9 months ago

After making request for rtspUri with the profile token, these are error response i got: Error fetching stream URI: Exception: DioException [receive timeout]: The request took longer than 0:00:10.000000 to receive data. It was aborted. To get rid of this exception, try raising the RequestOptions.receiveTimeout above the duration of 0:00:10.000000 or improve the response time of the server. I/flutter ( 5161): Error fetching stream URI: Exception: DioException [unknown]: null I/flutter ( 5161): Error: HttpException: Connection closed before full header was received, uri =

faithoflifedev commented 9 months ago

Hi @bubblesdev,

The simplest way to troubleshoot these issues is by using the included onvif cli command that is included in the package. You can find the install and quick start for these in the README. Once you have the cli application installed and authorized you can run commands like the following to get a full debug of the request/response for your device.

# list profile tokens for your device with debug
onvif media1 get-profiles --log-level all

#just list the tokens (no debug)
onvif media1 get-profiles | jq -r '.[]."@token"'

#get the stream uri (wuth debug)
onvif media1 get-stream-uri --profile-token Profile_1 --log-level all

If you add the output to the issue, I can make suggestions or resolve any bugs.

Overall, the error that your getting means that the http connection has timed out in its attempt to connect to your device. So it's likely that the IP address and port combination you're using to connect is incorrect.

Also the above commands are primarily targetted at macos and linux users. The jq utility can probably be installed on windows with chocolatey.

bubblesdev commented 9 months ago
Screen Shot 2024-01-23 at 12 36 07

I'm able to use the cli commands to fetch both snapshot and streamuri but doesn't work on the app as connections are refused and same errors above

faithoflifedev commented 9 months ago

Hi @bubblesdev,

I assume by "app", you mean a Flutter app? Since the cli command is running properly this means that the easy_onvif package works properly with your device. The likely issue in this case is the Flutter code you're using to connect to the device. Since I haven't seen the code, there's not a lot I can suggest.

bubblesdev commented 9 months ago

Here's the code below:

String? rtspUri; VideoPlayerController? _controller; bool connecting = true; late Onvif onvif; late final List profiles; String? snapshotUri; String url = '';

@override void initState() { initialize(); super.initState(); // Initialize ONVIF connection (replace with your camera details) }

Future initialize() async { onvif = await Onvif.connect( host: 'ipaddress', // Replace with camera IP address username: 'admin', password: 'password!');

setState(() {
  connecting = false;
});

}

Future _fetchStreamUri() async { if (!connecting) { try { profiles = await onvif.media.getProfiles(); initializeVideoPlayer(); try { if (profiles.isNotEmpty) { snapshotUri = await onvif.media.getSnapshotUri(profiles[0].token); print('Snapshot: $snapshotUri'); } } catch (err) { loggy.error(err.toString()); } var media = await onvif.media.getProfiles(); var profileToken = media.first.token; print('Profile: $profileToken'); try { setState(() { url = OnvifUtil.authenticatingUri(snapshotUri!, "admin", "password"); print('Snapshot URL: $url'); }); } catch (e) { print('Error constructing snapshot URL: $e'); }

    rtspUri = await onvif.media.getStreamUri(profileToken);
    print('RTSP URI: $rtspUri');
     // Start video playback
  } catch (e) {
    print('Error fetching stream URI: $e');
    if (e is DioException) {
      print('DioError details: ${e.type}, ${e.message}, ${e.error}');
    }
  }
}else{
  print('Not connected!');
}

}

Screen Shot 2024-01-23 at 18 46 58

void initializeVideoPlayer() async { if (rtspUri != null) { _controller = VideoPlayerController.networkUrl(rtspUri! as Uri); await _controller?.initialize(); await _controller?.play(); setState(() { }); // Update UI to display video } }

Thank you for help!

faithoflifedev commented 9 months ago

Flutter can be pretty tricky with it's use of Futures. In your case your initialize() method is returning before it's completed execution, due to it being an async method.

Have a look at the Flutter example that I've provided with the package. It might help you out - example.

bubblesdev commented 9 months ago

Thanks but I am still surprised that the getStreamUri still don't work after implementing your example and also the snapshotUri is fetched but doen't load as connection is refused, seems the uri is not in the right format: I also tried futurebuilder and it is still same error :/

Here's a log report 21:45:48.468250 DEBUG - getStreamUri [UI Loggy - LoggingInterceptors] 🐛 21:45:48.473820 DEBUG - URI: http://ipaddress:1091/Media2 [UI Loggy - LoggingInterceptors] 🐛 21:45:48.474655 DEBUG - REQUEST: <?xml version="1.0" encoding="UTF-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><Security xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1"><UsernameToken><Username>admin</Username><Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">2aP4Za4Vkugw1n81hutfqcfwuhs=</Password><Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">422l5F32DOFNSVOOnIg6Sw==</Nonce><Created xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2018-01-01T20:24:08.272919Z</Created></UsernameToken></Security></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetStreamUri xmlns="http://www.onvif.org/ver20/media/wsdl"><Protocol xmlns="http://www.onvif.org/ver10/schema">RTSP</Protocol><ProfileToken xmlns="http://www.onvif.org/ver10/schema">MainStream</ProfileToken></GetStreamUri></s:Body></s:Envelope> E/flutter ( 7892): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: DioException [unknown]: null E/flutter ( 7892): Error: HttpException: Connection closed before full header was received, uri = http://ipaddress:1091/Media2 E/flutter ( 7892): #0 Transport.sendRequest (package:easy_onvif/src/soap/transport.dart:51:7) E/flutter ( 7892): <asynchronous suspension> E/flutter ( 7892): #1 Media2.getStreamUri (package:easy_onvif/src/media2.dart:173:22) E/flutter ( 7892): <asynchronous suspension> E/flutter ( 7892): #2 Media.getStreamUri (package:easy_onvif/src/media.dart:256:11) E/flutter ( 7892): <asynchronous suspension> E/flutter ( 7892): #3 _MyHomePageState._initialize (package:onivf_app/stream_screen.dart:90:17) E/flutter ( 7892): <asynchronous suspension> E/flutter ( 7892): [UI Loggy - LoggingInterceptors] ‼️ 21:45:49.019338 ERROR - ERROR: DioException [unknown]: null Error: HttpException: Connection closed before full header was received, uri = http://ipaddress:1091/Media2

bubblesdev commented 9 months ago

After updating to the recent version 2.2.0. The streamUri was fetched succesfully, but the rtsp can't be opened: ibvlc demux: Failed to connect with rtsp://ipaddress:554/stream0?username=admin&password=password E/VLC (20644): [b4000072193c7190/5675] libvlc stream: Failed to connect to RTSP server ipaddress:554 E/VLC (20644): [b4000072193c7190/5675] libvlc stream: cannot connect to ipaddress:554 E/VLC (20644): [b4000072193c7190/5675] libvlc stream: Connection failed E/VLC (20644): [b4000072193c7190/5675] libvlc stream: VLC could not connect to "ipaddress:554". E/VLC (20644): [b400007219740290/5675] libvlc input: Your input can't be opened E/VLC (20644): [b400007219740290/5675] libvlc input: VLC is unable to open the MRL 'rtsp://ipaddress:554/stream0?username=admin&password=password'. Check the log for details.

Pls which video player can open the link

faithoflifedev commented 9 months ago

Hi @bubblesdev ,

Did you manually update the log message to remove the IP address from the log?

rtsp://ipaddress:554/stream0?username=admin&password=password

This package will return the rtsp uri from your device, anything after that is out of scope for thid project. For questions about errors from the VLC package should be asked there.

bubblesdev commented 9 months ago

Nah, i edited the ipaddress before commenting

muyiwexy commented 8 months ago

@faithoflifedev I on the other hand is getting DioException [bad response]: This exception was thrown because the response has a status code of 401 and RequestOptions.validateStatus was configured to throw for this status code.