Skip to main content

JPEG Image Live Streaming

Start the device's JPEG live-stream mode and render each complete JPEG image delivered by the SDK. Compatible level-2 streams can also deliver Opus audio and optional decoded PCM data.

Prerequisites​

  • The device is connected and ready and conforms to LiveStreamingAPI.
  • supportsJPEGImageLiveStreaming is true.
  • Your app can process image and optional audio callbacks without blocking the stream.

API Reference​

Framework​

AIBuds.xcframework

Import​

Swift
import AIBuds
import UIKit

Properties​

Swift
/// Whether JPEG image live streaming is supported
var supportsJPEGImageLiveStreaming: Bool { get }

Instance Method​

Swift
/// Start JPEG image live streaming
/// - Parameters:
///   - sessionStartCompletionHandler: Session start completion handler
///   - jpegDataReceivedHandler: JPEG data received handler
///   - opusDataReceivedHandler: Opus audio data received handler (only called for level-2 Ogg/MJPEG+Opus streams)
///   - sessionFinishHandler: Session finish handler
func startJPEGImageLiveStreaming(
    withSessionStartCompletionHandler sessionStartCompletionHandler: AIBudsLiveStreamingSessionStartCompletionHandler?,
    jpegDataReceivedHandler: AIBudsLiveStreamingJpegDataReceivedHandler?,
    opusDataReceivedHandler: AIBudsLiveStreamingOpusAudioDataReceivedHandler?,
    sessionFinishHandler: AIBudsLiveStreamingSessionFinishHandler?
)

See startJPEGImageLiveStreaming.

Callback Values​

HandlerValuesMeaning
sessionStartCompletionHandlersuccess, errorReports whether the live-stream session started.
jpegDataReceivedHandlerjpegDataDelivers one complete JPEG image as Data / NSData.
opusDataReceivedHandleropusData, decodedPcmAudioDataDelivers raw Opus and optional decoded PCM audio on compatible level-2 streams.
sessionFinishHandlerisUserInitiatedStop, errorReports the terminal session outcome and whether the stop was user initiated.

Return Value​

The method does not return a value directly. Media and lifecycle results are delivered through the handlers.

Usage Examples​

Swift
guard let streamingDevice = device as? LiveStreamingAPI,
    streamingDevice.supportsJPEGImageLiveStreaming
else { return }

streamingDevice.startJPEGImageLiveStreaming(
    withSessionStartCompletionHandler: { success, error in
        DispatchQueue.main.async {
            self.setStreamingActive(success)
            if let error { self.show(error) }
        }
    },
    jpegDataReceivedHandler: { jpegData in
        // Decode away from the UI update, then assign the image on the main queue.
        guard let image = UIImage(data: jpegData) else { return }
        DispatchQueue.main.async {
            self.imageView.image = image
        }
    },
    opusDataReceivedHandler: { opusData, decodedPcmAudioData in
        guard let decodedPcmAudioData, !decodedPcmAudioData.isEmpty else { return }
        self.consumePCM(decodedPcmAudioData)
    },
    sessionFinishHandler: { userStopped, error in
        DispatchQueue.main.async {
            self.setStreamingActive(false)
            self.handleFinish(userStopped: userStopped, error: error)
        }
    }
)

Error Handling​

  • Reject invalid JPEG data without replacing the last valid image.
  • Treat audio as optional. The audio handler is called only for compatible streams, and decoded PCM can be nil.
  • Treat sessionStartCompletionHandler failure as a failed session start.
  • In sessionFinishHandler, distinguish a user stop from an interruption and use error when present.
  • Do not infer frame loss, frame rate, or ordering guarantees beyond the callback sequence provided by the SDK.

Best Practices​

  1. Avoid retaining every JPEG frame; replace the displayed image or use a bounded buffer.
  2. Keep image decoding and UI assignment lightweight to avoid callback backlog.
  3. Clear frame counters and presentation state when a new session begins.
  4. Call stopLiveStreaming() when leaving the feature instead of only hiding the image view.
  5. Pass nil for opusDataReceivedHandler when audio is not needed.

Notes​

  • The public SDK does not specify a fixed JPEG resolution or frame rate.
  • JPEG mode does not require AIBudsLiveStream playback classes.
  • jpegDataReceivedHandler provides compressed JPEG data, not raw pixel buffers.
  • RTMP publishing is a separate AIBudsLiveStream.LiveStreamer lifecycle. Configure raw-feed mode with configure(outputURL:config:), then forward JPEG frames with pushJpegFrame(_:) and nonempty decoded PCM with pushPcmAudio(_:) only after streaming starts.