NetworkConfig

@objcMembers
@objc(BMPNetworkConfig)
public class NetworkConfig : NSObject

The network config gives the ability to influence network requests.

  • Type definition for the handler that allows changing HTTP requests.

    Note

    Customize the preprocessing logic based on the request type and request details to optimize request handling.

    Note

    While it’s possible to preprocess all HTTP requests, it’s important to tailor your logic to the specific type of request being handled. Extensive processing might potentially lead to playback delays or timeouts.

    Declaration

    Swift

    public typealias PreprocessHttpRequestHandler = (
        _ type: HttpRequestType,
        _ request: HttpRequest,
        _ completionHandler: @escaping (_ request: HttpRequest) -> Void
    ) -> Void

    Parameters

    type

    The type of the request.

    request

    The request that should be modified.

    completionHandler

    Completion handler which must be called with the modified HTTP request.

  • Type definition for the handler that needs to be called if a retry should happen from RetryHttpRequestHandler.

    Note

    Ensure that the retry delay is appropriate for the failure scenario to avoid overwhelming the server or client.

    Declaration

    Swift

    public typealias RetryHandler = (_ retryDelay: TimeInterval, _ request: HttpRequest) -> Void

    Parameters

    retryDelay

    The delay in seconds before the retry should happen.

    request

    The request that should be retried. This can be the original failed request or a modified version of it.

  • Type definition for the handler that needs to be called if no retry should happen from RetryHttpRequestHandler.

    Declaration

    Swift

    public typealias AbortHandler = () -> Void
  • Type definition to implement custom retry behavior for failed HTTP requests. Implementing it allows to define how and when retries should occur based on the request type, retry attempt, and response.

    Note

    Customize the retry logic based on the type of request, number of retries, and response details to optimize request handling.

    Declaration

    Swift

    public typealias RetryHttpRequestHandler = (
        _ type: HttpRequestType,
        _ retry: Int,
        _ response: HttpResponse,
        _ retryHandler: @escaping RetryHandler,
        _ abortHandler: @escaping AbortHandler
    ) -> Void

    Parameters

    type

    The type of the failed request.

    retry

    The current retry attempt number.

    response

    The response of the failed request, which contains a reference to the failed request.

    retryHandler

    A handler that must be called if a retry should happen, specifying the delay and the request to retry. Not calling this handler or abortHandler will result in a timeout (~20 seconds) imposed by AVFoundation, causing the request to fail.

    abortHandler

    A handler that must be called if no retry should happen. Not calling this handler or retryHandler will result in a timeout (~20 seconds) imposed by AVFoundation, causing the request to fail.

  • Type definition for the handler that allows processing HTTP responses.

    Note

    Customize the preprocessing logic based on the request type and response details to optimize response handling.

    Note

    While it’s possible to preprocess all HTTP responses, it’s important to tailor your logic to the specific type of request being handled. Extensive processing might potentially lead to playback delays or timeouts.

    Declaration

    Swift

    public typealias PreprocessHttpResponseHandler = (
        _ type: HttpRequestType,
        _ response: HttpResponse,
        _ completionHandler: @escaping (_ response: HttpResponse) -> Void
    ) -> Void

    Parameters

    type

    The type of the request.

    response

    The response of the request.

    completionHandler

    A completion handler that must be called with the modified HTTP response. Not calling the completion handler prevents the HTTP response from being consumed and blocks the player.

  • Called before an HTTP request is made. This delegate allows modifying the request parameters before the request is sent.

    Limitations

    • Requests of type HttpRequestType.keyHlsAes are not supported.
    • Requests of type HttpRequestType.mediaSubtitles are only supported for side-loaded subtitles.

    Note

    Implement the PreprocessHttpRequestDelegate protocol to customize the preprocessing of supported HTTP requests.

    Declaration

    Swift

    @available(*, deprecated, message: "Use preprocessHttpRequest instead.")
    public weak var preprocessHttpRequestDelegate: PreprocessHttpRequestDelegate?
  • Called before an HTTP request is made. This property can be used to modify the request parameters before the request is sent.

    • Usage Example:

      networkConfig.preprocessHttpRequest = { type, request, completionHandler in
      var modifiedRequest = httpRequest
      
      // Modify the request based on the type
      switch type {
      case .drmLicenseFairplay:
          // Example modification for DRM requests
          modifiedRequest.headers["Authorization"] = "Bearer someAccessToken"
      case .manifestHlsMaster:
          // Example modification for HLS Playlist requests
          modifiedRequest.url = modifiedRequest.url.appendingPathComponent("modified")
      default:
          break
      }
      
      // Call the completion handler with the modified request
      completionHandler(modifiedRequest)
      }
      

    Limitations

    • Requests of type HttpRequestType.keyHlsAes are not supported.
    • Requests of type HttpRequestType.mediaSubtitles are only supported for side-loaded subtitles.

    Note

    Refer to the PreprocessHttpRequestHandler documentation for additional information.

    Declaration

    Swift

    public var preprocessHttpRequest: PreprocessHttpRequestHandler?
  • Called when an HTTP request has failed. This property allows you to override the default retry behavior with a custom handler.

    A request is considered failed if the HTTP response status code is not within the 200-299 range:

    let success = response.statusCode >= 200 && response.statusCode < 300
    
    • Usage Example:

      let retryHttpRequestHandler = { requestType, retryCount, response, retryHandler, abortHandler in
      // Retry 3 times
      guard retryCount <= 3 else {
          // Abort retrying when threshold was reached
          abortHandler()
          return
      }
      
      // Customize delay for the next retry. For instance use exponential backoff:
      let retryDelay: TimeInterval = exp(0.25 * Double(retryCount))
      // Retry the original request again
      retryHandler(retryDelay, response.request)
      }
      

    Default Behavior

    If no custom handler is provided, the default retry logic will be applied:

    • The request will be retried up to a maximum of 3 times.
    • The delay between retries is calculated using a custom exponential backoff formula, with a maximum delay of 5 seconds:
      • For each retry attempt, the delay is determined by the formula exp(0.25 * retryCount), which produces a gradually increasing delay.
      • The delay is capped at 5 seconds to ensure retries occur within a reasonable timeframe.
    • If the retry count exceeds 3 attempts, the request will not be retried, and the abortHandler will be called.

    Customization

    When a custom retryHttpRequest handler is set, no internal retry handling is done at all. The custom handler needs to manage all retries. Some considerations for implementing your own handler include:

    • Ensure to set a maximum number of allowed retries to avoid retrying an unreasonable number of times.
    • Be aware of an underlying AVFoundation limit of approximately 20 seconds for performing retries. If a failed request is not successfully retried within that timeframe, an error will occur. Within that 20-second window, the number of retries that can be performed is unlimited.

    Limitations

    • Requests of type HttpRequestType.keyHlsAes are not supported.
    • Requests of type HttpRequestType.mediaSubtitles are only supported for side-loaded subtitles.

    Note

    Refer to the RetryHttpRequestHandler documentation for detailed usage examples and additional information.

    Declaration

    Swift

    public var retryHttpRequest: RetryHttpRequestHandler?
  • Called after an HTTP response is received. This property can be used to access or change properties of the response before it gets into the player.

    • Usage Example:

      let preprocessHttpResponseHandler: NetworkConfig.PreprocessHttpResponseHandler = { type, response, completionHandler in
      var modifiedResponse = response
      
      // Modify the response as needed based on the request type
      switch type {
      case .manifestHlsVariant:
          modifiedResponse = modifyResponseForHlsVariantType(response)
      case .mediaSubtitles:
          modifiedResponse = modifyResponseForMediaPosterType(response)
      default:
          break
      }
      
      // Call the completion handler with the modified response
      completionHandler(modifiedResponse)
      }
      

    Limitations

    • Requests of type HttpRequestType.keyHlsAes are not supported.
    • Requests of type HttpRequestType.mediaSubtitles are only supported for side-loaded subtitles.

    Note

    Refer to the PreprocessHttpResponseHandler documentation for a detailed usage example and additional information.

    Declaration

    Swift

    public var preprocessHttpResponse: PreprocessHttpResponseHandler?