Files
buzz/mobile/lib/features/channels/message_media.dart
klopez4212 6f5ec4a731 Add mobile voice notes (#7121)
## Summary
- add iOS and Android voice-note recording and preview directly in the
mobile composer
- add waveform playback, scrubbing, speed controls, haptics, and
one-shot playback in chat
- package recordings in a canonical H.264/AAC MP4 envelope on both
platforms so existing relays accept them
- preserve the shared composer interaction and attachment-card treatment
across mobile platforms

Mobile counterpart to #6978.

## Testing
- `just ci`
- `just mobile-check`
- `just mobile-test` (2,026 tests)
- Android debug build compiled, installed, launched, and Voice note
verified in the attachment menu on Pixel 10
- signed iOS device build installed on iPhone

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Kenny Lopez <klopez4212@gmail.com>
Signed-off-by: Carl <3c4caeafb646d23867f1c4832e68211d77e2561946171625f75c3ce1a3f2670f@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz>
Co-authored-by: Carl <3c4caeafb646d23867f1c4832e68211d77e2561946171625f75c3ce1a3f2670f@buzz.block.builderlab.xyz>
2026-09-03 08:07:04 +00:00

158 lines
4.1 KiB
Dart

import 'package:flutter/foundation.dart';
/// Media presentation selected for a message attachment URL.
enum MessageMediaKind { image, video, audio }
/// Parsed metadata from a NIP-92 `imeta` tag.
@immutable
class ImetaEntry {
final String url;
final String? mimeType;
final String? dimensions;
final String? thumb;
final String? image;
final String? alt;
final double? duration;
final String? filename;
final int? size;
const ImetaEntry({
required this.url,
this.mimeType,
this.dimensions,
this.thumb,
this.image,
this.alt,
this.duration,
this.filename,
this.size,
});
bool get isVideo => mimeType?.startsWith('video/') == true;
bool get isAudio => mimeType?.startsWith('audio/') == true;
String? get posterUrl => image ?? thumb;
double? get aspectRatio {
final parts = dimensions?.split('x');
if (parts == null || parts.length != 2) return null;
final width = double.tryParse(parts[0]);
final height = double.tryParse(parts[1]);
if (width == null || height == null || width <= 0 || height <= 0) {
return null;
}
return width / height;
}
}
/// Parses NIP-92 `imeta` tags into entries keyed by their attachment URL.
Map<String, ImetaEntry> parseImetaTags(List<List<String>> tags) {
final byUrl = <String, ImetaEntry>{};
for (final tag in tags) {
if (tag.isEmpty || tag.first != 'imeta') continue;
String? url;
String? mimeType;
String? dimensions;
String? thumb;
String? image;
String? alt;
double? duration;
String? filename;
int? size;
for (final part in tag.skip(1)) {
final separator = part.indexOf(' ');
if (separator <= 0) continue;
final key = part.substring(0, separator);
final value = part.substring(separator + 1);
switch (key) {
case 'url':
url = value;
case 'm':
mimeType = value;
case 'dim':
dimensions = value;
case 'thumb':
thumb = value;
case 'image':
image = value;
case 'alt':
alt = value;
case 'duration':
final parsedDuration = double.tryParse(value);
duration =
parsedDuration != null &&
parsedDuration.isFinite &&
parsedDuration >= 0
? parsedDuration
: null;
case 'filename':
filename = value;
case 'size':
size = int.tryParse(value);
}
}
if (url == null || url.isEmpty) continue;
byUrl[url] = ImetaEntry(
url: url,
mimeType: mimeType,
dimensions: dimensions,
thumb: thumb,
image: image,
alt: alt,
duration: duration,
filename: filename,
size: size,
);
}
return byUrl;
}
/// Classifies [url] using authoritative [imeta] before extension fallback.
MessageMediaKind? classifyMediaUrl(String url, {ImetaEntry? imeta}) {
final mimeType = imeta?.mimeType;
if (mimeType != null) {
final filename = imeta?.filename?.toLowerCase();
if (mimeType == 'video/mp4' &&
filename != null &&
filename.startsWith('voice-note-') &&
filename.endsWith('.mp4')) {
return MessageMediaKind.audio;
}
// An imeta MIME type is authoritative. The native video player chooses
// whether the device can decode the specific codec/container; rejecting
// every non-MP4 video here prevents it from even trying.
if (mimeType.startsWith('video/')) return MessageMediaKind.video;
if (mimeType.startsWith('image/')) return MessageMediaKind.image;
if (mimeType.startsWith('audio/')) return MessageMediaKind.audio;
}
final path = (Uri.tryParse(url)?.path ?? url).toLowerCase();
if (path.endsWith(_mp4Extension)) {
return MessageMediaKind.video;
}
if (_imageExtensions.any(path.endsWith)) {
return MessageMediaKind.image;
}
if (path.endsWith('.m4a') || path.endsWith('.aac')) {
return MessageMediaKind.audio;
}
return null;
}
const _imageExtensions = {
'.jpg',
'.jpeg',
'.png',
'.webp',
'.bmp',
'.heic',
'.heif',
'.avif',
};
const _mp4Extension = '.mp4';