shogo4405 / HaishinKit.dart

Camera and Microphone streaming library via RTMP for Flutter.
https://pub.dev/packages/haishin_kit
BSD 3-Clause "New" or "Revised" License
31 stars 19 forks source link

Page closed, camera idle, not closed #62

Open daijintaozz opened 1 month ago

daijintaozz commented 1 month ago

Describe the bug

import 'package:flutter_pet_community/tools.dart';
import 'package:haishin_kit/rtmp_connection.dart';
import 'package:haishin_kit/rtmp_stream.dart';
import 'package:haishin_kit/video_source.dart';

import 'package:audio_session/audio_session.dart';
import 'package:haishin_kit/audio_source.dart';
import 'package:haishin_kit/stream_view_texture.dart';

class TestView extends StatefulWidget {
  static const String routeName = "TestView";

  const TestView({super.key});

  @override
  State<TestView> createState() => _TestViewState();
}

class _TestViewState extends State<TestView> {
  String streamURL = "rtmp://192.168.0.102:1935/live";
  RtmpConnection? _connection;
  RtmpStream? _stream;
  bool _recording = false;
  CameraPosition currentPosition = CameraPosition.back;

  RtmpConnection? connection;
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    await Permission.camera.request();
    await Permission.microphone.request();

    // Set up AVAudioSession for iOS.
    final session = await AudioSession.instance;
    await session.configure(const AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.allowBluetooth,
    ));
    connection = await RtmpConnection.create();

    connection?.eventChannel.receiveBroadcastStream().listen((event) {
      switch (event["data"]["code"]) {
        case 'NetConnection.Connect.Success':
          _stream?.publish("live");
          setState(() {
            _recording = true;
          });
          break;
      }
    });

    RtmpStream stream = await RtmpStream.create(connection!);
    await stream.attachAudio(AudioSource());
    await stream.attachVideo(VideoSource(position: currentPosition));

    if (!mounted) return;

    setState(() {
      _connection = connection;
      _stream = stream;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('HaishinKit example app'), actions: [
        IconButton(
          icon: const Icon(Icons.flip_camera_android),
          onPressed: () {
            if (currentPosition == CameraPosition.front) {
              currentPosition = CameraPosition.back;
            } else {
              currentPosition = CameraPosition.front;
            }
            _stream?.attachVideo(VideoSource(position: currentPosition));
          },
        )
      ]),
      body: Center(
        child: _stream == null ? const Text("") : StreamViewTexture(_stream),
      ),
      floatingActionButton: FloatingActionButton(
        child: _recording ? const Icon(Icons.fiber_smart_record) : const Icon(Icons.not_started),
        onPressed: () async {
          if (_recording) {
            await Future.wait([
              _stream!.attachAudio(null),
              _stream!.attachVideo(null),
            ]);

            _connection?.close();

            setState(() {
              _recording = false;
            });
          } else {
            _connection?.connect(streamURL);
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    _connection?.close();
    _connection?.dispose();
    _stream?.close();
    _stream?.dispose();
    super.dispose();
  }
}

To Reproduce

After the page is closed and exited, the camera keeps printing and remains idle without being closed。

Expected behavior

Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_CLOSED for client com.samsung.adaptivebrightnessgo API Level 2

Version

haishin_kit: ^0.13.0

dart: ">=3.4.3 <4.0.0" flutter: ">=3.22.0"

Smartphone info.

No response

Additional context

No response

Screenshots

No response

Relevant log output

I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_IDLE for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_CLOSED for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_OPEN for client com.moyou.flutter_pet_community API Level 2
I/CameraManagerGlobal(19085): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_ACTIVE for client com.moyou.flutter_pet_community API Level 2
I/CameraManagerGlobal(19085): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_IDLE for client com.moyou.flutter_pet_community API Level 2
I/CameraManagerGlobal(19085): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_CLOSED for client com.moyou.flutter_pet_community API Level 2
E/CameraCaptureSession(19085): Session 0: Exception while stopping repeating: 
E/CameraCaptureSession(19085): android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): cancelRequest:688: Camera 0: Error clearing streaming request: Function not implemented (-38)
E/CameraCaptureSession(19085):  at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:1731)
E/CameraCaptureSession(19085):  at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:99)
E/CameraCaptureSession(19085):  at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1371)
E/CameraCaptureSession(19085):  at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:579)
E/CameraCaptureSession(19085):  at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:790)
E/CameraCaptureSession(19085):  at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:267)
E/CameraCaptureSession(19085):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
E/CameraCaptureSession(19085):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
E/CameraCaptureSession(19085):  at java.lang.Thread.run(Thread.java:1012)
E/CameraCaptureSession(19085): Caused by: android.os.ServiceSpecificException: cancelRequest:688: Camera 0: Error clearing streaming request: Function not implemented (-38) (code 10)
E/CameraCaptureSession(19085):  at android.os.Parcel.createExceptionOrNull(Parcel.java:3037)
E/CameraCaptureSession(19085):  at android.os.Parcel.createException(Parcel.java:3007)
E/CameraCaptureSession(19085):  at android.os.Parcel.readException(Parcel.java:2990)
E/CameraCaptureSession(19085):  at android.os.Parcel.readException(Parcel.java:2932)
E/CameraCaptureSession(19085):  at android.hardware.camera2.ICameraDeviceUser$Stub$Proxy.cancelRequest(ICameraDeviceUser.java:666)
E/CameraCaptureSession(19085):  at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:97)
E/CameraCaptureSession(19085):  ... 7 more
I/CameraManagerGlobal(19085): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_OPEN for client com.moyou.flutter_pet_community API Level 2
I/CameraManagerGlobal(19085): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_ACTIVE for client com.moyou.flutter_pet_community API Level 2
W/System  (19085): A resource failed to call Surface.release. 
W/System  (19085): A resource failed to call Surface.release. 
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_OPEN for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_ACTIVE for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_IDLE for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_CLOSED for client com.samsung.adaptivebrightnessgo API Level 2
D/BufferPoolAccessor2.0(19085): evictor expired: 1, evicted: 0
D/BufferPoolAccessor2.0(19085): bufferpool2 0xb400007bd4232c28 : 0(0 size) total buffers - 0(0 size) used buffers - 112/117 (recycle/alloc) - 6/224 (fetch/transfer)
D/BufferPoolAccessor2.0(19085): evictor expired: 1, evicted: 1
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_OPEN for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_ACTIVE for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_IDLE for client com.samsung.adaptivebrightnessgo API Level 2
I/CameraManagerGlobal(19085): Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_CLOSED for client com.samsung.adaptivebrightnessgo API Level 2