JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
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
287
288
289
290
291
292
293
294
295
296
297
298
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "LCLivePreviewPresenter+Control.h"
#import "LCLivePreviewDefine.h"
#import <objc/runtime.h>
#import "PHAsset+Lechange.h"
#import "UIImage+LeChange.h"
#import "LCLivePreviewPresenter+SDKListener.h"
 
static const void *kLCLivePreviewPresenterSavePath = @"LCLivePreviewPresenterSavePath";
 
@interface LCLivePreviewPresenter ()
 
/// 存储地址
@property (strong, nonatomic) NSString *savePath;
 
@end
 
@implementation LCLivePreviewPresenter (Control)
 
- (void)onFullScreen:(LCButton *)btn {
    self.videoManager.isFullScreen = !self.videoManager.isFullScreen;
    self.videoManager.isLockFullScreen = NO;
    [UIDevice lc_setRotateToSatusBarOrientation];
}
 
- (void)onAudio:(LCButton *)btn {
    if (self.videoManager.isOpenAudioTalk) {
        //对讲与音频只能保持一者运行
        [LCProgressHUD showMsg:@"livepreview_result_close_talk_first".lc_T];
    }
    if (self.videoManager.isSoundOn) {
        //关闭声音
        [self.playWindow stopAudio];
    } else {
        //开启声音
        [self.playWindow playAudio];
    }
    self.videoManager.isSoundOn = !self.videoManager.isSoundOn;
}
 
- (void)onPlay:(LCButton *)btn {
    if (self.videoManager.isPlay) {
        [self stopPlay];
    } else {
        [self startPlay];
    }
    NSLog(@"%@", [NSThread currentThread]);
}
 
 
- (void)stopPlay {
    [self saveThumbImage];
    if (self.videoManager.isOpenCloudStage) {
        [self onPtz:nil];
    }
    [self hideVideoLoadImage];
    [self showPlayBtn];
    self.videoManager.isPlay = NO;
    self.videoManager.isSoundOn = YES;
    [self.talker stopTalk];
    self.videoManager.isOpenAudioTalk = NO;
    [self.playWindow stopRtspReal:NO];
    [self.playWindow stopAudio];
}
 
- (void)startPlay {
    [self showVideoLoadImage];
    [self hidePlayBtn];
    [self.playWindow stopRtspReal:NO];
    [self.playWindow stopAudio];
    
    LCOpenSDK_ParamReal *param = [[LCOpenSDK_ParamReal alloc]init];
    param.defiMode = DEFINITION_MODE_SD;
    param.isOpt = YES;
    param.accessToken = [LCApplicationDataManager token];
    param.deviceID = self.videoManager.currentDevice.deviceId;
    param.channel = [self.videoManager.currentChannelInfo.channelId integerValue];
    param.psk = self.videoManager.currentPsk;
    param.playToken = self.videoManager.currentDevice.playToken;
    
    NSInteger inde = [self.playWindow playRtspReal:param];
    if (inde != 0) {
        
    }
   
    self.videoManager.isPlay = YES;
}
 
- (void)onQuality:(LCButton *)btn {
    [self.playWindow stopRtspReal:NO];
    [self hideVideoLoadImage];
    NSInteger definition = 0;
    if (self.videoManager.isSD) {
        //HD
        definition = DEFINITION_MODE_HG;
        self.videoManager.isSD = NO;
    } else {
        //SD
        definition = DEFINITION_MODE_SD;
        self.videoManager.isSD = YES;
    }
    [self showVideoLoadImage];
    
    LCOpenSDK_ParamReal *param = [[LCOpenSDK_ParamReal alloc]init];
    param.defiMode = definition;
    param.isOpt = YES;
    
    param.accessToken = LCApplicationDataManager.token;
    param.deviceID = self.videoManager.currentDevice.deviceId;
    param.channel = [self.videoManager.currentChannelInfo.channelId integerValue];
    param.psk = self.videoManager.currentPsk;
    param.playToken = self.videoManager.currentDevice.playToken;
    
    [self.playWindow playRtspReal:param];
    
}
 
- (void)onPtz:(LCButton *)btn {
    if (self.videoManager.isOpenCloudStage) {
        //关闭云台
        [self.liveContainer hidePtz];
        self.videoManager.isOpenCloudStage = NO;
    } else {
        //打开云台
        [self.liveContainer showPtz];
        self.videoManager.isOpenCloudStage = YES;
    }
}
 
- (void)onSnap:(LCButton *)btn {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
                                                         NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
 
    NSString *myDirectory =
        [libraryDirectory stringByAppendingPathComponent:@"lechange"];
    NSString *picDirectory =
        [myDirectory stringByAppendingPathComponent:@"picture"];
 
    NSDateFormatter *dataFormat = [[NSDateFormatter alloc] init];
    [dataFormat setDateFormat:@"yyyyMMddHHmmss"];
    NSString *strDate = [dataFormat stringFromDate:[NSDate date]];
    NSString *datePath = [picDirectory stringByAppendingPathComponent:strDate];
    NSString *picPath = [datePath stringByAppendingString:@".jpg"];
    NSLog(@"test jpg name[%@]\n", picPath);
 
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSError *pErr;
    BOOL isDir;
    if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:myDirectory
              withIntermediateDirectories:YES
                               attributes:nil
                                    error:&pErr];
    }
    if (NO == [fileManage fileExistsAtPath:picDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:picDirectory
              withIntermediateDirectories:YES
                               attributes:nil
                                    error:&pErr];
    }
 
    [self.playWindow snapShot:picPath];
    UIImage *image = [UIImage imageWithContentsOfFile:picPath];
    NSURL *imgURL = [NSURL fileURLWithPath:picPath];
 
    [PHAsset deleteFormCameraRoll:imgURL success:^{
    } failure:^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
                       });
    }];
 
    [PHAsset saveImageToCameraRoll:image url:imgURL success:^(void) {
        dispatch_async(dispatch_get_main_queue(), ^{
                           [LCProgressHUD showMsg:@"livepreview_localization_success".lc_T];
                       });
    } failure:^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
                           [LCProgressHUD showMsg:@"livepreview_localization_fail".lc_T];
                       });
    }];
}
 
