This is the 27th day of my participation in the August Genwen Challenge.More challenges in August


My column

  1. Explore the underlying principles of iOS
  2. Summary of iOS underlying principles of exploration

preface

This part mainly summarizes how to send the content collected by the camera and microphone on the mobile terminal to the server we built before using the RTMP protocol. Please refer to my previous brief on iOS organizing – About live streaming – setting up the server. All right, let’s get down to business.

LFLiveKit

I’m using LFLiveKit on iOS!

integration

Easy to integrate directly from CocoaPods:

pod 'LFLiveKit'
Copy the code

Next, pod Install is ready to use.

In the ViewController pushing the stream, import the header file:

#import "LFLiveKit/LFLiveKit.h"
Copy the code

Next, define the URL of our own push stream,

# define PUT_Strean @ "RTMP: / / 192.168.1.109:1935 / ZBCS/room" URL / / push flowCopy the code

Then, define LFLiveSession and a preview of UIView:

@property (nonatomic, strong) LFLiveSession *session;

@property (nonatomic, strong) UIView *pp;
Copy the code

use

LFLiveSession implements LFLiveSession and defines methods to start and stop pushing:

- (LFLiveSession*)session { if (! _session) { _session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration]  videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]]; _session.preView = self.pp; _session.running = YES; _session.showDebugInfo = YES; _session.captureDevicePosition = AVCaptureDevicePositionBack; _session.delegate = self; } return _session; } - (void)startLive { LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new]; streamInfo.url = [[NSUserDefaults standardUserDefaults] objectForKey:@"putURL"]; [self.session startLive:streamInfo]; } - (void)stopLive { [self.session stopLive]; }Copy the code

Handling agent events

Implement the LFLiveSessionDelegate delegate delegate delegate method (print logs and other information for debugging) :

LiveStateDidChange: livestession :(nullable LFLiveSession *) liveStateDidChange: (LFLiveState)state {switch (state) {case LFLiveReady: NSLog(@" ready "); break; Case LFLivePending: NSLog(@" pending "); break; Case LFLiveStart: NSLog(@" connected "); break; case LFLiveStop: self.button.selected = NO; self.button.backgroundColor = [UIColor blueColor]; NSLog(@" disconnected "); break; case LFLiveError: self.button.selected = NO; self.button.backgroundColor = [UIColor blueColor]; NSLog (@ "error"); break; Case LFLiveRefresh: NSLog(@" refreshing "); break; Default: NSLog(@" Connection status changed." ); break; } } - (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug*)debugInfo { NSLog(@"debug -- %@", debugInfo); } - (void)liveSession:(nullable LFLiveSession*)session errorCode:(LFLiveSocketErrorCode)errorCode {NSLog(@" connection failed -- %lu", (unsigned long)errorCode); self.button.selected = NO; self.button.backgroundColor = [UIColor blueColor]; }Copy the code

Finally, call the start livestreaming method where you need to start livestreaming. Call the stop live method where you need to stop live.