Top Banner
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Waiting, looping, switching, widening, optimizing Media #WWDC16 Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect
155

Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Jun 11, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Waiting, looping, switching, widening, optimizing

Media #WWDC16

Session 503

Advances in AVFoundation Playback

Sam Bushell Media Systems Architect

Page 2: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVFoundation

Page 3: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

File Playback

Editing

ExportVideo Capture

Video Effects

Audio Mixing

Video Processing

Photo Capture

Network Playback

Metadata

Subtitles

Alternate Audio

AVFoundation

Page 4: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

File Playback

Editing

ExportVideo Capture

Video Effects

Audio Mixing

Video Processing

Photo Capture

Network Playback

Metadata

Subtitles

Alternate Audio

AVFoundation

Page 5: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Kinds of Playback

4

Page 6: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Kinds of Playback

4

Local File file:///.../example.MOV

Page 7: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Kinds of Playback

4

Local File file:///.../example.MOV

Progressive Download https://example.com/example.MOV

Page 8: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Kinds of Playback

4

Local File file:///.../example.MOV

Progressive Download https://example.com/example.MOV

HTTP Live Streaming master playlist

Page 9: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Kinds of Playback

4

Local File file:///.../example.MOV

Progressive Download https://example.com/example.MOV

HTTP Live Streaming master playlist

video 6Mbit playlistvideo 4Mbit playlistvideo 2Mbit playlistaudio stereo playlistaudio surround playlist

Page 10: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Kinds of Playback

4

Local File file:///.../example.MOV

Progressive Download https://example.com/example.MOV

HTTP Live Streaming master playlist

video 6Mbit playlistvideo 4Mbit playlistvideo 2Mbit playlistaudio stereo playlistaudio surround playlist

segmentssegmentssegmentssegmentssegments

Page 11: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Overview

Page 12: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Overview

Automatic waiting for buffering

Page 13: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Overview

Automatic waiting for bufferingSimple way to loop playback

Page 14: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Overview

Automatic waiting for bufferingSimple way to loop playbackPlayback refinements

Page 15: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Overview

Automatic waiting for bufferingSimple way to loop playbackPlayback refinementsWide color

Page 16: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Overview

Automatic waiting for bufferingSimple way to loop playbackPlayback refinementsWide colorBest practices for being awesome

Page 17: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

BufferingPlease wait...

6

Page 18: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Media Playback Over the Internet

Playback is at the mercy of the network!• Start too soon → playback may stall

• Start too late → user unhappy

• Start when likely to keep up → just right

Page 19: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

ExistingAVPlayerItem Buffering State Properties

playbackLikelyToKeepUp

playbackBufferFull

playbackBufferEmpty

For progressive-download playback, in iOS 9• Wait until playbackLikelyToKeepUp or playbackBufferFull before setting AVPlayer.rate

For HLS, rules are simpler• Set AVPlayer.rate and it will automatically wait for buffering before playback begins

Page 20: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayer in iOS 10 / macOS Sierra / tvOS 10

Same rules for progressive and HLS• Set AVPlayer.rate when user clicks play• Automatically waits to buffer to avoid stalling

If network drops and playback stalls, playback will automatically resume when buffered

NEW

Page 21: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

App A

AVFoundation

AVKit MediaPlayer (deprecated)

AVFoundation AVFoundation

App B

App C

Page 22: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Autoplay or Autowait?

Page 23: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

automaticallyWaitsToMinimizeStalling

Page 24: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playing

Page 25: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playingplay()

Page 26: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playingplay() likelyToKeepUp

Page 27: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playingplay() likelyToKeepUp

bufferEmpty

Page 28: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playingplay() likelyToKeepUp

pause()

bufferEmpty

Page 29: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playingplay() likelyToKeepUp

pause()

pause()

bufferEmpty

Page 30: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

waitingpaused playing

playImmediately(atRate:)

play() likelyToKeepUp

pause()

pause()

bufferEmpty

playImmediately(atRate:)

Page 31: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

Page 32: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

Page 33: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

Page 34: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

