Skip to content

Commit

Permalink
fix #74
Browse files Browse the repository at this point in the history
  • Loading branch information
changsanjiang committed Nov 27, 2024
1 parent e5ae3bd commit 2d8704b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion SJMediaCacheServer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SJMediaCacheServer'
s.version = '2.0.3'
s.version = '2.0.4'
s.summary = <<-DESC
SJMediaCacheServer 是一个高效的 HTTP 媒体缓存框架,旨在代理媒体数据请求并优先提供缓存数据,从而减少网络流量并增强播放的流畅性。该框架支持两种类型的远程资源:基于文件的媒体,如 MP3、AAC、WAV、FLAC、OGG、MP4 和 MOV 等常见格式,以及 HLS(HTTP Live Streaming)流。它会自动解析 HLS 播放列表并代理各个媒体片段。
DESC
Expand Down
3 changes: 3 additions & 0 deletions SJMediaCacheServer/Core/Asset/FILE/FILEAsset.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ - (float)completeness {
NSString *pathExtension = response.pathExtension;
NSUInteger totalLength = response.totalLength;
NSUInteger offset = response.range.location;

if ( totalLength == NSUIntegerMax ) return nil;

if ( !mMetadataReady ) {
mMetadataReady = YES;
_totalLength = totalLength;
Expand Down
2 changes: 2 additions & 0 deletions SJMediaCacheServer/Core/Asset/HLS/HLSAsset.m
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ - (BOOL)isStored {
case MCSDataTypeFILEMask:
case MCSDataTypeFILE: return nil; /* return nil; */
case MCSDataTypeHLSSegment: { // only segment
if ( response.totalLength == NSUIntegerMax ) return nil;

@synchronized (self) {
id<HLSAssetSegment>content = nil;
NSString *segmentIdentifier = [self generateSegmentIdentifierWithOriginalURL:response.originalRequest.URL];
Expand Down
2 changes: 1 addition & 1 deletion SJMediaCacheServer/Core/Common/MCSInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark -

@protocol MCSResponse <NSObject>
@property (nonatomic, readonly) NSUInteger totalLength;
@property (nonatomic, readonly) NSUInteger totalLength; // 200请求时, NSUIntegerMax表示服务器未提供content-length;
@property (nonatomic, readonly) NSRange range; // 206请求时length不为0
@property (nonatomic, copy, readonly) NSString *contentType; // default is "application/octet-stream"
@end
Expand Down
24 changes: 18 additions & 6 deletions SJMediaCacheServer/Core/Download/MCSDownload.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "MCSUtils.h"
#import "MCSLogger.h"
#import "NSURLRequest+MCS.h"
#import "MCSConsts.h"

@interface NSURLSessionTask (MCSDownloadExtended)<MCSDownloadTask>

Expand Down Expand Up @@ -58,15 +59,26 @@ - (instancetype)init {
mDefaultResponseHandler = ^id<MCSDownloadResponse> _Nullable (NSURLSessionTask *task, NSURLResponse *response) {
if ( ![response isKindOfClass:NSHTTPURLResponse.class] ) return nil;
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
MCSResponseContentRange contentRange = MCSResponseGetContentRange(res);
if ( MCSResponseRangeIsUndefined(contentRange) ) return nil;
NSRange range = MCSResponseRange(contentRange);
NSUInteger totalLength = contentRange.totalLength;

NSInteger statusCode = res.statusCode;
NSString *_Nullable contentType = MCSResponseGetContentType(res);
NSString *_Nullable pathExtension = MCSSuggestedPathExtension(res);
return [MCSDownloadResponse.alloc initWithOriginalRequest:task.originalRequest currentRequest:task.currentRequest statusCode:statusCode pathExtension:pathExtension totalLength:totalLength range:range contentType:contentType];
switch (statusCode) {
case 206: {
MCSResponseContentRange contentRange = MCSResponseGetContentRange(res);
if ( MCSResponseRangeIsUndefined(contentRange) ) return nil;
NSRange range = MCSResponseRange(contentRange);
NSUInteger totalLength = contentRange.totalLength;
return [MCSDownloadResponse.alloc initWithOriginalRequest:task.originalRequest currentRequest:task.currentRequest statusCode:statusCode pathExtension:pathExtension totalLength:totalLength range:range contentType:contentType];
}
break;
case 200: {
NSUInteger totalLength = MCSResponseGetContentLength(res) ?: NSUIntegerMax;
NSRange range = NSMakeRange(0, totalLength);
return [MCSDownloadResponse.alloc initWithOriginalRequest:task.originalRequest currentRequest:task.currentRequest statusCode:statusCode pathExtension:pathExtension totalLength:totalLength range:range contentType:contentType];
}
break;
default: return nil;
}
};
_responseHandler = mDefaultResponseHandler;
}
Expand Down

0 comments on commit 2d8704b

Please sign in to comment.