Skip to main content
Skip table of contents

Network statistics

To test this feature and view the example code, please see the  Apple (FPS) SDK 5 Example Code Quick Start guide.

The CONNECT Player SDK provides classes and interfaces enabling you to access statistics concerning the player and network.

CODE
public class OTVNetworkAnalytics: NSObject {
	public var adaptiveStreaming: AdaptiveStreaming
	public var networkUsage: NetworkUsage
	public var contentServer: ContentServer
}

Network errors and http error code and message: 

CODE
public class OTVNetworkAnalytics: NSObject {
	// Error code, -1001 indicates other network error; 400 indicates http error, check httpError for status code 
  	public internal(set) var error: Int
	// HTTP response status code when gets http error
  	public internal(set) var httpError: Int {
	// HTTP error message
  	public internal(set) var httpErrorMessage: String?
}

Network analytics related to adaptive streaming

CODE
public protocol AdaptiveStreaming {
  /**
   Returns an array of bitrates available in the playlist
   - Returns: an array of bitrates available in the playlist, or nil if unknown
   */
  func availableBitrates() -> [Int]?
  /**
   Returns the bitrate from the playlist that has been selected for playback, in bits per second
   - Returns: the bitrate from the playlist that has been selected for playback, or 0 if unknown
   */
  func selectedBitrate() -> Double
}

Network analytics related to network usage

CODE
public protocol NetworkUsage {
  /**
   Returns the number of bytes downloaded so far
   - Returns: the number of bytes downloaded so far
   */
  func bytesDownloaded() -> Int64

  /**
   Returns the empirical throughput, in bits per second, across all media downloaded
   - Returns: the throughput, in bits per second
   */
  func downloadBitrate() -> Double

  /**
   Returns the video track’s average bit rate, in bits per second
   - Returns: the video track’s average bit rate, in bits per second
   */
  func downloadBitrateAverage() -> Double
}

Network analytics related to the content server

CODE
public protocol ContentServer {
  /**
   Returns the IP address of the content server
   - Returns: the IP address of the content server for the last segment, or nil if unknown
   */
  func finalIPAddress() -> String?

  /**
   Returns the URL of the selected playlist, after any redirects
   - Returns: the URL of the selected playlist, or nil if unknown
   */
  func finalURL() -> String?

  /**
   Returns the original URL of the stream
   - Returns: the original URL of the stream, or nil if unknown
   */
  func url() -> String?
}

Notification and all event type

CODE
public static let OTVNetworkAnalyticsNotification = Notification.Name("OTVNetworkAnalyticsNotification")
public enum Event: Int {
    case selectedBitrateChanged = 0
    case availableBitratesChanged = 1
    case urlChanged = 2
    case errorChanged = 3
  }

Observe the OTVNetworkAnalyticsNotification and get the event

CODE
NotificationCenter.default.addObserver(self,
                                       selector: #selector(handleNetworkAnalyticsNotification(notificaition:)),
                                       name: .OTVNetworkAnalyticsNotification,
                                       object: nil)

func handleNetworkAnalyticsNotification(notificaition: NSNotification) {
	if let otvNetworkNotificationType = notificaition.object as? OTVNetworkAnalytics.Event {
		switch otvNetworkNotificationType {
			case .selectedBitrateChanged:
				if let selectedBitrate = otvplayer?.networkAnalytics?.adaptiveStreaming.selectedBitrate() {
					// Handle selectedBitrate
				}
			case .availableBitratesChanged:
				if let availableBitrates = otvplayer?.networkAnalytics?.adaptiveStreaming.availableBitrates() {
					// Handle availableBitrates
				}	
			case .urlChanged:
				if let url = otvplayer?.networkAnalytics?.contentServer.url() {
					// Handle url
				}
			case .errorChanged:
				if let error = otvplayer?.networkAnalytics?.error {
					if error = -1001 {
						// Other network error
					} else if error == 400, 
						let httpError = otvplayer?.networkAnalytics?.httpError,
						let httpErrorMessage = otvplayer?.networkAnalytics?.httpErrorMessage {
						// Handle http error code and http error message
					}
				}
			}
		}
	}
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.