// // 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=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)length123 { //[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_len99){ audio.Abuf_p=(audio.Abuf_p+1)%100; } else { audio.Abuf_len++; } } } } else{//视频数据 NSData *data1 = [[NSData alloc] initWithBytes:bb length:60+32]; // 音视频数据开始下标 BAGLEN = [Pub getShortFromByte:bb at:POS_blen + 2]; // 获取帧序号 63 iframe_index = [Pub getShortFromByte:bb at:POS_iframe_index]; // 获取帧长度 65 alen = [Pub getShortFromByte:bb at:POS_alen]; // 当前包号 71 bag_index = bb[POS_bag_index]&0xff ; // 总包数 69 bag_num = bb[POS_bag_num]&0xff; // 数据长度 73 blen = [Pub getShortFromByte:bb at:POS_blen]; 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