メインコンテンツまでスキップ

RTSP ライブストリーミング

デバイスの RTSP セッションを開始し、コールバックで通知されるホットスポットフローを進め、返された RTSP アドレスを対応プレイヤーへ渡します。

Animated workflow

RTSP の起動と再生

RTSP の起動は、デバイスと Wi-Fi にまたがる複数段階のフローです。各段階を表示し、途中の成功を最終成功として扱わないでください。

ホストアプリ

ストリームを設定

既定の RTSP パラメーターを使用するか、文書化されたビットレート、品質、I フレーム値を設定します。

SDK + デバイス

ホットスポットを設定

ホットスポット設定の開始と完了を監視します。

デバイス

ライブモードへ移行

デバイスがライブストリーミングモードへ移行するまで待ちます。

デバイス

ホットスポットを待機

アプリが接続する前に、デバイスがホットスポットを開きます。

SDK + iOS

ホットスポットへ接続

通知された SSID を表示し、接続結果を監視します。

Wi-Fi
デバイス → アプリ

RTSP URL を受信

SDK から返された rtsp:// アドレスを検証して保持します。

AIBudsLiveStream

再生を開始

プレイヤー View を接続し、URL をライブ VideoSource として再生します。

ホストアプリ

セッション実行中

セッション開始の完了が成功した後にだけ実行中状態へ更新します。

ストリーミング中
最終コールバック

終了してクリーンアップ

プレイヤーリソースを停止し、ユーザー停止と中断を区別します。

RTSP アドレスの受信とセッション開始コールバックは別のイベントです。セッション終了またはユーザー停止まで再生リソースを保持してください。

前提条件

  • デバイスが接続済みで使用可能な状態にあり、LiveStreamingAPI に準拠していること。
  • supportsRTSPLiveStreamingtrue であること。
  • SDK が管理するデバイスホットスポット接続フローにアプリが対応していること。
  • 下記の SDK RTSP プレイヤーを使用する場合は AIBudsLiveStream.xcframework を追加すること。

API Reference

フレームワーク

AIBuds.xcframeworkAIBudsFoundation.xcframework、必要に応じて AIBudsLiveStream.xcframework

インポート

Swift
import AIBuds
import AIBudsFoundation
import AIBudsLiveStream

プロパティ

Swift
/// Whether RTSP live streaming is supported
var supportsRTSPLiveStreaming: Bool { get }

インスタンスメソッド

Swift
/// 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?
)

startRTSPLiveStreamingRTSPStreamParams を参照してください。

RTSP パラメーター

プロパティ意味
bitrateMbps 単位。0 はデバイスの既定値を要求します。
qualityFactor0 は可逆品質を要求し、値が大きいほど圧縮率が上がります。
iFrameIntervalI フレームまでの P/B フレーム数。値が大きいほど圧縮率が上がります。

現在の SDK では、これらの値についてデバイス共通の 0 以外の範囲を公開していません。対象デバイスの契約で対応値が提供されない限り、RTSPStreamParams.default を使用してください。

使用例

セッション全体でプレイヤーを保持します。例ではすべてのフローコールバックを使用し、有効な RTSP URL を受信してから再生を開始します。

Swift
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)
    }
)

エラー処理

ホットスポット設定、モード移行、ホットスポット接続、セッション開始、プレイヤー失敗、ユーザー操作以外の終了を、それぞれ別の失敗点として扱います。途中のコールバック成功は、後続段階や再生の成功を保証しません。

注意事項

  • SDK は RTSP アドレスを非同期で返します。推測でデバイス URL を組み立てないでください。
  • RTSP 再生中は、iPhone がデバイスのホットスポットへ接続し続ける必要があります。
  • Demo では受信した RTSP ソースを LiveStreamer で RTMP へ中継できます。これは独立した配信ライフサイクルで、アプリが用意した RTMP URL が必要です。