0

I have created a method to parse a .m3u8 file, download the referenced files, and recreate a new .m3u8 file to be served locally for offline playback. Everything does work, but perhaps I am misusing either NSScaner or Grand Central Dispatch, as in my mind the method should run through quickly and queue up downloads in GCD. The method, however, takes the full amount of time to run (in the background, yes, but I'd like to have the new file created as quickly as possible, not when everything is done downloading). Can anyone see where my bottleneck is? Thanks in advance.

- (void)beginDownloadAndCreateLocalM3U8FileForLocalPlaybackFromPlaylist:(NSString*)playlist forId:(NSString*)_id withProgressBlock:(void (^)(float))progress withCompletionBlock:(void (^)(id))success
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        @autoreleasepool {
            NSString *stringURL = playlist;
            NSURL  *url = [NSURL URLWithString:stringURL];
            NSError *error;
            NSString *stringData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
            NSString *foundData;
            NSScanner *scanner=[NSScanner scannerWithString:stringData];
            NSUInteger counter = 0;
            NSMutableString *m3u8 = [NSMutableString new];
            [scanner scanUpToString:@"#EXTINF" intoString:&foundData];
            [m3u8 appendString:foundData];
            NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];
            if (![self directoryExistsAtAbsolutePath:[NSString stringWithFormat:@"%@/Web", documentsDirectory]]) {
                [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/Web", documentsDirectory] withIntermediateDirectories: YES attributes:nil error: &error];
            }
            if (![self directoryExistsAtAbsolutePath:[NSString stringWithFormat:@"%@/Web/%@", documentsDirectory, _id]]) {
                [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/Web/%@", documentsDirectory, _id] withIntermediateDirectories: YES attributes:nil error: &error];
            } else {
                success([NSString stringWithFormat:@"http://127.0.0.1:12345/%@.m3u8", _id]);
                return;
            }
            while (![scanner isAtEnd]) {
                [scanner scanUpToString:@"\n" intoString: &foundData];
                if ([foundData hasPrefix:@"#EXTINF:"]) {
                    [m3u8 appendFormat:@"%@\n", foundData];
                } else if ([foundData hasPrefix:@"http:"]) {
                    NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:foundData]];
                    if ( urlData )
                    {
                        NSString  *filePath = [NSString stringWithFormat:@"%@/Web/%@/%i.ts", documentsDirectory, _id, counter];
                        NSString *localURL = [NSString stringWithFormat:@"http://127.0.0.1:12345/%@/%i.ts", _id, counter];
                        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                            @autoreleasepool {
                                [urlData writeToFile:filePath atomically:YES];
                            }
                        });
                        //assemble the m3u8 file here for each entry with the original durations.  No need to recalculate.
                        //NSLog(@"Adding this: %@", filePath);
                        [m3u8 appendFormat:@"%@\n", localURL];
                        counter++;
                    }
                }
            }
            [m3u8 appendString:@"#EXT-X-ENDLIST"];
            [m3u8 writeToFile:[NSString stringWithFormat:@"%@/Web/%@.m3u8", documentsDirectory, _id] atomically:YES encoding:NSUTF8StringEncoding error:&error];
            NSLog(@"Final m3u8 is %@", m3u8);
            success([NSString stringWithFormat:@"http://127.0.0.1:12345/%@.m3u8", _id]);
        }
    });
}

1 Answer 1

2

This line here

NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:foundData]];

... in your while loop is running synchronous with your scanner, your scanner can't progress to the next token until the URL it's downloading has downloaded. Try hoisting up the low-priority dispatch_async() to cover all of the logic of the else case, not just the writeToFile: call.

1
  • Of course! Thank you @iluvcapra. Always good to get a second set of eyes on things.
    – diatrevolo
    Feb 1, 2013 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.