Article · Video

Camera and H.264 live streaming in Adobe AIR, without AIR's camera

`Camera` plus `NetStream.attachCamera()` was fine in 2012. For live video on today's phones, the camera, the encoder and the decoder belong in native code.


AIR still lets you stream video the classic way: take a Camera, attach it to a NetStream, publish over RTMP, and display the remote stream with a Video object. It works on a desktop. On phones and tablets, especially older ones, it runs into its limits quickly.

Where the classic AIR camera path breaks

  • CPU and battery. AIR hands the frames through its own runtime before they reach an encoder. On an older Android tablet that's the difference between a smooth stream and a hot, stuttering device.
  • Picture quality. You get little control over resolution, frame rate, bitrate and keyframes, and AIR's quality settings interact in surprising ways.
  • Decoding and display. Showing an H.264 stream with AIR means AIR decodes it and draws it on the stage, with no hardware overlay, no crop-to-fill and awkward rotation handling.
  • Camera features. Torch, real optical zoom, camera switch with USB cameras on the Mac, snapshots straight from the sensor: none of that is reachable through Camera.
  • Audio session conflicts. Starting AIR's camera can reconfigure the audio session on iOS and break a running echo canceller (see our article on two-way voice).

How KlugMedia does it

KlugMedia moves the whole video path into one native session:

Apple platforms Android Windows
Capture AVFoundation Camera2 native implementation
Encoding VideoToolbox (hardware H.264) MediaCodec (hardware H.264) native implementation
Publish RTMP, classic H.264 FLV tags RTMP, classic H.264 FLV tags RTMP
Receive native H.264 decode, drawn above the AIR stage native decode on a SurfaceView / TextureView native

Because the video is sent as classic H.264 FLV tags (no enhanced RTMP), it plays on standard RTMP servers, including older ones.

Terminal
var media:CameraStreamANE = CameraStreamANE.instance;

// publish the back camera: 640×360, 12 fps, 800 kbps, keyframe every 2 s (defaults)
media.startPublish( "rtmp://your.server:1935/live", "camera-1" );

// on the other device: receive and draw it natively, crop-to-fill
media.startReceive( "rtmp://your.server:1935/live", "camera-1",
                    0, 0, 640, 480, 0, CameraStreamANE.RECEIVE_CROPFILL );

Things we learned in production

  • Native video floats above the AIR stage. Your AIR buttons end up underneath it. KlugMedia can draw its own native button strip and zoom controls on top of the video, and reports taps back to ActionScript.
  • Keep the device awake while streaming. iOS locks the screen after the auto-lock time and the camera stops. KlugMedia keeps the idle timer disabled while it publishes and restores your app's setting afterwards.
  • Recover from the unexpected. A phone call, a camera taken by another app, a lost network: the native session detects it, stops cleanly and reports it, so your app can show "Network Error" and reconnect instead of freezing.
  • Hardware encoders can be invalidated. iOS can drop a VideoToolbox session, for example after a background switch. KlugMedia rebuilds it and forces a fresh keyframe, so the viewer doesn't wait for a picture.
  • Lower frame rates often look sharper. At the same bitrate, 12 fps gives each frame more bits than 30 fps. For a monitoring camera that's the better trade.

Who it's for

Live monitoring, video chat, remote inspection, anything where an AIR app has to send or show live video reliably, including on the old phones and tablets that end up as permanent cameras.

KlugMedia: camera, H.264 video and two-way voice for Adobe AIR on iOS, Android, macOS and Windows. Documentation and pricing: klughomesolutions.com/klugmedia.html

More articles