ChangbaDevs / KTVHTTPCache

A powerful media cache framework.
MIT License
2.35k stars 435 forks source link
audio cache carthage cocoapods media player video

KTVHTTPCache

Cocoapods Version Cocoapods License Cocoapods platforms

KTVHTTPCache is a powerful HTTP caching framework, very suitable for caching multimedia resources.

Flow Chart

KTVHTTPCache Flow Chart

Features

Requirements

Supported Formats

Supports all resources transmitted based on HTTP protocol:

Installation

Installation with CocoaPods

To integrate KTVHTTPCache into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'KTVHTTPCache', '~> 3.0.0'

Run pod install

Installation with Carthage

To integrate KTVHTTPCache into your Xcode project using Carthage, specify it in your Cartfile:

github "ChangbaDevs/KTVHTTPCache" ~> 3.0.0

Run carthage update to build the framework and drag the built KTVHTTPCache.framework and KTVCocoaHTTPServer.framework into your Xcode project.

Design Principles

KTVHTTPCache was originally designed to realize the function of playing, downloading and caching multimedia resources during online playback. From the beginning of the design, the following principles were strictly followed:

The essence of this framework is to cache HTTP requests, and there is no restriction on the transmission content. Therefore, the application scenarios are not limited to online audio and video playback, but can also be used for file downloads, image loading, ordinary network requests and other scenarios.

Caching Strategy

Based on the principle of minimizing network usage, data is loaded in slices. There are two data sources: Network Source and File Source, which are used to download network data and read local data respectively. Dynamically generate data loading strategies by comparing the Range in the Request Header with the local cache status. For example, the Range in the Request Header is 0-1000, and there are already two pieces of data 200-500 and 700-800 in the local cache. Then 5 Sources will be generated correspondingly, which are:

They are managed by Source Manager and only provide a simple reading API to the outside world.

Usage

Use with AVPlayer

// 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];

Preloading

// The preloading range can be controlled through the Range parameter in the Request Header.
KTVHCDataRequest *request= [[KTVHCDataRequest alloc] initWithURL:URL headers:headers];
KTVHCDataLoader *loader = [KTVHTTPCache cacheLoaderWithRequest:request];
loader.delegate = self;
[loader prepare];

Activate AirPlay

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

URL Mapping

/**
  *  For example:
  *  http://www.xxxx.com/video.mp4?token=1
  *  and
  *  http://www.xxxx.com/video.mp4?token=2
  *  Although the URLs are different, they all point to the same file and can be returned in the block
  *  http://www.xxxx.com/video.mp4
  *  to map to the same cache
  */ 
[KTVHTTPCache encodeSetURLConverter:^NSURL *(NSURL *URL) {
    return URL;
}];

Network Configuration

// Set timeout interval.
[KTVHTTPCache downloadSetTimeoutInterval:30];

/**
  * For security/stability considerations, only responses with the following Content-Type are enabled by default:
  * - text/x
  * - video/x
  * - audio/x
  * - application/x-mpegURL
  * - vnd.apple.mpegURL
  * - application/mp4
  * - application/octet-stream
  * - binary/octet-stream
  * To open more types, use this API setting
  */
[KTVHTTPCache downloadSetAcceptableContentTypes:contentTypes];

// This handler is triggered when a Content-Type type is not accepted by default. You can decide whether to accept it by yourself.
[KTVHTTPCache downloadSetUnacceptableContentTypeDisposer:^BOOL(NSURL *URL, NSString *contentType) {
    return NO;
}];

Manage Cached Data

// If the URL has been fully cached, it will be automatically merged into a complete file after it is released by the local server.
NSURL *fileURL= [KTVHTTPCache cacheCompleteFileURLWithURL:originalURL];

Logging system

// 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];

License

KTVHTTPCache is released under the MIT license.

Author

Developed by Author