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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ To note a change, add a line under Unreleased, in a group such as Added, Changed
### Added

- 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.
- `-[ZENFrameRenderer renderExactFrameAtSeconds:width:height:completion:]`, which renders the frame a player shows at a time, rather than the keyframe at or before it.

### Fixed

- Frames rendered from a file whose timestamps don't start at 0, such as an MPEG transport stream, come from the time asked for. Before, times counted from timestamp 0, so frames came from too early and their actual times were off by the file's start, and a rough seek could land a keyframe or more late.

## 0.1.0 - 2026-11-19

Expand Down
4 changes: 4 additions & 0 deletions CoreZen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
CC00000228860A0000000006 /* TestDomainObject.m in Sources */ = {isa = PBXBuildFile; fileRef = CC00000128860A0000000009 /* TestDomainObject.m */; };
0313158D2D7374094EFBE6DC /* MediaTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D27F0ECF10C39A3A06669EF /* MediaTests.m */; };
030EFFD5692EA63A973B2FE9 /* video.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 7C383F9E59E944DEF41405A3 /* video.mp4 */; };
CCD9DDD06C5517F511A366AC /* start-offset.ts in Resources */ = {isa = PBXBuildFile; fileRef = 2431BF153CB5C75BE727866E /* start-offset.ts */; };
3A881C011BDBA50B90143879 /* audio.m4a in Resources */ = {isa = PBXBuildFile; fileRef = 318D307DF615F0093C6C9C47 /* audio.m4a */; };
7C83D0F62FF8F1D1569E4B13 /* silent.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 75E34DE8F29716419674A2D5 /* silent.mp4 */; };
55364405F0E577C93535A30F /* last-keyframe.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 212C13ED5A3034EA43EE5679 /* last-keyframe.mp4 */; };
Expand Down Expand Up @@ -300,6 +301,7 @@
CC00000128860A0000000009 /* TestDomainObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestDomainObject.m; sourceTree = "<group>"; };
7D27F0ECF10C39A3A06669EF /* MediaTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MediaTests.m; sourceTree = "<group>"; };
7C383F9E59E944DEF41405A3 /* video.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = video.mp4; sourceTree = "<group>"; };
2431BF153CB5C75BE727866E /* start-offset.ts */ = {isa = PBXFileReference; lastKnownFileType = file; path = "start-offset.ts"; sourceTree = "<group>"; };
318D307DF615F0093C6C9C47 /* audio.m4a */ = {isa = PBXFileReference; lastKnownFileType = file; path = audio.m4a; sourceTree = "<group>"; };
75E34DE8F29716419674A2D5 /* silent.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = silent.mp4; sourceTree = "<group>"; };
212C13ED5A3034EA43EE5679 /* last-keyframe.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = last-keyframe.mp4; sourceTree = "<group>"; };
Expand Down Expand Up @@ -844,6 +846,7 @@
isa = PBXGroup;
children = (
7C383F9E59E944DEF41405A3 /* video.mp4 */,
2431BF153CB5C75BE727866E /* start-offset.ts */,
318D307DF615F0093C6C9C47 /* audio.m4a */,
75E34DE8F29716419674A2D5 /* silent.mp4 */,
212C13ED5A3034EA43EE5679 /* last-keyframe.mp4 */,
Expand Down Expand Up @@ -1218,6 +1221,7 @@
buildActionMask = 2147483647;
files = (
030EFFD5692EA63A973B2FE9 /* video.mp4 in Resources */,
CCD9DDD06C5517F511A366AC /* start-offset.ts in Resources */,
3A881C011BDBA50B90143879 /* audio.m4a in Resources */,
7C83D0F62FF8F1D1569E4B13 /* silent.mp4 in Resources */,
55364405F0E577C93535A30F /* last-keyframe.mp4 in Resources */,
Expand Down
14 changes: 14 additions & 0 deletions CoreZen/CoreZen.docc/Media.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ ZENFrameRenderer *renderer = [ZENFrameRenderer frameRendererWithMediaFile:file];

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.

These renders show keyframes, which are quick to find, so a frame can come from a few seconds before the position asked for.

### Render the Frame a Player Shows

To capture the frame a player is showing, such as for a poster, render the exact frame at its position:

```objc
[renderer renderExactFrameAtSeconds:player.positionSeconds width:640 height:640 completion:^(ZENRenderedFrame *frame) {
// frame.image is the frame on screen, and frame.actualSeconds its time
}];
```

It decodes every frame from the keyframe before the position up to it, so it's slower than rendering a keyframe.

### 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.
Expand Down
53 changes: 42 additions & 11 deletions CoreZen/Media/FrameRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ typedef NS_ENUM(NSInteger, ZENFrameRequestType) {
/// 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.
/// Most renders show the keyframe at or before the position asked for, which is
/// fast, so the frame can come from earlier.
/// ``ZENFrameRenderer/renderExactFrameAtSeconds:width:height:completion:``
/// shows the frame at the position instead.
///
/// Each position is given three ways: in seconds, counted from the start of the
/// file as a player counts them; 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. Timestamps are the stream's own, which don't always start
/// at 0: in an MPEG transport stream, they usually start a second or so in. A
/// position the renderer couldn't work out, such as for a file it couldn't open,
/// is 0.
@interface ZENRenderedFrame : NSObject

/// The frame's image, or nil if it couldn't be rendered.
Expand Down Expand Up @@ -71,7 +77,7 @@ typedef NS_ENUM(NSInteger, ZENFrameRequestType) {
@class ZENMediaFile;
@protocol ZENFrameRenderController;

/// Renders a media file's frames as images, for thumbnails.
/// Renders a media file's frames as images, for thumbnails and posters.
///
/// 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
Expand All @@ -86,7 +92,12 @@ typedef NS_ENUM(NSInteger, ZENFrameRequestType) {
/// Makes a renderer for a media file's frames.
+ (instancetype)frameRendererWithMediaFile:(ZENMediaFile *)mediaFile;

/// Renders the frame at a time in seconds, at most a width and height in pixels.
/// Renders the keyframe at or before a time in seconds, at most a width and
/// height in pixels.
///
/// A keyframe is quick to render, but can be as far before the time as the
/// video's keyframes are apart, often a few seconds. For the frame a player
/// shows at the time, use ``renderExactFrameAtSeconds:width:height:completion:``.
///
/// 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
Expand All @@ -96,8 +107,28 @@ typedef NS_ENUM(NSInteger, ZENFrameRequestType) {
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.
/// Renders the frame a player shows at a time in seconds, at most a width and
/// height in pixels.
///
/// That's the first frame at or after the time, as a player shows after seeking
/// there, or the last frame if the time is past it. Given a player's position,
/// such as ``ZENMediaPlayer/positionSeconds``, it's the frame the player is
/// showing. It's slower than ``renderFrameAtSeconds:width:height:completion:``,
/// since it decodes every frame from the keyframe before the time up to it.
///
/// 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 *)renderExactFrameAtSeconds:(double)seconds
width:(NSUInteger)width
height:(NSUInteger)height
completion:(ZENRenderFrameResultsBlock)completion;

/// Renders the keyframe at or before a fraction of the duration, from 0 to 1,
/// at most a width and height in pixels.
///
/// Like ``renderFrameAtSeconds:width:height:completion:``, the keyframe can be
/// a few seconds before the position.
///
/// 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
Expand All @@ -107,7 +138,7 @@ typedef NS_ENUM(NSInteger, ZENFrameRequestType) {
height:(NSUInteger)height
completion:(ZENRenderFrameResultsBlock)completion;

/// Renders frames spread evenly across the file, each at most a width and
/// Renders keyframes 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
Expand Down
15 changes: 13 additions & 2 deletions CoreZen/Media/FrameRenderer.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ - (ZENWorkQueueToken *)renderFrameAtSeconds:(double)seconds
frame.requestType = ZENFrameRequestTypeSeconds;
frame.requestedSeconds = seconds;

return [self.frameRenderController renderFrame:frame size:NSMakeSize(width, height) completion:completion];
return [self.frameRenderController renderFrame:frame size:NSMakeSize(width, height) exact:NO completion:completion];
}

- (ZENWorkQueueToken *)renderExactFrameAtSeconds:(double)seconds
width:(NSUInteger)width
height:(NSUInteger)height
completion:(ZENRenderFrameResultsBlock)completion {
ZENRenderedFrame *frame = [ZENRenderedFrame new];
frame.requestType = ZENFrameRequestTypeSeconds;
frame.requestedSeconds = seconds;

return [self.frameRenderController renderFrame:frame size:NSMakeSize(width, height) exact:YES completion:completion];
}

- (ZENWorkQueueToken *)renderFrameAtPercentage:(double)percentage
Expand All @@ -55,7 +66,7 @@ - (ZENWorkQueueToken *)renderFrameAtPercentage:(double)percentage
frame.requestType = ZENFrameRequestTypePercentage;
frame.requestedPercentage = percentage;

return [self.frameRenderController renderFrame:frame size:NSMakeSize(width, height) completion:completion];
return [self.frameRenderController renderFrame:frame size:NSMakeSize(width, height) exact:NO completion:completion];
}

- (ZENWorkQueueToken *)renderFrames:(NSUInteger)count
Expand Down
3 changes: 3 additions & 0 deletions CoreZen/Media/LibAV/LibAVRenderController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

- (void)terminate;

// With exact NO, renders the keyframe at or before the frame's requested time;
// with YES, the first frame at or after it
- (ZENWorkQueueToken *)renderFrame:(ZENRenderedFrame *)renderedFrame
size:(NSSize)size
exact:(BOOL)exact
completion:(ZENRenderFrameResultsBlock)completion;

@end
Loading