Skip to content

Commit

Permalink
删除pcm vad,防止pcm漏掉,添加自动挂断回调
Browse files Browse the repository at this point in the history
Change-Id: I542db41ef624511fb17cc2855916156e6c84d8d0
  • Loading branch information
tonychanchen committed Nov 2, 2023
1 parent dd3214b commit 4905f8e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
28 changes: 27 additions & 1 deletion Source/LinkSDKDemo/Video/P2P/Controller/TIoTDemoVideoPushVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef NS_ENUM(NSInteger, TIotDemoDeviceDirection) {
};


@interface TIoTDemoVideoPushVC ()<H264EncoderDelegate,TIoTAACEncoderDelegate>
@interface TIoTDemoVideoPushVC ()<H264EncoderDelegate,TIoTAACEncoderDelegate, TIoTCoreXP2PBridgeDelegate>
@property (nonatomic, assign) CGRect screenRect;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *remoteVideoView; //录像中提示view
Expand Down Expand Up @@ -81,6 +81,30 @@ - (instancetype)init {
return self;
}

#pragma mark - TIoTCoreXP2PBridgeDelegate
- (NSString *)reviceDeviceMsgWithID:(NSString *)dev_name data:(NSData *)data {
NSString *deviceMsg = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"接收到设备主动发的消息==%@", deviceMsg);

return @"responseMES";
}

//下载完成事件
- (void)reviceEventMsgWithID:(NSString *)dev_name eventType:(XP2PType)eventType msg:(const char *)msg {
if (eventType == XP2PTypeClose) {

NSString *msgDetail = [NSString stringWithCString:msg encoding:[NSString defaultCStringEncoding]];
if ([msgDetail containsString:@"2000"]) {
[MBProgressHUD dismissInView:self.view];
[MBProgressHUD showError:@"语音对讲服务关闭"];
}
NSLog(@"msgDDD=%@",msgDetail);
}
}




- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
Expand All @@ -89,6 +113,8 @@ - (void)viewDidLoad {

_is_ijkPlayer_stream = YES;
//关闭日志
[TIoTCoreXP2PBridge sharedInstance].delegate = self;

[TIoTCoreXP2PBridge sharedInstance].logEnable = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];

