Skip to content
Draft
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
1 change: 1 addition & 0 deletions Sources/CSFBAudioEngine/Player/AudioPlayerNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class AudioPlayerNode final {

Decoder _Nullable CurrentDecoder() const noexcept;
void CancelActiveDecoders(bool cancelAllActive) noexcept;
bool CancelActiveDecoder(Decoder _Nonnull decoder) noexcept;

private:
#pragma mark - Decoding
Expand Down
17 changes: 17 additions & 0 deletions Sources/CSFBAudioEngine/Player/AudioPlayerNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,23 @@ bool PerformSeekIfRequired() noexcept
}
}

bool SFB::AudioPlayerNode::CancelActiveDecoder(Decoder _Nonnull decoder) noexcept
{
#if DEBUG
assert(decoder != nil);
#endif /* DEBUG */

std::lock_guard<SFB::UnfairLock> lock(mDecoderLock);
const auto iter = std::find_if(mActiveDecoders.cbegin(), mActiveDecoders.cend(), [&decoder](const auto& decoderState){
return decoderState->mDecoder == decoder;
});
if(iter == mActiveDecoders.cend())
return false;
(*iter)->mFlags.fetch_or(DecoderState::eFlagCancelRequested, std::memory_order_acq_rel);
mDecodingSemaphore.Signal();
return true;
}

#pragma mark - Decoding

void SFB::AudioPlayerNode::ProcessDecoders(std::stop_token stoken) noexcept
Expand Down
6 changes: 6 additions & 0 deletions Sources/CSFBAudioEngine/Player/SFBAudioPlayerNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ - (void)cancelActiveDecoders
_node->CancelActiveDecoders(true);
}

- (BOOL)cancelActiveDecoder:(id<SFBPCMDecoding>)decoder
{
NSParameterAssert(decoder != nil);
return _node->CancelActiveDecoder(decoder);
}

// AVAudioNode override
- (void)reset
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ NS_SWIFT_NAME(AudioPlayerNode) @interface SFBAudioPlayerNode : AVAudioSourceNode
/// A transition period occurs when decoder *A* has completed decoding but not yet completed rendering
/// and decoder *B* has started decoding but not yet started rendering.
- (void)cancelActiveDecoders;
/// Cancels an active decoder
/// - parameter decoder: The decoder to cancel
/// - returns: `YES` if the decoder was canceled successfully
- (BOOL)cancelActiveDecoder:(id<SFBPCMDecoding>)decoder;

#pragma mark - Playback Control

Expand Down