NEW

Page 35: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

AVPlayer.reasonForWaitingToPlay

NEW

NEW

Page 36: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

AVPlayer.reasonForWaitingToPlay

waiting

NEW

NEW

Page 37: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

AVPlayer.reasonForWaitingToPlay

waiting

1.0

NEW

NEW

Page 38: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

AVPlayer.reasonForWaitingToPlay

waiting

1.0

0.0

NEW

NEW

Page 39: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

AVPlayer.reasonForWaitingToPlay

waiting

1.0

0.0

WaitingToPlayAtSpecifiedRateNEW

NEW

Page 40: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Might not mean what you thought it meantAVPlayer.rate

AVPlayer.rate the app's requested playback rate

AVPlayerItem.timebase.rate the rate at which playback is actually occurring

AVPlayer.timeControlStatus Paused, WaitingToPlayAtSpecifiedRate, Playing

AVPlayer.reasonForWaitingToPlay

waiting

1.0

0.0

WaitingToPlayAtSpecifiedRate

WaitingToMinimizeStallsReason

NEW

NEW

Page 41: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

DemoAutowait

Moritz Wittenhagen

Page 42: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 43: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 44: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 45: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 46: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 47: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 48: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 49: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 50: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 51: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture
Page 52: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

rate vs timeControlStatus

AVPlayer.rate AVPlayer.timeControlStatus

0.0

1.0

1.0

waiting

paused

playing

Page 53: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Finding the Network Link Conditioner

Page 54: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Finding the Network Link Conditioner

Page 55: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Cautions

Page 56: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Cautions

Enabled automatically if app linked on or after iOS 10, OSX 10.12, tvOS 10• AVPlayer.automaticallyWaitsToMinimizeStalling = true

Page 57: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Cautions

Enabled automatically if app linked on or after iOS 10, OSX 10.12, tvOS 10• AVPlayer.automaticallyWaitsToMinimizeStalling = true

Opt out if using setRate(..., time:..., atHostTime:...) to synchronize playback with external timeline• AVPlayer.automaticallyWaitsToMinimizeStalling = false

• Otherwise, NSException

Page 58: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Cautions

Enabled automatically if app linked on or after iOS 10, OSX 10.12, tvOS 10• AVPlayer.automaticallyWaitsToMinimizeStalling = true

Opt out if using setRate(..., time:..., atHostTime:...) to synchronize playback with external timeline• AVPlayer.automaticallyWaitsToMinimizeStalling = false

• Otherwise, NSException

Never use the player rate to project currentTime into the future• Use currentItem's timebase rate for that instead

Page 59: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Made easierLooping

Page 60: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

When end reached, rewind?How Do You Loop an AVPlayerItem?

AVPlayer

AVPlayerItem

Page 61: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

When end reached, rewind?How Do You Loop an AVPlayerItem?

AVPlayer

- AVPlayerItem

Page 62: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

When end reached, rewind?How Do You Loop an AVPlayerItem?

AVPlayerItemAVPlayerItem AVPlayerItem

Latency Preroll

Page 63: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

When end reached, rewind?How Do You Loop an AVPlayerItem?

AVPlayerItem

AVPlayerItem

AVPlayerItem

Preroll

Page 64: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVQueuePlayer

AVPlayerItem

AVQueuePlayer

AVPlayerItem AVPlayerItem

Page 65: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVQueuePlayer

AVPlayerItem

AVQueuePlayer

AVPlayerItem AVPlayerItem

AVAsset

Page 66: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerItem

The "Treadmill"

AVQueuePlayer

AVPlayerItem AVPlayerItem

Page 67: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerItem

The "Treadmill"

AVQueuePlayer

AVPlayerItem AVPlayerItem

Page 68: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerItem

The "Treadmill"

AVPlayerItem

AVQueuePlayer

AVPlayerItem AVPlayerItem

Page 69: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerItem

The "Treadmill"

AVPlayerItem

AVQueuePlayer

AVPlayerItem AVPlayerItem

Page 70: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerItem

The "Treadmill"

AVPlayerItem

AVQueuePlayer

AVPlayerItem AVPlayerItem AVPlayerItem

Page 71: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

// Looping using AVQueuePlayer

override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change:

[NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {

if context == &ObserverContexts.currentItem {

guard let player = player else { return }

if player.items().isEmpty {

// Play queue emptied out due to bad player item. End looping.

}

else {

if let itemRemoved = change?[.oldKey] as? AVPlayerItem {

itemRemoved.seek(to: kCMTimeZero)

stopObserving()

player.insert(itemRemoved, after: nil)

startObserving()

}

}

}

// else ...

}

Page 72: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

// Looping using AVQueuePlayer

override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change:

[NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {

if context == &ObserverContexts.currentItem {

guard let player = player else { return }

if player.items().isEmpty {

// Play queue emptied out due to bad player item. End looping.

}

else {

if let itemRemoved = change?[.oldKey] as? AVPlayerItem {

itemRemoved.seek(to: kCMTimeZero)

stopObserving()

player.insert(itemRemoved, after: nil)

startObserving()

}

}

}

// else ...

}

Page 73: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

// Looping using AVQueuePlayer

override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change:

[NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {

if context == &ObserverContexts.currentItem {

guard let player = player else { return }

if player.items().isEmpty {

// Play queue emptied out due to bad player item. End looping.

}

else {

if let itemRemoved = change?[.oldKey] as? AVPlayerItem {

itemRemoved.seek(to: kCMTimeZero)

stopObserving()

player.insert(itemRemoved, after: nil)

startObserving()

}

}

}

// else ...

}

Page 74: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

// Looping using AVQueuePlayer

override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change:

[NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {

if context == &ObserverContexts.currentItem {

guard let player = player else { return }

if player.items().isEmpty {

// Play queue emptied out due to bad player item. End looping.

}

else {

if let itemRemoved = change?[.oldKey] as? AVPlayerItem {

itemRemoved.seek(to: kCMTimeZero)

stopObserving()

player.insert(itemRemoved, after: nil)

startObserving()

}

}

}

// else ...

}

Page 75: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerLooper

AVQueuePlayer

AVAsset

AVPlayerLooper

AVPlayerItem

Template

NEW

Page 76: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerLooper

AVQueuePlayer

AVAsset

AVPlayerItem AVPlayerItem AVPlayerItem

AVPlayerLooper

AVPlayerItem

Template

NEW

Page 77: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerLooper

player = AVQueuePlayer()

playerLayer = AVPlayerLayer(player: player)

playerItem = AVPlayerItem(url: videoURL)

playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)

player.play()

Page 78: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

DemoAVPlayerLooper

Page 79: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Make sure audio and video tracks are same lengthOptimizing Movies for Looping

Video

Audio

Page 80: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Make sure audio and video tracks are same lengthOptimizing Movies for Looping

Video

Audio

Video

Audio

Video

Audio

Page 81: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Make sure audio and video tracks are same lengthOptimizing Movies for Looping

Video

Audio

Video

Audio

Video

Audio

Page 82: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Make sure audio and video tracks are same lengthOptimizing Movies for Looping

Video

Audio

Video

Audio

Video

Audio

Page 83: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Under the hoodPlayback Refinements

Page 84: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

When Tracks Come and Go During Playback

Video

English Audio

English Subtitles

French Audio

Spanish Subtitles

Page 85: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

When Tracks Come and Go During Playback

Video

English Audio

English Subtitles

French Audio

Spanish Subtitles

Page 86: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Where once there were glitches Some More Smoothness

• Adding / Removing the only AVPlayerLayer on playing AVPlayer• Changing subtitle language on playing AVPlayer• Changing audio language on playing AVPlayer• Manually disabling / enabling tracks on playing AVPlayer

Page 87: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Preparing for Wide Color Video

Page 88: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Color Space Tagging in Media Files

Color space information is part of the metadata of video tracks

Standard tag numbers defined in ISO/IEC 23001-8, "Coding Independent Code Points"

SD HD P3 D65Color Primaries Rec. 601 Rec. 709 P3 D65

Transfer Characteristics Rec. 709 Rec. 709 Rec. 709

Y'CbCr Matrix Rec. 601 Rec. 709 Rec. 709

Page 89: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Detecting Wide Color Tags

Use AVMediaCharacteristicUsesWideGamutColorSpace

let wideGamutTracks = asset.tracks(

withMediaCharacteristic:AVMediaCharacteristicUsesWideGamutColorSpace)

if wideGamutTracks.count > 0 {

// use wide color aware processing

}

else {

// use Rec 709 processing

}

NEW

Page 90: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Specifying Working Color Space

AVPlayerItemVideoOutput, AVAssetReaderOutput and AVAssetWriterInput now support color space specification in outputSettings

let exampleSettings =

[AVVideoColorPropertiesKey:

[AVVideoColorPrimariesKey: AVVideoColorPrimaries_P3_D65,

AVVideoTransferFunctionKey: AVVideoTransferFunction_ITU_R_709_2,

AVVideoYCbCrMatrixKey: AVVideoYCbCrMatrix_ITU_R_709_2]]

let videoOutput = AVPlayerItemVideoOutput(outputSettings:exampleSettings)

let readerOutput = AVAssetReaderOutput(outputSettings:exampleSettings)

let writerInput = AVAssetWriterInput(mediaType:AVMediaTypeVideo, outputSettings:exampleSettings)

NEW

Page 91: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Preserving Wide Color Space

Alternatively, ask AVPlayerItemVideoOutput or AVAssetReaderOutput to keep buffers in original color space via AVVideoAllowWideColorKey

let allowWideColorSettings = [AVVideoAllowWideColorKey:true]

let videoOutput = AVPlayerItemVideoOutput(outputSettings:allowWideColorSettings)

let readerOutput = AVAssetReaderOutput(outputSettings:allowWideColorSettings)

NEW

Page 92: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Specifying working color spaceVideo Composition

let videoComposition = AVMutableVideoComposition()

videoComposition.colorPrimaries = AVVideoColorPrimaries_P3_D65

videoComposition.colorTransferFunction = AVVideoTransferFunction_ITU_R_709_2

videoComposition.colorYCbCrMatrix = AVVideoYCbCrMatrix_ITU_R_709_2

NEW

Page 93: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Declaring wide color awarenessCustom Video Compositor NEW

class MyCustomVideoCompositor : AVVideoCompositing {

// ...

var supportsWideColorSourceFrames: Boolean { return true }

}

Page 94: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Explicitly Tagging Buffers

If you generate source buffers for rendering, you may need to tag them

CVBufferSetAttachment(pixelBuffer, kCVImageBufferColorPrimariesKey,

kCVImageBufferColorPrimaries_P3_D65, kCVAttachmentMode_ShouldPropagate)

CVBufferSetAttachment(pixelBuffer, kCVImageBufferTransferFunctionKey,

kCVImageBufferTransferFunction_ITU_R_709_2, kCVAttachmentMode_ShouldPropagate)

CVBufferSetAttachment(pixelBuffer, kCVImageBufferYCbCrMatrixKey,

kCVImageBufferYCbCrMatrix_ITU_R_709_2, kCVAttachmentMode_ShouldPropagate)

writerAdaptor.append(pixelBuffer, withPresentationTime: PTS)

Page 95: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

How can I make my videos start as fast as possible?Best Practices for Playback

Page 96: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Speeding Up Local File Playback

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

playerItem)playerItem:

Page 97: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

1. set up audio-only playback

Speeding Up Local File Playback

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

playerItem)playerItem:

Page 98: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

1. set up audio-only playback

2. set up audio+video playback

Speeding Up Local File Playback

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

playerItem)playerItem:

