Skip to content

Commit

Permalink
集成视频推流
Browse files Browse the repository at this point in the history
  • Loading branch information
alinTest1234 committed Jul 8, 2016
1 parent e8101f5 commit af9e31e
Show file tree
Hide file tree
Showing 1,269 changed files with 48,034 additions and 4,514 deletions.
24 changes: 12 additions & 12 deletions MiaowShow/MiaowShow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BB219BC81D22D06C00AF44C1 /* ALinUserView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB219BC71D22D06C00AF44C1 /* ALinUserView.xib */; };
BB219BCB1D22E4C500AF44C1 /* NSObject+ALinHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = BB219BCA1D22E4C500AF44C1 /* NSObject+ALinHUD.m */; };
BB4980531D23FDC20078D725 /* ALinLiveViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = BB7207771D1BE2C2007930D4 /* ALinLiveViewCell.m */; };
BB5956C41D2FF6EE004C125B /* IJKMediaFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BB5956C31D2FF6EE004C125B /* IJKMediaFramework.framework */; };
BB7207751D1BDBDD007930D4 /* ALinLiveCollectionViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BB7207741D1BDBDD007930D4 /* ALinLiveCollectionViewController.m */; };
BB72077B1D1BE542007930D4 /* ALinLiveFlowLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = BB72077A1D1BE542007930D4 /* ALinLiveFlowLayout.m */; };
BB87BC401D25A3D4000B084D /* ALinLiveEndView.m in Sources */ = {isa = PBXBuildFile; fileRef = BB87BC3F1D25A3D4000B084D /* ALinLiveEndView.m */; };
Expand All @@ -34,7 +35,6 @@
BBE540F01D0F26F300BF723E /* login_video.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = BBE540EC1D0F26F300BF723E /* login_video.mp4 */; };
BBE540F11D0F26F300BF723E /* loginmovie.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = BBE540ED1D0F26F300BF723E /* loginmovie.mp4 */; };
BBE540F61D0F274400BF723E /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = BBE540F51D0F274400BF723E /* libz.tbd */; };
BBEBC2821D2BB76000A50F1D /* IJKMediaFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BBEBC2811D2BB76000A50F1D /* IJKMediaFramework.framework */; };
C70620BE1D12381A00DA3B9B /* UIImageView+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = C70620BD1D12381A00DA3B9B /* UIImageView+Extension.m */; };
C72A3F291D0F939D002FC3F5 /* UIViewController+SLHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = C72A3F281D0F939D002FC3F5 /* UIViewController+SLHUD.m */; };
C72A3F2D1D0F93CE002FC3F5 /* UIView+SLExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = C72A3F2C1D0F93CE002FC3F5 /* UIView+SLExtension.m */; };
Expand Down Expand Up @@ -98,6 +98,7 @@
BB219BC71D22D06C00AF44C1 /* ALinUserView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ALinUserView.xib; sourceTree = "<group>"; };
BB219BC91D22E4C500AF44C1 /* NSObject+ALinHUD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+ALinHUD.h"; sourceTree = "<group>"; };
BB219BCA1D22E4C500AF44C1 /* NSObject+ALinHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+ALinHUD.m"; sourceTree = "<group>"; };
BB5956C31D2FF6EE004C125B /* IJKMediaFramework.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = IJKMediaFramework.framework; sourceTree = "<group>"; };
BB7207731D1BDBDD007930D4 /* ALinLiveCollectionViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALinLiveCollectionViewController.h; sourceTree = "<group>"; };
BB7207741D1BDBDD007930D4 /* ALinLiveCollectionViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ALinLiveCollectionViewController.m; sourceTree = "<group>"; };
BB7207761D1BE2C2007930D4 /* ALinLiveViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALinLiveViewCell.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -140,7 +141,6 @@
BBE540EC1D0F26F300BF723E /* login_video.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = login_video.mp4; sourceTree = "<group>"; };
BBE540ED1D0F26F300BF723E /* loginmovie.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = loginmovie.mp4; sourceTree = "<group>"; };
BBE540F51D0F274400BF723E /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
BBEBC2811D2BB76000A50F1D /* IJKMediaFramework.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = IJKMediaFramework.framework; sourceTree = "<group>"; };
C70620BC1D12381A00DA3B9B /* UIImageView+Extension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+Extension.h"; sourceTree = "<group>"; };
C70620BD1D12381A00DA3B9B /* UIImageView+Extension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+Extension.m"; sourceTree = "<group>"; };
C72A3F241D0F90F5002FC3F5 /* PrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = "<group>"; };
Expand Down Expand Up @@ -215,7 +215,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
BBEBC2821D2BB76000A50F1D /* IJKMediaFramework.framework in Frameworks */,
BB5956C41D2FF6EE004C125B /* IJKMediaFramework.framework in Frameworks */,
BBE540F61D0F274400BF723E /* libz.tbd in Frameworks */,
70D249A48322E2E32E1F0507 /* libPods-MiaowShow.a in Frameworks */,
);
Expand Down Expand Up @@ -256,6 +256,14 @@
name = Frameworks;
sourceTree = "<group>";
};
BB5956C21D2FF6EE004C125B /* Frameworks */ = {
isa = PBXGroup;
children = (
BB5956C31D2FF6EE004C125B /* IJKMediaFramework.framework */,
);
path = Frameworks;
sourceTree = "<group>";
};
BB9F77331D116A62007BB373 /* View */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -389,7 +397,7 @@
BBE540E31D0F26F300BF723E /* Classes */ = {
isa = PBXGroup;
children = (
BBEBC2801D2BB76000A50F1D /* Frameworks */,
BB5956C21D2FF6EE004C125B /* Frameworks */,
C769A8A51D29FCB2001EFB5C /* Profile */,
C757830C1D0FB31700CD856B /* Network */,
C75783041D0FA60500CD856B /* Other */,
Expand Down Expand Up @@ -442,14 +450,6 @@
path = Resource;
sourceTree = "<group>";
};
BBEBC2801D2BB76000A50F1D /* Frameworks */ = {
isa = PBXGroup;
children = (
BBEBC2811D2BB76000A50F1D /* IJKMediaFramework.framework */,
);
path = Frameworks;
sourceTree = "<group>";
};
C70620BB1D12380500DA3B9B /* UIImageView */ = {
isa = PBXGroup;
children = (
Expand Down
258 changes: 201 additions & 57 deletions MiaowShow/MiaowShow/Classes/ShowTime/Controller/ShowTimeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,62 @@
//

#import "ShowTimeViewController.h"
#import "ALinGPUBeautifyFilter.h"
#import "ALinH264Encoder.h"
#import <AVFoundation/AVFoundation.h>
#import <GPUImage.h>
#import <LFLiveKit.h>

#define h264outputWidth 1280
#define h264outputHeight 720

@interface ShowTimeViewController () <GPUImageVideoCameraDelegate>
{
ALinH264Encoder *_h264Encoder;
}
@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;
@property (nonatomic, strong) GPUImageView *filterView;
@interface ShowTimeViewController () <LFLiveSessionDelegate>
@property (weak, nonatomic) IBOutlet UIButton *beautifulBtn;
@property (nonatomic, strong) GPUImageMovieWriter *writer;
@property (weak, nonatomic) IBOutlet UIButton *livingBtn;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
/** RTMP地址 */
@property (nonatomic, copy) NSString *rtmpUrl;
@property (nonatomic, strong) LFLiveSession *session;
@property (nonatomic, weak) UIView *livingPreView;
@end

@implementation ShowTimeViewController
- (UIView *)livingPreView
{
if (!_livingPreView) {
UIView *livingPreView = [[UIView alloc] initWithFrame:self.view.bounds];
livingPreView.backgroundColor = [UIColor clearColor];
livingPreView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:livingPreView atIndex:0];
_livingPreView = livingPreView;
}
return _livingPreView;
}
- (LFLiveSession*)session{
if(!_session){
/***  默认分辨率368 * 640 音频:44.1 iphone6以上48 双声道 方向竖屏 ***/
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Medium2] liveType:LFLiveRTMP];

/**   自己定制高质量音频128K 分辨率设置为720*1280 方向竖屏 */
/*
LFLiveAudioConfiguration *audioConfiguration = [LFLiveAudioConfiguration new];
audioConfiguration.numberOfChannels = 2;
audioConfiguration.audioBitrate = LFLiveAudioBitRate_128Kbps;
audioConfiguration.audioSampleRate = LFLiveAudioSampleRate_44100Hz;
LFLiveVideoConfiguration *videoConfiguration = [LFLiveVideoConfiguration new];
videoConfiguration.videoSize = CGSizeMake(720, 1280);
videoConfiguration.videoBitRate = 800*1024;
videoConfiguration.videoMaxBitRate = 1000*1024;
videoConfiguration.videoMinBitRate = 500*1024;
videoConfiguration.videoFrameRate = 15;
videoConfiguration.videoMaxKeyframeInterval = 30;
videoConfiguration.orientation = UIInterfaceOrientationPortrait;
videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280;
_session = [[LFLiveSession alloc] initWithAudioConfiguration:audioConfiguration videoConfiguration:videoConfiguration liveType:LFLiveRTMP];
*/

// 设置代理
_session.delegate = self;
_session.running = YES;
_session.preView = self.livingPreView;
}
return _session;
}

