NCPlayer.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. }
  160. func videoSeek(position: Float) {
  161. player?.position = position
  162. playerToolBar?.update(position: position)
  163. }
  164. func savePosition(_ position: Float) {
  165. if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue { return }
  166. let length = Int(player?.media?.length.intValue ?? 0)
  167. NCManageDatabase.shared.addVideo(metadata: metadata, position: position, length: length, autoplay: isPlay())
  168. }
  169. internal func downloadVideo(isEncrypted: Bool = false, requiredConvert: Bool = false) {
  170. guard let view = appDelegate.window?.rootViewController?.view else { return }
  171. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  172. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)!
  173. let hud = JGProgressHUD()
  174. var downloadRequest: DownloadRequest?
  175. hud.indicatorView = JGProgressHUDRingIndicatorView()
  176. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
  177. indicatorView.ringWidth = 1.5
  178. }
  179. hud.textLabel.text = NSLocalizedString(metadata.fileNameView, comment: "")
  180. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  181. hud.show(in: view)
  182. hud.tapOnHUDViewBlock = { hud in
  183. downloadRequest?.cancel()
  184. }
  185. NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { request in
  186. downloadRequest = request
  187. } taskHandler: { task in
  188. // task
  189. } progressHandler: { progress in
  190. hud.progress = Float(progress.fractionCompleted)
  191. } completionHandler: { _, _, _, _, _, afError, error in
  192. if afError == nil {
  193. NCManageDatabase.shared.addLocalFile(metadata: self.metadata)
  194. if isEncrypted {
  195. if let result = NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "fileNameIdentifier == %@ AND serverUrl == %@", self.metadata.fileName, self.metadata.serverUrl)) {
  196. NCEndToEndEncryption.sharedManager()?.decryptFile(self.metadata.fileName, fileNameView: self.metadata.fileNameView, ocId: self.metadata.ocId, key: result.key, initializationVector: result.initializationVector, authenticationTag: result.authenticationTag)
  197. }
  198. }
  199. if CCUtility.fileProviderStorageExists(self.metadata) || self.metadata.isDirectoryE2EE {
  200. let url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  201. if requiredConvert {
  202. #if MFFFLIB
  203. self.convertVideo(withAlert: false)
  204. #endif
  205. } else {
  206. self.openAVPlayer(url: url)
  207. }
  208. }
  209. }
  210. hud.dismiss()
  211. }
  212. }
  213. }
  214. extension NCPlayer: VLCMediaPlayerDelegate {
  215. func mediaPlayerStateChanged(_ aNotification: Notification) {
  216. guard let player = self.player else { return }
  217. switch player.state {
  218. case .stopped:
  219. if let url = self.url {
  220. NCManageDatabase.shared.addVideo(metadata: metadata, position: 0, autoplay: false)
  221. if !(self.detailView?.isShow() ?? false) {
  222. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
  223. }
  224. self.openAVPlayer(url: url)
  225. }
  226. print("Played mode: STOPPED")
  227. break
  228. case .opening:
  229. print("Played mode: OPENING")
  230. break
  231. case .buffering:
  232. print("Played mode: BUFFERING")
  233. break
  234. case .ended:
  235. print("Played mode: ENDED")
  236. playerToolBar?.update(position: player.position)
  237. break
  238. case .error:
  239. print("Played mode: ERROR")
  240. break
  241. case .playing:
  242. /*
  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. */
  257. print("Played mode: PLAYING")
  258. break
  259. case .paused:
  260. print("Played mode: PAUSED")
  261. playerToolBar?.update(position: player.position)
  262. break
  263. default: break
  264. }
  265. print(player.state)
  266. }
  267. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  268. self.playerToolBar?.update(position: player?.position)
  269. }
  270. func mediaPlayerTitleChanged(_ aNotification: Notification) {
  271. print(".")
  272. }
  273. func mediaPlayerChapterChanged(_ aNotification: Notification) {
  274. print(".")
  275. }
  276. func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
  277. print(".")
  278. }
  279. func mediaPlayerSnapshot(_ aNotification: Notification) {
  280. print(".")
  281. }
  282. func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
  283. // Handle other states...
  284. }
  285. func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
  286. // Handle other states...
  287. }
  288. }
  289. extension NCPlayer: VLCMediaThumbnailerDelegate {
  290. func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer) { }
  291. func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer, didFinishThumbnail thumbnail: CGImage) {
  292. var image: UIImage?
  293. do {
  294. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  295. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  296. image = UIImage(cgImage: thumbnail)
  297. // Update Playing Info Center
  298. let mediaItemPropertyTitle = MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle] as? String
  299. if let image = image, mediaItemPropertyTitle == metadata.fileNameView {
  300. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  301. return image
  302. }
  303. }
  304. // Preview
  305. if let data = image?.jpegData(compressionQuality: 0.5) {
  306. try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
  307. }
  308. // Icon
  309. if let data = image?.jpegData(compressionQuality: 0.5) {
  310. try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  311. }
  312. } catch let error as NSError {
  313. print("GeneratorImagePreview localized error:")
  314. print(error.localizedDescription)
  315. }
  316. }
  317. }