Files
yinmeng-ios/xplan-ios/Main/RTC/RtcImpl/ZegoRtcImpl.m

225 lines
6.2 KiB
Mathematica
Raw Normal View History

2021-10-21 15:10:53 +08:00
//
// ZegoRtcImpl.m
// xplan-ios
//
// Created by zu on 2021/10/20.
//
#import "ZegoRtcImpl.h"
#import <ZegoAudioRoom/ZegoAudioRoom.h>
2022-05-09 22:18:42 +08:00
@interface ZegoRtcImpl()<ZegoAudioRoomDelegate, ZegoAudioLivePublisherDelegate, ZegoSoundLevelDelegate, ZegoMediaPlayerEventDelegate>
2021-10-21 15:10:53 +08:00
@property (nonatomic, strong) ZegoAudioRoomApi *engine;
2022-05-09 22:18:42 +08:00
@property (nonatomic, strong) ZegoMediaPlayer *mediaPlayer;
///
@property (nonatomic,copy) NSString *filePath;
2021-10-21 15:10:53 +08:00
@end
@implementation ZegoRtcImpl
- (instancetype)initWithDelegate:(id<RtcImplDelegate>)delegate {
self = [super initWithDelegate:delegate];
if (self) {
#ifdef DEBUG
//
[ZegoAudioRoomApi setVerbose:YES];
[ZegoAudioRoomApi setUseTestEnv:YES];
#else
[ZegoAudioRoomApi setUseTestEnv:NO];
#endif
[ZegoAudioRoomApi setAudioDeviceMode:ZEGOAPI_AUDIO_DEVICE_MODE_GENERAL];
//48k
[ZegoAudioRoomApi setConfig:@"audio_encoder_samplerate=48000"];
_engine = [[ZegoAudioRoomApi alloc] initWithAppID:[self appID] appSignature:[self zegoAppSign]];
[_engine enableAEC:true];
[_engine enableAGC:true];
//
[_engine setManualPublish:true];
[_engine setManualPlay:true];
//delegate
[_engine setAudioRoomDelegate:self];
[_engine setAudioPublisherDelegate:self];
}
return self;
}
#pragma mark - RtcInterface impl
- (BOOL)joinChannel:(nonnull NSString *)channelId completion:(void (^ _Nullable)(void))completion {
2022-05-09 22:18:42 +08:00
//
self.mediaPlayer = [[ZegoMediaPlayer alloc] initWithPlayerType:MediaPlayerTypeAux];
[self.mediaPlayer setDelegate:self];
2021-10-21 15:10:53 +08:00
//
[[ZegoSoundLevel sharedInstance] setSoundLevelDelegate:self];
[[ZegoSoundLevel sharedInstance] setSoundLevelMonitorCycle:1200]; // [100, 3000] 200 ms
[[ZegoSoundLevel sharedInstance] startSoundLevelMonitor];
NSString *uid = [AccountInfoStorage instance].getUid;
//idname
[ZegoAudioRoomApi setUserID:uid userName:uid];
[self.engine setLatencyMode:ZEGOAPI_LATENCY_MODE_LOW3];
//
[self.engine setAudioBitrate:128000];
// (0: 使; 0: 使)
[ZegoAudioDevice enableAudioCaptureStereo:0];
//
[self.engine setAudioChannelCount:1];
//zego
int state = [self.engine loginRoom:channelId completionBlock:^(int errorCode) {
2021-11-01 18:25:13 +08:00
if (completion) {
completion();
}
2021-10-21 15:10:53 +08:00
}];
return state == 1;
}
2021-11-01 18:25:13 +08:00
- (BOOL)muteRemote:(BOOL)mute {
return [self.engine enableSpeaker:!mute];
}
2021-10-21 15:10:53 +08:00
- (void)broadcast:(BOOL)on {
2021-10-29 18:39:18 +08:00
if (on) {
[self.engine startPublish];
} else {
[self.engine stopPublish];
}
2021-10-21 15:10:53 +08:00
}
2021-11-01 18:25:13 +08:00
- (BOOL)muteLocal:(BOOL)mute {
return [self.engine enableMic:!mute];
}
- (void)exitChannel:(void (^ _Nullable)(void))completion {
2021-10-21 15:10:53 +08:00
[[ZegoSoundLevel sharedInstance] stopSoundLevelMonitor];
[self.engine logoutRoom];
if (completion) {
completion();
}
2021-10-21 15:10:53 +08:00
}
- (void)destory {
}
2022-05-09 22:18:42 +08:00
///
/// @param filePath
/// @param musicId TRTC
- (BOOL)playBackMusic:(NSString *)filePath musicId:(int)musicId {
[self.mediaPlayer stop];//need stop
if (filePath) {
self.filePath = filePath;
[self.engine muteAux:true];
[self.mediaPlayer start:filePath repeat:NO];
}
return YES;
}
///
/// @param state
- (BOOL)changePlayState:(BackMusicPlayState)state {
BOOL isPlaying = NO;
switch (state) {
case BackMusicPlayState_Stop:
{
[self.mediaPlayer stop];
[self.engine muteAux:false];
isPlaying = NO;
}
break;
case BackMusicPlayState_Pause:
{
[self.mediaPlayer pause];
isPlaying = NO;
}
break;
case BackMusicPlayState_Resume:
{
[self.mediaPlayer resume];
isPlaying = NO;
}
default:
break;
}
return isPlaying;
}
- (void)updateUserSound:(int)soundVol {
if (soundVol) {
[self.engine setCaptureVolume:soundVol];
}
}
- (void)updateMusicSound:(int)soundVol {
if (soundVol) {
[self.mediaPlayer setVolume:soundVol];
}
}
2021-12-06 20:43:58 +08:00
#pragma mark - ZegoAudioRoomDelegate
2021-10-29 18:39:18 +08:00
- (void)onStreamUpdated:(ZegoAudioStreamType)type stream:(ZegoAudioStream *)stream {
switch (type) {
case ZEGO_AUDIO_STREAM_ADD:
{
[self.engine startPlayStream:stream.streamID];
}
break;
case ZEGO_AUDIO_STREAM_DELETE:
{
[self.engine stopPlayStream:stream.streamID];
}
break;
default:
break;
}
2021-10-22 18:45:06 +08:00
}
2021-10-29 18:39:18 +08:00
- (void)onPublishStateUpdate:(int)stateCode streamID:(NSString *)streamID streamInfo:(NSDictionary *)info {
if (stateCode != 0) {
[self.engine restartPublishStream];
}
2021-10-22 18:45:06 +08:00
}
2021-10-29 18:39:18 +08:00
- (void)onSoundLevelUpdate:(NSArray<ZegoSoundLevelInfo *> *)soundLevels {
[self.delegate usersSpeaking:[self formatSpeakingUids:soundLevels]];
2021-10-22 18:45:06 +08:00
}
2021-10-29 18:39:18 +08:00
- (void)onCaptureSoundLevelUpdate:(ZegoSoundLevelInfo *)captureSoundLevel {
[self.delegate usersSpeaking:[self formatSpeakingUids:@[captureSoundLevel]]];
2021-10-22 18:45:06 +08:00
}
2022-05-09 22:18:42 +08:00
#pragma mark - ZegoMediaPlayerEventDelegate
///
- (void)onPlayEnd {
[self.delegate currentBackMusicPlayFinish:self.filePath];
}
2021-10-22 18:45:06 +08:00
#pragma mark - private method
- (NSMutableArray *)formatSpeakingUids:(NSArray<ZegoSoundLevelInfo *> *)soundLevels {
2021-10-21 15:10:53 +08:00
NSMutableArray *uids = [NSMutableArray array];
for (ZegoSoundLevelInfo *userInfo in soundLevels) {
NSString *uid = nil;
NSArray *strArray = [userInfo.streamID componentsSeparatedByString:@"-"];
if (strArray.count >= 2) {
uid = [strArray objectAtIndex:1];
if (userInfo.soundLevel > 0) {
[uids addObject:uid];
}
}
}
2021-10-22 18:45:06 +08:00
return uids;
2021-10-21 15:10:53 +08:00
}
- (uint32_t)appID {
return 1067458582;
}
- (NSData *)zegoAppSign {
Byte signkey[] = {0x2b,0x86,0x24,0xef,0xd9,0x96,0xf1,0x1c,0x6b,0xa2,0x28,0xc3,0xef,0x24,0xdd,0x64,0x2e,0xd7,0x33,0x3f,0x33,0x90,0x07,0x53,0xeb,0xd2,0xd2,0x4e,0xc5,0xed,0xfd,0x43};
return [NSData dataWithBytes:signkey length:32];
}
2022-05-09 22:18:42 +08:00
2021-10-21 15:10:53 +08:00
@end