Live Streaming

Last Updated on : 2024-06-27 07:32:51download

Create a ThingSmartCameraType object to establish a peer-to-peer (P2P) connection. Then, you can access features such as live streaming, video screenshots, recording, and live talk.

How it works

Live Streaming

Core methods

Demo Class Method Description Notes
ThingSmartCameraFactory +(id)cameraWithP2PType:deviceId:delegate:; Create a ThingSmartCameraType object. One device ID matches one ThingSmartCameraType object.
ThingSmartCameraType -(void)connectWithMode: Connect. When error -3 or -105 occurs, try reconnection.
- (void)startPreviewWithDefinition: Start live streaming at the specified resolution. Get the resolution from the reported device capability and invoke a callback through - (void)cameraDidBeginPreview: in ThingSmartCameraDelegate when live streaming starts.
- (int)stopPreview Stop live streaming. Invoke a callback through - (void)cameraDidStopPreview: in ThingSmartCameraDelegate when live streaming is stopped.
-(int)disConnect Disconnect. -
ThingSmartCameraDelegate -(void)cameraInitFailed: Failed to create a ThingSmartCameraType object. The P2P type is not supported.
-(void)cameraDidConnected: Connected successfully. -
- (void)cameraDidBeginPreview: Live streaming starts successfully. -
- (void)cameraDidStopPreview: Live streaming is stopped successfully. -
-(void)camera:didOccurredErrorAtStep:specificErrorCode: Return the failed streaming action and error code. Errors might occur when you connect, start, or stop live streaming.

Important notes

  • When using multithreading, avoid frequently calling methods in ThingSmartCameraType across threads to prevent deadlocks.
  • When switching from playback to live streaming, make sure the playback is stopped successfully, and vice versa.

Core code

Create a ThingSmartCameraType object

+ (id<ThingSmartCameraType>)cameraWithP2PType:(id)type deviceId:(NSString *)devId delegate:(id<ThingSmartCameraDelegate>)delegate;

- (void)cameraInitFailed:(ThingSmartCameraErrorCode)errorCode;

Connect

- (void)connectWithMode:(ThingSmartCameraConnectMode)mode;

- (void)cameraDidConnected:(id<ThingSmartCameraType>)camera;

Start live streaming

// Start live streaming
- (void)startPreviewWithDefinition:(ThingSmartCameraDefinition)definition;
//Live streaming started successfully
- (void)cameraDidBeginPreview:(id<ThingSmartCameraType>)camera;

Stop live streaming

//Stop live streaming
- (void)stopPreview;
//Live streaming stopped successfully
- (void)cameraDidStopPreview:(id<ThingSmartCameraType>)camera;

Disconnect

//Proactively disconnect
- (void)disConnect;
//Passively disconnect
- (void)cameraDisconnected:(id<ThingSmartCameraType>)camera specificErrorCode:(NSInteger)errorCode;

Example

//1. Create ThingSmartCameraType object
- (void)cameraInit {
    //Initialize the device
    self.device = [ThingSmartDevice deviceWithDeviceId:devId];

    // delegate: ThingSmartCameraDelegate  Used to monitor the connection status of P2P channels
    self.camera = [ThingSmartCameraFactory     cameraWithP2PType:@(self.device.deviceModel.p2pType)     deviceId:self.device.deviceModel.devId delegate:self];

    // Initialize video preview page
    self.videoView = self.camera.videoView;

    // Add the video rendering view to the screen
   [self.view addSubview:self.videoView];
}

//2. Connect
- (void)cameraConnect {
    [self.camera connectWithMode:ThingSmartCameraConnectAuto];
}
// Connection successful
- (void)cameraDidConnected:(id<ThingSmartCameraType>)camera {
    self.connected = YES;
    [self startPreview];
}
// Connection failed
- (void)cameraDisconnected:(id<ThingSmartCameraType>)camera specificErrorCode:(NSInteger)errorCode {
    // P2P is passively disconnected, usually caused by network jitter.
    self.connected = NO;
}

//3. Start live streaming
- (void)startPreview {
    [self.camera startPreviewWithDefinition:];
}
// Success callback 
- (void)cameraDidBeginPreview:(id<ThingSmartCameraType>)camera {
   self.previewing = YES;
}

//4. Stop live streaming
- (void)stopPreview {
    [self.camera stopPreview];
}
// Success callback 
- (void)cameraDidStopPreview:(id<ThingSmartCameraType>)camera {
    self.previewing = NO;
}

//5. Disconnect
- (void)disconnect {
    [self.camera disConnect];
}

#pragma mark - ThingSmartCameraDelegate

- (void)camera:(id<ThingSmartCameraType>)camera thing_didReceiveVideoFrame:(CMSampleBufferRef)sampleBuffer frameInfo:(ThingSmartVideoFrameInfo)frameInfo {

}

// Error callback
- (void)camera:(id<ThingSmartCameraType>)camera didOccurredErrorAtStep:(ThingCameraErrorCode)errStepCode specificErrorCode:(NSInteger)errorCode extErrorCodeInfo:(id<ThingSmartCameraExtErrorCodeInfo>)extErrorCodeInfo {
    if (errStepCode == Thing_ERROR_CONNECT_FAILED) {
         // P2P connection failed
        self.connected = NO;
    }
    else if (errStepCode == Thing_ERROR_START_PREVIEW_FAILED) {
        // Live streaming failed
        self.previewing = NO;
    }
}