萤石云 iOSSDK,移植跨平台相关工程
JLChen
2021-04-19 b7fa25faef4decf509a998ae861f059896ea6093
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
//
//  EZVideoTalkViewcontroller.m
//  EZBAVDemo
//
//  Created by kanhaiping on 2018/6/26.
//  Copyright © 2018年 hikvision. All rights reserved.
//
 
#import "EZVideoTalkViewcontroller.h"
#import "EZVideoTalkSDK.h"
#import "EZVideoTalkParam.h"
#import "UIView+Toast.h"
#import <AVFoundation/AVFoundation.h>
#import "EZOpenSDK.h"
#import "EZDeviceInfo.h"
#import "GlobalKit.h"
#import "EzvizWatchServerInfo.h"
#import "EZTokenKit.h"
 
@interface EZVideoTalkViewcontroller ()<EZVideoTalkSDKDelegate>
@property (nonatomic, assign) BOOL bEverOpenSound;
@property (nonatomic, strong) EZVideoTalkSDK *client;
 
@property (weak, nonatomic) IBOutlet UIView *localView;
@property (weak, nonatomic) IBOutlet UIView *remoteView;
@property (weak, nonatomic) IBOutlet UITextField *roomIDTextField;
 
@property (nonatomic, strong) EzvizWatchServerInfo *watchServerInfo;
 
@end
 
@implementation EZVideoTalkViewcontroller
 
+ (EZVideoTalkViewcontroller *) videoTalkVC {
    
    EZVideoTalkViewcontroller *vc = [[NSBundle mainBundle] loadNibNamed:@"EZVideoTalkViewcontroller" owner:nil options:nil].firstObject;
    return vc;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    if (self.view.gestureRecognizers.count <= 0) {
        [self.view addGestureRecognizer:gesture];
    }
    
    self.title = @"Video Talk";
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopTalk) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [self getWatchInfo];
}
 
- (void)viewWillDisappear:(BOOL)animated  {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if (self.client != nil) {
        [self stopTalk];
    }
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
#pragma mark Actions
 
- (IBAction)createRoomAction:(id)sender {
    
    [self startTalkWithRoomID:0];
}
 
- (IBAction)enterRoomAction:(id)sender {
    
    if (self.roomIDTextField.text.length <= 0 || [self.roomIDTextField.text integerValue] <= 0) {
        [self showMessage:@"请输入正确的房间号"];
        return;
    }
    
    [self startTalkWithRoomID:(int32_t)[self.roomIDTextField.text integerValue]];
    
    [self showMessage:@"加入房间"];
}
 
- (IBAction)leaveRoomAction:(id)sender {
    
    [self stopTalk];
    
    [self showMessage:@"已离开房间"];
}
 
- (void)tapAction:(id)sender {
    
    [self.roomIDTextField resignFirstResponder];
}
 
#pragma mark Helper
 
- (void)showMessage:(NSString *)message
{
    [self.view makeToast:message duration:2.0 position:CSToastPositionCenter];
}
 
#pragma mark BAV API
 
- (void)getWatchInfo {
    
    __weak typeof(self) weakSelf = self;
    [EZOpenSDK requestGetWatchServerInfo:self.deviceInfo.deviceSerial completion:^(id watchServerInfo, NSError *error) {
        
        weakSelf.watchServerInfo = watchServerInfo;
    }];
}
 
- (void)startTalkWithRoomID:(int32_t)roomID
{
    if (self.client != nil) {
        [self.client stop];
        self.client = nil;
    }
    
    EZVideoTalkParam *param = [EZVideoTalkParam new];
    param.iCltRole = roomID > 0 ? 1 : 0;
    param.iCltType = 1;
    param.iOtherCltType = 1;
//    param.iReason = 101;
    param.iStsPort = self.watchServerInfo.port;
    param.iRoomId = roomID > 0 ? roomID : 0;
    param.szOterId = self.deviceInfo.deviceSerial;
    param.szSelfId = @"1234567891";
    param.szAuthToken = [EZTokenKit sharedKit].token;
    param.szStsAddr = self.watchServerInfo.domain;
    
    [EZVideoTalkSDK setDebugVideoLog:YES];
    [EZVideoTalkSDK setDebugLogEnable:YES withLogCallback:^(NSString *logStr) {
        NSLog(@"%@", logStr);
    }];
    
    _client = [[EZVideoTalkSDK alloc] initWithParam:param localWindow:self.localView remoteWindow:self.remoteView];
    _client.delegate = self;
 
    //    [_client startWithType:EZVideoTalkCaptureVideo | EZVideoTalkCaptureAudio];
        [_client startWithType:EZVideoTalkCaptureAudio];
}
 
- (void)stopTalk
{
    [self.client stop];
    self.client = nil;
}
 
#pragma mark EZVideoTalkSDKDelegate
 
- (void)videoTalk:(EZVideoTalkSDK *)client didReceivedError:(int32_t)errorCode
{
    NSLog(@"%s recived errorcode:%d", __func__, errorCode);
 
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showMessage:[NSString stringWithFormat:@"BAVClient Error:%d",errorCode]];
    });
    
    [self stopTalk];
}
 
- (void)videoTalk:(EZVideoTalkSDK *)client didReceivedMessage:(EZVideoTalkMessageType)messageCode msg:(NSString *)msg
{
    NSLog(@"%s recived messagecode:%lu ,msg:%@", __func__, (unsigned long)messageCode, msg);
 
    dispatch_async(dispatch_get_main_queue(), ^{
 
        if (messageCode == EZVideoTalkMessageRoomCreated) {
 
            [self showMessage:[NSString stringWithFormat:@"已创建房间:%d", self.client.roomID]];
             self.roomIDTextField.text = [NSString stringWithFormat:@"%d", self.client.roomID];
        }
        else if (messageCode == EZVideoTalkMessageStartInputData) {
 
            [self showMessage:[NSString stringWithFormat:@"开始推流"]];
        }
    });
}
 
- (void)videoTalk:(EZVideoTalkSDK *)client didReceivedBavClientLogMsg:(NSString *)msg {
    
    NSLog(@"%s recived msg:%@", __func__, msg);
}
 
- (void)videoTalk:(EZVideoTalkSDK *)client playDelayTime:(int32_t)delayTime {
    
    NSLog(@"%s delayTime:%d", __func__, delayTime);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showMessage:[NSString stringWithFormat:@"播放卡顿-delayTime:%d", delayTime]];
    });
}
 
- (void)videoTalk:(EZVideoTalkSDK *)client playDelayTimeStatistics:(double)delayTimeStat {
    
    NSLog(@"%s delayTimeStat:%f", __func__, delayTimeStat);
}
 
- (void)videoTalk:(EZVideoTalkSDK *)client didDisplayWidth:(int32_t)width height:(int32_t)height {
    
    NSLog(@"%s", __func__);
    if (!self.bEverOpenSound) {
        self.bEverOpenSound = YES;
        [self.client openSound:YES];
    }
}
 
 
 
@end