IOS端接入
快速入门
本文主要介绍,如何基于TuyaOS SDK开发包实现一个简单的iOS客户端应用。
模块说明
模块 | 说明 |
ThingSmartCameraKitLite | 摄像机垂直化功能 SDK |
集成SDK
在 Podfile
文件中添加以下内容:
source 'https://github.com/tuya/tuya-pod-specs.git'
platform :ios, '12.0'
target 'your_target_name' do
pod "ThingSmartCameraKitLite"
end
直播拉流
前置条件
客户端工程里要提供MQTT能力,并在涂鸦平台上申请一个专门用户MQTT连接的三元数据【productId、deviceSecret、deviceName】【注:客户端申请的三元数据,仅用于连接mqtt】;MQTT连接成功后,要订阅设备topic信息。【注:topic信息由设备测提供,格式为:"/ext/visual/${productKey}/${deviceName}/out"】,同时客户端p2p连接的时涉及到MQTT数据发送的接口,需要你往把指定信息发往设备的topic【注:发送topic信息由设备测提供,格式为:"/ext/visual/${productKey}/${deviceName}/in"】
接口说明
1.创建camera实例
接口说明:创建camera对象实例
/// init camera device
/// - Parameters:
/// - productKey: your product key
/// - deviceSecret: your device secret
/// - deviceName: your deviceName name【your hardwoare id】
- (id<ThingSmartCameraTypeProtocol>)createCameraWithProductKey:(NSString *)productKey
deviceSecret:(NSString *)deviceSecret
deviceName:(NSString *)deviceName;
参数说明HardwareID
参数 | 说明 |
productKey | 三元组信息 productId |
deviceSecret | 三元组信息 device secret |
deviceName | 三元组信息 haredwareId |
接口示例
import <ThingSmartCameraKitLite/ThingSmartCameraDevice.h>
ThingSmartCameraDevice *device = [[ThingSmartCameraDevice alloc] createCameraWithProductKey:@"your productId" deviceSecret:@"your device secret" deviceName:@"you device name"];
device.delegate = self;
device.dataSource = self;
self.device = device;
dataSource接口说明: camera对象发起p2p连接时,涉及到mqtt消息的发送,会通过下面代理方法回调给开发者,开发者需要实现这个代理方法,并且在收到这个回调时,要把message数据通过MQTT发送到设备测
/// user mqtt to send message
/// - Parameter message: message
- (void)shouldSendMqttMessage:(NSDictionary *)message;
示例代码:
- (void)shouldSendMqttMessage:(NSDictionary *)message {
[self.mqttManager sendData:[[ThingCameraUtil thing_jsonStringFromDictionary:message] dataUsingEncoding:NSUTF8StringEncoding] topic:@"/ext/visual/wkNCAsnnlsYSnI6n/2ac4d7c62ffa/in" qos:MQTTQosLevelAtLeastOnce retain:NO];
}
2.发起连接
接口说明:发起p2p连接
/// connect
- (void)connect;
示例代码:
ThingSmartCameraDevice *device = [[ThingSmartCameraDevice alloc] createCameraWithProductKey:@"your productId" deviceSecret:@"your device secret" deviceName:@"you device name"];
[device connect];
连接成功回调:
发起connect后,如果连接成功,会回调下面这个方法
连接成功回调:
/// Them camera did connected
/// @param camera camera
- (void)cameraDidConnected:(id<ThingSmartCameraTypeProtocol>)camera;
连接失败回调:
type 对应 ThingSmartCameraVisualTypeConnect
/// The operate did occurred error
/// @param camera camera
/// @param errCode error code
/// @param type opera type
- (void)camera:(id<ThingSmartCameraTypeProtocol>)camera didOccurredError:(NSInteger)errCode type:(ThingSmartCameraVisualType)type;
3.发起拉流
接口说明:调用此接口前,务必保证p2p连接成功
/// start preview
- (void)startPreview;
示例代码:
ThingSmartCameraDevice *device = [[ThingSmartCameraDevice alloc] createCameraWithProductKey:@"your productId" deviceSecret:@"your device secret" deviceName:@"you device name"];
[device startPreview];
拉流成功接口回调:
/// The camera did begin preivew
/// @param camera camera
- (void)cameraDidBeginPreview:(id<ThingSmartCameraTypeProtocol>)camera;
拉流成功后,视频流以解码后的YUV数据格式对外数据,对应下面回调
/// The operate did occurred error
/// @param camera camera
/// @param errCode error code
/// @param type opera type
- (void)camera:(id<ThingSmartCameraTypeProtocol>)camera didOccurredError:(NSInteger)errCode type:(ThingSmartCameraVisualType)type;
4.暂停拉流
接口说明:务必保证在出流过程中,调用此方法,否则调用无效;
/// stop preview
- (void)stopPreview;
示例代码:
ThingSmartCameraDevice *device = [[ThingSmartCameraDevice alloc] createCameraWithProductKey:@"your productId" deviceSecret:@"your device secret" deviceName:@"you device name"];
[device stopPreview];
停止拉流成功代理方法:
/// The camera did stop live video.
/// @param camera camera
- (void)cameraDidStopPreview:(id<ThingSmartCameraTypeProtocol>)camera;
5.断开连接
接口说明:断开p2p连接
/// disconnect
- (void)disconnect;
示例代码:
ThingSmartCameraDevice *device = [[ThingSmartCameraDevice alloc] createCameraWithProductKey:@"your productId" deviceSecret:@"your device secret" deviceName:@"you device name"];
[device disconnect];
停止拉流成功代理方法:
/// The camera did stop live video.
/// @param camera camera
- (void)cameraDidStopPreview:(id<ThingSmartCameraTypeProtocol>)camera;
播放器
直播拉流成功后,使用SDK提供的播放进行绘制,用于显示在终端APP上,具体使用如下: 引入播放器组件,在camera对象YUV数据代理方法中,进行视频流绘制
接口说明:
开始绘制,收到YUV数据进行绘制时,确保调用一次下面方法
- (void)startPlay;
接口说明:
视频流数据绘制:
- (void)displayPixelBuffer:(CVPixelBufferRef)pixelBuffer featureRect:(CGRect)rect;
参数说明:
参数 | 说明 |
pixelBuffer | YUV数据 |
rect | 数据帧大小 |
代码示例:
#import<ThingSmartMediaUIKit/ThingSmartMediaVideoView.h>
//创建播放器:
- (void)createPlayerView {
if (!self.videoView) {
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
self.videoView = [[ThingSmartMediaVideoView alloc] initWithFrame:CGRectMake(0, 100, viewWidth, viewWidth*9/16)];
[self.view addSubview:self.videoView];
}
}
//绘制数据。在cameraSDK的YUV数据回调方法中进行绘制
- (void)camera:(id<ThingSmartCameraTypeProtocol>)cameraDevice didVideoFrameRecvedWithSampleBuffer:(CMSampleBufferRef)sampleBuffer videoFrameInfo:(ThingSmartVideoFrameInfo)videoFrameInfo {
if (!self.firstPix) {
self.firstPix = YES;
//
[self.videoView startPlay];
}
CVPixelBufferRef pixcelBuffer = (CVPixelBufferRef)sampleBuffer;
CVPixelBufferRetain(pixcelBuffer);
dispatch_async(dispatch_get_main_queue(), ^{
[self.videoView displayPixelBuffer:pixcelBuffer featureRect:CGRectMake(0, 0, videoFrameInfo.nWidth, videoFrameInfo.nHeight)];
CVPixelBufferRelease(pixcelBuffer);
});
}
云存储
创建云存储对象
接口说明
- (instancetype)initWithDeviceId:(NSString *)devId productId:(NSString *)productId token:(NSString *)token;
参数说明:
参数 | 说明 |
devId | 设备id |
productId | productId |
token | token信息 |
示例代码:
- (ThingSmarCameraLiteCloudManager *)cloudManager {
if (!_cloudManager) {
_cloudManager = [[ThingSmarCameraLiteCloudManager alloc] initWithDeviceId:@"2ac4d7c62ffa" productId:@"wkNCAsnnlsYSnI6n" token:@"token"];
_cloudManager.delegate = self;
}
return _cloudManager;
}
delegate 说明: 播放成功后 YUV数据的回调
/// receive cloud play back YUV data
/// - Parameters:
/// - player: player
/// - frameBuffer: YUV buffer
/// - frameInfo: frame info
- (void)cloudVideoPlayer:(ThingSmarCameraLiteCloudManager *)player didReceivedFrame:(CMSampleBufferRef)frameBuffer videoFrameInfo:(ThingSmartVideoFrameInfo)frameInfo;
获取录像片段
获取云存储播放片段信息
/// load cloud datas
/// - Parameters:
/// - params: params
/// - success: success callback
/// - fail: fail callback
- (void)loadCloudData:(ThingSmarCameraLiteCloudParamas *)params success:(ThingSmartCameraCloudDataHandler)success fail:(ThingSmartCameraCloudDataFailHandler)fail;
ThingSmarCameraLiteCloudParamas 参数说明
参数 | 类型 | 说明 |
startTime | NSTimeInterval | 开始时间 |
endTime | NSTimeInterval | 结束时间 |
pageIndex | NSInteger | 分页下标 |
pageSize | NSInteger | 分页大小 |
metaData | id | 录像片段信息 |
示例代码
ThingSmarCameraLiteCloudParamas *params = [[ThingSmarCameraLiteCloudParamas alloc] init];
params.startTime = 12345;
params.endTime = 67890;
params.pageIndex = 0;
params.pageSize = 20;
[self.cloudManager loadCloudData:params success:^(NSString * _Nonnull jsonData) {
} fail:^(NSError * _Nonnull error) {
}];
播放云存储片段
接口说明:
/// play cloud video
/// - Parameters:
/// - startTime: startTime
/// - endTime: endTime
/// - pageIndex: page index
/// - pageSize: page size
- (void)playCloudDatas:(ThingSmarCameraLiteCloudParamas *)params
responseHandler:(void (^)(const char* msg,int errCode))responseHander
playFinishHandler:(void (^)(const char* msg,int errCode))finishaHandler;
ThingSmarCameraLiteCloudParamas 参数说明 同上
示例:
ThingSmarCameraLiteCloudParamas *params = [[ThingSmarCameraLiteCloudParamas alloc] init];
params.startTime = 1737096276;
params.endTime = 1737096306;
params.pageIndex = 1;
params.pageSize = 20;
[self.cloudManager loadCloudData:params success:^(NSString * _Nonnull jsonData) {
if (jsonData) {
params.metaData = jsonData;
[self.cloudManager playCloudDatas:params responseHandler:^(const char * _Nonnull msg,
int errCode) {
if (errCode == 0) {
}
} playFinishHandler:^(const char * _Nonnull msg, int errCode) {
}];
}
} fail:^(NSError * _Nonnull error) {
}];
云存储示例代码:
- (void)_loadCloudData {
if (!self.cloudManager) {
self.cloudManager = [[ThingSmarCameraLiteCloudManager alloc] initWithDeviceId:@"2ac4d7c62ffa" productId:@"wkNCAsnnlsYSnI6n" token:self.token];
self.cloudManager.delegate = self;
}
ThingSmarCameraLiteCloudParamas *params = [[ThingSmarCameraLiteCloudParamas alloc] init];
params.startTime = 1737096276;
params.endTime = 1737096306;
params.pageIndex = 1;
params.pageSize = 20;
[self.cloudManager loadCloudData:params success:^(NSString * _Nonnull jsonData) {
if (jsonData) {
params.metaData = jsonData;
[self.cloudManager playCloudDatas:params responseHandler:^(const char * _Nonnull msg, int errCode) {
if (errCode == 0) {
}
} playFinishHandler:^(const char * _Nonnull msg, int errCode) {
}];
}
} fail:^(NSError * _Nonnull error) {
}];
[SVProgressHUD showWithStatus:@"正在获取视频流"];
}
#pragma mark - ThingSmartCameraCloudDelegate
- (void)cloudVideoPlayer:(ThingSmarCameraLiteCloudManager *)player didReceivedFrame:(CMSampleBufferRef)frameBuffer videoFrameInfo:(ThingSmartVideoFrameInfo)frameInfo {
if (!self.firstPix) {
self.firstPix = YES;
[SVProgressHUD dismiss];
[self.videoView startPlay];
[player updateMute:NO];
}
CVPixelBufferRef pixcelBuffer = (CVPixelBufferRef)frameBuffer;
CVPixelBufferRetain(pixcelBuffer);
dispatch_async(dispatch_get_main_queue(), ^{
[self.videoView displayPixelBuffer:pixcelBuffer featureRect:CGRectMake(0, 0, frameInfo.nWidth, frameInfo.nHeight)];
CVPixelBufferRelease(pixcelBuffer);
});
}
- 本页导读 (0)
- 快速入门
- 直播拉流
- 前置条件
- 接口说明
- 1.创建camera实例
- 2.发起连接
- 3.发起拉流
- 4.暂停拉流
- 5.断开连接
- 播放器
- 云存储
- 创建云存储对象
- 获取录像片段
- 播放云存储片段