Skip to content

Synchronous vs asynchronous connection

TVideoGrabber can connect to IP cameras and URLs:

  • synchronously, if OpenURLAsync is disabled: in this case, StartPreview() or StartRecording() blocks until the connection succeeds or fails,
  • asynchronously, if OpenURLAsync is enabled (default): in this case, StartPreview() or StartRecording() returns immediately, so the main thread is not blocked and the application continues to respond to key and mouse events.

    Once the connection is completed, the OnPreviewStarted or OnRecordingStarted event occurs to notify the application that the component is running.

    If the connection fails, the OnLog event occurs with an "e_failed_to_start_preview" or "e_failed_to_start_recording" LogType error.

Example (pseudo-code) of a generic set of StartPreview() / StartRecording() functions that handle the asynchronous connection result through the OnPreviewStarted() / OnRecordingStarted() and OnLog() events:

function StartPreview()
{
    VideoGrabber.OpenURLAsync = true // default
    VideoGrabber.VideoSource = vs_IPCamera
    VideoGrabber.IPCameraURL = "rtsp://192.168.0.25/axis-media/media.amp?videocodec=h264"
    VideoGrabber.SetAuthentication (at_IPCamera, "root", "admin")
    VideoGrabber.StartPreview ()
}

function StartRecord()
{
    VideoGrabber.OpenURLAsync = true // default
    VideoGrabber.VideoSource = vs_IPCamera
    VideoGrabber.IPCameraURL = "rtsp://192.168.0.25/axis-media/media.amp?videocodec=h264&audio=1"
    VideoGrabber.SetAuthentication (at_IPCamera, "root", "admin")
    VideoGrabber.RecordingInNativeFormat = true
    VideoGrabber.RecordingMethod = rm_MP4
    VideoGrabber.AudioRecording = true
    VideoGrabber.StartRecording ()
}

event function OnRecordingStartedEvent(Sender, FileName)
{
    ShowMessage (FileName + " recording started!")
}

event function OnPreviewStartedEvent(Sender)
{
    ShowMessage ("preview started!")
}

event function OnLogEvent(Sender, LogType, Severity, InfoMsg)
{
    if (LogType = e_failed_to_start_recording) {
        ShowMessage ("error: recording failed to start")
    }
    else if (LogType = e_failed_to_start_preview) {
        ShowMessage ("error: preview failed to start")
    }
}