- (void)viewDidLoad {
[super viewDidLoad];
Expand All @@ -37,25 +74,14 @@ - (void)setup{
self.beautifulBtn.layer.cornerRadius = self.beautifulBtn.height * 0.5;
self.beautifulBtn.layer.masksToBounds = YES;

// 开启前置摄像头的720
self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionFront];
self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
self.videoCamera.delegate = self;
// 设置前置的时候不是镜像
self.videoCamera.horizontallyMirrorRearFacingCamera = YES;
[self.videoCamera addAudioInputsAndOutputs]; // 添加麦克风/声音的输出输入设备
self.filterView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
self.filterView.center = self.view.center;
[self.view insertSubview:self.filterView atIndex:0];
[self.videoCamera addTarget:self.filterView];
[self.videoCamera startCameraCapture];
self.livingBtn.backgroundColor = KeyColor;
self.livingBtn.layer.cornerRadius = self.livingBtn.height * 0.5;
self.livingBtn.layer.masksToBounds = YES;

// 默认开启美颜效果
[self openBeautiful];
self.statusLabel.numberOfLines = 0;

_h264Encoder = [ALinH264Encoder alloc];
[_h264Encoder initWithConfiguration];
[_h264Encoder initEncode:h264outputWidth height:h264outputHeight];
// 默认开启后置摄像头, 怕我的面容吓到你们了...
self.session.captureDevicePosition = AVCaptureDevicePositionBack;
}
// 关闭直播
- (IBAction)close {
Expand All @@ -65,40 +91,158 @@ - (IBAction)close {
// 开启/关闭美颜相机
- (IBAction)beautiful:(UIButton *)sender {
sender.selected = !sender.selected;
if (!sender.selected) { // 开启了美图过滤
[self openBeautiful];
}else{ // 关闭美图过滤
[self.videoCamera removeAllTargets];
[self.videoCamera addTarget:self.filterView];
}
// 默认是开启了美颜功能的
self.session.beautyFace = !self.session.beautyFace;
}

// 开启美颜效果
- (void)openBeautiful
{
[self.videoCamera removeAllTargets];
ALinGPUBeautifyFilter *beautifyFilter = [[ALinGPUBeautifyFilter alloc] init];
[self.videoCamera addTarget:beautifyFilter];
[beautifyFilter addTarget:self.filterView];
}

// 切换前置/后置摄像头
- (IBAction)switchCamare:(UIButton *)sender {
[self.videoCamera rotateCamera];
AVCaptureDevicePosition devicePositon = self.session.captureDevicePosition;
self.session.captureDevicePosition = (devicePositon == AVCaptureDevicePositionBack) ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
NSLog(@"切换前置/后置摄像头");
}

#pragma mark - <GPUImageVideoCameraDelegate>
- (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
// 获取当前的信息
CVPixelBufferRef bufferRef = CMSampleBufferGetImageBuffer(sampleBuffer);
// 获取视频宽度
size_t width = CVPixelBufferGetWidth(bufferRef);
size_t height = CVPixelBufferGetHeight(bufferRef);
NSLog(@"%ld %ld", width, height);
NSLog(@"%@", [self.videoCamera videoCaptureConnection].audioChannels);
[_h264Encoder encode:sampleBuffer];
- (IBAction)living:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.selected) { // 开始直播
LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
// 如果是跟我blog教程搭建的本地服务器, 记得填写你电脑的IP地址
stream.url = @"rtmp://192.168.1.102:1935/rtmplive/room";
self.rtmpUrl = stream.url;
[self.session startLive:stream];
}else{ // 结束直播
[self.session stopLive];
self.statusLabel.text = [NSString stringWithFormat:@"状态: 直播被关闭\nRTMP: %@", self.rtmpUrl];
}
}

#pragma mark -- LFStreamingSessionDelegate
/** live status changed will callback */
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state{
NSString *tempStatus;
switch (state) {
case LFLiveReady:
tempStatus = @"准备中";
break;
case LFLivePending:
tempStatus = @"连接中";
break;
case LFLiveStart:
tempStatus = @"已连接";
break;
case LFLiveStop:
tempStatus = @"已断开";
break;
case LFLiveError:
tempStatus = @"连接出错";
break;
default:
break;
}
self.statusLabel.text = [NSString stringWithFormat:@"状态: %@\nRTMP: %@", tempStatus, self.rtmpUrl];
}

/** live debug info callback */
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug*)debugInfo{

}