Page 99: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

player.replaceCurrentItemWithPlayerItem(playerItem)

Order Matters

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

)

playerItem

Ask for the final goal first

Page 100: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

player.replaceCurrentItemWithPlayerItem(playerItem)

Order Matters

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

)

playerItem

Ask for the final goal first

Page 101: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

player.replaceCurrentItemWithPlayerItem(playerItem)

Order Matters

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

)

playerItem

Ask for the final goal first

Page 102: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

1. set up audio+video playbackplayer.replaceCurrentItemWithPlayerItem(playerItem)

Order Matters

let asset = AVURLAsset(url: url)

let playerItem = AVPlayerItem(asset: asset)

let player = AVPlayer(

let playerLayer = AVPlayerLayer(player: player)

)

playerItem

Ask for the final goal first

Page 103: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Best Practice

Configure AVPlayer and AVPlayerItem firstConnect AVPlayerLayer to AVPlayer, or AVPlayerItemVideoOutput to AVPlayerItemplayer.play()

player.replaceCurrentItemWithPlayerItem(playerItem)

Page 104: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Consider the network round-tripsSpeeding Up HTTP Live Streaming

Page 105: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Consider the network round-tripsSpeeding Up HTTP Live Streaming

Retrieve master playlist

App Server

Page 106: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Consider the network round-tripsSpeeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keys