Expand Down
2 changes: 0 additions & 2 deletions Source/LinkSDKDemo/Video/P2P/Controller/TIoTSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ - (void)cacheCurrentAudioSession {

//需要录音时,AudioSession的设置代码如下:
- (void)resumeRTCAudioSession {
return;
self.session = [AVAudioSession sharedInstance];
if (self.session.category != AVAudioSessionCategoryPlayAndRecord) {
[self cacheCurrentAudioSession]; //先缓存之前的
Expand All @@ -103,7 +102,6 @@ - (void)resumeRTCAudioSession {
}
//功能结束时重置audioSession,重置到缓存的audioSession设置
- (void)resetToCachedAudioSession {
return;
if (!cachedCategory) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ - (NSString *)reviceDeviceMsgWithID:(NSString *)dev_name data:(NSData *)data {
}

//下载完成事件
- (void)reviceEventMsgWithID:(NSString *)dev_name eventType:(XP2PType)eventType {
- (void)reviceEventMsgWithID:(NSString *)dev_name eventType:(XP2PType)eventType msg:(nonnull const char *)msg{
if (eventType == XP2PTypeDownloadEnd) {//下载完成的事件

if (self.downLoading) {
Expand Down
10 changes: 5 additions & 5 deletions Source/SDK/LinkVideo/FLV/TIoTAVCaptionFLV.mm
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ - (void)setPitch:(int)pitch {
ijk_soundtouch_handle = ijk_soundtouch_create(1.0, _pitch, tmpChannel, 16000);
}

static uint8_t trae_pcm_buffer[512];
//static uint8_t trae_pcm_buffer[512];
static uint8_t trae_aac_buffer[8192];
float indata[FRAME_LEN];
TPCircularBuffer aac_circularBuffer;
Expand All @@ -377,12 +377,12 @@ static void record_callback(uint8_t *buffer, int size, void *u)
{
// printf("pcm_size_callback: %d\n", size);
TIoTAVCaptionFLV *vc = (__bridge TIoTAVCaptionFLV *)(u);
memset(trae_pcm_buffer, 0, 512);
/*memset(trae_pcm_buffer, 0, 512);
UInt32 len = [vc.pcmRecord getData:&pcm_circularBuffer :trae_pcm_buffer :512];
if (len < 512) {
return;
}
/*
// check vad
int temp;
for(int i = 0; i< FRAME_LEN; i++) {
Expand All @@ -401,13 +401,13 @@ static void record_callback(uint8_t *buffer, int size, void *u)
if (!vc.isVadRecongize) {
return;
}
[vc.pcmRecord addData:&aac_circularBuffer :trae_pcm_buffer :512];
*/

//pcm=>aac
[vc.pcmRecord addData:&aac_circularBuffer :trae_pcm_buffer :512];
dispatch_async(vc.audioEncodeQueue, ^{
static int tmpChannelDataLen = 2048;//vc.pcmRecord.pcmStreamDescription.mChannelsPerFrame * 2048;
UInt32 aaclen = [vc.pcmRecord getData:&aac_circularBuffer :trae_aac_buffer :tmpChannelDataLen];
UInt32 aaclen = [vc.pcmRecord getData:&pcm_circularBuffer :trae_aac_buffer :tmpChannelDataLen];
if (aaclen < tmpChannelDataLen) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion Source/SDK/LinkVideo/TIoTCoreXP2PBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ extern NSNotificationName const TIoTCoreXP2PBridgeNotificationStreamEnd;

/*
* sdk 事件消息,事件对应类型与意义详见 XP2PType 类型说明
* msg 事件详情
*/
- (void)reviceEventMsgWithID:(NSString *)dev_name eventType:(XP2PType)eventType;
- (void)reviceEventMsgWithID:(NSString *)dev_name eventType:(XP2PType)eventType msg:(const char*) msg;
@end


Expand Down
37 changes: 17 additions & 20 deletions Source/SDK/LinkVideo/TIoTCoreXP2PBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ - (void)cancelTimer;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
id<TIoTCoreXP2PBridgeDelegate> delegate = [TIoTCoreXP2PBridge sharedInstance].delegate;
if ([delegate respondsToSelector:@selector(reviceEventMsgWithID:eventType:)]) {
[delegate reviceEventMsgWithID:DeviceName eventType:type];
if ([delegate respondsToSelector:@selector(reviceEventMsgWithID:eventType:msg:)]) {
[delegate reviceEventMsgWithID:DeviceName eventType:type msg:msg];
}
});
}
Expand All @@ -116,23 +116,25 @@ void XP2PDataMsgHandle(const char *idd, uint8_t* recv_buf, size_t recv_len) {
char *msg = (char *)recv_buf;
printf("device feedback ==> %s\n",msg);

NSString *DeviceName = [NSString stringWithCString:idd encoding:[NSString defaultCStringEncoding]]?:@"";
NSData *DeviceData = [NSData dataWithBytes:recv_buf length:recv_len];

NSString *response = @"{\"status\":0}"; //默认返回值

id<TIoTCoreXP2PBridgeDelegate> delegate = [TIoTCoreXP2PBridge sharedInstance].delegate;
if ([delegate respondsToSelector:@selector(reviceDeviceMsgWithID:data:)]) {
NSString *response = [delegate reviceDeviceMsgWithID:DeviceName data:DeviceData];

if (response) {
NSUInteger length = strlen(response.UTF8String);
char *response_msg = (char *)malloc(length + 1);
strncpy(response_msg, response.UTF8String, length);
response_msg[length] = '\0';

return response_msg;
NSString *DeviceName = [NSString stringWithCString:idd encoding:[NSString defaultCStringEncoding]]?:@"";
NSData *DeviceData = [NSData dataWithBytes:recv_buf length:recv_len];
NSString *res = [delegate reviceDeviceMsgWithID:DeviceName data:DeviceData];
if (res) {
response = res;
}
}
return NULL;

NSUInteger length = strlen(response.UTF8String);
char *response_msg = (char *)malloc(length + 1);
strncpy(response_msg, response.UTF8String, length);
response_msg[length] = '\0';

return response_msg;
}

typedef char *(*device_data_recv_handle_t)(const char *id, uint8_t *recv_buf, size_t recv_len);
Expand Down Expand Up @@ -357,7 +359,7 @@ - (void)setupAVAudioSession:(TIoTCoreAudioConfig *)audio_config {
}

- (void)sendVoiceToServer:(NSString *)dev_name channel:(NSString *)channel_number audioConfig:(TIoTCoreAudioConfig *)audio_config videoConfig:(TIoTCoreVideoConfig *)video_config {
[self setupAVAudioSession:audio_config];
// [self setupAVAudioSession:audio_config];
// NSString *audioFile = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"testVideoStreamfile.flv"];
// [[NSFileManager defaultManager] removeItemAtPath:audioFile error:nil];
// [[NSFileManager defaultManager] createFileAtPath:audioFile contents:nil attributes:nil];
Expand Down Expand Up @@ -465,11 +467,6 @@ - (XP2PErrCode)stopVoiceToServer {
[systemAvCapture stopCapture];

int errorcode = stopSendService(self.dev_name.UTF8String, nullptr);

AVAudioSession *avsession = [AVAudioSession sharedInstance];
[avsession setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[avsession setActive:YES error:nil];

return (XP2PErrCode)errorcode;
}

Expand Down

0 comments on commit 4905f8e

Please sign in to comment.