Content-Length: 525954 | pFad | http://github.com/muxinc/mux-player-swift/commit/87865e66bd2192937f23fd36d8c419b97c5fc230

0A feat: AVPlayerViewController signed playback URL convenience APIs (#8) · muxinc/mux-player-swift@87865e6 · GitHub
Skip to content

Commit

Permalink
feat: AVPlayerViewController signed playback URL convenience APIs (#8)
Browse files Browse the repository at this point in the history
* Add identifying information and year to LICENSE

* refactor: API revisions

* chore: Add 5.8 Package.swift

* chore: convert AVPlayerItem constructors to internal

* fix: accept custom domain as a String and prepend with stream
  • Loading branch information
andrewjl-mux authored Oct 2, 2023
1 parent 8c8e083 commit 87865e6
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct MainView: UIViewControllerRepresentable {
class MainViewController: UIViewController {

lazy var playerViewController = AVPlayerViewController(
publicPlaybackID: playbackID
playbackID: playbackID
)

var playbackID: String = "qxb01i6T202018GFS02vp9RIe01icTcDCjVzQpmaB00CUisJ4"
Expand Down Expand Up @@ -75,9 +75,10 @@ class MainViewController: UIViewController {
playerViewController.player?.play()
}

override func viewDidDisappear(_ animated: Bool) {
override func viewWillDisappear(_ animated: Bool) {
playerViewController.player?.pause()
playerViewController.stopMonitoring()
super.viewDidDisappear(animated)
super.viewWillDisappear(animated)
}

}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2023 Mux, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.9

import PackageDescription

Expand Down
44 changes: 44 additions & 0 deletions Package@swift-5.8.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// swift-tools-version: 5.8

import PackageDescription

let package = Package(
name: "MuxAVPlayerSDK",
platforms: [
.iOS(.v15)
],
products: [
.library(
name: "MuxAVPlayerSDK",
targets: ["MuxAVPlayerSDK"]
),
],
dependencies: [
.package(
url: "https://github.com/muxinc/mux-stats-sdk-avplayer",
exact: "3.3.1"
),
.package(
url: "https://github.com/apple/swift-docc-plugin",
from: "1.2.0"
),
],
targets: [
.target(
name: "MuxAVPlayerSDK",
dependencies: [
.product(
name: "MUXSDKStats",
package: "mux-stats-sdk-avplayer"
)
]
),
.testTarget(
name: "MuxAVPlayerSDKTests",
dependencies: [
"MuxAVPlayerSDK"
]
),
]
)

99 changes: 73 additions & 26 deletions Sources/MuxAVPlayerSDK/PublicAPI/Extensions/AVPlayerItem+Mux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,67 @@
import AVFoundation
import Foundation

extension AVPlayerItem {
fileprivate func makePlaybackURL(
playbackID: String,
playbackOptions: PlaybackOptions
) -> URL {

var components = URLComponents()
components.scheme = "https"

if let customDomain = playbackOptions.customDomain {
components.host = "stream.\(customDomain)"
} else {
components.host = "stream.mux.com"
}

components.path = "/\(playbackID).m3u8"

if case PlaybackOptions.PlaybackPolicy.public(let publicPlaybackOptions) = playbackOptions.playbackPolicy {
var queryItems: [URLQueryItem] = []

if publicPlaybackOptions.useRedundantStreams {
queryItems.append(
URLQueryItem(
name: "redundant_streams",
value: "true"
)
)
}

if publicPlaybackOptions.maximumResolutionTier != .default {
queryItems.append(
URLQueryItem(
name: "max_resolution",
value: publicPlaybackOptions.maximumResolutionTier.queryValue
)
)
}

components.queryItems = queryItems
} else if case PlaybackOptions.PlaybackPolicy.signed(let signedPlaybackOptions) = playbackOptions.playbackPolicy {

var queryItems: [URLQueryItem] = []

queryItems.append(
URLQueryItem(
name: "token",
value: signedPlaybackOptions.playbackToken
)
)

components.queryItems = queryItems

}

guard let playbackURL = components.url else {
preconditionFailure("Invalid playback URL components")
}

return playbackURL
}

internal extension AVPlayerItem {

//github.com/ Initializes a player item with a playback URL that
//github.com/ references your Mux Video at the supplied playback ID.
Expand All @@ -17,20 +77,11 @@ extension AVPlayerItem {
//github.com/
//github.com/ - Parameter playbackID: playback ID of the Mux Asset
//github.com/ you'd like to play
convenience init(publicPlaybackID: String) {
guard let baseURL = URL(string: "https://stream.mux.com") else {
preconditionFailure("Invalid base URL string")
}

var components = URLComponents(
url: baseURL,
resolvingAgainstBaseURL: false
convenience init(playbackID: String) {
let playbackURL = makePlaybackURL(
playbackID: playbackID,
playbackOptions: PlaybackOptions()
)
components?.path = "/\(publicPlaybackID).m3u8"

guard let playbackURL = components?.url else {
preconditionFailure("Invalid playback URL components")
}

self.init(url: playbackURL)
}
Expand All @@ -40,20 +91,16 @@ extension AVPlayerItem {
//github.com/ The playback ID must be public.
//github.com/
//github.com/ - Parameters:
//github.com/ - publicPlaybackID: playback ID of the Mux Asset
//github.com/ - playbackID: playback ID of the Mux Asset
//github.com/ you'd like to play
//github.com/ - customDomain: custom playback domain, custom domains
//github.com/ need to be configured as described [here](https://docs.mux.com/guides/video/use-a-custom-domain-for-streaming#use-your-own-domain-for-delivering-videos-and-images) first
convenience init(publicPlaybackID: String, customDomain: URL) {
var components = URLComponents(
url: customDomain,
resolvingAgainstBaseURL: false
convenience init(
playbackID: String,
playbackOptions: PlaybackOptions
) {
let playbackURL = makePlaybackURL(
playbackID: playbackID,
playbackOptions: playbackOptions
)
components?.path = "/\(publicPlaybackID).m3u8"

guard let playbackURL = components?.url else {
preconditionFailure("Invalid playback URL components")
}

self.init(url: playbackURL)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ extension AVPlayerViewController {
//github.com/ Initializes an AVPlayerViewController that's configured
//github.com/ to play your Mux Asset as well as monitor and report
//github.com/ back it's playback performance.
//github.com/ - Parameter publicPlaybackID: playback ID of the Mux
//github.com/ - Parameter playbackID: playback ID of the Mux
//github.com/ Asset you'd like to play
public convenience init(publicPlaybackID: String) {
public convenience init(playbackID: String) {
self.init()

let playerItem = AVPlayerItem(publicPlaybackID: publicPlaybackID)
let playerItem = AVPlayerItem(playbackID: playbackID)

let player = AVPlayer(playerItem: playerItem)

self.player = player

let monitoringOptions = MonitoringOptions(playbackID: publicPlaybackID)
let monitoringOptions = MonitoringOptions(
playbackID: playbackID
)

Monitor.shared.setupMonitoring(
playerViewController: self,
Expand All @@ -33,7 +35,7 @@ extension AVPlayerViewController {
//github.com/ to play your Mux Asset as well as monitor and report
//github.com/ back it's playback performance.
//github.com/ - Parameters:
//github.com/ - publicPlaybackID: playback ID of the Mux Asset
//github.com/ - playbackID: playback ID of the Mux Asset
//github.com/ you'd like to play
//github.com/ - monitoringOptions: Options to customize monitoring
//github.com/ data reported by Mux
Expand All @@ -43,7 +45,7 @@ extension AVPlayerViewController {
) {
self.init()

let playerItem = AVPlayerItem(publicPlaybackID: publicPlaybackID)
let playerItem = AVPlayerItem(playbackID: publicPlaybackID)

let player = AVPlayer(playerItem: playerItem)

Expand All @@ -59,23 +61,27 @@ extension AVPlayerViewController {
//github.com/ to play your Mux Asset as well as monitor and report
//github.com/ back it's playback performance.
//github.com/ - Parameters:
//github.com/ - publicPlaybackID: playback ID of the Mux Asset
//github.com/ - playbackID: playback ID of the Mux Asset
//github.com/ you'd like to play
//github.com/ - customDomain: custom playback domain, custom domains
//github.com/ need to be configured as described [here](https://docs.mux.com/guides/video/use-a-custom-domain-for-streaming#use-your-own-domain-for-delivering-videos-and-images) first
convenience init(publicPlaybackID: String, customDomain: URL) {
//github.com/ - play
convenience init(
playbackID: String,
playbackOptions: PlaybackOptions
) {
self.init()

let playerItem = AVPlayerItem(
publicPlaybackID: publicPlaybackID,
customDomain: customDomain
playbackID: playbackID,
playbackOptions: playbackOptions
)

let player = AVPlayer(playerItem: playerItem)

self.player = player

let monitoringOptions = MonitoringOptions(playbackID: publicPlaybackID)
let monitoringOptions = MonitoringOptions(
playbackID: playbackID
)

Monitor.shared.setupMonitoring(
playerViewController: self,
Expand All @@ -87,22 +93,22 @@ extension AVPlayerViewController {
//github.com/ to play your Mux Asset as well as monitor and report
//github.com/ back it's playback performance.
//github.com/ - Parameters:
//github.com/ - publicPlaybackID: playback ID of the Mux Asset
//github.com/ - playbackID: playback ID of the Mux Asset
//github.com/ you'd like to play
//github.com/ - customDomain: custom playback domain, custom
//github.com/ domains need to be configured as described [here](https://docs.mux.com/guides/video/use-a-custom-domain-for-streaming#use-your-own-domain-for-delivering-videos-and-images) first
//github.com/ - monitoringOptions: Options to customize monitoring
//github.com/ data reported by Mux
convenience init(
publicPlaybackID: String,
customDomain: URL,
playbackID: String,
playbackOptions: PlaybackOptions,
monitoringOptions: MonitoringOptions
) {
self.init()

let playerItem = AVPlayerItem(
publicPlaybackID: publicPlaybackID,
customDomain: customDomain
playbackID: playbackID,
playbackOptions: playbackOptions
)

let player = AVPlayer(playerItem: playerItem)
Expand Down
Loading

0 comments on commit 87865e6

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/muxinc/mux-player-swift/commit/87865e66bd2192937f23fd36d8c419b97c5fc230

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy