跳到主要内容

RTSP 直播

启动设备 RTSP 会话,按照回调提供的热点流程执行,并将返回的 RTSP 地址传给兼容播放器。

Animated workflow

RTSP 启动与播放

RTSP 启动是多阶段设备与 Wi-Fi 流程。应显示各阶段,但不要将中间阶段视为最终成功。

宿主 App

配置视频流

使用默认 RTSP 参数,或设置文档明确的码率、质量和 I 帧值。

SDK + 设备

配置热点

观察热点配置开始和完成。

设备

进入直播模式

等待设备进入直播模式。

设备

等待热点

设备先开启热点,应用随后才能加入。

SDK + iOS

连接热点

显示提供的 SSID 并观察连接结果。

Wi-Fi
设备 → 应用

接收 RTSP URL

验证并保留 SDK 返回的 rtsp:// 地址。

AIBudsLiveStream

开始播放

附加播放器视图,并将 URL 作为直播 VideoSource 播放。

宿主 App

直播已启动

只有会话启动回调成功后,才将本地状态更新为直播中。

直播中
最终回调

结束并清理

停止播放器资源并区分用户停止和意外中断。

RTSP 地址和会话启动回调是独立事件。请保留播放资源,直到会话结束或用户停止。

前提条件

  • 设备已连接并就绪,且符合 LiveStreamingAPI
  • supportsRTSPLiveStreamingtrue
  • 应用已准备好执行由 SDK 管理的设备热点连接流程。
  • 使用下方 SDK RTSP 播放器时,请添加 AIBudsLiveStream.xcframework

API 参考

框架

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 参数

属性含义
bitrate单位 Mbps;0 表示请求设备默认值。
qualityFactor0 表示请求无损质量;值越大压缩越强。
iFrameIntervalI 帧前的 P/B 帧数量;值越大压缩越强。

SDK 当前没有公开与设备无关的非零取值范围。除非目标设备契约提供支持值,否则优先使用 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 可以选择通过 LiveStreamer 将收到的 RTSP 源转发到 RTMP;这是独立的发布生命周期,并且需要应用提供 RTMP URL。