Ozeki SDK 摄像头流断开连接

Ozeki SDK camera stream disconnecting

所以我正在为 onvif IP 摄像机构建一个应用程序,我将其设置如下:

public partial class MainWindow : Window
{
    private VideoViewerWPF _videoViewerWpf;
    private BitmapSourceProvider _provider;
    private IIPCamera _ipCamera;
    private MediaConnector _connector;
    private MotionDetector _detector;

    public MainWindow()
    {
        InitializeComponent();
        _connector = new MediaConnector();
        _provider = new BitmapSourceProvider();
        _detector = new MotionDetector();
        SetVideoViewer();

    }

    private void SetVideoViewer()
    {
        _videoViewerWpf = new VideoViewerWPF
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Background = Brushes.Black,
            Width = 1280,
            Height = 720
        };

        CameraBox.Children.Add(_videoViewerWpf);
        _videoViewerWpf.SetImageProvider(_provider);
    }

    private void CameraStateChanged(object sender, CameraStateEventArgs e)
    {
        Dispatcher.Invoke(() => {
            labeltest.Content = e.State.ToString();
        });
    }

    private void ConnectIPCamera_Click(object sender, RoutedEventArgs e)
    {
        var host = HostTextBox.Text;
        var user = UserTextBox.Text;
        var pass = Password.Password;

        _ipCamera = IPCameraFactory.GetCamera(host, user, pass);
        if (_ipCamera == null) return;
        _connector.Connect(_ipCamera.VideoChannel, _detector);
        _connector.Connect(_detector, _provider);
        _ipCamera.Connect();
        _ipCamera.CameraStateChanged += CameraStateChanged;
        _videoViewerWpf.Start();
    }

    private void DisconnectIPCamera_Click(object sender, RoutedEventArgs e)
    {
        _videoViewerWpf.Stop();

        _ipCamera.Disconnect();
        _ipCamera.Dispose();

        _connector.Disconnect(_ipCamera.VideoChannel, _detector);
        _connector.Disconnect(_detector, _provider);
    }

    void GuiThread(Action action)
    {
        Dispatcher.BeginInvoke(action);
    }

    private void StartMotionDetection()
    {
        _detector.HighlightMotion = HighlightMotion.Highlight;
        _detector.MotionColor = MotionColor.Red;
        _detector.MotionDetection += Detector_MotionDetection;
        _detector.Start();
    }

    void Detector_MotionDetection(object sender, MotionDetectionEvent e)
    {
        GuiThread(() =>
        {
            if (e.Detection)
                MotionEventLabel.Content = "Motion Detected";
            else
                MotionEventLabel.Content = "Motion Stopped";
        });
    }

    private void StopMotionDetection()
    {
        _detector.Stop();
        _detector.MotionDetection -= Detector_MotionDetection;
        _detector.Dispose();
    }

    private void MotionChecked(object sender, RoutedEventArgs e)
    {
        MotionEventLabel.Content = String.Empty;
        if (sender is CheckBox check)
        {
            if ((bool)check.IsChecked)
                StartMotionDetection();
            else
                StopMotionDetection();
        }
    }

    private void Start_Click(object sender, RoutedEventArgs e)
    {
        if(_ipCamera.State == IPCameraState.Connected)
        {
            _ipCamera.CurrentStream = _ipCamera.AvailableStreams.FirstOrDefault(x => x.Name == "MJPEG");

            _ipCamera.Start();
        }
    }

它连接得很好,但是当我开始流式传输时,它开始连接流式传输一秒钟,然后断开与网络的连接。我尝试了我在互联网上可以找到的方法,但没有解决问题,我觉得我没有正确设置,但这是第一次使用 onvif 相机和图书馆,任何建议或信息都适用,谢谢!

可能是推错了,我上次就是这样。您可以在配置文件的 onvif 设备管理器上更改它...创建一个新的配置文件,选择 H264 流(无论是哪个)并删除所有其他已经存在的配置文件,然后保存并尝试您的程序,它应该工作:)