/** callback socket errorcode */
- (void)liveSession:(nullable LFLiveSession*)session errorCode:(LFLiveSocketErrorCode)errorCode{

}

@end

#pragma mark - 未继承前的代码
//#define h264outputWidth 1280
//#define h264outputHeight 720
//
//@interface ShowTimeViewController () <GPUImageVideoCameraDelegate>
//{
// ALinH264Encoder *_h264Encoder;
//}
//@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;
//@property (nonatomic, strong) GPUImageView *filterView;
//@property (weak, nonatomic) IBOutlet UIButton *beautifulBtn;
//@property (nonatomic, strong) GPUImageMovieWriter *writer;
//@end
//
//@implementation ShowTimeViewController
//
//- (void)viewDidLoad {
// [super viewDidLoad];
//
// [self setup];
//}
//
//- (void)setup{
// self.beautifulBtn.layer.cornerRadius = self.beautifulBtn.height * 0.5;
// self.beautifulBtn.layer.masksToBounds = YES;
//
// // 开启前置摄像头的720
// self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionFront];
// self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
// self.videoCamera.delegate = self;
// // 设置前置的时候不是镜像
// self.videoCamera.horizontallyMirrorRearFacingCamera = YES;
// [self.videoCamera addAudioInputsAndOutputs]; // 添加麦克风/声音的输出输入设备
// self.filterView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
// self.filterView.center = self.view.center;
// [self.view insertSubview:self.filterView atIndex:0];
// [self.videoCamera addTarget:self.filterView];
// [self.videoCamera startCameraCapture];
//
// // 默认开启美颜效果
// [self openBeautiful];
//
// _h264Encoder = [ALinH264Encoder alloc];
// [_h264Encoder initWithConfiguration];
// [_h264Encoder initEncode:h264outputWidth height:h264outputHeight];
//}
//// 关闭直播
//- (IBAction)close {
// [self dismissViewControllerAnimated:YES completion:nil];
//}
//
//// 开启/关闭美颜相机
//- (IBAction)beautiful:(UIButton *)sender {
// sender.selected = !sender.selected;
// if (!sender.selected) { // 开启了美图过滤
// [self openBeautiful];
// }else{ // 关闭美图过滤
// [self.videoCamera removeAllTargets];
// [self.videoCamera addTarget:self.filterView];
// }
//}
//
//// 开启美颜效果
//- (void)openBeautiful
//{
// [self.videoCamera removeAllTargets];
// ALinGPUBeautifyFilter *beautifyFilter = [[ALinGPUBeautifyFilter alloc] init];
// [self.videoCamera addTarget:beautifyFilter];
// [beautifyFilter addTarget:self.filterView];
//}
//
//// 切换前置/后置摄像头
//- (IBAction)switchCamare:(UIButton *)sender {
// [self.videoCamera rotateCamera];
// NSLog(@"切换前置/后置摄像头");
//}
//
//#pragma mark - <GPUImageVideoCameraDelegate>
//- (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
//{
// // 获取当前的信息
// CVPixelBufferRef bufferRef = CMSampleBufferGetImageBuffer(sampleBuffer);
// // 获取视频宽度
// size_t width = CVPixelBufferGetWidth(bufferRef);
// size_t height = CVPixelBufferGetHeight(bufferRef);
// NSLog(@"%ld %ld", width, height);
// NSLog(@"%@", [self.videoCamera videoCaptureConnection].audioChannels);
// [_h264Encoder encode:sampleBuffer];
//}
//
//@end
Loading

0 comments on commit af9e31e

Please sign in to comment.