RTMP Streaming from Mobile Device
RTMP — a protocol from 2002, invented by Macromedia for Flash. In 2025 it still lives because YouTube Live, Twitch, Facebook Live, Restream, and most media servers accept it. From mobile, RTMP streams launch via specialized libraries — native iOS/Android doesn't do this out of the box.
Why RTMP and Not Something Else
RTMP uses TCP, ensures reliable data delivery, and works through most corporate firewalls (port 1935). Latency — 1–3 seconds, acceptable for streaming to platforms. For ultra-low latency (<500 ms) use WebRTC or SRT, but YouTube and Twitch don't accept them directly — need a re-streamer on server.
iOS: HaishinKit
The best Swift-native option. No FFmpeg dependency, hardware encoding via VideoToolbox.
Minimal setup:
import HaishinKit
let rtmpConnection = RTMPConnection()
let rtmpStream = RTMPStream(connection: rtmpConnection)
rtmpStream.videoSettings = VideoCodecSettings(
videoSize: CGSize(width: 1280, height: 720),
bitRate: 2_000_000,
profileLevel: kVTProfileLevel_H264_High_AutoLevel as String
)
rtmpStream.audioSettings = AudioCodecSettings(bitRate: 128_000)
// Attach camera
let camera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
try rtmpStream.attachCamera(camera)
try rtmpStream.attachAudio(AVCaptureDevice.default(for: .audio))
// Preview
let hkView = MTHKView(frame: previewView.bounds)
hkView.videoGravity = .resizeAspectFill
rtmpStream.addOutput(hkView)
previewView.addSubview(hkView)
// Start
rtmpConnection.connect("rtmp://live.example.com/live")
rtmpStream.publish("stream_key")
Reconnection on break: subscribe to RTMPConnection.Event.rtmpStatus, on code == RTMPStatusCode.connectClosed — reconnectDelay(3) and retry connect(). HaishinKit has built-in reconnect mechanism.
Monitoring: rtmpStream.info.byteCount and RTMPStream.currentFPS — track actual FPS. If below 20 — signal of poor connection.
Android: rtmp-rtsp-stream-client-java
Library by Pedro Vicente. Supports Camera1, Camera2, CameraX, Screen capture. Hardware encoding via MediaCodec.
val rtmpCamera = RtmpCamera2(binding.surfaceView, object : ConnectCheckerRtmp {
override fun onConnectionSuccessRtmp() { /* update UI */ }
override fun onConnectionFailedRtmp(reason: String) {
rtmpCamera.stopStream()
retryConnection()
}
override fun onDisconnectRtmp() { retryConnection() }
override fun onAuthErrorRtmp() { /* show error */ }
override fun onAuthSuccessRtmp() {}
override fun onNewBitrateRtmp(bitrate: Long) { updateBitrateUI(bitrate) }
})
// Prepare (resolution, bitrate, FPS, audio)
rtmpCamera.prepareVideo(1280, 720, 30, 2_000_000) && rtmpCamera.prepareAudio(128_000, 44100, true)
rtmpCamera.startStream("rtmp://live.example.com/live/stream_key")
RtmpCamera2 accepts SurfaceView or TextureView. For Jetpack Compose: AndroidView { RtmpCamera2(context, ...) }.
Bitrate adaptation: rtmpCamera.setVideoBitrateOnFly(newBitrate) — change bitrate without restarting stream.
RTMP Authentication
Twitch, YouTube, Facebook require stream key in URL: rtmp://live.twitch.tv/app/{stream_key}. Some services use RTMP auth: rtmp://user:pass@server/app/stream.
For YouTube Live additionally need rtmps:// (TLS): HaishinKit supports, rtmp-rtsp-stream-client-java — via RtmpsCamera2.
Common Issues
Startup delay. First seconds stream is unstable — SPS/PPS NAL units not yet cached on server. Solution: on iOS set kVTCompressionPropertyKey_AllowFrameReordering: false — B-frames disabled, latency reduced.
Video without audio for viewers. Audio stream not initialized before broadcast. Ensure prepareAudio() called before startStream().
Black screen at startup. AVCaptureSession not initialized in time — startStream() called before startPreview(). Order matters: attachCamera → startPreview → rtmpConnection.connect → rtmpStream.publish.
Crash on background. AVCaptureSession should stop on applicationDidEnterBackground if broadcast shouldn't continue in background. If it should — need UIBackgroundTask and UIBackgroundModes: audio.
Timeline
Integrate RTMP streaming on one platform with preview, connection to specific server, and error handling — 2–3 work days.