App Server

Page 107: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Consider the network round-tripsSpeeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keysRetrieve selected variant playlist

App Server

Page 108: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Consider the network round-tripsSpeeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keysRetrieve selected variant playlistRetrieve segments

App Server

Page 109: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Consider the network round-tripsSpeeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keysRetrieve selected variant playlistRetrieve segments

Can you do some of these before the user hits "play"?

App Server

Page 110: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Preloading the Master PlaylistSpeeding Up HTTP Live Streaming

var asset = AVURLAsset(url: url)

asset.loadValuesAsynchronously(forKeys: ["duration"], completionHandler: nil)

Page 111: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Compress PlaylistsSpeeding Up HTTP Live Streaming

Compress Master Playlists and Variant Playlists with gzip• Your server may be able to do this for you

Page 112: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Initiate key exchange earlierSpeeding Up FairPlay Streaming Startup

var asset = AVURLAsset(url: url)

asset.resourceLoader.preloadsEligibleContentKeys = true

Master playlist must contain SESSION-KEY declarations

Page 113: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

What else can we do?Speeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keysRetrieve selected variant playlistRetrieve segments

App Server

Page 114: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

What else can we do?Speeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keysRetrieve selected variant playlistRetrieve segments

App Server

Title Card}

Page 115: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

What else can we do?Speeding Up HTTP Live Streaming

Retrieve master playlistRetrieve content keysRetrieve selected variant playlistRetrieve segments

App Server

Title Card}Play}

Page 116: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Preload segments before playbackSpeeding Up HTTP Live Streaming NEW

// on title card

var playerItem = AVPlayerItem(asset: asset)

playerItem.preferredForwardBufferDuration = CMTime(value: 5, timescale: 1)

let player = AVPlayer()

let playerLayer = AVPlayerLayer(player: player) // keep the layer hidden

player.replaceCurrentItemWithPlayerItem(playerItem)

// as soon as playback begins, reset it to default

playerItem.preferredForwardBufferDuration = kCMTimeZero

Page 117: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Preload segments before playbackSpeeding Up HTTP Live Streaming NEW

// on title card

var playerItem = AVPlayerItem(asset: asset)

playerItem.preferredForwardBufferDuration = CMTime(value: 5, timescale: 1)

let player = AVPlayer()

let playerLayer = AVPlayerLayer(player: player) // keep the layer hidden

player.replaceCurrentItemWithPlayerItem(playerItem)

// as soon as playback begins, reset it to default

playerItem.preferredForwardBufferDuration = kCMTimeZero

Page 118: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Preload segments before playbackSpeeding Up HTTP Live Streaming NEW

// on title card

var playerItem = AVPlayerItem(asset: asset)

playerItem.preferredForwardBufferDuration = CMTime(value: 5, timescale: 1)

let player = AVPlayer()

let playerLayer = AVPlayerLayer(player: player) // keep the layer hidden

player.replaceCurrentItemWithPlayerItem(playerItem)

