NCPlayer.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // NCPlayer.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/07/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. import UIKit
  26. import AVFoundation
  27. import MediaPlayer
  28. import JGProgressHUD
  29. import Alamofire
  30. class NCPlayer: NSObject {
  31. internal let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. internal var url: URL
  33. internal var playerToolBar: NCPlayerToolBar?
  34. internal var viewController: UIViewController
  35. internal var autoPlay: Bool
  36. internal var isProxy: Bool
  37. private var imageVideoContainer: imageVideoContainerView
  38. private var detailView: NCViewerMediaDetailView?
  39. private var observerAVPlayerItemDidPlayToEndTime: Any?
  40. private var observerAVPlayertTime: Any?
  41. public var player: AVPlayer?
  42. public var durationTime: CMTime = .zero
  43. public var metadata: tableMetadata
  44. public var videoLayer: AVPlayerLayer?
  45. public var isSubtitleShowed: Bool = false{
  46. didSet {
  47. self.playerToolBar?.changeSubtitleIconTo(visible: isSubtitleShowed)
  48. }
  49. }
  50. public var subtitleUrls: [URL] = []
  51. // MARK: - View Life Cycle
  52. init(url: URL, autoPlay: Bool, isProxy: Bool, imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?, viewController: UIViewController) {
  53. self.url = url
  54. self.autoPlay = autoPlay
  55. self.isProxy = isProxy
  56. self.imageVideoContainer = imageVideoContainer
  57. self.playerToolBar = playerToolBar
  58. self.metadata = metadata
  59. self.detailView = detailView
  60. self.viewController = viewController
  61. super.init()
  62. do {
  63. try AVAudioSession.sharedInstance().setCategory(.playback)
  64. try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.none)
  65. try AVAudioSession.sharedInstance().setActive(true)
  66. } catch {
  67. print(error)
  68. }
  69. openAVPlayer()
  70. }
  71. internal func openAVPlayer() {
  72. #if MFFFLIB
  73. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: NCGlobal.shared.fileNameVideoEncoded) {
  74. self.url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: NCGlobal.shared.fileNameVideoEncoded))
  75. self.isProxy = false
  76. }
  77. if MFFF.shared.existsMFFFSession(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))) {
  78. return
  79. } else {
  80. MFFF.shared.dismissMessage()
  81. }
  82. #endif
  83. self.setUpForSubtitle()
  84. self.isSubtitleShowed = false
  85. print("Play URL: \(self.url)")
  86. player = AVPlayer(url: self.url)
  87. playerToolBar?.setMetadata(self.metadata)
  88. if metadata.livePhoto {
  89. player?.isMuted = false
  90. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  91. player?.isMuted = CCUtility.getAudioMute()
  92. } else {
  93. player?.isMuted = CCUtility.getAudioMute()
  94. if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
  95. player?.seek(to: time)
  96. }
  97. }
  98. player?.currentItem?.asset.loadValuesAsynchronously(forKeys: ["playable"], completionHandler: {
  99. var error: NSError? = nil
  100. let status = self.player?.currentItem?.asset.statusOfValue(forKey: "playable", error: &error) ?? .unknown
  101. DispatchQueue.main.async {
  102. switch status {
  103. case .loaded:
  104. self.durationTime = self.player?.currentItem?.asset.duration ?? .zero
  105. NCManageDatabase.shared.addVideoTime(metadata: self.metadata, time: nil, durationTime: self.durationTime)
  106. self.activateObserver(playerToolBar: self.playerToolBar)
  107. self.videoLayer = AVPlayerLayer(player: self.player)
  108. self.videoLayer!.frame = self.imageVideoContainer.bounds
  109. self.videoLayer!.videoGravity = .resizeAspect
  110. if self.metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  111. self.imageVideoContainer.layer.addSublayer(self.videoLayer!)
  112. self.imageVideoContainer.playerLayer = self.videoLayer
  113. self.imageVideoContainer.metadata = self.metadata
  114. self.imageVideoContainer.image = self.imageVideoContainer.image?.image(alpha: 0)
  115. }
  116. self.playerToolBar?.setBarPlayer(ncplayer: self)
  117. self.generatorImagePreview()
  118. if !(self.detailView?.isShow() ?? false) {
  119. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId":self.metadata.ocId, "enableTimerAutoHide": false])
  120. }
  121. if self.autoPlay {
  122. self.player?.play()
  123. }
  124. break
  125. case .failed:
  126. self.playerToolBar?.hide()
  127. if self.isProxy && NCKTVHTTPCache.shared.getDownloadStatusCode(metadata: self.metadata) == 200 {
  128. let alertController = UIAlertController(title: NSLocalizedString("_error_", value: "Error", comment: ""), message: NSLocalizedString("_video_not_streamed_", comment: ""), preferredStyle: .alert)
  129. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", value: "Yes", comment: ""), style: .default, handler: { _ in
  130. self.downloadVideo()
  131. }))
  132. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", value: "No", comment: ""), style: .default, handler: { _ in }))
  133. self.viewController.present(alertController, animated: true)
  134. } else {
  135. #if MFFFLIB
  136. if error?.code == AVError.Code.fileFormatNotRecognized.rawValue {
  137. self.convertVideo(withAlert: true)
  138. break
  139. }
  140. #endif
  141. if let title = error?.localizedDescription, let description = error?.localizedFailureReason {
  142. NCContentPresenter.shared.messageNotification(title, description: description, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorGeneric, priority: .max)
  143. } else {
  144. NCContentPresenter.shared.messageNotification("_error_", description: "_error_something_wrong_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorGeneric, priority: .max)
  145. }
  146. }
  147. break
  148. case .cancelled:
  149. break
  150. default:
  151. break
  152. }
  153. }
  154. })
  155. }
  156. internal func downloadVideo(requiredConvert: Bool = false) {
  157. guard let view = appDelegate.window?.rootViewController?.view else { return }
  158. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  159. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  160. let hud = JGProgressHUD()
  161. var downloadRequest: DownloadRequest?
  162. hud.indicatorView = JGProgressHUDRingIndicatorView()
  163. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
  164. indicatorView.ringWidth = 1.5
  165. }
  166. hud.show(in: view)
  167. hud.textLabel.text = NSLocalizedString(metadata.fileNameView, comment: "")
  168. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  169. hud.tapOnHUDViewBlock = { hud in
  170. downloadRequest?.cancel()
  171. }
  172. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { request in
  173. downloadRequest = request
  174. } taskHandler: { task in
  175. // task
  176. } progressHandler: { progress in
  177. hud.progress = Float(progress.fractionCompleted)
  178. } completionHandler: { _, _, _, _, _, error, _, _ in
  179. if error == nil {
  180. NCManageDatabase.shared.addLocalFile(metadata: self.metadata)
  181. let urlVideo = NCKTVHTTPCache.shared.getVideoURL(metadata: self.metadata)
  182. if let url = urlVideo.url {
  183. self.url = url
  184. self.isProxy = urlVideo.isProxy
  185. if requiredConvert {
  186. #if MFFFLIB
  187. self.convertVideo(withAlert: false)
  188. #endif
  189. } else {
  190. self.openAVPlayer()
  191. }
  192. }
  193. }
  194. hud.dismiss()
  195. }
  196. }
  197. deinit {
  198. print("deinit NCPlayer")
  199. deactivateObserver()
  200. }
  201. func activateObserver(playerToolBar: NCPlayerToolBar?) {
  202. self.playerToolBar = playerToolBar
  203. // At end go back to start & show toolbar
  204. observerAVPlayerItemDidPlayToEndTime = NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main) { notification in
  205. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  206. NCKTVHTTPCache.shared.saveCache(metadata: self.metadata)
  207. self.videoSeek(time: .zero)
  208. if !(self.detailView?.isShow() ?? false) {
  209. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
  210. }
  211. self.playerToolBar?.updateToolBar()
  212. }
  213. }
  214. // Evey 1 second update toolbar
  215. observerAVPlayertTime = player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: .main, using: { _ in
  216. if self.player?.currentItem?.status == .readyToPlay {
  217. self.playerToolBar?.updateToolBar()
  218. }
  219. })
  220. NotificationCenter.default.addObserver(self, selector: #selector(generatorImagePreview), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
  221. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  222. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  223. NotificationCenter.default.addObserver(self, selector: #selector(playerPause), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
  224. NotificationCenter.default.addObserver(self, selector: #selector(playerPlay), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
  225. }
  226. func deactivateObserver() {
  227. if isPlay() {
  228. playerPause()
  229. }
  230. self.playerToolBar = nil
  231. if let observerAVPlayerItemDidPlayToEndTime = self.observerAVPlayerItemDidPlayToEndTime {
  232. NotificationCenter.default.removeObserver(observerAVPlayerItemDidPlayToEndTime)
  233. }
  234. self.observerAVPlayerItemDidPlayToEndTime = nil
  235. if let observerAVPlayertTime = self.observerAVPlayertTime {
  236. player?.removeTimeObserver(observerAVPlayertTime)
  237. }
  238. self.observerAVPlayertTime = nil
  239. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
  240. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  241. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  242. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
  243. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
  244. }
  245. // MARK: - NotificationCenter
  246. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  247. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue, let playerToolBar = self.playerToolBar {
  248. if !playerToolBar.isPictureInPictureActive() {
  249. playerPause()
  250. }
  251. }
  252. }
  253. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  254. playerToolBar?.updateToolBar()
  255. }
  256. // MARK: -
  257. func isPlay() -> Bool {
  258. if player?.rate == 1 { return true } else { return false }
  259. }
  260. @objc func playerPlay() {
  261. player?.play()
  262. self.playerToolBar?.updateToolBar()
  263. }
  264. @objc func playerPause() {
  265. player?.pause()
  266. self.playerToolBar?.updateToolBar()
  267. if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
  268. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  269. }
  270. }
  271. func videoSeek(time: CMTime) {
  272. player?.seek(to: time)
  273. self.saveTime(time)
  274. }
  275. func saveTime(_ time: CMTime) {
  276. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { return }
  277. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationTime: nil)
  278. generatorImagePreview()
  279. }
  280. func saveCurrentTime() {
  281. if let player = self.player {
  282. saveTime(player.currentTime())
  283. }
  284. }
  285. @objc func generatorImagePreview() {
  286. guard let time = player?.currentTime(), !metadata.livePhoto, metadata.classFile != NCCommunicationCommon.typeClassFile.audio.rawValue else { return }
  287. var image: UIImage?
  288. if let asset = player?.currentItem?.asset {
  289. do {
  290. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  291. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  292. let imageGenerator = AVAssetImageGenerator(asset: asset)
  293. imageGenerator.appliesPreferredTrackTransform = true
  294. let cgImage = try imageGenerator.copyCGImage(at: time, actualTime: nil)
  295. image = UIImage(cgImage: cgImage)
  296. // Update Playing Info Center
  297. let mediaItemPropertyTitle = MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle] as? String
  298. if let image = image, mediaItemPropertyTitle == metadata.fileNameView {
  299. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  300. return image
  301. }
  302. }
  303. // Preview
  304. if let data = image?.jpegData(compressionQuality: 0.5) {
  305. try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
  306. }
  307. // Icon
  308. if let data = image?.jpegData(compressionQuality: 0.5) {
  309. try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  310. }
  311. } catch let error as NSError {
  312. print(error.localizedDescription)
  313. }
  314. }
  315. }
  316. }