基于CocoaHTTPServer iOS HTTP 服务器的相册资源共享服务框架

说明及注意事项

  • FFmpeg Version 2.8
  • FFmpeg 仅编译部必需的功能
  • FFmpeg 需要导入的动态库libz.tbd,libiconv.tbd,libbz2.tbd
  • iOS 10 + 需要添加相册访问权限,否则无法访问相册资源
  • iOS 9 + 使用HTTP请求需要在Info.plist中添加 NSAllowsArbitraryLoadsYES
  • 若需要媒体共享服务被访问期间可后台运行,需要在 Info.plist中添加 Required background modes 添加条目选择App plays audio or streams audio/video using AirPlay
  • 必需在同一局域网下方可访问

如何使用

1. CocoaPods集成

1
2
3
4
5
platform :ios, '7.0'

target 'ALAssetCocoaHTTPServerDemo' do
pod 'ALAssetCocoaHTTPServer',:svn =>"http://172.20.3.159/svn/Android/App/ScreensInteract3/sourcecode/trunk/IOS-901/MultiScreenFramework/ALAssetCocoaHTTPServer/", :tag =>"0.0.2"
end

2. 使用范例

1
2
3
4
5
6
7
8
9
10

HTTPServer *httpServer = [[HTTPServer alloc] init];
[httpServer setType:@"_http._tcp."];
[httpServer setDocumentRoot:NSHomeDirectory()];
[httpServer setPort:12345];
NSError *error;
if([httpServer start:&error])
NSLog(@"Started HTTP Server on http://%@:%hu", [httpServer domain], [httpServer listeningPort]);
else
NSLog(@"Error starting HTTP Server: %@", error);

3. 打开调试打印

1
2
3
4
5
6
7
8
9
10
#import "DDTTYLogger.h"
@interface AppDelegate ()
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
}
@end

4. 请求url样式

若当前IP为192.168.1.100

实现原理

1.图片文件请求

图片文件的获取比较简单,直接通过url定位到系统相册资源后通过调用系统接口获取对应数据

2.视频请求流程简介

1
2
3
4
5
6
7
8
9
10
st=>start: 客户端
io=>inputoutput: verification
op=>operation: Your Operation
cond=>condition: Yes or No?
sub=>subroutine: Your Subroutine
e=>end

st->io->op->cond
cond(yes)->e
cond(no)->sub->io
1
2
3
4
5
6
7
8
9
10
11
sequenceDiagram
客户端->CocoaHTTPServer: 访问m3u8文件
CocoaHTTPServer->m3u8生成器: 请求m3u8文件
note right of m3u8生成器:根据请求媒体\n文件及分片长度\n生成m3u8文件
m3u8生成器-->CocoaHTTPServer:返回生成文件
CocoaHTTPServer-->客户端:返回m3u8文件
客户端->CocoaHTTPServer: 请求ts文件
CocoaHTTPServer->ts文件管理: 请ts视频文件
note right of ts文件管理:获取本地沙盒目录下的\nts缓存文件或根据请求\nurl临时转码,切片,\n封装而生成,同时删除\n失效的ts临时文件
ts文件管理-->CocoaHTTPServer:返回获取文件
CocoaHTTPServer-->客户端:返回ts视频文件

iOS文件只能访问沙盒路径内的资源,在通过将http请求相册视频资源文件时,可通过系统接口来获取; 为了解决系统部分mov格式视频部分平台播放器解码不支持的问题,在获取资源文件后将原文件转码为了mp4格式; 为了使转码后的文件不占用太多的内存空间,转码时根据请求文件的大小,只将部分请求的文件转换为mp4格式;

  • 获取m3u8文件

    通过url定位到系统相册资源

    根据资源文件时长计算出分片策略

    文件信息,分片时长创建ts切片对应url

    生成相应m3u8文件

  • 获取ts切片资源

    播放器解析m3u8文件解析获取ts分片视频资源url

  • 生成ts切片

    根据url获取视频文件信息,切片时长和序列号

    通过系统接口获取到对应时间段的视频切片数据

    若为mov格式,则将其转换为mp4格式

    使用FFmpegmp4格式切片数据转换为ts封装格式