// as soon as playback begins, reset it to default

playerItem.preferredForwardBufferDuration = kCMTimeZero

Page 119: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Improving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 120: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Improving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 121: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Improving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 122: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Improving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 123: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Improving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 124: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Improving Initial Quality

2GDimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 125: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

AVPlayerLayer Improving Initial Quality

Size your AVPlayerLayer appropriately and connect it to AVPlayer early• Before bringing in playerItem

Set AVPlayerLayer.contentsScale on retina iOS devices

Page 126: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Control initial variant selectionImproving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 127: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Control initial variant selectionImproving Initial Quality

?Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 128: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Control initial variant selectionImproving Initial Quality

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 129: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Buffering time to start playback (10-second segments)Improving Initial Quality

Dimensions Video bitrate Buffering time @ 2 Mbit/sec

400 x 224 110 kbit/sec 0.55 sec

400 x 224 400 kbit/sec 2 sec

640 x 360 600 kbit/sec 3 sec

960 x 540 1800 kbit/sec 9 sec

1280 x 720 4500 kbit/sec 22.5 sec

1920 x 1080 11000 kbit/sec 55 sec

Page 130: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Use previous playback's statisticsImproving Initial Quality

if let lastAccessLogEvent = previousPlayerItem.accessLog()?.events.last {

lastObservedBitrate = lastAccessLogEvent.observedBitrate

}

Page 131: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option A: Use preferredPeakBitRate to restrict first choiceControlling Initial Variant Selection

Dimensions Video bitrate

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1280 x 720 4500 kbit/sec

1920 x 1080 11000 kbit/sec

Page 132: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option A: Use preferredPeakBitRate to restrict first choiceControlling Initial Variant Selection

Dimensions Video bitrate

1920 x 1080 11000 kbit/sec

1280 x 720 4500 kbit/sec

960 x 540 1800 kbit/sec

640 x 360 600 kbit/sec

400 x 224 400 kbit/sec

400 x 224 110 kbit/sec

Page 133: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option A: Use preferredPeakBitRate to restrict first choiceControlling Initial Variant Selection

Dimensions Video bitrate

1920 x 1080 11000 kbit/sec

1280 x 720 4500 kbit/sec

960 x 540 1800 kbit/sec

640 x 360 600 kbit/sec

400 x 224 400 kbit/sec

400 x 224 110 kbit/sec

// before playback

playerItem.preferredPeakBitRate = 2000

// shortly after playback starts

playerItem.preferredPeakBitRate = 0

Page 134: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option A: Use preferredPeakBitRate to restrict first choiceControlling Initial Variant Selection

Dimensions Video bitrate

1920 x 1080 11000 kbit/sec

1280 x 720 4500 kbit/sec

960 x 540 1800 kbit/sec

640 x 360 600 kbit/sec

400 x 224 400 kbit/sec

400 x 224 110 kbit/sec

// before playback

playerItem.preferredPeakBitRate = 2000

// shortly after playback starts

playerItem.preferredPeakBitRate = 0

Page 135: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option A: Use preferredPeakBitRate to restrict first choiceControlling Initial Variant Selection

Dimensions Video bitrate

1920 x 1080 11000 kbit/sec

1280 x 720 4500 kbit/sec

960 x 540 1800 kbit/sec

640 x 360 600 kbit/sec

400 x 224 400 kbit/sec

400 x 224 110 kbit/sec

// before playback

playerItem.preferredPeakBitRate = 2000

// shortly after playback starts

playerItem.preferredPeakBitRate = 0

Page 136: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option B: Dynamically rewrite master playlistControlling Initial Variant Selection

Dimensions Video bitrate

1280 x 720 4500 kbit/sec

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1920 x 1080 11000 kbit/sec

Page 137: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option B: Dynamically rewrite master playlistControlling Initial Variant Selection

Dimensions Video bitrate

1280 x 720 4500 kbit/sec

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1920 x 1080 11000 kbit/sec

var asset = AVURLAsset(url: NSURL(string: "myscheme://file.m3u8")!)

