Zechst
Zechst

Reputation: 1

Launch camera using video_stream package causes runtime error, crashes application on flutter

I am trying to broadcast livestream, following this documentation: https://blog.flutterflow.io/flutter-mux-live-streaming/

However, when clicking on my camera button to launch Camera Preview screen, I am receiving a run time error:

[VERBOSE-3:platform_message_handler_ios.mm(106)] Check failed: task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread().
* thread #6, queue = 'com.marshalltechnology.video_stream.dispatchqueue', stop reason = signal SIGABRT
frame #0: 0x00000001d9339b38 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
->  0x1d9339b38 <+8>:  b.lo   0x1d9339b58               ; <+40>
0x1d9339b3c <+12>: pacibsp
0x1d9339b40 <+16>: stp    x29, x30, [sp, #-0x10]!
0x1d9339b44 <+20>: mov    x29, sp
Target 0: (Runner) stopped.
Lost connection to device.

I am suspecting it's an issue with the video_stream package

Currently using these package dependencies Permission Handler, Video_stream

This was working previously on an old build

My Mac is currently running on

Flutter stable 3.0.5, Xcode 13.4.1

Here's my code snippet, rather similar to the documentation provided above

Main.dart

void main() async { 
  WidgetsFlutterBinding.ensureInitialized();
  
  // Get the available device cameras
  try {
    cameras = await availableCameras();
    } on CameraException catch (e) {
    debugPrint(e.toString());
  }
  runApp(const MyApp());
}

Livestream.dart

class LivestreamScreen extends StatefulWidget {

  @override
  State<LivestreamScreen> createState() => _LivestreamScreenState();
}

class _LivestreamScreenState extends State<LivestreamScreen> {
  CameraController? _controller;

  bool _isCameraPermissionGranted = false;
  bool _isCameraInitialized = false;
  bool _isInitializing = false;
  bool _isStreaming = false;
  bool _isFrontCamSelected = true;

 _getPermissionStatus() async {
 // Get the camera permission, if granted start initializing it
 await Permission.camera.request();
 var status = await Permission.camera.status;

 if (status.isGranted) {
    log('Camera Permission: GRANTED');
    setState(() {
     _isCameraPermissionGranted = true;
    });

  // Set and initialize the new camera with front camera
  // camera[0]: Back camera of the device.
  // camera[1]: Front camera of the device.
  _onNewCameraSelected(cameras[1]);
} else {
  log('Camera Permission: DENIED');
    }
} 

void _onNewCameraSelected(CameraDescription cameraDescription) async {
// Initialize a new camera
setState(() {
  _isCameraInitialized = false;
});

final previousCameraController = _controller;

final CameraController cameraController = CameraController(
  cameraDescription,
  ResolutionPreset.high,
  enableAudio: true,
  androidUseOpenGL: true,
);

await previousCameraController?.dispose();

if (mounted) {
setState(() {
    _controller = cameraController;
  });
}

_controller!.addListener(() {
  // _isStreaming = _controller!.value.isStreamingVideoRtmp;
  _isCameraInitialized = _controller!.value.isInitialized;

  if (mounted) setState(() {});
});

try {
  await cameraController.initialize();
} on CameraException catch (e) {
  log('Error initializing camera: $e');
}

    if (mounted) {
      setState(() {
      _isCameraInitialized = _controller!.value.isInitialized;
     });
   }
 } 

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // App state changed before we got the chance to initialize.
    if (_controller == null || !_controller!.value.isInitialized) {
      return;
    }
    if (state == AppLifecycleState.inactive) {
      _controller?.dispose();
    } else if (state == AppLifecycleState.resumed) {
      if (_controller != null) {
        _onNewCameraSelected(_controller!.description!);
      }
    }
  }   
@override
  void initState() {
    _getPermissionStatus();

    super.initState();
  }

  @override
      void dispose() {
        _controller?.dispose();

        super.dispose();
      }

 
@override
Widget build (BuildContext context) {
 return Scaffold(
appBar: AppBar{
actions: [
IconButton(
    onPressed: () {
       _isFrontCamSelected
           ? _onNewCameraSelected(cameras[0])
           : _onNewCameraSelected(cameras[1]);

       setState(() {
         _isFrontCamSelected = !_isFrontCamSelected;
       }
      );
     },
    ],
body: Stack(
  children: [
     _isCameraPermissionGranted
        ? _isCameraInitialized
          ? Stack(
             children: [
              ClipRRect( 
                child: AspectRatio(
                  aspectRatio: _controller!.value.aspectRatio,
                  child: CameraPreview(_controller!),
                ),
               ),
            // UI body code
           ],
          : Center(
              child: CircularProgressIndicator(),
            )
        : Column(
        Text(
          'Permission denied',
        ),
       ElevatedButton(
         onPressed: () {
          _getPermissionStatus();
         },
         child: Text(
           'Give permission'
           ),
          }
         )
        ],
       ),
      ],
     ),
    ),   
   }
 

Appreciate any help on this

Upvotes: 0

Views: 333

Answers (1)

Zechst
Zechst

Reputation: 1

I tried downgraded my flutter to 2.10.0 to test if it’s the package’s compatibility issue and true enough, it worked. Guessing there’s some breaking changes in the dependency. I’ll look for a workaround with another package at the meantime

Upvotes: 0

Related Questions