JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "LCDeviceVideotapePlayManager.h"
#import <LCOpenSDKDynamic/LCOpenSDK/LCOpenSDK_Download.h>
#import "PHAsset+Lechange.h"
#import <LCOpenSDKDynamic/LCOpenSDK/LCOpenSDK_DownloadListener.h>
#import "LCUIKit.h"
 
static LCDeviceVideotapePlayManager *manager = nil;
 
@interface LCDeviceVideotapePlayManager ()<LCOpenSDK_DownloadListener>
 
@property (nonatomic, strong) NSDate *recordTime;
 
@property (nonatomic, assign) NSInteger recordReceive;
 
@end
 
@implementation LCDeviceVideotapePlayManager
 
+ (instancetype)manager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [LCDeviceVideotapePlayManager new];
        manager.isPlay = NO;
        manager.pausePlay = NO;
        manager.isSD = YES;
        manager.isSoundOn = YES;
        manager.isFullScreen = NO;
        manager.isOpenCloudStage = NO;
        manager.isOpenAudioTalk = NO;
        manager.isOpenRecoding = NO;
        manager.isLockFullScreen = NO;
        manager.playSpeed = 1;
    });
    return manager;
}
 
- (NSMutableDictionary *)downloadQueue {
    if (!_downloadQueue) {
        _downloadQueue = [NSMutableDictionary dictionary];
    }
    return _downloadQueue;
}
 
- (LCDeviceInfo *)currentDevice {
    return [LCDeviceVideoManager manager].currentDevice;
}
 
- (NSInteger)currentChannelIndex {
    return [LCDeviceVideoManager manager].currentChannelIndex;
}
 
- (void)setCloudVideotapeInfo:(LCCloudVideotapeInfo *)cloudVideotapeInfo {
    _cloudVideotapeInfo = cloudVideotapeInfo;
    _localVideotapeInfo = nil;
}
 
- (void)setLocalVideotapeInfo:(LCLocalVideotapeInfo *)localVideotapeInfo {
    _localVideotapeInfo = localVideotapeInfo;
    _cloudVideotapeInfo = nil;
}
 
- (void)startDeviceDownload {
    LCVideotapeDownloadInfo *info = [LCVideotapeDownloadInfo new];
    info.recordId = self.cloudVideotapeInfo ? self.cloudVideotapeInfo.recordRegionId : self.localVideotapeInfo.recordId;
    info.deviceId = self.currentDevice.deviceId;
    info.channelId = self.currentChannelInfo.channelId;
    info.localPath = [self _getCloudDownloadPath:info.recordId];
    [self.downloadQueue setObject:info forKey:[NSString stringWithFormat:@"%ld", (long)info.index]];
    [[LCOpenSDK_Download shareMyInstance] setListener:self];
    //开始下载进程
    if (self.cloudVideotapeInfo) {
        //开始下载云
        NSInteger result = [[LCOpenSDK_Download shareMyInstance] startDownload:info.index filepath:info.localPath token:[LCApplicationDataManager token] devID:info.deviceId channelID:[info.channelId integerValue] psk:info.deviceId recordRegionId:info.recordId Type:self.cloudVideotapeInfo.type Timeout:60];
        if (result != 0) {
            NSLog(@"下载云录像返回码:%ld",(long)result);
        }
 
    } else {
        //开始下载设备录像
        NSInteger result = [[LCOpenSDK_Download shareMyInstance] startDownload:info.index filepath:info.localPath token:[LCApplicationDataManager token] devID:info.deviceId decryptKey:info.deviceId fileID:info.recordId speed:1];
        if (result != 0) {
            NSLog(@"下载设备录像返回码:%ld",(long)result);
        }
    }
}
 
- (NSString *)currentVideotapeId {
    if (self.cloudVideotapeInfo) {
        return self.cloudVideotapeInfo.recordRegionId;
    }
    if (self.localVideotapeInfo) {
        return self.localVideotapeInfo.recordId;
    }
    return @"";
}
 
- (LCVideotapeDownloadInfo *)currentDownloadInfo {
    LCVideotapeDownloadInfo *result = nil;
    NSString *recordId = self.cloudVideotapeInfo ? self.cloudVideotapeInfo.recordRegionId : self.localVideotapeInfo.recordId;
    for (NSString *key in self.downloadQueue) {
        LCVideotapeDownloadInfo *info = [self.downloadQueue objectForKey:key];
        if ([info.recordId isEqualToString:recordId]) {
            result = info;
            break;
        }
    }
    return result;
}
 
- (void)updateDownload:(NSInteger)index Recieve:(NSInteger)recieve Status:(LCVideotapeDownloadState)status {
    if (status != -1) {
        NSLog(@"画面当前状态D:%ld", status);
    }
    if (![self.downloadQueue valueForKey:[NSString stringWithFormat:@"%ld", index]]) {
        return;//如果没有下载池中未保存该状态
    }
    [self willChangeValueForKey:@"downloadQueue"];
    //recieve为-1表示更新下载状态,status为-1表示更新接受到的数据
    LCVideotapeDownloadInfo *info = [self currentDownloadInfo];
    if (recieve != -1) {
        info.recieve = recieve;
    }
    if (status != -1) {
        info.donwloadStatus = status;
        if (status == LCVideotapeDownloadStatusEnd) {
            //下载正常结束时,保存到相册
            [self _saveToAlbumWithPath:info.localPath];
        }
    }
    NSLog(@"dddddddd%@", info);
    [self didChangeValueForKey:@"downloadQueue"];
}
 
