SellyCloudSDK_demo/Example/SellyCloudSDK/SCViewController.m

234 lines
10 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SCViewController.m
// SellyCloudSDK
//
// Created by Caleb on 07/07/2025.
// Copyright (c) 2025 Caleb. All rights reserved.
//
#import "SCViewController.h"
#import "Masonry/Masonry.h"
#import "SCLivePusherViewController.h"
#import <SellyCloudSDK/SellyCloudManager.h>
#import "SCVideoPlayerViewController.h"
#import "UIView+SellyCloud.h"
#import "SellyVideoCallViewController.h"
#import "SellyVideoCallConferenceController.h"
@interface SCViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *audioOnlySwi;
@property (weak, nonatomic) IBOutlet UITextField *maxBitrateTf;
@property (weak, nonatomic) IBOutlet UITextField *minBitrateTf;
@property (weak, nonatomic) IBOutlet UITextField *fpsTf;
@property (weak, nonatomic) IBOutlet UITextField *videoSizeTf;
@property (weak, nonatomic) IBOutlet UITextField *pushNameTf;
@property (weak, nonatomic) IBOutlet UITextField *playNameTf;
@property (weak, nonatomic) IBOutlet UISegmentedControl *seg;
@property (weak, nonatomic) IBOutlet UITextField *p2pChannelTf;
@property (weak, nonatomic) IBOutlet UITextField *conferenceChannelTf;
@property (nonatomic, assign) CGSize videoSize; //视频最终输出分辨率,可以随意设置,最好不要超过采集分辨率
@property (nonatomic, assign) SellyLiveVideoSessionPreset sessionPreset; //视频采集分辨率
@end
@implementation SCViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// int randomNumber = 100 + arc4random_uniform(900);
// self.p2pChannelTf.text = [NSString stringWithFormat:@"test%d",randomNumber];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.whiteColor;
self.title = @"首页";
const char *kiwiAppkey = "5XTXUZ/aqOwfjA4zQkY7VpjcNBucWxmNGY4vFNhwSM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9gc6e/2vt1emOsP3/ipHVJf16r/9S4+dGKIjPX6QcHIIL2AMu2Je07nPmEoz7KaeOShox4bG3puMQdkdQo6kRIFpUzwUty+4EWqHmyPHGkGGGfI8gj0EreiZwgVJmBQ/8S5wlK+iUp+TVeoXo=";
[SellyCloudManager.sharedInstance startWithKey:@"" appName:@"live"];
//初始化洋葱盾相关
[SellyCloudManager setKiwiAppKey:kiwiAppkey name:"123"];
int randomNumber = 100 + arc4random_uniform(900);
self.pushNameTf.text = [NSString stringWithFormat:@"test%d",randomNumber];
self.playNameTf.text = @"test";
self.fpsTf.text = @"18";
NSInteger defaultQuality = 360;
if(defaultQuality == 360) {
//360
self.maxBitrateTf.text = @"2000";
self.minBitrateTf.text = @"1000";
self.videoSizeTf.text = @"360*640";
self.videoSize = CGSizeMake(360, 640);
self.sessionPreset = SellyLiveVideoResolution640x360;
}
else if(defaultQuality == 540){
//540
self.maxBitrateTf.text = @"3500";
self.minBitrateTf.text = @"1000";
self.videoSizeTf.text = @"540*960";
self.videoSize = CGSizeMake(540, 960);
self.sessionPreset = SellyLiveVideoResolution960x540;
}
else if(defaultQuality == 720){
//720
self.maxBitrateTf.text = @"5000";
self.minBitrateTf.text = @"1000";
self.videoSizeTf.text = @"720*1280";
self.videoSize = CGSizeMake(720, 1280);
self.sessionPreset = SellyLiveVideoResolution1280x720;
}
self.seg.selectedSegmentIndex = 1;
//音视频聊天配置
self.p2pChannelTf.text = @"test123";
self.conferenceChannelTf.text = @"test1123";
[SellyRTCEngine.sharedEngine startUp];
SellyRTCEngine.sharedEngine.userId = self.pushNameTf.text;
SellyRTCEngine.sharedEngine.supportSimulcast = false;
}
- (IBAction)startLive:(id)sender {
SellyPlayerStreamInfo *streamInfo = SellyPlayerStreamInfo.new;
streamInfo.streamId = self.pushNameTf.text;
streamInfo.protocol = (self.seg.selectedSegmentIndex?@"rtc":@"rtmp");
SellyLiveVideoConfiguration *videoConfig = SellyLiveVideoConfiguration.defaultConfiguration;
videoConfig.videoBitRate = self.maxBitrateTf.text.integerValue*1000;
videoConfig.videoMinBitRate = self.minBitrateTf.text.integerValue*1000;
videoConfig.videoFrameRate = self.fpsTf.text.integerValue;
videoConfig.videoSize = self.videoSize;
videoConfig.sessionPreset = self.sessionPreset;
videoConfig.videoMaxKeyframeInterval = videoConfig.videoFrameRate*2; //2秒一个关键帧
videoConfig.autorotate = false;
videoConfig.outputImageOrientation = UIInterfaceOrientationPortrait;
SCLivePusherViewController *vc = SCLivePusherViewController.new;
vc.streamInfo = streamInfo;
vc.audioOnly = self.audioOnlySwi.on;
vc.videoConfig = videoConfig;
[self.navigationController pushViewController:vc animated:true];
}
- (IBAction)startPlay:(id)sender {
SellyPlayerStreamInfo *streamInfo = SellyPlayerStreamInfo.new;
streamInfo.streamId = self.playNameTf.text;
streamInfo.protocol = (self.seg.selectedSegmentIndex?@"rtc":@"rtmp");
SCVideoPlayerViewController *vc = SCVideoPlayerViewController.new;
vc.streamInfo = streamInfo;
[self.navigationController pushViewController:vc animated:true];
}
- (void)updateVideoSize:(NSString *)title {
NSInteger width = [[title componentsSeparatedByString:@"*"].firstObject integerValue];
NSInteger height = [[title componentsSeparatedByString:@"*"].lastObject integerValue];
self.videoSize = CGSizeMake(height, width);
}
- (IBAction)startP2p:(id)sender {
SellyRTCVideoConfiguration *config = SellyRTCVideoConfiguration.defaultConfig;
config.frameRate = [self.fpsTf.text integerValue];
config.maxBitrate = [self.maxBitrateTf.text integerValue]*1000;
config.minBitrate = [self.minBitrateTf.text integerValue]*1000;
SellyVideoCallViewController *vc = SellyVideoCallViewController.new;
vc.channelId = self.p2pChannelTf.text;
vc.videoConfig = config;
[self.navigationController pushViewController:vc animated:true];
}
- (IBAction)startConference:(id)sender {
SellyRTCVideoConfiguration *config = SellyRTCVideoConfiguration.defaultConfig;
config.frameRate = [self.fpsTf.text integerValue];
config.maxBitrate = [self.maxBitrateTf.text integerValue]*1000;
config.minBitrate = [self.minBitrateTf.text integerValue]*1000;
SellyVideoCallConferenceController *vc = SellyVideoCallConferenceController.new;
vc.channelId = self.conferenceChannelTf.text;
vc.videoConfig = config;
[self.navigationController pushViewController:vc animated:true];
}
// 阻止键盘出现
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == self.videoSizeTf) {
[self showVideoSize];
return false;
}
return true;
}
- (void)showVideoSize {
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"请选择分辨率" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertVc addAction:[UIAlertAction actionWithTitle:@"360*480" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.videoSizeTf.text = action.title;
self.sessionPreset = SellyLiveVideoResolution480x360;
[self updateVideoSize:action.title];
}]];
[alertVc addAction:[UIAlertAction actionWithTitle:@"480*480" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.videoSizeTf.text = action.title;
self.sessionPreset = SellyLiveVideoResolution480x480;
[self updateVideoSize:action.title];
}]];
[alertVc addAction:[UIAlertAction actionWithTitle:@"360*640" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.videoSizeTf.text = action.title;
self.sessionPreset = SellyLiveVideoResolution640x360;
[self updateVideoSize:action.title];
}]];
[alertVc addAction:[UIAlertAction actionWithTitle:@"480*640" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.videoSizeTf.text = action.title;
self.sessionPreset = SellyLiveVideoResolution640x480;
[self updateVideoSize:action.title];
}]];
[alertVc addAction:[UIAlertAction actionWithTitle:@"540*960" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.videoSizeTf.text = action.title;
self.sessionPreset = SellyLiveVideoResolution960x540;
[self updateVideoSize:action.title];
}]];
[alertVc addAction:[UIAlertAction actionWithTitle:@"720*1280" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.videoSizeTf.text = action.title;
self.sessionPreset = SellyLiveVideoResolution1280x720;
[self updateVideoSize:action.title];
}]];
[alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertVc animated:true completion:nil];
}
- (IBAction)uploadLog:(id)sender {
[SellyCloudManager uploadLog:^(NSString * _Nullable url, NSError * _Nullable error) {
if (error) {
[self.view showToast:error.localizedDescription];
}
else {
[UIPasteboard generalPasteboard].string = url;
[self.view showToast:@"日志上传成功,谢谢您的反馈。\n日志url已复制到剪贴板可以粘贴之后发给别人哦"];
}
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.view endEditing:true];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end