Skip to content

Media Streaming over BiDi

mobile: startMediaStream sends the screen to the client as WebDriver BiDi events instead of serving it over a host port. It supports both jpeg frames (an MJPEG-like stream) and H.264/HEVC video with optional AAC audio. The event contract is the same as in the Android drivers, so one client implementation works with both.

Note

Simulators (Xcode 26+) and real devices running iOS/tvOS 27+ are supported, with the same events. Use mobile: getMediaStreamCapabilities to find out what the current session offers.

  1. Start a session with BiDi enabled (webSocketUrl: true) and subscribe to the appium:mediaStream module.
  2. Call mobile: startMediaStream with kind set to jpeg or video.
  3. Handle the appium:mediaStream.units events. JPEG units are complete images. Video units are Annex-B and every keyframe carries the codec configuration, so decoding can begin at any keyframe.
  4. Call mobile: stopMediaStream when done. Streams are also stopped when the session ends.

Every unit is base64-encoded into a JSON message, so this costs more CPU and bandwidth than the plain WDA MJPEG server. A full resolution jpeg stream of a modern iPhone is about 250 KB per frame, so pick a lower scale and fps, or prefer video for long or high resolution streams.

Media events share the session's BiDi WebSocket with command responses and other events. If the stream saturates the downstream connection, those messages can be delayed by seconds, even when video frames are dropped. For latency-sensitive tests, lower the video bitrate or reduce the JPEG scale and fps.

Notes:

  • Subscribe first and only then start the stream, since events are only emitted while a client is subscribed.
  • A static screen produces no new frames. The jpeg stream repeats the last frame every idleHeartbeatMs, and a video stream sends nothing until the screen changes, unless keyFrameIntervalMs is set.
  • The simulator encoder sends a keyframe every 2 * fps encoded frames, which is about every 2 seconds on a constantly changing screen and much less often on a slowly changing one. A client that subscribes while the stream is running, or was skipped for being too slow, does not wait for that: the driver asks the encoder for a keyframe as soon as the next unit arrives. The driver asks at most once a second, so a request that comes sooner is sent when that second has passed.
  • Appium closes the connection with code 1013 if the client cannot keep up with the stream, and closes it when the session is deleted, so no ended event follows a session deletion.

Keyframes at a fixed interval

By default the encoder decides when to send a keyframe, which suits a client that only displays the stream. A client that records it, cuts it into segments (HLS, fragmented MP4) or relays it to viewers who may join at any moment needs a keyframe at a known interval, also while nothing on the screen changes. Pass keyFrameIntervalMs (1000-60000) to mobile: startMediaStream for that:

  • While a client is subscribed, the driver asks the encoder for a keyframe whenever the last keyframe it sent is older than keyFrameIntervalMs. The keyframe follows the request within a frame or two, so two keyframes are slightly more than the value apart (about 4.03 seconds for 4000 on a simulator at 30 fps).
  • Keyframes that the encoder sends by itself count too, so nothing is requested while those come often enough.
  • On a static screen the requested keyframe is the only unit that is sent, so the stream has at least one frame per interval.
  • Nothing is requested while no client is subscribed. A client that subscribes later waits for its first keyframe no longer than about the interval, also on a static screen.
  • A keyframe is a complete picture and much larger than the other frames, so a short interval costs bandwidth even when nothing changes.
  • Only video streams have keyframes, so the option is rejected for jpeg.
  • On a real device the driver already asks for a keyframe every 2 seconds (see below). The option only adds requests to that, so it makes a difference only if it is set below 2000.

Real devices

A real device needs a RemoteXPC tunnel and the optional @appium/media-proxy package. The device streams HEVC, which the driver transcodes to H.264 or JPEG, so on a device:

  • H.264 is encoded by VideoToolbox on macOS and Media Foundation on Windows. On Linux (and where those fail to open) it needs Cisco's OpenH264, which the driver doesn't install: download the exact version it requires from Cisco and set MEDIA_PROXY_OPENH264_PATH to the library, as described in the media-proxy docs. Without it, mobile: getMediaStreamCapabilities offers only the jpeg kind (with a reason), and starting an H.264 stream fails with an error that says what to install.
  • Only one stream can run at a time, because stopping the device's capture stops everything it streams. Stop the running stream before starting another.
  • Only the h264 codec is available. With audio: true the device's AAC-ELD audio is transcoded to AAC-LC (48 kHz stereo, ADTS), as on a simulator.
  • The stream is upright and cropped to the visible display: the device always sends the display's native portrait buffer, padded with black to a multiple of 16 pixels, and the driver rotates and crops it. When the device rotates, the driver notices within about a second, turns the stream, and sends appium:mediaStream.formatChanged with the new size. A video stream also starts a new GOP with a keyframe.
  • The device sends a single keyframe when it starts and none afterwards, and its picture can become damaged during large screen changes (an app launch, a rotation) and stay that way. The driver therefore asks the device for a keyframe every 2 seconds, which is also what gives a late subscriber a keyframe within about 100 ms.
  • The animation iOS plays while rotating is part of the stream, so a few tilted or briefly glitchy frames follow a rotation.
  • The device's HEVC stream is decoded in software, and the H.264 or JPEG output is encoded on the host. On a Mac the H.264 path takes roughly 0.4 CPU cores at 60 fps. jpeg and the OpenH264 fallback cost more.