RTSP Canlı Yayını
Cihazda bir RTSP oturumu başlatın, callback'lerle bildirilen erişim noktası akışını izleyin ve döndürülen RTSP adresini uyumlu bir oynatıcıya aktarın.
RTSP başlatma ve oynatma
RTSP başlatma, cihaz ve Wi-Fi tarafında birden çok aşamadan oluşur. Hiçbir ara aşamayı nihai başarı olarak değerlendirmeden her birini kullanıcıya gösterin.
Ön Koşullar
- Cihaz bağlı ve hazırdır;
LiveStreamingAPIprotokolüne uyar. supportsRTSPLiveStreamingdeğeritrueolmalıdır.- Uygulamanız, SDK tarafından yönetilen cihaz erişim noktası bağlantı akışına hazırdır.
- Aşağıda gösterilen SDK RTSP oynatıcısını kullanıyorsanız
AIBudsLiveStream.xcframeworkekleyin.
API Referansı
Framework
AIBuds.xcframework, AIBudsFoundation.xcframework ve isteğe bağlı olarak AIBudsLiveStream.xcframework
İçe Aktarma
- Swift
- Objective-C
import AIBuds
import AIBudsFoundation
import AIBudsLiveStream#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
#import <AIBudsFoundation/AIBudsFoundation-Swift.h>
#import <AIBudsLiveStream/AIBudsLiveStream-Swift.h>Özellikler
- Swift
- Objective-C
/// Whether RTSP live streaming is supported
var supportsRTSPLiveStreaming: Bool { get }/// Whether RTSP live streaming is supported.
@property(nonatomic, readonly) BOOL supportsRTSPLiveStreaming;Örnek Yöntemi
- Swift
- Objective-C
/// Start RTSP live streaming
/// - Parameters:
/// - params: RTSP stream parameters
/// - configureHotspotStartingHandler: Configure hotspot starting handler
/// - hotspotConfigureCompletionHandler: Hotspot configure completion handler
/// - enterLiveStreamingModeStartingHandler: Enter live streaming mode starting handler
/// - enterLiveStreamingModeCompletedHandler: Enter live streaming mode completed handler
/// - waitingForHotspotOpenHandler: Waiting for hotspot open handler
/// - connectDeviceHotspotStartingHandler: Connect device hotspot starting handler
/// - deviceHotspotConnectCompletionHandler: Device hotspot connect completion handler
/// - rtspAddressReceivedHandler: RTSP address received handler
/// - sessionStartCompletionHandler: Session start completion handler
/// - sessionFinishHandler: Session finish handler
func startRTSPLiveStreaming(
withParams params: RTSPStreamParams,
configureHotspotStartingHandler: AIBudsLiveStreamingConfigureHotspotStartingHandler?,
hotspotConfigureCompletionHandler: AIBudsLiveStreamingHotspotConfigureCompletionHandler?,
enterLiveStreamingModeStartingHandler: AIBudsEnterLiveStreamingModeStartingHandler?,
enterLiveStreamingModeCompletedHandler: AIBudsEnterLiveStreamingModeCompletionHandler?,
waitingForHotspotOpenHandler: AIBudsLiveStreamingStartingToWaitForHotspotOpenHandler?,
connectDeviceHotspotStartingHandler: AIBudsLiveStreamingConnectDeviceHotspotStartingHandler?,
deviceHotspotConnectCompletionHandler:
AIBudsLiveStreamingDeviceHotspotConnectCompletionHandler?,
rtspAddressReceivedHandler: AIBudsLiveStreamingRtspAddressReceivedHandler?,
sessionStartCompletionHandler: AIBudsLiveStreamingSessionStartCompletionHandler?,
sessionFinishHandler: AIBudsLiveStreamingSessionFinishHandler?
)/// Start RTSP live streaming
/// - Parameters:
/// - params: RTSP stream parameters
/// - configureHotspotStartingHandler: Configure hotspot starting handler
/// - hotspotConfigureCompletionHandler: Hotspot configure completion handler
/// - enterLiveStreamingModeStartingHandler: Enter live streaming mode starting handler
/// - enterLiveStreamingModeCompletedHandler: Enter live streaming mode completed handler
/// - waitingForHotspotOpenHandler: Waiting for hotspot open handler
/// - connectDeviceHotspotStartingHandler: Connect device hotspot starting handler
/// - deviceHotspotConnectCompletionHandler: Device hotspot connect completion handler
/// - rtspAddressReceivedHandler: RTSP address received handler
/// - sessionStartCompletionHandler: Session start completion handler
/// - sessionFinishHandler: Session finish handler
- (void)startRTSPLiveStreamingWithParams:(AIBudsRTSPStreamParams *)params
configureHotspotStartingHandler:
(AIBudsLiveStreamingConfigureHotspotStartingHandler _Nullable)
configureHotspotStartingHandler
hotspotConfigureCompletionHandler:
(AIBudsLiveStreamingHotspotConfigureCompletionHandler _Nullable)
hotspotConfigureCompletionHandler
enterLiveStreamingModeStartingHandler:(AIBudsEnterLiveStreamingModeStartingHandler _Nullable)
enterLiveStreamingModeStartingHandler
enterLiveStreamingModeCompletedHandler:(AIBudsEnterLiveStreamingModeCompletionHandler _Nullable)
enterLiveStreamingModeCompletedHandler
waitingForHotspotOpenHandler:
(AIBudsLiveStreamingStartingToWaitForHotspotOpenHandler _Nullable)
waitingForHotspotOpenHandler
connectDeviceHotspotStartingHandler:
(AIBudsLiveStreamingConnectDeviceHotspotStartingHandler _Nullable)
connectDeviceHotspotStartingHandler
deviceHotspotConnectCompletionHandler:
(AIBudsLiveStreamingDeviceHotspotConnectCompletionHandler _Nullable)
deviceHotspotConnectCompletionHandler
rtspAddressReceivedHandler:(AIBudsLiveStreamingRtspAddressReceivedHandler _Nullable)
rtspAddressReceivedHandler
sessionStartCompletionHandler:
(AIBudsLiveStreamingSessionStartCompletionHandler _Nullable)
sessionStartCompletionHandler
sessionFinishHandler:
(AIBudsLiveStreamingSessionFinishHandler _Nullable)sessionFinishHandler;startRTSPLiveStreaming ile RTSPStreamParams öğelerine bakın.
RTSP Parametreleri
| Özellik | Anlamı |
|---|---|
bitrate | Mbps. 0, cihaz varsayılanını ister. |
qualityFactor | 0, kayıpsız kalite ister; daha büyük değerler sıkıştırmayı artırır. |
iFrameInterval | Bir I-frame öncesindeki P/B kare sayısıdır; daha büyük değerler sıkıştırmayı artırır. |
SDK şu anda bu değerler için cihazdan bağımsız, sıfırdan farklı aralıklar yayımlamaz. Hedef cihaz sözleşmesi desteklenen değerleri belirtmiyorsa RTSPStreamParams.default kullanın.
Kullanım Örnekleri
Oynatıcıyı oturum boyunca saklayın. Örnek, iş akışındaki tüm callback’leri kullanır ve oynatmayı yalnızca geçerli bir RTSP URL’si alındıktan sonra başlatır.
- Swift
- Objective-C
let player = LiveStreamingPlayer()
player.addVideoView(to: videoContainerView)
guard let streamingDevice = device as? LiveStreamingAPI,
streamingDevice.supportsRTSPLiveStreaming
else { return }
streamingDevice.startRTSPLiveStreaming(
withParams: .default,
configureHotspotStartingHandler: { showStage("Configuring hotspot") },
hotspotConfigureCompletionHandler: { success, error in
showStage(
success
? "Hotspot configured"
: error?.localizedDescription ?? "Hotspot configuration failed")
},
enterLiveStreamingModeStartingHandler: { showStage("Entering live-stream mode") },
enterLiveStreamingModeCompletedHandler: { success, error in
showStage(
success
? "Live-stream mode entered" : error?.localizedDescription ?? "Mode entry failed")
},
waitingForHotspotOpenHandler: { showStage("Waiting for device hotspot") },
connectDeviceHotspotStartingHandler: { ssid in showStage("Connecting to \(ssid)") },
deviceHotspotConnectCompletionHandler: { success, error in
showStage(
success
? "Device hotspot connected"
: error?.localizedDescription ?? "Hotspot connection failed")
},
rtspAddressReceivedHandler: { address in
guard let url = URL(string: address), url.scheme?.lowercased() == "rtsp" else { return }
let source = VideoSource(url: url, videoType: .normal)
source.isLiveStream = true
player.play(source: source)
},
sessionStartCompletionHandler: { success, error in
setStreamingActive(success)
if let error { show(error) }
},
sessionFinishHandler: { userStopped, error in
player.stop()
setStreamingActive(false)
handleFinish(userStopped: userStopped, error: error)
}
)self.mediaPlayer = [[AIBudsLiveStreamingPlayer alloc] init];
[self.mediaPlayer addVideoViewToView:self.videoContainerView];
id<AIBudsLiveStreamingAPI> streamingDevice = (id<AIBudsLiveStreamingAPI>)self.device;
if (![streamingDevice conformsToProtocol:@protocol(AIBudsLiveStreamingAPI)] ||
!streamingDevice.supportsRTSPLiveStreaming) {
return;
}
__weak typeof(self) weakSelf = self;
[streamingDevice startRTSPLiveStreamingWithParams:[AIBudsRTSPStreamParams defaultParams]
configureHotspotStartingHandler:^{
[weakSelf showStage:@"Configuring hotspot"];
}
hotspotConfigureCompletionHandler:^(BOOL success, NSError *error) {
[weakSelf handleStageResult:success error:error];
}
enterLiveStreamingModeStartingHandler:^{
[weakSelf showStage:@"Entering live-stream mode"];
}
enterLiveStreamingModeCompletedHandler:^(BOOL success, NSError *error) {
[weakSelf handleStageResult:success error:error];
}
waitingForHotspotOpenHandler:^{
[weakSelf showStage:@"Waiting for device hotspot"];
}
connectDeviceHotspotStartingHandler:^(NSString *ssid) {
[weakSelf showStage:[NSString stringWithFormat:@"Connecting to %@", ssid]];
}
deviceHotspotConnectCompletionHandler:^(BOOL success, NSError *error) {
[weakSelf handleStageResult:success error:error];
}
rtspAddressReceivedHandler:^(NSString *address) {
NSURL *url = [NSURL URLWithString:address];
if (![url.scheme.lowercaseString isEqualToString:@"rtsp"])
return;
AIBudsVideoSource *source =
[[AIBudsVideoSource alloc] initWithUrl:url
videoType:AIBudsLiveStreamingVideoTypeNormal];
source.isLiveStream = YES;
[weakSelf.mediaPlayer playWithSource:source];
}
sessionStartCompletionHandler:^(BOOL success, NSError *error) {
[weakSelf setStreamingActive:success];
if (error)
[weakSelf showError:error];
}
sessionFinishHandler:^(BOOL userStopped, NSError *error) {
[weakSelf.mediaPlayer stop];
[weakSelf setStreamingActive:NO];
[weakSelf handleFinishUserStopped:userStopped error:error];
}];Hata Yönetimi
Erişim noktası yapılandırmasını, moda geçişi, erişim noktası bağlantısını, oturum başlangıcını, oynatıcı hatasını ve kullanıcı dışı sonlanmayı ayrı hata noktaları olarak ele alın. Başarılı bir ara callback, sonraki aşamaların veya oynatmanın da başarılı olacağını garanti etmez.
Notlar
- SDK, RTSP adresini eşzamansız döndürür; varsayımla cihaz URL’si oluşturmayın.
- RTSP oynatma sırasında iPhone’un cihaz erişim noktasına bağlı kalması gerekir.
- Demo, alınan RTSP kaynağını isteğe bağlı olarak
LiveStreamerüzerinden RTMP’ye aktarabilir. Bu ayrı bir yayıncı yaşam döngüsüdür ve uygulamanın sağladığı bir RTMP URL’si gerektirir.