1038 lines
34 KiB
Objective-C
Executable File

//
// talk_Class.m
// myhome
//
// Created by user on 12-11-15.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "talk_Class.h"
#import "AppDelegate.h"
#import "Pub.h"
#import "UdpHelper.h"
#import "Call.h"
#import "Pub.h"
#import "Sformat.h"
//#import "mainViewController.h"
#define k
@implementation talk_Class{
NSData *adata;
int fno;
int send_num;
AppDelegate *app;
int iframe_index,alen,blen,bag_index,bag_num,BAGLEN;
}
@synthesize status;
@synthesize audio;
@synthesize arecord;
@synthesize iframe;
static bool isTalkStop;
static bool isVedioStop;
- (id)init {
app = [Pub getApp];
remotePort = 8302;
self.status = 0;
_audioEquMode = k8130;
[playAudio initAudioSession];
isTalkStop = YES;
/*
audio = [[playAudio alloc] init];
[NSThread detachNewThreadSelector:@selector(audio_open_thr) toTarget:self withObject:nil];
*/
//arecord = [[recordAudio alloc] init];
//arecord.audio_process = [[webrtc_audio_processing alloc] init];
//[NSThread detachNewThreadSelector:@selector(arecord_open_thr) toTarget:self withObject:nil];
iframe = [[IframeInfo alloc] init];
return self;
}
- (void)audioStartWithMode:(int)mod {
// app.audio_process = [[webrtc_audio_processing alloc] init];
// [app.audio_process WebRtcProcess_Init];
arecord = [[recordAudio alloc] init];
[arecord AudioRecordStartWithMode:mod];
//[arecord AudioRecordStart];
//f[arecord AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
audio = [[playAudio alloc] init];
[audio AudioPlayStartWithMode:mod];
isTalkStop = NO;
//arecord = [[recordAudio alloc] init];
//arecord.audio_process = [[webrtc_audio_processing alloc] init];
//[self arecord_open_thr];
}
- (void)audioEnd {
if(audio)
[audio AudioEnd];
audio = nil;
if(arecord)
[arecord AudioEnd];
arecord = nil;
// [app.audio_process WebRtcProcess_Free];
// app.audio_process = nil;
isTalkStop = YES;
isVedioStop = YES;
}
- (void)audio_open_thr {
[audio AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
[audio AudioEnd];
}
- (void)arecord_open_thr {
//int mod = kWebrtc;
//arecord = [[recordAudio alloc] init];
//[arecord AudioRecordStartWithMode:mod];
[arecord AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
[arecord AudioEnd];
}
- (void)connect_ini {
_connect_reg_time = [Sformat timestamp];
Byte connect_bb[512];
[self addHead:@"XXXCID" toByte:connect_bb];
connect_bb[6] = 150;
if(status==3){
connect_bb[6] = 152;
}
connect_bb[7] = 1;
connect_bb[8] = 9;
//本地
[self addEquid:remoteEquid addIp:remoteIp toByte:connect_bb at:9];
//远程
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:connect_bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:connect_bb at:33];
connect_data=[[NSData alloc] initWithBytes:connect_bb length:100];
}
- (void)connect {
// if ([Sformat timestamp] - _connect_reg_time >= 5000) {//5s没有保持连接命令
// if (status==3) {
// NSLog(@"对方掉线,停止监视:%ld",[Sformat timestamp] - _connect_reg_time);
// [self watchEnd];
// if([Pub getApp].callOut){
// [[Pub getApp].callOut UIClose];
// }
// return;
// } else if ((status==8)|(status==6)) {//|
// NSLog(@"对方掉线,停止对讲:%ld",[Sformat timestamp] - _connect_reg_time);
// [self talkEnd];
// if([Pub getApp].callOut){
// [[Pub getApp].callOut UIClose];
// }
// return;
// }
// }
//NSLog(@"connect remoteip=%@;port=%d",remoteIp,remotePort);
//NSLog(@"connect %@",[Sformat Hex:connect_data]);
[[Pub getApp].udp.udp sendData:connect_data
toHost:remoteIp
port:remotePort
withTimeout:-1
tag:0];
if([Pub getApp].callOut && (status==8 || status==3) ){
[[Pub getApp].callOut setTime:_talkOnTime];
}
}
- (void)WatchCallAnswer:(NSData *)data fromHost:(NSString *)host {
// NSLog(@"%s", __FUNCTION__);
// if (status==1) {
// status=3;
// //bb[6]=152;//监视
// [self connect_ini];
// timer = [NSTimer scheduledTimerWithTimeInterval:3
// target:self selector:@selector(connect)
// userInfo:nil repeats:YES];
// //打开播放界面
// //[self performSelectorOnMainThread:@selector(showplayer) withObject:nil waitUntilDone:YES];
// //tiaoshi[self showplayer];
// [player1 setviewsWithMode:_vedioCodecMode];
// //打开 视频解码,视频播放 线程
// } else if ((status==2)|(status==22)) {
// status=4;
// //bb[6]=150;//呼叫
// [self check_connect_ini];
// timer = [NSTimer scheduledTimerWithTimeInterval:1
// target:self selector:@selector(check_connect)
// userInfo:nil repeats:YES];
// [app ring];
// //tiaoshi[self showplayer];
// [player1 setviewsWithMode:_vedioCodecMode];
// } else {
// return;
// }
}
- (void)getdadong:(NSData *)data fromHost:(NSString *)host fromPort:(int)port {
remoteIp = [NSString stringWithFormat:@"%@", host];//[[NSString alloc]initWithFormat:host];
remotePort = port;
}
- (void)RecvNSReply:(NSData *)data fromHost:(NSString *)host fromPort:(int)port {
// Byte *rr = (Byte *)[data bytes];
//
// Byte *tempbb;
// NSString *tempS;
// NSData *tempData;
// int bblen=100;//512
// Byte bb[bblen];
// NSArray *tempA;
//
// tempS=@"XXXCID";
// tempData=[tempS dataUsingEncoding:NSUTF8StringEncoding];
// tempbb = (Byte *)[tempData bytes];
// for (int i=0;i<6;i++) {
// bb[i]=tempbb[i];
// }
// if (status==1) {
// remoteIp = [[NSString alloc]initWithFormat:@"%@",host];
// remotePort = port;
// NSLog(@"%@", remoteIp);
// bb[6]=152;//监视
// /*status=3;
// [self connect_ini];
// timer = [NSTimer scheduledTimerWithTimeInterval:3
// target:self selector:@selector(connect)
// userInfo:nil repeats:YES];
// //打开播放界面
// //[self performSelectorOnMainThread:@selector(showplayer) withObject:nil waitUntilDone:YES];
// [self showplayer];
// //打开 视频解码,视频播放 线程*/
// } else if (status==2) {
// status=22;
//
// remoteIp = [NSString stringWithFormat:@"%@", host];//[[NSString alloc]initWithFormat:host];
// remotePort = port;
// NSLog(@"%@", remoteIp);
// bb[6] = 150;//呼叫
//
// /*status=4;
// [self check_connect_ini];
// timer = [NSTimer scheduledTimerWithTimeInterval:1
// target:self selector:@selector(check_connect)
// userInfo:nil repeats:YES];
//
// */
// }
// else {
// return;
// }
// bb[7] = 1;
// bb[8] = 1;
// //bb[22] = 3;
// //本地
// tempS = app.localequid;
// tempData = [tempS dataUsingEncoding:NSUTF8StringEncoding];
// tempbb = (Byte *)[tempData bytes];
// memcpy(bb+9, tempbb, [tempData length]);
// for (int i=(int)[tempData length];i<20;i++) {
// bb[i+9] = 0;
// }
// ///
// tempA = [app.localip componentsSeparatedByString:@"."];
// for (int i=0;i<tempA.count;i++){
// bb[29+i] = [(NSString *)[tempA objectAtIndex:i] intValue];
// }
// //远程
// ///
// tempS = [app.equtype stringByAppendingString:app.equid];
// tempData = [tempS dataUsingEncoding:NSUTF8StringEncoding];
// tempbb = (Byte *)[tempData bytes];
// memcpy(bb+33, tempbb, [tempData length]);
// for (int i=(int)[tempData length]; i<20; i++) {
// bb[i+33] = 0;
// }
// ///
// //tempA = [app.localip componentsSeparatedByString:@"."];
// memcpy(bb+53, rr+55, 4);
//
// for (int i=0; i<4; i++) {
// bb[57+i] = 0;
// }
// bb[22] = 3;
// NSData *data2 = [[NSData alloc] initWithBytes:bb length:bblen];
// NSLog(@"=------------------=port=%d", remotePort);
//
// if (status==1) {
// [app senddataInThr:data2 toHost:remoteIp toPort:remotePort byPort:8302 at:6 setTag:0];
// }
// else{
// [app senddataInThr:data2 toHost:remoteIp toPort:remotePort byPort:8302 at:6 setTag:1];
// }
/*[app.udp2 sendData:data2
toHost:remoteIp
port:8302
withTimeout:-1
tag:0];*/
}
- (void)showplayer {
// //app.player1.hidden=YES;
// //myPlayer *player=[[myPlayer alloc]initWithTitle:@"编辑设备信息" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
// //[player show];
// //player1=[[player alloc] initWithFrame:CGRectMake(2,90,316,290)];
// player1 = [[player alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
// [player1 setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0]];
// //UINavigationController *nav = (UINavigationController *)app.window.rootViewController;
// UIViewController *view = [self getCurrentVC];//[[nav viewControllers] objectAtIndex:[nav viewControllers].count-1];
// [view.view addSubview:player1];
// //暂时 测试用
// //player1.player2 = [[videoPlayer alloc] initWithFrame:CGRectMake(0,0,320,480)];
// if (_vedioCodecMode == 2) { //for ffmpge decode
// player1.player2 = [[videoPlayer alloc] init];
// [player1.player2 initVideo];
// }
// //player1.player2.outputWidth = 426;
// //player1.player2.outputHeight = 320;
}
- (UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else
result = window.rootViewController;
return result;
}
- (void)addHead:(NSString *)head toByte:(Byte *)bb {
Byte *tempbb;
NSString *tempS;
NSData *tempData;
tempS = @"XXXCID";
tempData = [tempS dataUsingEncoding:NSUTF8StringEncoding];
tempbb = (Byte *)[tempData bytes];
memcpy(bb,tempbb,6);
}
- (void)addEquid:(NSString *)equid addIp:(NSString *)ip toByte:(Byte *)bb at:(int)pos {
Byte *tempbb;
NSString *tempS;
NSData *tempData;
NSArray *tempA;
//本地
tempS=equid;
tempData=[tempS dataUsingEncoding:NSUTF8StringEncoding];
tempbb = (Byte *)[tempData bytes];
memcpy(bb+pos, tempbb, [tempData length]);
for (int i=(int)[tempData length];i<20;i++) {
bb[i+pos]=0;
}
///
tempA=[ip componentsSeparatedByString:@"."];
for (int i=0;i<tempA.count;i++){
bb[i+pos+20]=[(NSString *)[tempA objectAtIndex:i] intValue];
}
}
- (NSString *)getIpFromBtye:(Byte *)bb At:(int)pos{
return [[NSString alloc] initWithFormat:@"%d.%d.%d.%d",bb[pos],bb[pos+1],bb[pos+2],bb[pos+3]];
}
- (NSString *)getIpv6FromBtye:(Byte *)bb At:(int)pos{
return [[NSString alloc] initWithFormat:@"%x:%x:%x:%x:%x:%x:%x:%x",[self getShortFromBtyeDesc:bb At:pos],[self getShortFromBtyeDesc:bb At:pos+2],[self getShortFromBtyeDesc:bb At:pos+4],[self getShortFromBtyeDesc:bb At:pos+6],[self getShortFromBtyeDesc:bb At:pos+8],[self getShortFromBtyeDesc:bb At:pos+10],[self getShortFromBtyeDesc:bb At:pos+12],[self getShortFromBtyeDesc:bb At:pos+14]];
}
- (int)getPortFromBtye:(Byte *)bb At:(int)pos{
return (bb[pos]&0xff)+((bb[pos+1]<<8)&0xff00);
}
- (int)getShortFromBtyeDesc:(Byte *)bb At:(int)pos{
return (bb[pos+1]&0xff)+((bb[pos]<<8)&0xff00);
}
- (void)watchByServer {
//[self status_ini];
remoteIp = [Pub getApp].sysinfo.server_wan;
remotePort = [Pub getApp].sysinfo.server_port;
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6]=152;
bb[7]=1;
bb[8]=1;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:bb at:33];
NSData *data = [[NSData alloc] initWithBytes:bb length:512];
//[app senddataInThr:data toHost:remoteIp byPort:8302];
[[Pub getApp].udp senddataInThr:data toHost:remoteIp toPort:remotePort at:15 setTag:0];
}
- (void)watch {
//[self status_ini];
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6]=152;
bb[7]=1;
bb[8]=1;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:bb at:33];
NSData *data = [[NSData alloc] initWithBytes:bb length:512];
//[app senddataInThr:data toHost:remoteIp byPort:8302];
[[Pub getApp].udp senddataInThr:data toHost:remoteIp toPort:remotePort at:15 setTag:0];
}
- (void)watchSucc:(Byte *)bb Len:(int)bblen Formip:(NSString *)ip fromport:(int)port{
if(status == 0) {//空闲
status = 3;
isVedioStop= NO;
_talkOnTime = [Sformat timestamp];
//定时报告
[self connect_ini];
[self connect];
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self selector:@selector(connect)
userInfo:nil repeats:YES];
}
}
- (void)watchEnd {
//[self status_ini];
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6]=152;
bb[7]=1;
bb[8]=30;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:bb at:33];
NSData *data = [[NSData alloc] initWithBytes:bb length:512];
//[app senddataInThr:data toHost:remoteIp byPort:8302];
[[Pub getApp].udp senddataInThr:data toHost:remoteIp toPort:remotePort at:3 setTag:0];
}
- (void)talkEnd {
if(status==6){
[[Pub getApp] ring_stop];
}
//[self status_ini];s
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6]=150;
bb[7]=1;
bb[8]=30;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:bb at:33];
NSData *data = [[NSData alloc] initWithBytes:bb length:512];
//NSLog(@"talkEnd remoteip=%@;port=%d",remoteIp,remotePort);
//NSLog(@"talkEnd %@",[Sformat Hex:data]);
[[Pub getApp].udp senddataInThr:data toHost:remoteIp toPort:remotePort at:3 setTag:0];
}
- (void)NSAsk:(Byte *)bb Len:(int)bblen Formip:(NSString *)ip fromport:(int)port {
//[self status_ini];
// [self addHead:@"XXXCID" toByte:bb];
// bb[6]=154;
// bb[7]=2;
// bb[32]=1;
// NSString *equid=[[NSString alloc]initWithFormat:@"%@0",[app.localequid substringWithRange:NSMakeRange(0,11)]];
// NSLog(@" shoudao解析地址:%@",equid);
// [self addEquid:equid addIp:app.localip toByte:bb at:33];
// NSData *data = [[NSData alloc] initWithBytes:bb length:512];
// [app.udp2 sendData:data
// toHost:ip
// port:port
// withTimeout:-1
// tag:0];
}
- (void)talkOn
{
if (status!=6)return;//若不是被叫未接听状态 返回
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6] = 150;
bb[7] = 1;
bb[8] = 6;
bb[22] = 6;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:bb at:33];
NSData *data = [[NSData alloc] initWithBytes:bb length:512];
[[Pub getApp].udp senddataInThr:data toHost:remoteIp toPort:remotePort at:6 setTag:0];
}
- (void)openDoor:(NSString *)pwd
{
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6]=150;
// if(status==0 ||status==3){
// bb[6]=152;
// }
bb[7]=1;
bb[8]=10;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
// [self addEquid:[Pub getApp].sysinfo.equid addIp:[Pub getLocalIP] toByte:bb at:33];
[Pub addGKB:pwd toByte:bb at:57];
NSData *data = [[NSData alloc] initWithBytes:bb length:512];
[[Pub getApp].udp senddataInThr:data toHost:remoteIp toPort:remotePort at:3 setTag:0];
}
- (void)talkOnByRemote {//对方接听
// if (status!=4)return;//若不是主叫未接听状态 返回
// self.isShort = YES;
// status = 5;
// _isShort = YES;
// [player1 setTag:5];
// [app ring_stop];
// //打开播放界面
// //[self performSelectorOnMainThread:@selector(showplayer) withObject:nil waitUntilDone:YES];
// //[self showplayer];
// //开启 音频解码 音频播放 线程
//
// [self audioStartWithMode:_audioEquMode];
// //[audio AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
// //[arecord AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
// [NSThread detachNewThreadSelector:@selector(send_audio_thr) toTarget:self withObject:nil];
}
- (void)talkOnRec:(Byte *)bb {//接听成功
NSLog(@"talkon");
if (status!=6)return;//若不是主叫未接听状态 返回
[[Pub getApp] ring_stop];
_isShort=false;
status=8;
NSLog(@"接听成功 _isShort = %d", _isShort);
_talkOnTime = [Sformat timestamp];
[[Pub getApp].callOut AnswerSucc];
[[Pub getApp].callOut setTime:_talkOnTime];
// [app ring_stop];
// [player1 setTag:8];
//定时报告
/*;
[self connect_ini];
[self connect];
timer = [NSTimer scheduledTimerWithTimeInterval:3
target:self selector:@selector(connect)
userInfo:nil repeats:YES];*/
//[self showplayer];
//开启 音频解码,视频解码,音频播放,视频播放 线程
#if 1
//[audio AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
//[arecord AudioStartWithRate:8000 setChannels:1 setSamples:8000 setBuf_samples:64 setVersion:self.Version];
//[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(audioStart) userInfo:nil repeats:NO];
[self audioStartWithMode:_audioEquMode];
[NSThread detachNewThreadSelector:@selector(send_audio_thr) toTarget:self withObject:nil];
#endif
}
- (void)audioStart {
[self audioStartWithMode:_audioEquMode];
[NSThread detachNewThreadSelector:@selector(send_audio_thr) toTarget:self withObject:nil];
}
- (BOOL)isCallMe:(Byte *)bb {
// [Pub getApp].sysinfo.equid
if([@"P18682150237" isEqualToString:[self getEquidFrombb:bb at:33]]){
return YES;
}
return NO;
}
- (BOOL)isToMe:(Byte *)bb {
if([[Pub getApp].sysinfo.equid isEqualToString:[self getEquidFrombb:bb at:33]]){
return YES;
}
return NO;
}
- (NSString *)getEquidFrombb:(Byte *)bb at:(int)pos {
NSString *equid = nil;
int equlen=8;
if (bb[pos]==77) {//M
equlen=8;
}
else if(bb[pos]==87) {//W
equlen=5;
}
else if(bb[pos]==72){//H
equlen=12;
}
else if(bb[pos]==83){//S
equlen=12;
}
else if(bb[pos]==0x50){//P
equlen=12;
}
else if(bb[pos]==0x54){//T
equlen=16;
}
else{
equlen=12;
}
Byte tempbb[equlen];
memcpy(tempbb, bb+pos, equlen);
NSData *aadata = [[NSData alloc] initWithBytes:tempbb length:equlen];
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
equid = [[NSString alloc] initWithData:aadata encoding:gbkEncoding];
return equid;
}
//对方呼叫
- (void)BeCall:(Byte *)bb Len:(int)bblen Formip:(NSString *)ip fromport:(int)port {
NSLog(@"Call------------------------");
//test close
if([self isCallMe:bb]==NO) return;
NSLog(@"isCallMe");
Byte bbsend[512];
if(status == 0) {//空闲
[[Pub getApp] ring];
remoteEquid = [self getEquidFrombb:bb at:9];
remoteIp = ip ;
remotePort = port;
status = 6;
isVedioStop= NO;
memcpy(bbsend, bb, 62);
bbsend[7] = 1;
bbsend[8] = 4;
NSLog(@"replay talk ip=%@;port=%d", ip, port);
[[Pub getApp].udp sendDataInMain:bbsend length:512 toHost:ip toPort:port];
memcpy(bbsend, bb, 62);
bbsend[7] = 1;
bbsend[8] = 5;
NSLog(@"replay talk ip=%@;port=%d", ip, port);
[[Pub getApp].udp sendDataInMain:bbsend length:512 toHost:ip toPort:port];
//定时报告
[self connect_ini];
[self connect];
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self selector:@selector(connect)
userInfo:nil repeats:YES];
[Pub getApp].callOut = [[Call alloc] init];
[Pub getApp].callOut.CallTag = @"BECALL";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Pub getApp].callOut];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[[Pub getApp].window.rootViewController presentViewController:nav animated:YES completion:nil];
}
else {//忙
if([remoteEquid isEqualToString:[self getEquidFrombb:bb at:9]]){
memcpy(bbsend,bb,62);
bbsend[7] = 1;
bbsend[8] = 4;
[[Pub getApp].udp sendDataInMain:bbsend length:100 toHost:ip toPort:port];
}
else{
NSLog(@"");
memcpy(bbsend,bb,62);
bbsend[7] = 1;
bbsend[8] = 2;
[[Pub getApp].udp sendDataInMain:bbsend length:100 toHost:ip toPort:port];
}
}
}
static char cnt;
- (void)send_audio_thr {
Byte bb[512];
[self addHead:@"XXXCID" toByte:bb];
bb[6]=150;
bb[7]=1;
bb[8]=7;
[self addEquid:remoteEquid addIp:remoteIp toByte:bb at:9];
// [self addEquid:app.sysinfo.equid addIp:app.sysinfo.equip toByte:bb at:33];
[self addEquid:@"P18682150237" addIp:@"192.168.9.7" toByte:bb at:33];
//if(true)return;
bb[57]=1;bb[58]=1;bb[59]=1;bb[60]=1;//时间戳
bb[61]=1;bb[62]=0;//音频
bb[63]=1;bb[64]=0;//帧序号
bb[65]=64;bb[66]=0;bb[67]=0;bb[68]=0;//
bb[69]=1;bb[70]=0;
bb[71]=1;bb[72]=0;
bb[73]=64;bb[74]=0;
bb[75]=176;bb[76]=4;
fno=0;
//NSLog(@"Abuf_len %d isShort %d, status %d", arecord.Abuf_len, isShort, status);
while((status==5)|(status==8)) {
@try{
if(arecord==nil)continue;
//NSLog(@"Abuf_len = %d", arecord.Abuf_len);
//经常崩溃 warning
if (arecord.Abuf_len>=5) {
//NSLog(@"_audioEquMode = %d", _audioEquMode);
if (false) {//_isShort
if (_audioEquMode == k8130) {
memcpy(bb+9, arecord.Abuf+64*arecord.Abuf_p, 64);
bb[8]=17;
adata = [[NSData alloc] initWithBytes:bb length:73];
//NSLog(@"------");
send_num++;
if (send_num>1) {
//[self performSelectorOnMainThread:@selector(sendDataInMain) withObject:nil waitUntilDone:YES];
}
}
if (_audioEquMode == kWebrtc) {
int pos = 240*arecord.Abuf_p;
int len = arecord.Abuf[pos];
memcpy(bb+21, &arecord.Abuf[pos+1], len);
bb[8] = 22;
bb[9] = 0x80;
bb[10] = 103;//80 67 7a d5 e5 72 77 7e 58 3c bf 1
bb[11] = 0x7a;
bb[12] = 0xd5;
bb[13] = (cnt++)&0xff;
bb[14] = 0x72;
bb[15] = 0x77;
bb[16] = 0x7e;
bb[17] = 0x58;
bb[18] = 0x3c;
bb[19] = 0xbf;
bb[20] = 0x1;
bb[13] = (cnt++)&0xff;
adata = [[NSData alloc] initWithBytes:bb length:21+len];
}
send_num ++;
if (send_num>1) {
//NSLog(@"record send ...");
//[self performSelectorOnMainThread:@selector(sendDataInMain) withObject:nil waitUntilDone:YES];
send_num--;
}
} else {
// if (self.Version==8130) {
// memcpy(bb+86, arecord.Abuf+64*arecord.Abuf_p, 64);
// } else {
// memcpy(bb+77, arecord.Abuf+64*arecord.Abuf_p, 64);
// }
//
// fno++;
// if (fno>=65536)fno=1;
// bb[64] = (fno&0x0000ff00)>>8;
// bb[63] = (fno&0x000000ff);
// adata = [[NSData alloc] initWithBytes:bb length:150];
// //NSLog(@"------");
// send_num++;
// if (send_num>1) {
// //[self performSelectorOnMainThread:@selector(sendDataInMain) withObject:nil waitUntilDone:YES];
// send_num--;
// }
bb[73]=0x40; bb[74]=1;
fno++;
if (fno>=65536)fno=1;
[Pub addShort:fno toByte:bb at:63];
for(int p=0;p<5;p++){
memcpy(bb+77+64*p, arecord.Abuf+64*arecord.Abuf_p, 64);
arecord.Abuf_len -=1 ;
arecord.Abuf_p = (arecord.Abuf_p+1)%ABUF_NUM;
}
if(app.TalkSendAudio)
[app.udp sendDataInMain:bb length:512 toHost:remoteIp toPort:remotePort];
//[self performSelectorOnMainThread:@selector(sendDataInMain) withObject:nil waitUntilDone:YES];
}
}
} @catch (NSException * ex) {
NSLog(@"%@",ex);
}
}
}
- (void)getImgData:(Byte *)bb len:(int)l {
//[player1 getImg:bb len:l];
}
- (void)getAVData:(Byte *)bb withLength:(unsigned long)len {
//NSLog(@"get audio data ...");
if (bb[61]==1){//音频数据
// audio.EuqAudioType = kWebrtc;
// audio.AudiodecodeType = kIsac;
// if ((audio.Abuf_len<ABUF_NUM) && (isTalkStop == NO)) {
// int pp = (audio.Abuf_p+audio.Abuf_len)%ABUF_NUM;
// audio.Abuf_len++;
// NSLog(@"audio get");
// audio.Abuf[160*pp] = blen-9-12;//首字节为数据长度
// memcpy(audio.Abuf+160*pp+1, bb+9+12, 159); //head 9byte | data 172byte() 数据
// }
//NSLog(@"音频数据");
if(status!=8 && status!=5)return;
if(audio){
//NSLog(@"getAVData %@",[Sformat Hex:[[NSData alloc] initWithBytes:bb length:len]]);
for(int p=0;p<5;p++){
memcpy(audio.Abuf + 64*((audio.Abuf_p+audio.Abuf_len)%100), bb+77+64*p, 64);
if (audio.Abuf_len>99){
audio.Abuf_p=(audio.Abuf_p+1)%100;
}
else {
audio.Abuf_len++;
}
}
}
}
else{//视频数据
BAGLEN = [Pub getShortFromByte:bb at:POS_blen + 2];
iframe_index = [Pub getShortFromByte:bb at:POS_iframe_index];
alen = [Pub getShortFromByte:bb at:POS_alen];
blen = [Pub getShortFromByte:bb at:POS_blen];
bag_index = bb[POS_bag_index]&0xff ;
bag_num = bb[POS_bag_num]&0xff;
if(iframe_index != iframe.iframe_index){
iframe = [[IframeInfo alloc] init];
iframe.iframe_index = iframe_index;
iframe.bag_num = bag_num;
iframe.cur_len = alen;
iframe.bb = (Byte*)malloc(alen);
}
iframe.bag_receive ++;
memcpy(iframe.bb + BAGLEN*(bag_index-FIRSTINDEX), bb + POS_data, blen);
if(iframe.bag_num == iframe.bag_receive){
//NSLog(@"播放第%d帧",iframe.iframe_index);
if([Pub getApp].callOut){
[[Pub getApp].callOut RefImg:[[NSData alloc] initWithBytes:iframe.bb length:iframe.cur_len]];
}
}
}
}
- (void)getAVData:(Byte *)bb length:(int)blen {
if (isVedioStop == NO) {
//[player1 getH264AVData:bb length:blen];
}
}
- (void)addbbToBuf:(Byte *)bb bblen:(int)len outp:(int)p bbnum:(int)num to:(Byte *)buf {
}
- (void)G711Decode:(Byte *)bb to:(Byte *)bb_de {
}
- (void)status_ini_onMain {
[self performSelectorOnMainThread:@selector(status_ini) withObject:nil waitUntilDone:YES];
}
- (void)NSToServer {
// if (status==1){//watch
// [self status_ini];
// [app alertInMain:@"找不到设备"];
// return;
// }
// NSLog(@"向服务器解析");
// Byte *tempbb;
// NSString *tempS;
//
// NSData *tempData;
// int bblen=100;//512
// Byte bb[bblen];
// //NSArray *tempA;
//
// tempS=@"XXXCID";
// tempData=[tempS dataUsingEncoding:NSUTF8StringEncoding];
// tempbb = (Byte *)[tempData bytes];
// for (int i=0;i<6;i++){
// bb[i]=tempbb[i];
// }
// bb[6]=155;//主机名yuanc解析
// bb[7]=1;
// //本地
// ///
// [self addEquid:app.localequid addIp:app.localip toByte:bb at:8];
// //远程
// [self addEquid:[app.equtype stringByAppendingString:app.equid] addIp:app.localip toByte:bb at:32];
// [self addInt:app.sysinfo._areaid toByte:bb at:52];
//
// app.talk_class.status=2;//等待呼叫
// NSData *data = [[NSData alloc] initWithBytes:bb length:bblen];
//
// NSString *temps=[app getServerIp];
// [app senddataInThr:data toHost:temps toPort:8302 byPort:8302 at:3 setTag:0];
}
- (void)addInt:(int)port toByte:(Byte *)bb at:(int)pos {
bb[pos+3]=(Byte)((port & 0xFF000000) >> 24);
bb[pos+2]= (Byte)((port & 0x00FF0000) >> 16);
bb[pos+1]=(Byte)((port & 0x0000FF00) >> 8);
bb[pos]= (Byte)((port & 0x000000FF) );
}
- (void)status_ini {
NSLog(@"status=%d返回初始状态",status);
if(timer){
[timer invalidate];
timer = nil;
}
if(status>0){
iframe = [[IframeInfo alloc] init];
if (status==8 || status==5) {
[self audioEnd];
}
else if(status==6){
[[Pub getApp] ring_stop];
}
}
// if (status==1){//等待监视
// [player1 close];
// //[player1 release];
// status=0;
//
// } else if ((status==2)|(status==22)) {//等待呼叫
// [player1 close];
// //[player1 release];
// status=0;
//
// } else if (status==3) {//监视中
// [timer invalidate];
// [player1 close];
// //[player1 release];
// status=0;
// //结束 视频编码,视频播放线程
// } else if (status==4) {//呼叫成功
// [timer invalidate];
// status=0;
// [player1 close];
// //[player1 release];
// [app ring_stop];
// } else if (status==5) {//主叫通话中
// [timer invalidate];
// [player1 close];
// //[player1 release];
// status=0;
// //结束 音频编码,音频播放,视频编码,视频播放线程
// [self audioEnd];
// //[audio AudioEnd];
// //[arecord AudioEnd];
//
// } else if (status==6) {//被呼叫
// [timer invalidate];
// status=0;
// [player1 close];
// //[player1 release];
// [app ring_stop];
// }
// else if (status==8) {//被叫通话中
// [timer invalidate];
// NSLog(@"被叫通话中");
// [player1 close];
// //[player1 release];
// status=0;
// //结束 音频编码,音频播放,视频编码,视频播放线程
// //[audio AudioEnd];
// [self audioEnd];
// //[arecord AudioEnd];
// }
/*AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];*/
status=0;
}
- (BOOL)isVideoPlaying {
// if (player1 == nil) {
// return NO;
// }
//
// return !player1.isExitH264Play;
return NO;
}
- (NSString *)getRemoteEqu{
return remoteEquid;
}
- (void)SetRemoteEqu:(NSString *)equid{
remoteEquid = equid;
}
- (NSString *)getRemoteIp{
return remoteIp;
}
- (void)SetRemoteIp:(NSString *)ip{
remoteIp = ip;
}
- (int)getRemotePort{
return remotePort;
}
- (void)SetRemotePort:(int)port{
remotePort = port;
}
@end