Skip to content

Commit 09b15f3

Browse files
leyleoleyleo
authored andcommitted
add smart session init method, update demo
1 parent e1f91c1 commit 09b15f3

File tree

7 files changed

+206
-57
lines changed

7 files changed

+206
-57
lines changed

VideoCore.podspec

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Pod::Spec.new do |s|
77
intention of being an audio and video manipulation
88
and streaming pipeline for iOS.
99
DESC
10-
s.homepage = "https://github.com/jgh-/VideoCore"
10+
s.homepage = "https://github.com/leyleo/VideoCore"
1111
s.license = 'MIT'
12-
s.authors = { "James Hurley" => "jamesghurley@gmail.com" }
13-
s.source = { :git => "https://github.com/jgh-/VideoCore.git", :tag => s.version.to_s }
12+
s.authors = { "leyleo" => "liuley@163.com" }
13+
s.source = { :git => "https://github.com/leyleo/VideoCore.git"}
1414

1515
s.requires_arc = false
1616

17-
s.header_dir = 'videocore'
17+
s.header_dir = 'VideoCore'
1818
s.header_mappings_dir = '.'
1919

2020
s.source_files = [ 'mixers/**/*.h*', 'mixers/**/*.cpp', 'mixers/**/*.m*',
@@ -27,13 +27,14 @@ Pod::Spec.new do |s|
2727
'filters/**/*.cpp', 'filters/**/*.h*' ]
2828

2929
s.frameworks = [ 'VideoToolbox', 'AudioToolbox', 'AVFoundation', 'CFNetwork', 'CoreMedia',
30-
'CoreVideo', 'OpenGLES', 'Foundation', 'CoreGraphics' ]
30+
'CoreVideo', 'OpenGLES', 'Foundation', 'CoreGraphics','SystemConfiguration' ]
3131

3232
s.libraries = 'c++'
3333

3434
s.dependency 'boost', '~> 1.51.0'
3535
s.dependency 'glm', '~> 0.9.4.6'
3636
s.dependency 'UriParser-cpp', '~> 0.1.3'
37+
s.dependency 'Reachability'
3738

3839
s.xcconfig = { "HEADER_SEARCH_PATHS" => "${PODS_ROOT}/boost" }
3940