asset.resourceLoader.setDelegate(...)

Page 138: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option B: Dynamically rewrite master playlistControlling Initial Variant Selection

Dimensions Video bitrate

1280 x 720 4500 kbit/sec

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1920 x 1080 11000 kbit/sec

var asset = AVURLAsset(url: NSURL(string: "myscheme://file.m3u8")!)

asset.resourceLoader.setDelegate(...)

Page 139: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Option B: Dynamically rewrite master playlistControlling Initial Variant Selection

Dimensions Video bitrate

1280 x 720 4500 kbit/sec

400 x 224 110 kbit/sec

400 x 224 400 kbit/sec

640 x 360 600 kbit/sec

960 x 540 1800 kbit/sec

1920 x 1080 11000 kbit/sec

var asset = AVURLAsset(url: NSURL(string: "myscheme://file.m3u8")!)

asset.resourceLoader.setDelegate(...)

Page 140: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Profile Your Code Too

Page 141: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Profile Your Code Too

Look for delays in your code, before AVFoundation is called

Page 142: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Profile Your Code Too

Look for delays in your code, before AVFoundation is calledDon't wait for likelyToKeepUp notification before setting rate

Page 143: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Profile Your Code Too

Look for delays in your code, before AVFoundation is calledDon't wait for likelyToKeepUp notification before setting rate

Make sure you release AVPlayers and AVPlayerItems from old playback sessions

Page 144: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Profile Your Code Too

Look for delays in your code, before AVFoundation is calledDon't wait for likelyToKeepUp notification before setting rate

Make sure you release AVPlayers and AVPlayerItems from old playback sessionsUse Allocations Instrument to check AVPlayer and AVPlayerItem lifespans

Page 145: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Profile Your Code Too

Look for delays in your code, before AVFoundation is calledDon't wait for likelyToKeepUp notification before setting rate

Make sure you release AVPlayers and AVPlayerItems from old playback sessionsUse Allocations Instrument to check AVPlayer and AVPlayerItem lifespans

Suspend other network activity in your app during network playback

Page 146: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Summary

Page 147: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Summary

automaticallyWaitsToMinimizeStalling

Page 148: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Summary

automaticallyWaitsToMinimizeStallingAVPlayerLooper

Page 149: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Summary

automaticallyWaitsToMinimizeStallingAVPlayerLooperEnabling and disabling tracks during playback is smoother

Page 150: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Summary

automaticallyWaitsToMinimizeStallingAVPlayerLooperEnabling and disabling tracks during playback is smootherPrepare for wide color video

Page 151: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Summary

automaticallyWaitsToMinimizeStallingAVPlayerLooperEnabling and disabling tracks during playback is smootherPrepare for wide color videoOptimize playback startup through cunning and measurement

Page 152: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

More Information

https://developer.apple.com/wwdc16/503

Page 153: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Related Sessions

Advances in iOS Photography Pacific Heights Tuesday 11:00AM

What's New in HTTP Live Streaming Mission Wednesday 3:00PM

Working with Wide Color Mission Thursday 1:40PM

AVKit on tvOS Presidio Friday 11:00AM

HTTP Live Streaming Authoring and Validation Video Watch on Demand

Page 154: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture

Labs

AVFoundation / AVKit Lab Graphics, Games, and Media Lab C Wednesday 9:00AM

AVFoundation / AVKit Lab Graphics, Games, and Media Lab C Wednesday 1:00PM

HTTP Live Streaming Lab Graphics, Games, and Media Lab C Wednesday 4:00PM

Photo Capture Lab Graphics, Games, and Media Lab C Thursday 9:00AM

AVFoundation / HTTP Live Steaming Lab Graphics, Games, and Media Lab D Thursday 9:00AM

AVKit Lab Graphics, Games, and Media Lab C Friday 1:00PM

Page 155: Advances in AVFoundation Playback - Apple Inc. · Session 503 Advances in AVFoundation Playback Sam Bushell Media Systems Architect. AVFoundation. File Playback Editing Video Capture