iOS开发日记--运用PeergineSDk 处理P2P视频直播

最近在做P2P的项目,关于视频监控直播方面的,用到了Peergine的P2P视频直播模块,下面总结一下爬坑经历。

sdk介绍

关于SDK的介绍,在官网有详细介绍,你可以现在Peergine的SDK来看,里边包括了文档和demo(如下图)

iOS开发日记--运用PeergineSDk 处理P2P视频直播_第1张图片

SDK安装包中附带了两个小demo,一个是多源音视频播放端demo,一个是单源音视频播放端demo。为了更好的接入到项目中,我把demo程序修改了一下。

准备工作

1.将安装包中的Include和Lib这两个文件夹添加到项目中

iOS开发日记--运用PeergineSDk 处理P2P视频直播_第2张图片

2.添加系统库文件
libiconv.tbd: 系统自带多语言字符集库
libz.1.2.5.tbd:系统自带zip压缩库
libresolv.tbd:系统自带的域名解析库
依赖框架库(如下图)

iOS开发日记--运用PeergineSDk 处理P2P视频直播_第3张图片

3.将Appdelegate.m修改为Appdelegate.mm;
4.在BuildSetting中修改Enable BitCode为NO;

代码部分

VideoVideController.h

#import 
#import "pgLibLiveMultiRender.h"


@class VideoViewController;
@interface DemoLiveEvent : pgLibLiveMultiRenderEvent
{
    VideoViewController *m_App;
}
-(id)init:(VideoViewController *)App;
-(void)OnEvent:(NSString *)sAction data:(NSString *)sData capid:(NSString *)sCapID;
@end

@interface VideoViewController : UIViewController
{
    DemoLiveEvent* m_LiveEvent;
    pgLibLiveMultiRender* m_Live;
    UIView* m_pView;
    NSString* m_sCapID;
    NSString* m_sReplysPeer;
    NSString* m_sReplysFile;
    BOOL m_bNeedCallback;
    BOOL m_bIsReplying;
    BOOL    m_bTransfering ;
    int m_iMethID;
    NSString* filePath;
}
static void MyAudioSessionInterruptionListener(void * inClientData, UInt32 inInterruptionState);

-(void)OnEventProc:(NSString*)sAction data:(NSString*)sData capID:(NSString*)sCapID;
@end

VideoViewController.m

#import "VideoViewController.h"
#import 
#import 
#import "pgLibLiveMultiView.h"

@implementation DemoLiveEvent

-(id)init:(VideoViewController*) App
{
    if (self = [super init]) {
        m_App = App;
    }
    return self;
}

-(void)OnEvent:(NSString *)sAction data:(NSString *)sData capid:(NSString *)sCapID
{
    [m_App OnEventProc:sAction data:sData capID:sCapID];
}

@end
@interface VideoViewController ()

@end

初始化函数

 (instancetype)init
{
    self = [super init];
    if (self) {
        
        m_LiveEvent = [[DemoLiveEvent alloc]init:self];
        m_Live = [[pgLibLiveMultiRender alloc]init:m_LiveEvent];
        m_pView = 0;
        m_sCapID = @"";
    }
    return self;
}

创建视频view。注意 这里的m_pView不能够用xib创建,否则会显示不出图像。

  //Crerte live view。
    int iErr = [m_Live Initialize:@"ios_test" pass:@"" svrAddr:@"connect.peergine.com:7781" relayAddr:@"" p2pTryTime:1 initParam:@""];
    if (iErr != 0) {
        NSLog(@"pgLibLiveMulti:initialize failed ! iErr = %d",iErr);
        NSString *sInfo = [NSString stringWithFormat:@"Initialize failed! iErr=%d",iErr];
        [self showAlert:sInfo];
    }
    m_pView = [pgLibLiveMultiView Get:@"View0"];
    m_pView.frame = CGRectMake(0, 120, SCREEN_WIDTH, 350);
    [self.view addSubview:m_pView];
    
    NSError *error = nil;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
    [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    [audioSession setActive:YES error:&error];
    //定义记录文件全名以及路径的字符串filePath
    NSString *documentDirectory = [self GetDocDir];
    filePath = [documentDirectory stringByAppendingPathComponent:@"pgtest"];
    
    
    //查找文件 如果不存在,就创建一个文件
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
        NSLog(@"File no exsit, create new one");
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    }
    

开始连接与取消连接

//开始连接  video 和audio
-(void)startConnect{
   m_sCapID = M_SCAPID;
    [m_Live Connect:m_sCapID];
    [m_Live VideoStart:m_sCapID videoID:0 param:@"" nodeView:m_pView];
    [m_Live AudioStart:m_sCapID audioID:0 param:@""];
    
}
//取消连接
-(void)stopConnect{
    [m_Live AudioStop:m_sCapID audioID:0];
    [m_Live VideoStop:m_sCapID videoID:0];
    [m_Live Disconnect:m_sCapID];
}

清理P2P直播实例
如果音频视频已经打开,必须先关闭,然后在执行清理实例操作。

   if (m_pView != nil) {
        [pgLibLiveMultiView Release:m_pView];
        m_pView = nil;
    }
    [m_Live Clean];

调用其他业务功能函数,根据实际调用的具体API

MessageSemd: 

最后附demo示例

你可能感兴趣的:(iOS开发日记--运用PeergineSDk 处理P2P视频直播)