Pato Srna
Pato Srna

Reputation: 100

Ozeki SDK camera stream disconnecting

So I am building up an app for onvif IP camera, I have it set up like this:

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();
        }
    }

It connect just fine but the moment I start the stream it starts connecting the stream for a second and then disconnects from the network. I tried what I could find on the internet but nothing fixed the issue, I feel like I dont have it set up right but this is first time working with onvif camera and the library, any advice or info appriciated, thanks!

Upvotes: 0

Views: 1374

Answers (1)

user10499793
user10499793

Reputation: 26

Its probably because of the wrong streaming, thats what happened to me last time. You can change it on the onvif device manager on profiles...Make a new profile,choose the H264 stream( no matter which one) and delete all the other profiles that already exist, then save it and give a try to your programm, it should work then :)

Upvotes: 1

Related Questions