Search code examples
iosobjective-cvideomultifile-uploader

Uploading multiple chunked videos to server


I am trying to upload multiple videos split to chunks to a server.

I get the AVAssets from the camera roll, get their URLs, put them into a queue and open an Input Stream with the URL for the first file. I only open the next file after I am done with the previous one.

When I upload one video everything goes through fine but when I select multiple videos I get an error. I am using stream polling and this is the Input Stream read code:

int size = 1024;
uint8_t readBuffer[size];
NSMutableData *collectorBuffer = [NSMutableData data];
NSInteger length = 0;
NSInteger totalReadBytes = 0;

while (totalReadBytes < kChunkSize) {
    length = [self.inputStream read:readBuffer maxLength:size];

    if (length) {
        [collectorBuffer appendBytes:(const void *)readBuffer length:length];
        totalReadBytes += length;
    } else {
        break;
    }
}

The error I get is length being -1 and the streamError is:

Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={_kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=1}

I am aware that I can copy the videos to the sandbox and then upload them but the whole point is to not allocate so much memory. Every app I have checked makes only 1 video upload at a time. Is it even possible to make such an upload on iOS?


Solution

  • In you question you say that you first get all URLs, then start reading them. This problem can be solved by only getting one URL at a time.

    I don't know why this happens, but my hypothesis is that (since you're working with the camera roll, as opposed to your app's sandbox) you are granted permissions to access the last URL that you obtained from an asset.

    In your case, you get all the URLs, and attempt to read from the first one. However, you most probably only have permissions to read the last URL:

    1. Get first URL (have permission for first URL)
    2. Get second URL (have permission for second URL, lose permissions for first URL)
    3. ...