api/iOS/VCSimpleSession.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ typedef NS_ENUM(NSInteger, VCSessionState)
4545
VCSessionStateStarted,
4646
VCSessionStateEnded,
4747
VCSessionStateError
48-
4948
};
5049
/** 摄像头类型:VCCameraStateFront, VCCameraStateBack*/
5150
typedef NS_ENUM(NSInteger, VCCameraState)
@@ -142,6 +141,16 @@ typedef NS_ENUM(NSInteger, VCVideoQuality) {
142141

143142
@property (nonatomic, assign) id<VCSessionDelegate> delegate;
144143
#pragma mark - 初始化
144+
/**
145+
根据当前网络状态和状态栏的朝向自动初始化:
146+
147+
当前网络为Wifi条件时,视频为720p,当isPortrait=YES时采用 VCVideoQuality720x1280,NO时采用 VCVideoQuality1280x720;
148+
149+
当前为移动网络时,视频为480p,当isPortrait=YES时采用 VCVideoQuality480x640,NO时采用 VCVideoQuality640x480;
150+
151+
其他网络状态时,视频为360p,当isPortrait=YES时采用 VCVideoQuality360x480,NO时采用VCVideoQuality480x360.
152+
*/
153+
-(id)initWithCurrentStatus;
145154
// -----------------------------------------------------------------------------
146155
/**
147156
初始化推流Session

api/iOS/VCSimpleSession.mm

Lines changed: 98 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ of this software and associated documentation files (the "Software"), to deal
3333
#include <videocore/transforms/AspectTransform.h>
3434
#include <videocore/transforms/PositionTransform.h>
3535

36+
#import "Reachability.h"
37+
3638
#ifdef __APPLE__
3739
# include <videocore/mixers/Apple/AudioMixer.h>
3840
# include <videocore/transforms/Apple/MP4Multiplexer.h>
@@ -147,6 +149,18 @@ @interface VCSimpleSession()
147149
VCFilter _filter;
148150
}
149151
@property (nonatomic, readwrite) VCSessionState rtmpSessionState;
152+
/**
153+
根据当前网络状态自动初始化:
154+
155+
当前网络为Wifi条件时,视频为720p,当isPortrait=YES时采用 VCVideoQuality720x1280,NO时采用 VCVideoQuality1280x720;
156+
157+
当前为移动网络时,视频为480p,当isPortrait=YES时采用 VCVideoQuality480x640,NO时采用 VCVideoQuality640x480;
158+
159+
其他网络状态时,视频为360p,当isPortrait=YES时采用 VCVideoQuality360x480,NO时采用VCVideoQuality480x360.
160+
161+
@param isPortrait 屏幕宽高是否设置为竖屏方向
162+
*/
163+
-(id)initWithCurrentNet:(BOOL)isPortrait;
150164

151165
- (void) setupGraph;
152166

@@ -468,50 +482,80 @@ - (instancetype) initWithVideoSize:(CGSize)videoSize
468482
-(instancetype) initWithQuality:(VCVideoQuality)quality
469483
{
470484
if (self = [super init]) {
471-
CGSize videoSize;
472-
int bit = 0;
473-
int frameRate = 30;
474-
BOOL userOrientation = NO;
475-
switch (quality) {
476-
case VCVideoQuality1280x720:
477-
bit = 10506240;
478-
videoSize = CGSizeMake(1280, 720);
479-
break;
480-
case VCVideoQuality720x1280:
481-
bit = 10506240;
482-
videoSize = CGSizeMake(720, 1280);
483-
break;
484-
case VCVideoQuality640x480:
485-
bit = 3502080;
486-
videoSize = CGSizeMake(640, 480);
487-
break;
488-
case VCVideoQuality480x640:
489-
bit = 3502080;
490-
videoSize = CGSizeMake(480, 640);
491-
break;
492-
case VCVideoQuality480x360:
493-
bit = 700000;
494-
videoSize = CGSizeMake(480, 360);
495-
break;
496-
case VCVideoQuality360x480:
497-
bit = 700000;
498-
videoSize = CGSizeMake(360, 480);
499-
break;
500-
default:
501-
break;
502-
}
503-
if (bit > 0) {
504-
[self initInternalWithVideoSize:videoSize
505-
frameRate:frameRate
506-
bitrate:bit
507-
useInterfaceOrientation:userOrientation
508-
cameraState:VCCameraStateBack
509-
aspectMode:VCAspectModeFit];
485+
[self initInternalWithQuality:quality];
486+
}
487+
return self;
488+
}
489+
490+
-(id)initWithCurrentStatus
491+
{
492+
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
493+
BOOL isPortrait = UIInterfaceOrientationIsPortrait(orientation);
494+
return [self initWithCurrentNet:isPortrait];
495+
}
496+
497+
-(id)initWithCurrentNet:(BOOL)isPortrait
498+
{
499+
self = [super init];
500+
if (self) {
501+
NetworkStatus status = [self currentStatus];
502+
if (status == ReachableViaWiFi) {
503+
isPortrait ? [self initInternalWithQuality:VCVideoQuality720x1280]:[self initInternalWithQuality:VCVideoQuality1280x720];
504+
} else if (status == ReachableViaWWAN) {
505+
isPortrait ? [self initInternalWithQuality:VCVideoQuality640x480]:[self initInternalWithQuality:VCVideoQuality640x480];
506+
} else {
507+
isPortrait ? [self initInternalWithQuality:VCVideoQuality480x360]:[self initInternalWithQuality:VCVideoQuality480x360];
510508
}
509+
_useInterfaceOrientation = YES;
510+
_orientationLocked = YES;
511511
}
512512
return self;
513513
}
514514

515+
-(void) initInternalWithQuality:(VCVideoQuality)quality
516+
{
517+
CGSize videoSize;
518+
int bit = 0;
519+
int frameRate = 30;
520+
BOOL userOrientation = NO;
521+
switch (quality) {
522+
case VCVideoQuality1280x720:
523+
bit = 10506240;
524+
videoSize = CGSizeMake(1280, 720);
525+
break;
526+
case VCVideoQuality720x1280:
527+
bit = 10506240;
528+
videoSize = CGSizeMake(720, 1280);
529+
break;
530+
case VCVideoQuality640x480:
531+
bit = 3502080;
532+
videoSize = CGSizeMake(640, 480);
533+
break;
534+
case VCVideoQuality480x640:
535+
bit = 3502080;
536+
videoSize = CGSizeMake(480, 640);
537+
break;
538+
case VCVideoQuality480x360:
539+
bit = 700000;
540+
videoSize = CGSizeMake(480, 360);
541+
break;
542+
case VCVideoQuality360x480:
543+
bit = 700000;
544+
videoSize = CGSizeMake(360, 480);
545+
break;
546+
default:
547+
break;
548+
}
549+
if (bit > 0) {
550+
[self initInternalWithVideoSize:videoSize
551+
frameRate:frameRate
552+
bitrate:bit
553+
useInterfaceOrientation:userOrientation
554+
cameraState:VCCameraStateBack
555+
aspectMode:VCAspectModeFit];
556+
}
557+
}
558+
515559
- (void) initInternalWithVideoSize:(CGSize)videoSize
516560
frameRate:(int)fps
517561
bitrate:(int)bps
@@ -752,7 +796,21 @@ - (void)setFilter:(VCFilter)filterToChange {
752796
// Private Methods
753797
// -----------------------------------------------------------------------------
754798
#pragma mark - Private Methods
755-
799+
-(NetworkStatus)currentStatus
800+
{
801+
NSLog(@"current status: %ld",[[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]);
802+
return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus];
803+
}
804+
-(BOOL)isWifi
805+
{
806+
NSLog(@"iswifi: %ld",[[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]);
807+
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
808+
}
809+
-(BOOL)isNet
810+
{
811+
NSLog(@"iswifi: %ld",[[Reachability reachabilityForInternetConnection] currentReachabilityStatus]);
812+
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
813+
}
756814

757815
- (void) setupGraph
758816
{

docs/API.md

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,81 @@ pod 'VideoCore', :git=> 'https://github.com/leyleo/VideoCore.git'
1414
#### 方法二:使用lib库文件
1515
Todo
1616

17-
### API说明
17+
### Quick Start
18+
* 引入头文件
19+
20+
```
21+
#import "VCSimpleSession.h"
22+
```
23+
* 初始化
24+
25+
```
26+
self.session = [[VCSimpleSession alloc] initWithCurrentStatus]; // 基于当前网络状况和设备朝向自动初始化
27+
self.session.delegate = self; // 设置代理VCSessionDelegate,用于监听推流连接状态
28+
self.session.previewView.frame = CGRectMake(0, 0, self.session.videoSize.width, self.session.videoSize.height);
29+
[self.view addSubview:self.session.previewView]; // 添加预览视图
30+
```
31+
* 开始推流
32+
33+
```
34+
self.pushUrl = @"rtmp://xxxxx";
35+
[self.session startRtmpSessionWithURL:self.pushUrl];
36+
```
37+
* 结束推流
38+
39+
```
40+
[self.session endRtmpSession];
41+
```
42+
* 监听连接状态
43+
44+
需要添加`VCSessionDelegate`
45+
46+
```
47+
- (void) connectionStatusChanged: (VCSessionState) sessionState
48+
{
49+
switch(sessionState) {
50+
case VCSessionStateStarting:
51+
case VCSessionStateStarted:
52+
case VCSessionStatePreviewStarted:
53+
case VCSessionStateEnded:
54+
case VCSessionStateError:
55+
case VCSessionStateNone:
56+
}
57+
}
58+
```
59+
* 切换摄像头
60+
61+
```
62+
self.session.cameraState = VCCameraStateFront;
63+
self.session.cameraState = VCCameraStateBack;
64+
```
65+
* 添加滤镜效果
66+
67+
```
68+
[self.session setFilter: VCFilterNormal];
69+
[self.session setFilter: VCFilterGray];
70+
[self.session setFilter: VCFilterInvertColors];
71+
...
72+
```
73+
## 详细API说明
1874
`VCSimpleSession`类完成了音视频采集,并通过RTMP协议上推到服务端的功能。
1975
#### 初始化
20-
有五种初始化方法,用来设置视频的分辨率、相关品质及其他相关参数:
21-
76+
有六种初始化方法,用来设置视频的分辨率、相关品质及其他相关参数:
2277

2378
```
2479
方法一:
2580
/**
26-
初始化推流Session,推荐该种使用方式,默认配置了不同分辨率下的品质。
81+
根据当前网络状态和状态栏的朝向自动初始化,推荐该种使用方式,默认配置了不同分辨率下的品质:
82+
当前网络为Wifi条件时,视频为720p,当isPortrait=YES时采用 VCVideoQuality720x1280,NO时采用 VCVideoQuality1280x720;
83+
当前为移动网络时,视频为480p,当isPortrait=YES时采用 VCVideoQuality480x640,NO时采用 VCVideoQuality640x480;
84+
其他网络状态时,视频为360p,当isPortrait=YES时采用 VCVideoQuality360x480,NO时采用VCVideoQuality480x360.
85+
*/
86+
-(id)initWithCurrentStatus;
87+
```
88+
```
89+
方法二:
90+
/**
91+
初始化推流Session,默认配置了不同分辨率下的品质。
2792
@param quality 视频大小,包括以下几个选项
2893
VCVideoQuality1280x720,
2994
VCVideoQuality720x1280,
@@ -35,7 +100,7 @@ Todo
35100
-(instancetype) initWithQuality:(VCVideoQuality)quality;
36101
```
37102
```
38-
方法二
103+
方法三
39104
/**
40105
初始化推流Session
41106
@param videoSize 视频分辨率大小
@@ -48,7 +113,7 @@ Todo
48113
bitrate:(int)bps;
49114
```
50115
```
51-
方法三
116+
方法四
52117
/**
53118
初始化推流Session
54119
@param videoSize 视频分辨率大小
@@ -63,7 +128,7 @@ Todo
63128
useInterfaceOrientation:(BOOL)useInterfaceOrientation;
64129
```
65130
```
66-
方法四
131+
方法五
67132
/**
68133
初始化推流Session
69134
@param videoSize 视频分辨率大小
@@ -80,7 +145,7 @@ Todo
80145
cameraState:(VCCameraState) cameraState;
81146
```
82147
```
83-
方法五
148+
方法六
84149
/**
85150
初始化推流Session
86151
@param videoSize 视频分辨率大小

sample/SampleBroadcaster/Podfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ PODS:
1515
- boost/shared_ptr-includes (1.51.0)
1616
- boost/string_algorithms-includes (1.51.0)
1717
- glm (0.9.4.6)
18+
- Reachability (3.2)
1819
- UriParser-cpp (0.1.3)
1920
- VideoCore (0.3.2):
2021
- boost (~> 1.51.0)
2122
- glm (~> 0.9.4.6)
23+
- Reachability
2224
- UriParser-cpp (~> 0.1.3)
2325

2426
DEPENDENCIES:
@@ -31,7 +33,8 @@ EXTERNAL SOURCES:
3133
SPEC CHECKSUMS:
3234
boost: 6c5c8d8daef26ac83c1e1c00ad14de62b80161d7
3335
glm: e22c8d9df10404059b19c2952f9fa44d9a9f3056
36+
Reachability: 33e18b67625424e47b6cde6d202dce689ad7af96
3437
UriParser-cpp: cbbe00080ee432ec4833dca863a7fc83adc9b01a
35-
VideoCore: ee91bb8b71919ac7a1cbc752b72272fc53a0567f
38+
VideoCore: 01964901393405c013f314bb9041f11961263ccf
3639

3740
COCOAPODS: 0.38.2

0 commit comments

Comments
 (0)