NCPlayer.swift 15 KB

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