DOUMPMediaLibraryAssetLoader.m
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* vim: set ft=objc fenc=utf-8 sw=2 ts=2 et: */
/*
* DOUAudioStreamer - A Core Audio based streaming audio player for iOS/Mac:
*
* https://github.com/douban/DOUAudioStreamer
*
* Copyright 2013-2016 Douban Inc. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* Chongyu Zhu <i@lembacon.com>
*
*/
#if TARGET_OS_IPHONE
#import "DOUMPMediaLibraryAssetLoader.h"
#import <AVFoundation/AVFoundation.h>
#include <CommonCrypto/CommonDigest.h>
@interface DOUMPMediaLibraryAssetLoader () {
@private
NSString *_cachedPath;
AVAssetExportSession *_exportSession;
}
@end
@implementation DOUMPMediaLibraryAssetLoader
+ (instancetype)loaderWithURL:(NSURL *)url
{
return [[[self class] alloc] initWithURL:url];
}
- (instancetype)initWithURL:(NSURL *)url
{
self = [super init];
if (self) {
_assetURL = url;
}
return self;
}
- (void)start
{
if (_exportSession != nil) {
return;
}
AVURLAsset *asset = [AVURLAsset assetWithURL:_assetURL];
if (asset == nil) {
[self _reportFailure];
return;
}
_exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetPassthrough];
if (_exportSession == nil) {
[self _reportFailure];
return;
}
[_exportSession setOutputFileType:AVFileTypeCoreAudioFormat];
[_exportSession setOutputURL:[NSURL fileURLWithPath:[self cachedPath]]];
__weak typeof(self) weakSelf = self;
[_exportSession exportAsynchronouslyWithCompletionHandler:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf _exportSessionDidComplete];
}];
}
- (void)cancel
{
if (_exportSession == nil) {
return;
}
[_exportSession cancelExport];
_exportSession = nil;
}
- (void)_exportSessionDidComplete
{
if ([_exportSession status] != AVAssetExportSessionStatusCompleted ||
[_exportSession error] != nil) {
[self _reportFailure];
return;
}
[self _invokeCompletedBlock];
}
- (void)_invokeCompletedBlock
{
@synchronized(self) {
if (_completedBlock != NULL) {
_completedBlock();
}
}
}
- (void)_reportFailure
{
_failed = YES;
[self _invokeCompletedBlock];
}
- (NSString *)cachedPath
{
if (_cachedPath == nil) {
NSString *filename = [NSString stringWithFormat:@"douas-mla-%@.%@", [[self class] _sha256ForURL:_assetURL], [self fileExtension]];
_cachedPath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:_cachedPath]) {
[[NSFileManager defaultManager] removeItemAtPath:_cachedPath error:NULL];
}
}
return _cachedPath;
}
- (NSString *)mimeType
{
return AVFileTypeCoreAudioFormat;
}
- (NSString *)fileExtension
{
return @"caf";
}
+ (NSString *)_sha256ForURL:(NSURL *)url
{
NSString *string = [url absoluteString];
unsigned char hash[CC_SHA256_DIGEST_LENGTH];
CC_SHA256([string UTF8String], (CC_LONG)[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding], hash);
NSMutableString *result = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
for (size_t i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
[result appendFormat:@"%02x", hash[i]];
}
return result;
}
@end
#endif /* TARGET_OS_IPHONE */