Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

ios - Swift Mjpeg Streaming Only Showing Single Frame

I am trying to create a Mjepg stream. I have followed a tutorial and it's not a lot of code to get a stream to lead but I don't understand why it's not working for me.

//Class properties

private var mjpegSession : URLSession?
private var mjpegData:Data = Data()

override func viewDidLoad(){
...
mjpegSession =  URLSession(configuration: .default, delegate: self, delegateQueue: nil)
        if let url = URL(string: "http://cam6284208.miemasu.net/nphMotionJpeg?Resolution=640x480") {
           mjpegSession?.dataTask(with: url).resume()
        }
}

extension ViewController: URLSessionDataDelegate, URLSessionTaskDelegate {
    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
        mjpegData.append(data)
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
           if mjpegData.count > 0 {
                DispatchQueue.main.async{
                    if let image = UIImage(data: self.mjpegData){
                        self.cameraImageView.image = image
                    }
                }
            }
            mjpegData.removeAll()
            

        completionHandler(.allow)
    }
 }

The tutorial removed all the data in on the line that says mjpegData.removeAll()

However if I do that the image is incomplete and there's errors in the log about not being able to decode an image. If I remove it, I get a complete frame or image but I only get one. Using that url in VLC shows a constant stream so I know the url is valid stream. Thanks for everyone's time.

question from:https://stackoverflow.com/questions/65617278/swift-mjpeg-streaming-only-showing-single-frame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So I found a solution having to do with queues and background threads that seemed to fix my problem. Here's the solution in case anyone else runs into this.

//Class properties

private var mjpegSession : URLSession?
private var mjpegData:Data = Data()

override func viewDidLoad(){
...
self.queue = DispatchQueue(label: "MjpegQueue")
        self.mjpegSession =  URLSession(configuration: .default, delegate: self, delegateQueue: nil)
        if let url = URL(string: "http://cam6284208.miemasu.net/nphMotionJpeg?Resolution=640x480") {
           DispatchQueue.global().async {
                self.mjpegSession?.dataTask(with: url).resume()
            }
        }
}

extension ViewController: URLSessionDataDelegate, URLSessionTaskDelegate {
    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
        mjpegData.append(data)
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
                   queue.sync{
            if mjpegData.count > 0 {
                
                let data = self.mjpegData
                if let image = UIImage(data: data){
                    DispatchQueue.main.async {
                        self.cameraImageView.image = image
                    }
                }
            }
            
            mjpegData.removeAll()
        }
        
        
        
        completionHandler(.allow)
    }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...