NCPlayer.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 NextcloudKit
  25. import UIKit
  26. import AVFoundation
  27. import MediaPlayer
  28. import JGProgressHUD
  29. import Alamofire
  30. import MobileVLCKit
  31. class NCPlayer: NSObject {
  32. internal let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. internal var url: URL?
  34. internal weak var playerToolBar: NCPlayerToolBar?
  35. internal weak var viewController: UIViewController?
  36. private weak var imageVideoContainer: imageVideoContainerView?
  37. private weak var detailView: NCViewerMediaDetailView?
  38. private weak var viewerMediaPage: NCViewerMediaPage?
  39. var player: VLCMediaPlayer?
  40. var thumbnailer: VLCMediaThumbnailer?
  41. var metadata: tableMetadata
  42. var singleTapGestureRecognizer: UITapGestureRecognizer!
  43. // MARK: - View Life Cycle
  44. init(imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?, viewController: UIViewController, viewerMediaPage: NCViewerMediaPage?) {
  45. self.imageVideoContainer = imageVideoContainer
  46. self.playerToolBar = playerToolBar
  47. self.metadata = metadata
  48. self.detailView = detailView
  49. self.viewController = viewController
  50. self.viewerMediaPage = viewerMediaPage
  51. super.init()
  52. do {
  53. try AVAudioSession.sharedInstance().setCategory(.playback)
  54. try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.none)
  55. try AVAudioSession.sharedInstance().setActive(true)
  56. } catch {
  57. print(error)
  58. }
  59. }
  60. deinit {
  61. print("deinit NCPlayer with ocId \(metadata.ocId)")
  62. player?.stop()
  63. if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
  64. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  65. }
  66. }
  67. func openAVPlayer(url: URL) {
  68. var position: Float = 0
  69. self.url = url
  70. self.singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  71. #if MFFFLIB
  72. MFFF.shared.setDelegate = self
  73. MFFF.shared.dismissMessage()
  74. NotificationCenter.default.addObserver(self, selector: #selector(convertVideoDidFinish(_:)), name: NSNotification.Name(rawValue: self.metadata.ocId), object: nil)
  75. if CCUtility.fileProviderStorageExists(metadata) {
  76. self.url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: NCGlobal.shared.fileNameVideoEncoded))
  77. self.isProxy = false
  78. }
  79. if MFFF.shared.existsMFFFSession(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))) {
  80. return
  81. }
  82. #endif
  83. print("Play URL: \(url)")
  84. player = VLCMediaPlayer()
  85. player?.media = VLCMedia(url: url)
  86. player?.delegate = self
  87. player?.media?.addOption("--network-caching=10000")
  88. let volume = CCUtility.getAudioVolume()
  89. if metadata.livePhoto {
  90. player?.audio?.volume = 0
  91. } else if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue {
  92. player?.audio?.volume = Int32(volume)
  93. } else {
  94. player?.audio?.volume = Int32(volume)
  95. if let result = NCManageDatabase.shared.getVideoPosition(metadata: metadata) {
  96. position = result
  97. player?.position = position
  98. }
  99. }
  100. player?.drawable = self.imageVideoContainer
  101. if let view = player?.drawable as? UIView {
  102. view.isUserInteractionEnabled = true
  103. view.addGestureRecognizer(singleTapGestureRecognizer)
  104. }
  105. if NCManageDatabase.shared.getVideoAutoplay(metadata: metadata) {
  106. playerPlay()
  107. playerToolBar?.show(enableTimerAutoHide: true)
  108. } else {
  109. playerToolBar?.show(enableTimerAutoHide: false)
  110. }
  111. playerToolBar?.setBarPlayer(ncplayer: self, position: position)
  112. playerToolBar?.setMetadata(self.metadata)
  113. if let media = player?.media {
  114. thumbnailer = VLCMediaThumbnailer(media: media, andDelegate: self)
  115. }
  116. }
  117. func deactivatePlayer() {
  118. playerPause()
  119. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
  120. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  121. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  122. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
  123. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
  124. }
  125. // MARK: - UIGestureRecognizerDelegate
  126. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  127. viewerMediaPage?.didSingleTapWith(gestureRecognizer: gestureRecognizer)
  128. }
  129. // MARK: - NotificationCenter
  130. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  131. if metadata.classFile == NKCommon.TypeClassFile.video.rawValue, let playerToolBar = self.playerToolBar {
  132. if !playerToolBar.isPictureInPictureActive() {
  133. playerPause()
  134. }
  135. }
  136. }
  137. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  138. playerToolBar?.update(position: player?.position)
  139. }
  140. // MARK: -
  141. func isPlay() -> Bool {
  142. return player?.isPlaying ?? false
  143. }
  144. @objc func playerPlay() {
  145. player?.play()
  146. if let position = NCManageDatabase.shared.getVideoPosition(metadata: self.metadata) {
  147. player?.position = position
  148. playerToolBar?.update(position: position)
  149. } else {
  150. playerToolBar?.update(position: player?.position)
  151. }
  152. }
  153. @objc func playerPause() {
  154. player?.pause()
  155. playerToolBar?.update(position: player?.position)
  156. if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
  157. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  158. }
  159. thumbnailer?.fetchThumbnail()
  160. }
  161. func videoSeek(position: Float) {
  162. player?.position = position
  163. playerToolBar?.update(position: position)
  164. }
  165. func savePosition(_ position: Float) {
  166. if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue { return }
  167. let length = Int(player?.media?.length.intValue ?? 0)
  168. NCManageDatabase.shared.addVideo(metadata: metadata, position: position, length: length, autoplay: isPlay())
  169. }
  170. internal func downloadVideo(isEncrypted: Bool = false, requiredConvert: Bool = false) {
  171. guard let view = appDelegate.window?.rootViewController?.view else { return }
  172. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  173. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)!
  174. let hud = JGProgressHUD()
  175. var downloadRequest: DownloadRequest?
  176. hud.indicatorView = JGProgressHUDRingIndicatorView()
  177. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
  178. indicatorView.ringWidth = 1.5
  179. }
  180. hud.textLabel.text = NSLocalizedString(metadata.fileNameView, comment: "")
  181. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  182. hud.show(in: view)
  183. hud.tapOnHUDViewBlock = { hud in
  184. downloadRequest?.cancel()
  185. }
  186. NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { request in
  187. downloadRequest = request
  188. } taskHandler: { task in
  189. // task
  190. } progressHandler: { progress in
  191. hud.progress = Float(progress.fractionCompleted)
  192. } completionHandler: { _, _, _, _, _, afError, error in
  193. if afError == nil {
  194. NCManageDatabase.shared.addLocalFile(metadata: self.metadata)
  195. if isEncrypted {
  196. if let result = NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "fileNameIdentifier == %@ AND serverUrl == %@", self.metadata.fileName, self.metadata.serverUrl)) {
  197. NCEndToEndEncryption.sharedManager()?.decryptFile(self.metadata.fileName, fileNameView: self.metadata.fileNameView, ocId: self.metadata.ocId, key: result.key, initializationVector: result.initializationVector, authenticationTag: result.authenticationTag)
  198. }
  199. }
  200. if CCUtility.fileProviderStorageExists(self.metadata) || self.metadata.isDirectoryE2EE {
  201. let url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  202. if requiredConvert {
  203. #if MFFFLIB
  204. self.convertVideo(withAlert: false)
  205. #endif
  206. } else {
  207. self.openAVPlayer(url: url)
  208. }
  209. }
  210. }
  211. hud.dismiss()
  212. }
  213. }
  214. }
  215. extension NCPlayer: VLCMediaPlayerDelegate {
  216. func mediaPlayerStateChanged(_ aNotification: Notification) {
  217. guard let player = self.player else { return }
  218. switch player.state {
  219. case .stopped:
  220. if let url = self.url {
  221. NCManageDatabase.shared.addVideo(metadata: metadata, position: 0, autoplay: false)
  222. if !(self.detailView?.isShow() ?? false) {
  223. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
  224. }
  225. self.openAVPlayer(url: url)
  226. }
  227. print("Played mode: STOPPED")
  228. break
  229. case .opening:
  230. print("Played mode: OPENING")
  231. break
  232. case .buffering:
  233. print("Played mode: BUFFERING")
  234. break
  235. case .ended:
  236. print("Played mode: ENDED")
  237. playerToolBar?.update(position: player.position)
  238. break
  239. case .error:
  240. print("Played mode: ERROR")
  241. break
  242. case .playing:
  243. var codecNameVideo, codecNameAudio, codecAudioChannelLayout, codecAudioLanguage, codecQuality: String?
  244. if let tracksInformation = player.media?.tracksInformation {
  245. for case let track as [String:Any] in tracksInformation {
  246. if track["type"] as? String == "video" {
  247. codecNameVideo = track["codec"] as? String
  248. }
  249. if track["type"] as? String == "audio" {
  250. codecNameAudio = track["codec"] as? String
  251. }
  252. }
  253. NCManageDatabase.shared.addVideoCodec(metadata: metadata, codecNameVideo: codecNameVideo, codecNameAudio: codecNameAudio, codecAudioChannelLayout: codecAudioChannelLayout, codecAudioLanguage: codecAudioLanguage, codecMaxCompatibility: true, codecQuality: codecQuality)
  254. }
  255. let metaDictionary = player.media?.metaData
  256. print("Played mode: PLAYING")
  257. break
  258. case .paused:
  259. print("Played mode: PAUSED")
  260. playerToolBar?.update(position: player.position)
  261. break
  262. default: break
  263. }
  264. print(player.state)
  265. }
  266. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  267. self.playerToolBar?.update(position: player?.position)
  268. }
  269. func mediaPlayerTitleChanged(_ aNotification: Notification) {
  270. guard let player = self.player else { return }
  271. print(".")
  272. }
  273. func mediaPlayerChapterChanged(_ aNotification: Notification) {
  274. guard let player = self.player else { return }
  275. print(".")
  276. }
  277. func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
  278. guard let player = self.player else { return }
  279. print(".")
  280. }
  281. func mediaPlayerSnapshot(_ aNotification: Notification) {
  282. guard let player = self.player else { return }
  283. print(".")
  284. }
  285. func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
  286. // Handle other states...
  287. }
  288. func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
  289. // Handle other states...
  290. }
  291. }
  292. extension NCPlayer: VLCMediaThumbnailerDelegate {
  293. func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer) { }
  294. func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer, didFinishThumbnail thumbnail: CGImage) {
  295. var image: UIImage?
  296. do {
  297. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  298. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  299. image = UIImage(cgImage: thumbnail)
  300. // Update Playing Info Center
  301. let mediaItemPropertyTitle = MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle] as? String
  302. if let image = image, mediaItemPropertyTitle == metadata.fileNameView {
  303. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  304. return image
  305. }
  306. }
  307. // Preview
  308. if let data = image?.jpegData(compressionQuality: 0.5) {
  309. try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
  310. }
  311. // Icon
  312. if let data = image?.jpegData(compressionQuality: 0.5) {
  313. try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  314. }
  315. } catch let error as NSError {
  316. print("GeneratorImagePreview localized error:")
  317. print(error.localizedDescription)
  318. }
  319. }
  320. }