Live streaming JPEG images in tvOS Swift

379 Views Asked by At

We are using ZoneMinder live cam streaming software where it streams JPEG images (MJPEG), this works well if applies to HTML <img/> tag which shows auto-updated images every 30 seconds:

<html>
    <body>
        <img src="https://oursite/cgi-bin-zm/nph-zms?scale=50&width=1920px&height=1080px&mode=jpeg&maxfps=30&monitor=21&connkey=150000&rand=1598607232" width="860px" height="340px"/>
    </body>
</html>

Using Swift 5 I tried to print the URL/image to an UIImageView in the following way:

let liveURL = URL(string:"https://oursite/cgi-bin-zm/nph-zms?scale=50&width=1920px&height=1080px&mode=jpeg&maxfps=30&monitor=21&connkey=150000&rand=1598607232")
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue:OperationQueue.main)
        
session.dataTask(with: url) { (data, request, error) in
     guard let data = data, error == nil else { return }
     print("Download Finished")
     DispatchQueue.main.async() { [weak self] in
         self?.imgLive.image = UIImage(data: data)
     }
}.resume()

Initially, I was having self-signed SSL error accessing the domain, this resolved after using self as URLSessionDelegate.

I confirm with the above code-block I can able to fetch any regular image from the other remote location.

Using our live-feed image provider URL, however I see the data under dataTask method always is 0 bytes.

How I can access such URL provides streaming images, and displaying effectively in Swift?

1

There are 1 best solutions below

0
Santanu Karar On BEST ANSWER

I think I don't need to re-invent the wheel when someone already invented it.

https://github.com/WrathChaos/MJPEGStreamLib - This worked well for me to stream MJPEG images from a remote location!