- (void)onAudioTalk:(LCButton *)btn {
    if (!self.videoManager.isOpenAudioTalk) {
        //对讲开启
//        [self.playWindow stopAudio];
        [self.talker stopTalk];
        [LCProgressHUD showHudOnView:nil];
        
        LCOpenSDK_ParamTalk *param = [[LCOpenSDK_ParamTalk alloc]init];
        param.isOpt = YES;
        
        param.accessToken = LCApplicationDataManager.token;
        param.deviceID = self.videoManager.currentChannelInfo.deviceId;
        param.channel = (self.videoManager.currentDevice.channels.count > 1 || [self.videoManager.currentDevice.catalog isEqualToString:@"NVR"]) ? [self.videoManager.currentChannelInfo.channelId intValue] : -1;
        param.psk = self.videoManager.currentPsk;
        param.playToken = self.videoManager.currentDevice.playToken;
        
        NSInteger result = [self.talker playTalk:param];
        if (result != 0) {
            //错误处理
        }
    } else {
        //结束对讲,此处result 返回永远返回0
        NSInteger result = [self.talker stopTalk];
        [LCProgressHUD showMsg:@"play_module_video_close_talk".lc_T];
        [self.talker setListener:nil];
        self.talker = nil;
    }
    //如果原来就开启声音,此处需要重新开启
    if (self.videoManager.isSoundOn) {
        [self.playWindow playAudio];
    }
    self.videoManager.isOpenAudioTalk = !self.videoManager.isOpenAudioTalk;
}
 
- (void)onLockFullScreen:(LCButton *)btn {
    self.videoManager.isLockFullScreen = !self.videoManager.isLockFullScreen;
}
 
- (void)onRecording:(LCButton *)btn {
    if (!self.videoManager.isOpenRecoding) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *libraryDirectory = [paths objectAtIndex:0];
 
        NSString *myDirectory = [libraryDirectory stringByAppendingPathComponent:@"lechange"];
        NSString *davDirectory = [myDirectory stringByAppendingPathComponent:@"video"];
 
        NSLog(@"test name[%@]\n", davDirectory);
        NSDateFormatter *dataFormat = [[NSDateFormatter alloc] init];
        [dataFormat setDateFormat:@"yyyyMMddHHmmss"];
        NSString *strDate = [dataFormat stringFromDate:[NSDate date]];
        NSString *datePath = [davDirectory stringByAppendingPathComponent:strDate];
        self.savePath = [datePath stringByAppendingFormat:@"_video_%@.mp4", @"asdasdadd"];
 
        NSFileManager *fileManage = [NSFileManager defaultManager];
        NSError *pErr;
        BOOL isDir;
        if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) {
            [fileManage createDirectoryAtPath:myDirectory
                  withIntermediateDirectories:YES
                                   attributes:nil
                                        error:&pErr];
        }
        if (NO == [fileManage fileExistsAtPath:davDirectory isDirectory:&isDir]) {
            [fileManage createDirectoryAtPath:davDirectory
                  withIntermediateDirectories:YES
                                   attributes:nil
                                        error:&pErr];
        }
        NSInteger result = [self.playWindow startRecord:self.savePath recordType:1];
        if (result != 0) {
            [LCProgressHUD showMsg:@"play_module_media_play_record_failed".lc_T];
        } else {
            [LCProgressHUD showMsg:@"play_module_video_media_start_record".lc_T];
        }
    } else {
        weakSelf(self);
        [self.playWindow stopRecord];
        //结束录像时,延时0.5秒进行保存,否则会引起保存失败
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSURL *davURL = [NSURL fileURLWithPath:weakself.savePath];
            //判断是否可以保存到相册
            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(weakself.savePath)) {
                [PHAsset deleteFormCameraRoll:davURL success:^{
                } failure:^(NSError *error) {
                }];
                [PHAsset saveVideoAtURL:davURL success:^(void) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                                       [LCProgressHUD showMsg:@"livepreview_localization_success".lc_T];
                                   });
                } failure:^(NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                                       [LCProgressHUD showMsg:@"livepreview_localization_fail".lc_T];
                                   });
                }];
            } else {
                [LCProgressHUD showMsg:@"livepreview_localization_fail".lc_T];
            }
        });
    }
    self.videoManager.isOpenRecoding = !self.videoManager.isOpenRecoding;
}
 
////关联存储地址对象
- (NSString *)savePath {
    return objc_getAssociatedObject(self, kLCLivePreviewPresenterSavePath);
}
 
- (void)setSavePath:(NSString *)savePath {
    objc_setAssociatedObject(self, kLCLivePreviewPresenterSavePath, savePath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
@end