Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'Failed to load resource: The operation couldn't be completed. Protocol Error" in iOS 12 Video?

I'm working on a video streaming component and videos seem to be working normally on every platform except for iOS 12. The video works for previous iOS versions (10.3 and 11.0). When I check the network requests, I get the following error. I've tried looking through changes made in iOS 12 related to video streaming but I couldn't find anything specific. Any help would be appreciated! I've also attached the code for the stream endpoint below. Thanks!

exports.DECRYPT = async function(req, res, {
    content_id
}) {
    let result = await FileFactory.Decrypt({
        content_id
    });
    if (result.content_type.includes('video')) {
        let fileSize = result.original_size;
        const range = req.headers.range;
        const parts = range ? range.replace(/bytes=/, "").split("-") : undefined;
        const start = parts ? parseInt(parts[0], 10) : undefined;
        const end = parts && parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
        res.setHeader('Accept-Ranges', 'bytes');
        res.setHeader('Cache-Control', 'no-cache');
        res.setHeader('Content-Type', result.content_type);
        res.setHeader('Content-Length', end - start + 1);
        res.setHeader('Content-Range', `bytes ${start}-${end}/${fileSize}`);
        res.setHeader('Connection', 'Keep-Alive');
        res.setHeader('Content-Encoding', 'identity');
        if (start === 0 && !(parts[1])) {
            res.statusCode = 200;
        } else {
            res.statusCode = 206;
        }
        let stream = request({
            url: result.url
        }).pipe(result.decipher);
        let pointer = 0;
        stream.on('data', (chunk) => {
            pointer += chunk.length;
            if (pointer > start) {
                res.write(chunk.slice(start - pointer, end + 1));
            } 
            if (pointer > end) {
                stream.destroy("Chunk loaded");
                res.end();
            }
        });
        stream.on('error', function(e) {
        });
        stream.on('end', () => {
            res.end();
        });
    } else {
        res.setHeader('Content-Type', result.content_type);
        res.setHeader('Accept-Ranges', 'bytes');
        res.statusCode = 200;
        request(result.url).pipe(result.decipher).pipe(res);
    }
};
like image 248
SKzzz Avatar asked Nov 05 '25 04:11

SKzzz


1 Answers

It appears that safari has some issue with HTTP/2. After some research, I ended up removing HTTP/2 configuration from nginx.

like image 187
Gerhat Avatar answered Nov 06 '25 19:11

Gerhat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!