# CLDecryptorAPI Protocol

    public protocol CLDecryptorAPI : InterceptableAPI

API for extracting decrypted compressed audio samples from protected content.

Operation-specific failures are returned through each method’s `Result` as a `PRESTOerror`. SDK-level and component-level errors can also be observed through `PRESTOplaySDK.shared.onError`.

- `canDecrypt(_:)`

  Checks if the current decryptor can extract samples for the content received in the player configuration.

  #### Declaration

  Swift

      func canDecrypt(_ config: PlayerConfiguration) -> Bool

  #### Parameters

  `config` | Player configuration.

  #### Return Value

  `true` if the current decryptor can decrypt samples for the configuration.

- `prepare(onTrackModel:_:)` Default implementation

  Opens the content, initializes the DRM/session state, and prepares audio track/format metadata.

  Call this once before reading or streaming samples.

  #### Default Implementation

  #### Declaration

  Swift

      func prepare(
          onTrackModel: (([AudioTrack]) -> AudioTrack?)?,
          _ completion: @escaping (Swift.Result<StreamInfo, PRESTOerror>) -> Void
      )

  #### Parameters

  `onTrackModel` | Optional selector called with discovered audio tracks before the final `StreamInfo` is returned. Return one of the supplied tracks to select it, or `nil` to keep the default selection.
  `completion` | Called with stream metadata after selection is applied, or with an error if preparation fails.

- `readNextSample(_:)`

  Reads the next audio decryptor event.

  Events can describe a format change, decrypted sample, stream discontinuity, or end of stream. Continue calling this method until an `.endOfStream` event is returned.

  #### Declaration

  Swift

      func readNextSample(
          _ completion: @escaping (Swift.Result<SampleInfoEvent, PRESTOerror>) -> Void
      )

  #### Parameters

  `completion` | Called with the next event or an error.

- `readNextSamples(maxCount:maxDuration:_:)`

  Reads a bounded batch of audio decryptor events.

  #### Declaration

  Swift

      func readNextSamples(
          maxCount: Int,
          maxDuration: CMTime?,
          _ completion: @escaping (Swift.Result<[SampleInfoEvent], PRESTOerror>) -> Void
      )

  #### Parameters

  `maxCount` | Maximum number of sample events to return.
  `maxDuration` | Maximum aggregate sample duration to return. Format and discontinuity events do not count towards the duration.
  `completion` | Called with the next event batch or an error.

- `streamSamples(onEvent:completion:)`

  Streams audio decryptor events until the end of the stream is reached.

  This is a convenience wrapper around repeated reads. The `onEvent` closure receives all event types, including `.endOfStream`.

  #### Declaration

  Swift

      func streamSamples(
          onEvent: @escaping (SampleInfoEvent) -> Void,
          completion: @escaping (Swift.Result<Void, PRESTOerror>) -> Void
      )

  #### Parameters

  `onEvent` | Called for each event as it is read, including `.endOfStream`.
  `completion` | Called when streaming reaches end of stream or fails.

- `close()`

  Closes the decryptor and releases associated session resources.

  Call this when the application no longer needs samples. A closed decryptor should not be reused; create a new instance for another extraction pass.

  #### Declaration

  Swift

      func close()