Media capture, preview, and editing
ZapFast lets people create and exchange media without leaving a chat. It captures camera and microphone input, stages files for review, lets people inspect or change pictures, plays supported video and voice messages, and validates shared locations before send. This page is a child of Media and calling; see Calls and audio processing for calls and Stickers and emoji for sticker and emoji flows.
Media lifecycle
Media moves through a short path from local input to a chat message. The chat UI keeps review and playback in the same context, while background work prepares large media and sends the final message.
This diagram shows the main path from capture or selection to review, optional editing, and delivery.
flowchart LR
A[Camera or microphone] --> B[Capture]
C[File or pasted picture] --> D[Attachment staging]
B --> E[Preview or playback]
D --> E
E --> F{Picture edit?}
F -->|Yes| G[Crop or turn]
F -->|No| H[Send]
G --> H
I[Location text] --> J[Validate and preview]
J --> H
Camera capture
Camera frames
Camera capture provides live pictures for video calls. ZapFast lists available cameras, chooses a usable size and frame rate within the call budget, converts supported camera formats to a common picture form, and supplies frames to both the call encoder and the local preview. The camera is released when the stream stops.
Camera access can fail when no device exists, permission is refused, the device disappears, or the device does not offer a readable format. The call can then report a clear capture failure instead of using invalid frames.
References: src/camera.rs• L1-10 src/camera.rs• L45-116 src/camera.rs• L156-194 src/camera.rs• L649-786
Attachment staging
Attachment review
The attachment review area is the hand-off between choosing media and sending it. It keeps each selected picture, file, or document visible in a small strip and shows one selected item in a larger view. People can add more items, remove individual items, discard the set, add a caption in the composer, and send the staged set in order.
Pictures can enter the review as local files or pasted pixels. Picture files can be edited as photos, while documents remain ordinary files even when their file type is an image. Large pasted pictures receive a bounded preview so review stays responsive without changing the source that will be sent.
References: src/…/attachment_preview.rs• L1-10 src/…/attachment_preview.rs• L39-87 src/…/attachment_preview.rs• L89-183 src/…/attachment_preview.rs• L290-376 src/app.rs• L1060-1118 src/app.rs• L6794-6937
Image preview and editing
Media viewer
The media viewer is a full-window view for supported photos and videos from a chat. It can browse the chat's media album, show an item that is not downloaded yet, and offer download without substituting another file. The viewer also supports closing, saving, opening in another app, copying a picture, returning to the message, and message actions such as reply or forward.
Photos keep their aspect ratio and support fit-to-window, original-size, zoom, and pan behavior. The viewer treats a video as video rather than passing it to an image decoder. A motion photo can switch between its still picture and its short video.
References: src/image_preview.rs• L15-45 src/image_preview.rs• L138-248 src/…/image_preview.rs• L27-176 src/…/image_preview.rs• L226-507 src/…/image_preview.rs• L753-835
Picture editing
Picture editing changes a staged photo before it is sent. The editor shows the current picture with a crop region and lets the person move or resize that region, turn the picture left or right, reset the changes, cancel, or keep the result. The staged source remains unchanged until the edit is accepted.
After acceptance, the edited picture becomes the attachment that the send path uses. The output keeps transparency when needed and uses a common photo format for opaque results. The edit is named from its content so the same result can be reused without creating a second copy.
References: src/…/image_editor.rs• L1-200 src/…/picture_edit.rs• L13-21 src/…/picture_edit.rs• L51-101 src/…/picture_edit.rs• L104-155 src/app.rs• L8483-8563
Video playback
Video player
The video player plays one video at a time in a message or in the media viewer. It provides play and pause, seeking, mute, playback speed, progress, and a larger viewer mode. The picture and sound share one playback position when sound is available.
Playback begins with a poster or loading state and reports unsupported media to the system player. It limits decoded picture size for message display, uses a larger size in the full viewer, and stops or pauses media that is no longer visible. A missing video is shown with its thumbnail and a download action instead of a blank player.
References: src/video.rs• L1-9 src/video.rs• L43-89 src/video.rs• L669-705 src/video.rs• L744-832 src/video.rs• L894-1053 src/…/image_preview.rs• L838-979
Voice messages
Voice recording
Voice recording replaces the composer while it is active. The person sees elapsed time and a live level waveform, then can discard or send the recording. The recorder reads the default microphone on a separate capture path, converts the result to mono at the shared voice rate, and ends cleanly when stopped or when the device stalls.
Recordings shorter than the minimum usable length are not sent. A microphone failure stays in the recording flow as an error rather than producing an empty voice message.
References: src/audio.rs• L664-815 src/audio.rs• L847-915 src/…/conversation.rs• L11560-11647 src/app.rs• L8621-8645 src/app.rs• L11304-11369
Voice playback
Voice messages use a speech-focused format and carry a waveform for the message view. Playback supports one active voice clip at a time, pause and resume, seeking, mute behavior through the shared audio controls, and supported speed changes. If a voice message plays while its chat is not open, a compact bar gives access to its progress and lets the person return to the message.
The playback path tolerates common phone recording variations by decoding supported Opus streams, reducing them to mono, and handling malformed or incomplete input as a playback error. Quiet recordings are raised within a fixed limit so normal speech remains audible without excessive gain.
References: src/voice.rs• L1-18 src/voice.rs• L19-63 src/voice.rs• L138-190 src/voice.rs• L215-277 src/audio.rs• L104-124 src/audio.rs• L178-195 src/…/voice_bar.rs• L1-141
Location features
Location input
The location dialog turns pasted coordinates or supported map links into a location message. It shows the parsed point before send and offers a map link for the same point. ZapFast does not ask a map service for the device's current position, so the person supplies the location text.
The parser accepts coordinate pairs and known Google Maps, Apple Maps, OpenStreetMap, and geo: links. It checks that the result is a real point on Earth. Short links and links from unknown sites are rejected because reading them would require a network lookup or could identify the wrong place.
References: src/geo.rs• L1-7 src/geo.rs• L9-67 src/geo.rs• L69-137 src/…/locations.rs• L10-99 src/app.rs• L6622-6638
Location messages
A sent location becomes a chat message with latitude and longitude. A normal location can be opened in a map through the generated map URL. Live locations are a separate message type: the chat keeps one message for the sharing session and records later positions, status, and whether newer updates remain available only on the phone.
References: src/model.rs• L838-875 src/geo.rs• L43-49 src/…/conversation.rs• L7890-7910 src/…/conversation.rs• L8310-8365
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the attachment identity contract aligned between staging, background preview work, picture editing, and completion events. A result tied to a list position can update the wrong attachment after removal.
- Keep the viewer's media-kind rules aligned with the album and with the video player. A file classified as a photo must not be sent to video playback, and a file classified as a video must not be sent to an image decoder.
- Keep picture edit output aligned with the send path for both file-backed and pasted pictures. The accepted crop and turn must be applied to the same source that the editor showed.
- Keep voice recording, encoding, waveform data, and playback on the same sample-rate and channel contract.
- Keep location parsing and location sending aligned so the coordinates shown in the dialog are the coordinates placed in the outgoing message.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Camera permission is refused or the device stops delivering frames | A call cannot provide a local camera picture | Camera open and read operations return a capture error and stop the source |
| An attachment preview finishes after its item was removed or edited | A preview could appear on the wrong staged item | Preview work follows a stable attachment identity |
| A picture cannot be decoded or its edited source is missing | The picture cannot be edited or previewed | The UI reports the error and keeps the source or offers external opening where possible |
| A video uses an unsupported track | In-app playback cannot start | The player reports the file as unsupported and hands it to the system player |
| The microphone returns no usable samples or stalls | No voice message is sent | Recording ends with an error or rejects recordings below the minimum length |
| A location is out of range, ambiguous, or only a short link | The send control stays disabled | Offline parsing accepts only known formats and valid Earth coordinates |
Key files
| File | Why to read it |
|---|---|
| src/app.rs | Connects media actions to staging, editing, playback, location sending, and backend commands. |
| src/…/attachment_preview.rs | Shows the staged attachment set and the selected item before send. |
| src/…/image_preview.rs | Defines the full-window photo and video viewer. |
| src/image_preview.rs | Defines media-viewer state, album routing, media-kind checks, and zoom behavior. |
| src/…/image_editor.rs | Shows the picture crop and turn experience. |
| src/…/picture_edit.rs | Produces the accepted edited picture for sending. |
| src/video.rs | Defines video playback state, timing, sound coordination, and fallback behavior. |
| src/audio.rs | Coordinates microphone recording and voice playback. |
| src/voice.rs | Defines the voice-message audio format, waveform, and sample conversion rules. |
| src/…/locations.rs | Presents location input, validation feedback, and send controls. |
| src/geo.rs | Parses coordinates and supported map links without a network lookup. |