KTVHTTPCache Documentation

repository·master·Indexed 25 days ago

https://github.com/changbadevs/ktvhttpcache

An HTTP caching framework for multimedia resources supporting MP4, HLS (m3u8), and audio types. It enables simultaneous playback, downloading, and caching via a local proxy server, compatible with AVPlayer and FFmpeg-based players. Features include preloading with KTVHCDataLoader, AirPlay support, URL mapping for dynamic resources, and configurable network settings for timeouts and Content-Types.

Tokens
1.5K
Snippets
8
Records
8
Agent score
32%

What's inside KTVHTTPCache

  1. Install KTVHTTPCache via CocoaPods or Carthage

    master

    You can integrate KTVHTTPCache into your Xcode project using either CocoaPods or Carthage.

    CocoaPods

    Add the following to your Podfile:

    pod 'KTVHTTPCache', '~> 3.0.0'

    Then run pod install.

    Carthage

    Add the following to your Cartfile:

    github "ChangbaDevs/KTVHTTPCache" ~> 3.0.0

    Then run carthage update. After the build completes, drag both KTVHTTPCache.framework and KTVCocoaHTTPServer.framework into your Xcode project.

    pod 'KTVHTTPCache', '~> 3.0.0'
  2. Configure network settings and acceptable Content-Types

    master

    You can customize the network behavior of the cache, including timeout intervals and allowed MIME types.

    • downloadSetTimeoutInterval:: Sets the timeout.
    • downloadSetAcceptableContentTypes:: Defines which Content-Type values are allowed. By default, it supports common video/audio types like video/x, application/mp4, etc.
    • downloadSetUnacceptableContentTypeDisposer:: A handler triggered when a Content-Type is not in the default allowed list, allowing you to manually decide whether to accept it.
    // Set timeout interval.
    [KTVHTTPCache downloadSetTimeoutInterval:30];
    
    // Set custom acceptable content types
    [KTVHTTPCache downloadSetAcceptableContentTypes:contentTypes];
    
    // Handle unacceptable content types manually
    [KTVHTTPCache downloadSetUnacceptableContentTypeDisposer:^BOOL(NSURL *URL, NSString *contentType) {
        return NO;
    }];
  3. Use KTVHTTPCache with AVPlayer

    master

    To use KTVHTTPCache for caching multimedia during playback, you must start the local proxy server, generate a proxy URL from your original resource URL, and then initialize your player with that proxy URL. While demonstrated with AVPlayer, it works with any FFmpeg-based player.

    // 1.Start local proxy server.
    [KTVHTTPCache proxyStart:&error];
    
    // 2.Generate proxy URL.
    NSURL *proxyURL = [KTVHTTPCache proxyURLWithOriginalURL:originalURL];
    
    // 3.Create AVPlayer with proxy URL.
    AVPlayer *player = [AVPlayer playerWithURL:proxyURL];
  4. Map dynamic URLs to the same cache using URL Mapping

    master

    If different URLs point to the same underlying resource (e.g., due to changing tokens), you can use encodeSetURLConverter: to map them to a single canonical URL. This ensures the framework treats them as the same cached file.

    [KTVHTTPCache encodeSetURLConverter:^NSURL *(NSURL *URL) {
        return URL;
    }];
  5. Use the KTVHTTPCache logging system

    master

    The framework includes a complete logging system for tracing issues. You can enable console logs, write logs to a file, and retrieve error information for specific URLs.

    // Get error information for a specified URL.
    NSError *error = [KTVHTTPCache logErrorForURL:URL];
    
    // Enable console output logs.
    [KTVHTTPCache logSetConsoleLogEnable:YES];
    
    // Write logs to file.
    [KTVHTTPCache logSetRecordLogEnable:YES];
    NSString *logFilePath = [KTVHTTPCache logRecordLogFilePath];
  6. Preload resources with KTVHCDataLoader

    master

    You can preload data using KTVHCDataLoader for standard requests or KTVHCDataHLSLoader for HLS (m3u8) content. You can monitor preload status in real-time via the loader's delegate. The preloading range is controlled by the Range parameter in the Request Header.

    // Standard request preloading
    KTVHCDataRequest *request= [[KTVHCDataRequest alloc] initWithURL:URL headers:headers];
    KTVHCDataLoader *loader = [KTVHTTPCache cacheLoaderWithRequest:request];
    loader.delegate = self;
    [loader prepare];
    
    // HLS content preloading
    KTVHCDataRequest *request= [[KTVHCDataRequest alloc] initWithURL:URL headers:nil];
    KTVHCDataHLSLoader *loader = [KTVHTTPCache cacheHLSLoaderWithRequest:request];
    loader.delegate = self;
    [loader prepare];
  7. Manage and retrieve cached data

    master

    The default cache limit is 500MB. Once reached, the framework uses an elimination mechanism to remove the oldest data. To get the local file URL for a resource that has been fully cached, use cacheCompleteFileURLWithURL:.

    NSURL *fileURL= [KTVHTTPCache cacheCompleteFileURLWithURL:originalURL];
  8. Activate AirPlay support

    master

    By default, the local server only accepts requests from localhost for stability, which disables AirPlay. To enable AirPlay, use proxyURLWithOriginalURL:bindToLocalhost: and set the bindToLocalhost parameter to NO.

    // Set bindToLocalhost to NO to activate AirPlay.
    NSURL *proxyURL = [KTVHTTPCache proxyURLWithOriginalURL:originalURL bindToLocalhost:NO];