Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To note a change, add a line under Unreleased, in a group such as Added, Changed

### Added

- Documentation for the persistence API, built with DocC: doc comments in its headers, which Xcode shows in Quick Help, and a Persistence article with an example.
- Documentation for the persistence and media APIs, built with DocC: doc comments in their headers, which Xcode shows in Quick Help, and an article with examples for each.

## 0.1.0 - 2026-11-19

Expand Down
1 change: 1 addition & 0 deletions CoreZen/CoreZen.docc/CoreZen.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ CoreZen's [README](https://github.com/znelson/CoreZen#readme) says how to get it
### Essentials

- <doc:Persistence>
- <doc:Media>
58 changes: 58 additions & 0 deletions CoreZen/CoreZen.docc/Media.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Media

Play media files with mpv, and read their details and render their frames with FFmpeg.

## Overview

A ``ZENMediaPlayer`` plays a file with [mpv](https://mpv.io), and shows its video in a ``ZENMediaPlayerView``. A ``ZENMediaPlayerControlsView`` adds buttons to play, pause, step and skip, and a ``ZENMediaPlayerCTIView`` adds a timeline.

A ``ZENMediaFile`` reads a file's duration, frame size and codecs with [FFmpeg](https://ffmpeg.org), and a ``ZENFrameRenderer`` renders its frames as images, for thumbnails.

### Play a File

```objc
ZENMediaPlayer *player = [ZENMediaPlayer new];
[playerView attachPlayer:player];
[player loadFileURL:fileURL];
[player startPlayback];
```

The file loads paused, and `startPlayback` lets it play once it has loaded. To learn when the file is ready, or why it couldn't play, observe the player's ``ZENMediaPlayer/status`` with key-value observing. Its properties change only on the main thread.

### Render Thumbnails

```objc
ZENMediaFile *file = [ZENMediaFile mediaFileWithURL:fileURL];
ZENFrameRenderer *renderer = [ZENFrameRenderer frameRendererWithMediaFile:file];
[renderer renderFrames:10 width:320 height:180 completion:^(NSArray<ZENRenderedFrame *> *frames) {
dispatch_async(dispatch_get_main_queue(), ^{
// Show each frame's image, from the start of the file to its end
});
}];
```

Making a media file reads from disk, so for a file that may be slow to reach, make it on a background queue. Terminate the media file once you're done with it, which also stops any renders still waiting.

### Quit Cleanly

Before the app quits, call ``ZENMediaPlayer/terminateAllPlayers`` and ``ZENMediaFile/terminateAllMediaFiles``, so that every player shuts its mpv instance down, and every media file closes its file.

## Topics

### Playback

- ``ZENMediaPlayer``

### Player Views

- ``ZENMediaPlayerView``
- ``ZENMediaPlayerControlsView``
- ``ZENMediaPlayerCTIView``

### Files and Frames

- ``ZENMediaFile``
- ``ZENFrameRenderer``
- ``ZENRenderedFrame``
- ``ZENRenderFrameResultsBlock``
- ``ZENRenderFramesResultsBlock``
28 changes: 28 additions & 0 deletions CoreZen/CoreZen.docc/ZENMediaFile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# ``ZENMediaFile``

## Topics

### Opening a File

- ``mediaFileWithURL:``
- ``fileURL``

### Duration and Frame Size

- ``durationMicroseconds``
- ``frameWidth``
- ``frameHeight``
- ``rotationDegrees``
- ``pixelAspectRatio``

### Codecs

- ``videoCodecName``
- ``videoCodecLongName``
- ``audioCodecName``
- ``audioCodecLongName``

### Closing a File

- ``terminateMediaFile``
- ``terminateAllMediaFiles``
39 changes: 39 additions & 0 deletions CoreZen/CoreZen.docc/ZENMediaPlayer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ``ZENMediaPlayer``

## Topics

### Loading and Playing

- ``loadFileURL:``
- ``startPlayback``
- ``pausePlayback``
- ``stepOneFrame:``

### Seeking

- ``seekRelativeSeconds:``
- ``seekAbsoluteSeconds:``
- ``seekAbsolutePercentage:``
- ``seeking``

### Observing the Player

- ``status``
- ``error``
- ``fileURL``
- ``paused``
- ``positionSeconds``
- ``durationSeconds``
- ``positionPercent``
- ``ZENMediaPlayerStatus``
- ``ZENMediaPlayerErrorDomain``
- ``ZENMediaPlayerErrorCode``

### Showing Video

- ``playerView``

### Shutting Down

- ``terminatePlayer``
- ``terminateAllPlayers``
21 changes: 21 additions & 0 deletions CoreZen/CoreZen.docc/ZENRenderedFrame.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ``ZENRenderedFrame``

## Topics

### The Image

- ``image``

### The Position Asked For

- ``requestType``
- ``requestedSeconds``
- ``requestedPercentage``
- ``requestedTimestamp``
- ``ZENFrameRequestType``

### The Position Rendered

- ``actualSeconds``
- ``actualPercentage``
- ``actualTimestamp``
75 changes: 61 additions & 14 deletions CoreZen/Media/FrameRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,109 @@

NS_ASSUME_NONNULL_BEGIN

// Completion block types
/// A block called with one rendered frame.
typedef void (^ZENRenderFrameResultsBlock)(ZENRenderedFrame *frame);

/// A block called with several rendered frames.
typedef void (^ZENRenderFramesResultsBlock)(NSArray<ZENRenderedFrame *> *frames);

/// How a frame's position was asked for.
typedef NS_ENUM(NSInteger, ZENFrameRequestType) {
/// As a time in seconds from the start of the file.
ZENFrameRequestTypeSeconds,
/// As a fraction of the file's duration, from 0 to 1.
ZENFrameRequestTypePercentage
};

// Rendered frame result
/// A frame rendered from a media file, with the position it was asked for and
/// the position it came from.
///
/// The renderer shows the keyframe at or before the position asked for, which
/// is fast, so the frame can come from earlier. Each position is given three
/// ways: in seconds; as a fraction of the duration, from 0 to 1, despite the
/// name "percentage"; and as a timestamp, in the units of the video stream's
/// time base. A position the renderer couldn't work out, such as for a file it
/// couldn't open, is 0.
@interface ZENRenderedFrame : NSObject

// The rendered video frame image
/// The frame's image, or nil if it couldn't be rendered.
///
/// It fits within the width and height asked for, in the shape the video is
/// shown in: stretched to its pixel aspect ratio and turned upright.
@property (nonatomic, strong, nullable) NSImage *image;

// The requested position of the frame
/// Whether the frame was asked for by time or by fraction of the duration.
@property (nonatomic) ZENFrameRequestType requestType;

/// The position asked for, as a timestamp in the video stream's time base.
@property (nonatomic) int64_t requestedTimestamp;

/// The position asked for, in seconds.
@property (nonatomic) double requestedSeconds;

/// The position asked for, as a fraction of the duration from 0 to 1.
@property (nonatomic) double requestedPercentage;

// The actual rendered position of the frame
/// The position of the frame rendered, as a timestamp in the video stream's
/// time base.
@property (nonatomic) int64_t actualTimestamp;

/// The position of the frame rendered, in seconds.
@property (nonatomic) double actualSeconds;

/// The position of the frame rendered, as a fraction of the duration from 0
/// to 1.
@property (nonatomic) double actualPercentage;

@end

@class ZENMediaFile;
@protocol ZENFrameRenderController;

// Interface for requesting rendered frames
//
// Every render method calls its completion exactly once, on a background queue.
// A frame's image is nil when it couldn't be rendered: the file has no video
// stream or couldn't be opened, the request was canceled, or the media file was
// terminated. renderFrames: only returns frames that have an image.
/// Renders a media file's frames as images, for thumbnails.
///
/// Renders run one at a time, in the order they're asked for. Every render
/// method calls its completion exactly once, on a background queue. A frame's
/// image is nil when it couldn't be rendered: the file has no video stream or
/// couldn't be opened, the render was canceled, or the media file was
/// terminated.
@interface ZENFrameRenderer : NSObject

/// The media file the renderer renders frames from.
@property (nonatomic, strong, readonly) ZENMediaFile *mediaFile;

/// Makes a renderer for a media file's frames.
+ (instancetype)frameRendererWithMediaFile:(ZENMediaFile *)mediaFile;

// Each returns a token to cancel the render with, or nil if there's nothing to
// cancel: the media file was already terminated, or renderFrames: was asked for
// no frames. The completion is called either way.
/// Renders the frame at a time in seconds, at most a width and height in pixels.
///
/// It returns a token to cancel the render with, or nil if there's nothing to
/// cancel because the media file was terminated already. The completion is
/// called either way.
- (nullable ZENWorkQueueToken *)renderFrameAtSeconds:(double)seconds
width:(NSUInteger)width
height:(NSUInteger)height
completion:(ZENRenderFrameResultsBlock)completion;

/// Renders the frame at a fraction of the duration, from 0 to 1, at most a
/// width and height in pixels.
///
/// It returns a token to cancel the render with, or nil if there's nothing to
/// cancel because the media file was terminated already. The completion is
/// called either way.
- (nullable ZENWorkQueueToken *)renderFrameAtPercentage:(double)percentage
width:(NSUInteger)width
height:(NSUInteger)height
completion:(ZENRenderFrameResultsBlock)completion;

/// Renders frames spread evenly across the file, each at most a width and
/// height in pixels.
///
/// The frames go from the start of the file, at 0, to its end, at 1, and a
/// single frame comes from the middle. The completion gets the frames that
/// have an image, in order. It returns a token that cancels the renders not
/// yet run. It's nil when the count is 0, and can be nil when the media file
/// was terminated already. The completion is called either way.
- (nullable ZENWorkQueueToken *)renderFrames:(NSUInteger)count
width:(NSUInteger)width
height:(NSUInteger)height
Expand Down
78 changes: 60 additions & 18 deletions CoreZen/Media/MediaFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,89 @@

NS_ASSUME_NONNULL_BEGIN

/// A media file's details, read with FFmpeg: its duration, frame size,
/// rotation and codecs.
///
/// Making one opens the file and reads its details, which reads from disk, so a
/// file on a slow volume can keep the calling thread waiting. Call
/// ``terminateMediaFile`` once you're done with it, or
/// ``terminateAllMediaFiles`` before the app quits.
@interface ZENMediaFile : NSObject <ZENIdentifiable>

/// The file's URL.
@property (nonatomic, strong, readonly) NSURL *fileURL;

/// Opens a media file and reads its details.
///
/// A file that can't be opened still makes a media file, whose details are 0
/// or nil.
+ (instancetype)mediaFileWithURL:(NSURL *)url;

// Terminate frame renderer (and frame render controller) if one was created,
// then terminate media file format context
/// Closes the file, and stops its frame renderer's work, if it has one.
///
/// Renders still waiting complete with no image. A media file released without
/// this terminates itself, and logs that it wasn't terminated first.
- (void)terminateMediaFile;

// Call before application terminates to terminate all ZENMediaFile instances
/// Terminates every media file that hasn't been terminated.
///
/// Call it before the app quits.
+ (void)terminateAllMediaFiles;

/// The file's duration in microseconds, or 0 if it's unknown or the file
/// couldn't be read.
- (NSUInteger)durationMicroseconds;

// The video's size as it's shown: the stored frame stretched to its pixel aspect
// ratio, then turned by its rotation. So a phone's portrait video is taller than
// it is wide, and a DVD's anamorphic 720x480 widescreen frames are 853x480. 0 if
// there's no video.
/// The video's width as it's shown, or 0 if there's no video.
///
/// The size as it's shown is the stored frame stretched to its pixel aspect
/// ratio, then turned by its rotation. So a phone's portrait video is taller
/// than it is wide, and a DVD's anamorphic 720x480 widescreen frames are
/// 853x480.
- (NSUInteger)frameWidth;

/// The video's height as it's shown, or 0 if there's no video.
///
/// Like ``frameWidth``, it's the size after the pixel aspect ratio and the
/// rotation.
- (NSUInteger)frameHeight;

// How far the video's frames are turned clockwise to show them upright: 0, 90, 180
// or 270 degrees. Phones store portrait video as landscape frames tagged with 90.
// ZENFrameRenderer's images are already turned.
/// How far the video's frames are turned clockwise to show them upright: 0,
/// 90, 180 or 270 degrees.
///
/// Phones store portrait video as landscape frames tagged with 90.
/// ``ZENFrameRenderer``'s images are already turned.
- (NSUInteger)rotationDegrees;

// How wide each stored pixel is shown, relative to its height: 1 for square
// pixels, as most video has, and more than 1 for frames stored narrower than
// they're shown, like a DVD's anamorphic widescreen (32:27, about 1.185).
/// How wide each stored pixel is shown, relative to its height.
///
/// It's 1 for square pixels, as most video has, and more than 1 for frames
/// stored narrower than they're shown, like a DVD's anamorphic widescreen
/// (32:27, about 1.185).
- (double)pixelAspectRatio;

// Short names of the codecs of the file's video and audio, like "h264" and "aac". nil if
// the file has no stream of that kind that can be decoded, like the audio of a video with
// no sound, or if the file couldn't be read.
/// The short name of the video's codec, like "h264".
///
/// It's nil if the file has no video stream that can be decoded, or if the file
/// couldn't be read.
- (nullable NSString *)videoCodecName;

/// The short name of the audio's codec, like "aac".
///
/// It's nil if the file has no audio stream that can be decoded, like a video
/// with no sound, or if the file couldn't be read.
- (nullable NSString *)audioCodecName;

// Descriptive names of the same codecs, like "AAC (Advanced Audio Coding)". nil in the
// same cases.
/// The descriptive name of the video's codec, like "H.264 / AVC / MPEG-4 AVC /
/// MPEG-4 part 10".
///
/// It's nil in the same cases as ``videoCodecName``.
- (nullable NSString *)videoCodecLongName;

/// The descriptive name of the audio's codec, like "AAC (Advanced Audio
/// Coding)".
///
/// It's nil in the same cases as ``audioCodecName``.
- (nullable NSString *)audioCodecLongName;

@end
Expand Down
Loading