Multi-view Video Streaming

Last Updated on : 2024-12-27 09:05:07download

Create a ThingSmartCameraType object and use isSupportedVideoSplitting to determine whether the video stream can be split. If video splitting is supported, the SDK will split the video stream according to the configuration protocol and render the video stream. Otherwise, ordinary live streaming is displayed.

How it works

Multi-view Video Streaming

Core methods

Demo Class Method Description Remarks
ThingSmartCameraFactory +(id)cameraWithP2PType:deviceId:delegate: Create a ThingSmartCameraType object. One device ID matches one ThingSmartCameraType object.
ThingSmartCameraType advancedConfig.isSupportedVideoSplitting Specifies whether to support video splitting. If video splitting is not supported, please configure a specific protocol.
-(BOOL)registerVideoViewIndexPairs: Bind render view with camera ID -
-(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.
-(BOOL)swapVideoIndex:forVideoIndex: Swap camera views Before calling this method, make sure the player is bound.
- (int)stopPreview Stop live streaming Invoke a callback through - (void)cameraDidStopPreview: in ThingSmartCameraDelegate when live streaming is stopped.
-(BOOL)uninstallVideoViewIndexPairs: Unbind render view from camera ID. -
-(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

  • If the video stream is not split or an exception occurs, confirm whether the splitting protocol is configured correctly.
  • When using multithreading, be aware of ThingSmartCameraType object management, and avoid frequently calling methods in ThingSmartCameraType across threads to prevent deadlocks.

Core code

Create a ThingSmartCameraType object

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

- (void)cameraInitFailed:(ThingSmartCameraErrorCode)errorCode;

Specifies whether to support video splitting

@property (nonatomic, strong, readonly) id <ThingSmartCameraAdvancedConfig> advancedConfig;

@property (nonatomic, assign, readonly) BOOL isSupportedVideoSplitting;

Bind render view with camera ID

- (BOOL)registerVideoViewIndexPairs:(NSArray<id<ThingSmartVideoViewIndexPair>> *)viewIndexPairs;

Connect

- (void)connectWithMode:(ThingSmartCameraConnectMode)mode;

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

Start live streaming

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

Swap camera views

// Swap camera views
- (BOOL)swapVideoIndex:(ThingSmartVideoIndex)videoIndex forVideoIndex:(ThingSmartVideoIndex)forVideoIndex;

Stop live streaming

You can refer to the complete process in SDK sample.

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

Unbind render view from camera ID

// Unbind render view from camera ID
- (BOOL)uninstallVideoViewIndexPairs:(NSArray<id<ThingSmartVideoViewIndexPair>> *)viewIndexPairs;

Disconnect

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

Example

You can refer to the complete process in SDK sample.


// 1. Create a ThingSmartCameraType object
- (void)cameraInit {
    // Initialize the device
    self.device = [ThingSmartDevice deviceWithDeviceId:devId];
    // delegate: ThingSmartCameraDelegate, used to listen for the P2P connection status
    self.camera = [ThingSmartCameraFactory     cameraWithP2PType:@(self.device.deviceModel.p2pType)     deviceId:self.device.deviceModel.devId delegate:self];


    // Initialize the video preview page
        BOOL isSupportedVideoSplitting = self.camera.advancedConfig.isSupportedVideoSplitting;
    if (isSupportedVideoSplitting) {
        [self.camera registerVideoViewIndexPairs:viewIndexPairs]
    } else {
        self.videoView = self.camera.videoView;

// Add a rendered video view to a video screen
       [self.view addSubview:self.videoView];
    }
}
// 2. Connect
- (void)cameraConnect {
    [self.camera connectWithMode:ThingSmartCameraConnectAuto];
}
// Connection is successful
- (void)cameraDidConnected:(id<ThingSmartCameraType>)camera {
    self.connected = YES;
    [self startPreview];
}
// Connection failed
- (void)cameraDisconnected:(id<ThingSmartCameraType>)camera specificErrorCode:(NSInteger)errorCode {
    // A P2P connection is closed due to unstable network conditions.
    self.connected = NO;
}

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

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

// 5. Unbind the player
- (void)unbindVideoViews {
    [self.camera uninstallVideoViewIndexPairs:viewIndexPairs]
}

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

#pragma mark - ThingSmartCameraDelegate

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

}

// The failure callback
- (void)camera:(id<ThingSmartCameraType>)camera didOccurredErrorAtStep:(ThingCameraErrorCode)errStepCode specificErrorCode:(NSInteger)errorCode extErrorCodeInfo:(id<ThingSmartCameraExtErrorCodeInfo>)extErrorCodeInfo {
    if (errStepCode == Thing_ERROR_CONNECT_FAILED) {
         // Failed to create a P2P connection
        self.connected = NO;
    }
    else if (errStepCode == Thing_ERROR_START_PREVIEW_FAILED) {
        // Failed to start live streaming
        self.previewing = NO;
    }
}