Skip to content

Commit

Permalink
fix issue where videos were being reused incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
esetnik committed Nov 22, 2017
1 parent dfed9de commit e28226b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Aerial.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
AA7E2E5E1FC62E8B00E5F320 /* AerialPlayerItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA7E2E5D1FC62E8B00E5F320 /* AerialPlayerItem.swift */; };
FA143CE61BDA3EEF0041A82B /* AVKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA143CE51BDA3EEF0041A82B /* AVKit.framework */; };
FA36BD3F1BE57F8E00D5E03B /* VideoDownload.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA36BD3E1BE57F8E00D5E03B /* VideoDownload.swift */; };
FA36BD401BE57F8E00D5E03B /* VideoDownload.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA36BD3E1BE57F8E00D5E03B /* VideoDownload.swift */; };
Expand Down Expand Up @@ -57,6 +58,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
AA7E2E5D1FC62E8B00E5F320 /* AerialPlayerItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AerialPlayerItem.swift; sourceTree = "<group>"; };
FA143CD61BDA3E880041A82B /* AerialApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AerialApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
FA143CE51BDA3EEF0041A82B /* AVKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVKit.framework; path = System/Library/Frameworks/AVKit.framework; sourceTree = SDKROOT; };
FA36BD3E1BE57F8E00D5E03B /* VideoDownload.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = VideoDownload.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
Expand Down Expand Up @@ -218,6 +220,7 @@
children = (
FAC36F431BE1756D007F2A20 /* AerialView.swift */,
FAC36F441BE1756D007F2A20 /* CheckCellView.swift */,
AA7E2E5D1FC62E8B00E5F320 /* AerialPlayerItem.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -454,6 +457,7 @@
FAC36F571BE1756D007F2A20 /* PreferencesWindowController.swift in Sources */,
FAC36F671BE1778C007F2A20 /* ManifestLoader.swift in Sources */,
FAC36F591BE1756D007F2A20 /* AerialVideo.swift in Sources */,
AA7E2E5E1FC62E8B00E5F320 /* AerialPlayerItem.swift in Sources */,
FAF450211BE2B45D00C1F98A /* VideoLoader.swift in Sources */,
FAC36F6A1BE1780B007F2A20 /* Debug.swift in Sources */,
FAB22A7E1BE17D7D0065C0F5 /* AssetLoaderDelegate.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions Aerial/Resources/PreferencesWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<window identifier="preferencesWindow" title="Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="0.0" y="0.0" width="625" height="365"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1418"/>
<view key="contentView" identifier="windowView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="625" height="365"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand All @@ -41,7 +41,7 @@
<font key="font" metaFont="system"/>
<tabViewItems>
<tabViewItem label="Main" identifier="1" id="dfl-QA-fvD">
<view key="view" id="zsM-Ha-kCO">
<view key="view" ambiguous="YES" id="zsM-Ha-kCO">
<rect key="frame" x="10" y="33" width="127" height="0.0"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -202,7 +202,7 @@
</view>
</tabViewItem>
<tabViewItem label="Cache" identifier="2" id="kp1-xv-zNG">
<view key="view" ambiguous="YES" id="93q-v8-yxM">
<view key="view" id="93q-v8-yxM">
<rect key="frame" x="10" y="33" width="607" height="286"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down
6 changes: 5 additions & 1 deletion Aerial/Source/Models/AerialVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import Foundation

class AerialVideo: CustomStringConvertible {
class AerialVideo: CustomStringConvertible, Equatable {
static func ==(lhs: AerialVideo, rhs: AerialVideo) -> Bool {
return lhs.id == rhs.id && lhs.url == rhs.url
}

let id: String
let name: String
let type: String
Expand Down
7 changes: 6 additions & 1 deletion Aerial/Source/Models/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ManifestLoader {
}
}

func randomVideo() -> AerialVideo? {
func randomVideo(excluding: [AerialVideo]) -> AerialVideo? {
let shuffled = loadedManifest.shuffled()
for video in shuffled {
let inRotation = preferences.videoIsInRotation(videoID: video.id)
Expand All @@ -38,6 +38,11 @@ class ManifestLoader {
continue
}

if excluding.contains(video) {
debugLog("video is excluded because it's already in use: \(video)")
continue
}

// check if we're in offline mode
if offlineMode == true {
if video.isAvailableOffline == false {
Expand Down
20 changes: 20 additions & 0 deletions Aerial/Source/Views/AerialPlayerItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// AerialPlayerItem.swift
// Aerial
//
// Created by Ethan Setnik on 11/22/17.
// Copyright © 2017 John Coates. All rights reserved.
//
import AVFoundation
import AVKit

class AerialPlayerItem: AVPlayerItem {
var video: AerialVideo?

init(video: AerialVideo) {
let videoURL = video.url
let asset = CachedOrCachingAsset(videoURL)
super.init(asset: asset, automaticallyLoadedAssetKeys: nil)
self.video = video
}
}
18 changes: 10 additions & 8 deletions Aerial/Source/Views/AerialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ class AerialView: ScreenSaverView {
debugLog("playing next video for player \(String(describing: player))")
}

// MARK: - Playing Videos

func playNextVideo() {
let notificationCenter = NotificationCenter.default

Expand All @@ -221,18 +219,22 @@ class AerialView: ScreenSaverView {
AerialView.previewView?.playerLayer.player = self.player
}

let randomVideo = ManifestLoader.instance.randomVideo()
// get a list of current videos that should be excluded from the candidate selection
// for the next video. This prevents the same video from being shown twice in a row
// as well as the same video being shown on two different monitors even when sharingPlayers
// is false
let currentVideos: [AerialVideo] = AerialView.players.flatMap { (player) -> AerialVideo? in
(player.currentItem as? AerialPlayerItem)?.video
}

let randomVideo = ManifestLoader.instance.randomVideo(excluding: currentVideos)

guard let video = randomVideo else {
NSLog("Aerial: Error grabbing random video!")
return
}
let videoURL = video.url

let asset = CachedOrCachingAsset(videoURL)
// let asset = AVAsset(URL: videoURL)

let item = AVPlayerItem(asset: asset)
let item = AerialPlayerItem(video: video)

player.replaceCurrentItem(with: item)

Expand Down

0 comments on commit e28226b

Please sign in to comment.