Ozeki SDK相机流断开连接

时间:2017-11-06 07:42:13

标签: c# ip-camera onvif ozeki

所以我正在为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相机和图书馆,任何建议或信息appriciated,谢谢!

1 个答案:

答案 0 :(得分:0)

这可能是由于错误的流式传输,这就是我上次发生的事情。您可以在onvif设备管理器上的配置文件中对其进行更改...创建一个新的配置文件,选择H264流(无论哪个),并删除所有其他已存在的配置文件,然后保存并尝试进行编程,然后就可以了:)