JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
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
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "LCLivePreviewPresenter+SDKListener.h"
#import "UIImageView+Surface.h"
#import "LCLivePreviewPresenter+Control.h"
 
@implementation LCLivePreviewPresenter (SDKListener)
 
//videos
 
- (void)onReceiveData:(NSInteger)len Index:(NSInteger)index {
    NSLog(@"");
}
 
- (void)onPlayerResult:(NSString *)code Type:(NSInteger)type Index:(NSInteger)index {
    // play
    weakSelf(self);
    NSLog(@"LIVE_PLAY-CODE:%@,TYPE:%ld", code, type);
    dispatch_async(dispatch_get_main_queue(), ^{
        if (99 == type) {
            if ([code isEqualToString:@"-1000"]) {
                //请求超时处理
                self.videoManager.isPlay = NO;
//                [LCProgressHUD showMsg:[NSString stringWithFormat:@"mobile_common_net_fail".lc_T, [code integerValue]]];
                [self showErrorBtn];
            } else if (![code isEqualToString:@"0"]) {
                //业务请求失败处理
                self.videoManager.isPlay = NO;
//                [LCProgressHUD showMsg:[NSString stringWithFormat:@"mobile_common_net_fail".lc_T, [code integerValue]]];
                [self showErrorBtn];
            } else {
                //成功
            }
        }
        if (type == 5) {
            //不处理
            if ([code integerValue] == STATE_DHHTTP_KEY_ERROR) {
                [self showErrorBtn];
                if (![self.videoManager.currentPsk isEqualToString:self.videoManager.currentDevice.deviceId]) {
                    //自定义id时先改成默认的设备ID重试
                    self.videoManager.currentPsk = @"";
                    [self hideErrorBtn];
                    [self onPlay:nil];
                } else {
                    [self showPSKAlert];
                }
            }
            if ([code integerValue] != 0 && [code integerValue] != STATE_DHHTTP_OK) {
                [self showErrorBtn];
            }
        }
        if (type == 0) {
            self.videoManager.playStatus = [code integerValue];
            if ([RTSP_Result_String(STATE_RTSP_DESCRIBE_READY) isEqualToString:code]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                });
                return;
            }
            if ([RTSP_Result_String(STATE_RTSP_PLAY_READY) isEqualToString:code]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                });
                return;
            }
            if ([RTSP_Result_String(STATE_RTSP_KEY_MISMATCH) isEqualToString:code]) {
                [weakself showErrorBtn];
                [weakself showPSKAlert];
            } else {
                [weakself showErrorBtn];
            }
        }
    });
    return;
}
 
- (void)onPlayBegan:(NSInteger)index {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.videoManager.playStatus = 1001;
        [self saveThumbImage];
        [self hideVideoLoadImage];
    });
}
 
//audio
- (void)onTalkResult:(NSString *)error TYPE:(NSInteger)type
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"开启对讲回调error = %@, type = %ld", error, (long)type);
        weakSelf(self);
        [LCProgressHUD hideAllHuds:nil];
        if (99 == type) {   //网络请求失败
            dispatch_async(dispatch_get_main_queue(), ^{
                weakself.videoManager.isOpenAudioTalk = NO;
                [LCProgressHUD showMsg:@"play_module_video_preview_talk_failed".lc_T];
            });
            return;
        }
        if (nil != error && [RTSP_Result_String(STATE_RTSP_DESCRIBE_READY) isEqualToString:error]) {
            dispatch_async(dispatch_get_main_queue(), ^{
            });
            return;
        }
        if (nil != error && [RTSP_Result_String(STATE_RTSP_PLAY_READY) isEqualToString:error]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //对讲连接成功建立
                self.videoManager.isOpenAudioTalk = YES;
                [LCProgressHUD showMsg:@"device_mid_open_talk_success".lc_T];
            });
            return;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [LCProgressHUD showMsg:@"play_module_video_preview_talk_failed".lc_T];
            weakself.videoManager.isOpenAudioTalk = NO;
        });
    });
}
 
- (void)saveThumbImage {
    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];
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIImageView new] lc_storeImage:image ForDeviceId:self.videoManager.currentDevice.deviceId ChannelId:self.videoManager.currentChannelInfo.channelId];
    });
}
 
@end