- (void)_saveToAlbumWithPath:(NSString *)path {
    /* 延时保存相册,因为下载成功之后stopDownload方法在另一个线程,所以转码可能还不成功*/
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSURL *dowmloadRUL = [NSURL fileURLWithPath:path];
        [PHAsset deleteFormCameraRoll:dowmloadRUL success:^{
        } failure:^(NSError *error) {
            NSLog(@"删除失败:%@", error.description);
        }];
        [PHAsset saveVideoAtURL:dowmloadRUL success:^(void) {
            dispatch_async(dispatch_get_main_queue(), ^{
                               NSLog(@"保存成功");
                               [LCProgressHUD showMsg:@"mobile_common_data_download_success".lc_T];
                           });
        } failure:^(NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                               NSLog(@"保存失败:%@", error.description);
                               [LCProgressHUD showMsg:@"mobile_common_data_download_fail".lc_T];
                           });
        }];
    });
}
 
- (void)cancleDownload:(NSString *)recordId {
    LCVideotapeDownloadInfo *info = [self currentDownloadInfo];
    info.donwloadStatus = LCVideotapeDownloadStatusCancle;
    [[LCOpenSDK_Download shareMyInstance] stopDownload:info.index];
}
 
- (NSString *)_getCloudDownloadPath:(NSString *)recordId {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
 
    NSString *myDirectory = [libraryDirectory stringByAppendingPathComponent:@"lechange"];
    NSString *downloadDirectory = [myDirectory stringByAppendingPathComponent:@"download"];
 
    NSString *infoPath = [downloadDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%f_download", [[NSDate new]timeIntervalSince1970]]];
    NSString *downPath = [infoPath stringByAppendingString:@".mp4"];
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSError *pErr;
    NSLog(@"下载地址 = %@", downPath);
    BOOL isDir;
    if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
    if (NO == [fileManage fileExistsAtPath:downloadDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:downloadDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
    return downPath;
}
 
#pragma mark - 下载回调
- (void)onDownloadReceiveData:(NSInteger)index datalen:(NSInteger)datalen {
    NSLog(@"REVIEVE_DOWN===%ld", datalen);
    self.recordReceive += datalen;
    //每隔0.5加载数据,避免过快加载导致UI卡顿
    if (self.recordTime == nil || [[NSDate date] timeIntervalSinceDate:self.recordTime] >= 0.5) {
        self.recordTime = [NSDate date];
        [self updateDownload:index Recieve:self.recordReceive Status:-1];
        self.recordReceive = 0;
    }
}
 
- (void)onDownloadState:(NSInteger)index code:(NSString *)code type:(NSInteger)type {
    NSInteger codeInteger = [code integerValue];
            NSLog(@"录像下载回调INDEX:%ld******TYEP:%ld*******CODE:%@", index, type, code);
    //        [LCProgressHUD showMsg:[NSString stringWithFormat:@"录像下载回调INDEX:%ld******TYEP:%ld*******CODE:%@",index,type,code]];
            //    对SDK回调的数据进行转换
            if (type == 0) {
                //设备录像下载
                switch (codeInteger) {
                    case 1: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusFail];
                    }
                    break;
                        break;
                    case 4: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusBegin];
                    }
                    break;
                    case 5: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusEnd];
                    }
                    break;
                    case 7: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusTimeout];
                    }
                    break;
                    default:
                        break;
                }
            } else if (type == 1) {
                //云录像下载
                switch (codeInteger) {
                    case 0: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusFail];
                    }
                    break;
                    case 1: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusBegin];
                    }
                    break;
                    case 2: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusEnd];
                    }
                    break;
                    case 5: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusCancle];
                    }
                    break;
                    case 6: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusSuspend];
                    }
                    break;
                    case 7: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusTimeout];
                    }
                    break;
                    case 9: {
                        LCVideotapeDownloadInfo *info = [self currentDownloadInfo];
                        if (info.donwloadStatus != LCVideotapeDownloadStatusPartDownload) {
                            [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusPartDownload];
                        }
                    }
                    break;
                    case 11: {
                        [self updateDownload:index Recieve:-1 Status:LCVideotapeDownloadStatusKeyError];
                    }
                    break;
                    default:
                        break;
                }
            } else {
                //其他错误暂不处理
            }
}
 
@end
 
@implementation LCVideotapeDownloadInfo
 
- (instancetype)init {
    if (self = [super init]) {
//取当前时间戳作为索引
        self.index = [[NSDate new] timeIntervalSince1970];
    }
    return self;
}
 
@end