Skip to content

Commit

Permalink
Final fixes for gapless playback, v22 release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Dec 21, 2024
1 parent c4f3d60 commit 5a04bb3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 51 deletions.
Binary file not shown.
6 changes: 2 additions & 4 deletions Documentation/Release Notes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# What's New in Version 4.0.0-preview21
# What's New in Version 4.0.0-preview22

## #74 - Prevent log file bloating (ffmpeg I/O errors)

## Fixed window layout bug
## Fixed gapless playback

## Fixed other bugs

Expand Down
71 changes: 37 additions & 34 deletions Source/Core/Playback/Delegates/PlaybackDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,40 +139,59 @@ class PlaybackDelegate: PlaybackDelegateProtocol {
trackReader.loadArtAsync(for: firstTrack, immediate: true)
}

func previousTrack() {
private func changeGaplessTrack(mustStopIfNoTrack: Bool, trackProducer: TrackProducer) {

guard state.isPlayingOrPaused else {return}
let beginTrack = playQueueDelegate.currentTrack
let beginState = player.state

if isInGaplessPlaybackMode {
if let newTrack = trackProducer() {

let beginTrack = playQueueDelegate.currentTrack
let beginState = player.state
do {
try trackReader.prepareForPlayback(track: newTrack)

} catch {

Messenger.publish(TrackNotPlayedNotification(oldTrack: beginTrack, errorTrack: newTrack,
error: error as? DisplayableError ?? TrackNotPlayableError(newTrack.file)))
}

let endTrack = playQueueDelegate.previous()
player.playGapless(tracks: playQueueDelegate.tracksPendingPlayback)

messenger.publish(TrackTransitionNotification(beginTrack: beginTrack, beginState: beginState,
endTrack: endTrack, endState: player.state))
} else if mustStopIfNoTrack {
doStop(beginTrack)

} else {
doPlay({playQueueDelegate.previous()})
return
}

messenger.publish(TrackTransitionNotification(beginTrack: beginTrack, beginState: beginState,
endTrack: playQueueDelegate.currentTrack, endState: player.state))
}

func nextTrack() {
func previousTrack() {

guard state.isPlayingOrPaused else {return}

if isInGaplessPlaybackMode {

let beginTrack = playQueueDelegate.currentTrack
let beginState = player.state
changeGaplessTrack(mustStopIfNoTrack: false) {
playQueueDelegate.previous()
}

let endTrack = playQueueDelegate.next()
player.playGapless(tracks: playQueueDelegate.tracksPendingPlayback)
} else {
doPlay({playQueueDelegate.previous()})
}
}

func nextTrack() {

guard state.isPlayingOrPaused else {return}

if isInGaplessPlaybackMode {

messenger.publish(TrackTransitionNotification(beginTrack: beginTrack, beginState: beginState,
endTrack: endTrack, endState: player.state))
changeGaplessTrack(mustStopIfNoTrack: false) {
playQueueDelegate.next()
}

} else {
doPlay({playQueueDelegate.next()})
Expand Down Expand Up @@ -298,26 +317,10 @@ class PlaybackDelegate: PlaybackDelegateProtocol {

if isInGaplessPlaybackMode {

let beginTrack = playQueueDelegate.currentTrack
let beginState = player.state

if let subsequentTrack = playQueueDelegate.subsequent() {

do {
try trackReader.prepareForPlayback(track: subsequentTrack)

} catch {

Messenger.publish(TrackNotPlayedNotification(oldTrack: beginTrack, errorTrack: subsequentTrack,
error: error as? DisplayableError ?? TrackNotPlayableError(subsequentTrack.file)))
}

player.playGapless(tracks: playQueueDelegate.tracksPendingPlayback)
changeGaplessTrack(mustStopIfNoTrack: true) {
playQueueDelegate.subsequent()
}

messenger.publish(TrackTransitionNotification(beginTrack: beginTrack, beginState: beginState,
endTrack: playQueueDelegate.currentTrack, endState: player.state))

} else {
doTrackPlaybackCompleted()
}
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/Playback/FFmpegPlaybackContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ class FFmpegPlaybackContext: PlaybackContextProtocol {
}
}

func refresh() throws {

fileContext = try FFmpegFileContext(for: file)
decoder = try FFmpegDecoder(for: fileContext!)
}

func close() {

decoder = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ extension FFmpegScheduler {

func playGapless(tracks: [Track], currentSession: PlaybackSession) {

doPlayGapless(firstTrack: tracks[0],
doPlayGapless(firstTrack: tracks[0], fromTime: 0,
otherTracks: tracks.count > 1 ? Array(tracks[1..<tracks.count]) : [],
currentSession: currentSession)
}

fileprivate func doPlayGapless(firstTrack: Track, fromTime time: Double? = nil, otherTracks: [Track], currentSession: PlaybackSession, beginPlayback: Bool = true) {
fileprivate func doPlayGapless(firstTrack: Track, fromTime time: Double, otherTracks: [Track], currentSession: PlaybackSession, beginPlayback: Bool = true) {

guard let thePlaybackCtx = firstTrack.playbackContext as? FFmpegPlaybackContext,
let decoder = thePlaybackCtx.decoder else {
Expand Down Expand Up @@ -132,7 +132,13 @@ extension FFmpegScheduler {
guard let nextTrack = currentGaplessTrack else {return}

do {
try trackReader.prepareForPlayback(track: nextTrack)

if let ctx = nextTrack.playbackContext as? FFmpegPlaybackContext {
try ctx.refresh()

} else {
try trackReader.prepareForPlayback(track: nextTrack)
}

} catch {

Expand All @@ -149,15 +155,15 @@ extension FFmpegScheduler {
}

// The new track must always start from 0.
do {
try newDecoder.seek(to: 0)

} catch {

Messenger.publish(TrackNotPlayedNotification(oldTrack: session.track, errorTrack: nextTrack,
error: error as? DisplayableError ?? TrackNotPlayableError(nextTrack.file)))
return
}
// do {
// try newDecoder.seek(to: 0)
//
// } catch {
//
// Messenger.publish(TrackNotPlayedNotification(oldTrack: session.track, errorTrack: nextTrack,
// error: error as? DisplayableError ?? TrackNotPlayableError(nextTrack.file)))
// return
// }

theTrack = nextTrack
seekPos = 0
Expand Down Expand Up @@ -241,7 +247,6 @@ extension FFmpegScheduler {
return
}
}


self.continueSchedulingGaplessAsync(for: session)
}
Expand Down

0 comments on commit 5a04bb3

Please sign in to comment.