NCPlayer.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. internal var isStartPlayer: Bool
  37. internal var isStartObserver: Bool
  38. internal var subtitleUrls: [URL] = []
  39. internal var currentSubtitle: URL?
  40. private weak var imageVideoContainer: imageVideoContainerView?
  41. private weak var detailView: NCViewerMediaDetailView?
  42. private var observerAVPlayerItemDidPlayToEndTime: Any?
  43. private var observerAVPlayertTime: Any?
  44. var player: VLCMediaPlayer?
  45. var metadata: tableMetadata
  46. // MARK: - View Life Cycle
  47. init(imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?, viewController: UIViewController) {
  48. self.isStartPlayer = false
  49. self.isStartObserver = false
  50. self.imageVideoContainer = imageVideoContainer
  51. self.playerToolBar = playerToolBar
  52. self.metadata = metadata
  53. self.detailView = detailView
  54. self.viewController = viewController
  55. super.init()
  56. do {
  57. try AVAudioSession.sharedInstance().setCategory(.playback)
  58. try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.none)
  59. try AVAudioSession.sharedInstance().setActive(true)
  60. } catch {
  61. print(error)
  62. }
  63. }
  64. deinit {
  65. print("deinit NCPlayer with ocId \(metadata.ocId)")
  66. }
  67. func openAVPlayer(url: URL, autoplay: Bool) {
  68. self.url = url
  69. #if MFFFLIB
  70. MFFF.shared.setDelegate = self
  71. MFFF.shared.dismissMessage()
  72. NotificationCenter.default.addObserver(self, selector: #selector(convertVideoDidFinish(_:)), name: NSNotification.Name(rawValue: self.metadata.ocId), object: nil)
  73. if CCUtility.fileProviderStorageExists(metadata) {
  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. }
  80. #endif
  81. print("Play URL: \(url)")
  82. player = VLCMediaPlayer()
  83. player?.media = VLCMedia(url: url)
  84. player?.media?.addOption("--network-caching=10000")
  85. player?.delegate = self
  86. playerToolBar?.show()
  87. playerToolBar?.setMetadata(self.metadata)
  88. #if MFFFLIB
  89. setUpForSubtitle()
  90. #endif
  91. let volume = CCUtility.getAudioVolume()
  92. if metadata.livePhoto {
  93. player?.audio?.volume = 0
  94. } else if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue {
  95. player?.audio?.volume = Int32(volume)
  96. } else {
  97. player?.audio?.volume = Int32(volume)
  98. if let position = NCManageDatabase.shared.getVideoPosition(metadata: metadata) {
  99. player?.position = position
  100. }
  101. }
  102. player?.drawable = self.imageVideoContainer
  103. if autoplay {
  104. player?.play()
  105. }
  106. self.playerToolBar?.setBarPlayer(ncplayer: self)
  107. }
  108. // MARK: - NotificationCenter
  109. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  110. if metadata.classFile == NKCommon.TypeClassFile.video.rawValue, let playerToolBar = self.playerToolBar {
  111. if !playerToolBar.isPictureInPictureActive() {
  112. playerPause()
  113. }
  114. }
  115. }
  116. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  117. playerToolBar?.updateToolBar()
  118. }
  119. // MARK: -
  120. func isPlay() -> Bool {
  121. return player?.isPlaying ?? false
  122. }
  123. @objc func playerPlay() {
  124. player?.play()
  125. playerToolBar?.updateToolBar()
  126. }
  127. @objc func playerPause() {
  128. player?.pause()
  129. playerToolBar?.updateToolBar()
  130. if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
  131. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  132. }
  133. }
  134. func videoSeek(position: Float) {
  135. player?.position = position
  136. savePosition(position)
  137. }
  138. func videoStop() {
  139. if let url = self.url {
  140. if !(self.detailView?.isShow() ?? false) {
  141. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
  142. }
  143. self.openAVPlayer(url: url, autoplay: false)
  144. }
  145. }
  146. func savePosition(_ position: Float) {
  147. if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue { return }
  148. let length = Int(player?.media?.length.intValue ?? 0)
  149. NCManageDatabase.shared.addVideo(metadata: metadata, position: position, length: length)
  150. generatorImagePreview()
  151. }
  152. func saveCurrentTime() {
  153. if let player = self.player {
  154. savePosition(player.position)
  155. }
  156. }
  157. @objc func generatorImagePreview() {
  158. /*
  159. guard let time = player.time, !metadata.livePhoto, metadata.classFile != NKCommon.TypeClassFile.audio.rawValue else { return }
  160. var image: UIImage?
  161. if let asset = player?.currentItem?.asset {
  162. do {
  163. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  164. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  165. let imageGenerator = AVAssetImageGenerator(asset: asset)
  166. imageGenerator.appliesPreferredTrackTransform = true
  167. let cgImage = try imageGenerator.copyCGImage(at: time, actualTime: nil)
  168. image = UIImage(cgImage: cgImage)
  169. // Update Playing Info Center
  170. let mediaItemPropertyTitle = MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle] as? String
  171. if let image = image, mediaItemPropertyTitle == metadata.fileNameView {
  172. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  173. return image
  174. }
  175. }
  176. // Preview
  177. if let data = image?.jpegData(compressionQuality: 0.5) {
  178. try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
  179. }
  180. // Icon
  181. if let data = image?.jpegData(compressionQuality: 0.5) {
  182. try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  183. }
  184. } catch let error as NSError {
  185. print("GeneratorImagePreview localized error:")
  186. print(error.localizedDescription)
  187. }
  188. }
  189. */
  190. }
  191. internal func downloadVideo(isEncrypted: Bool = false, requiredConvert: Bool = false) {
  192. guard let view = appDelegate.window?.rootViewController?.view else { return }
  193. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  194. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)!
  195. let hud = JGProgressHUD()
  196. var downloadRequest: DownloadRequest?
  197. hud.indicatorView = JGProgressHUDRingIndicatorView()
  198. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
  199. indicatorView.ringWidth = 1.5
  200. }
  201. hud.textLabel.text = NSLocalizedString(metadata.fileNameView, comment: "")
  202. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  203. hud.show(in: view)
  204. hud.tapOnHUDViewBlock = { hud in
  205. downloadRequest?.cancel()
  206. }
  207. NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { request in
  208. downloadRequest = request
  209. } taskHandler: { task in
  210. // task
  211. } progressHandler: { progress in
  212. hud.progress = Float(progress.fractionCompleted)
  213. } completionHandler: { _, _, _, _, _, afError, error in
  214. if afError == nil {
  215. NCManageDatabase.shared.addLocalFile(metadata: self.metadata)
  216. if isEncrypted {
  217. if let result = NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "fileNameIdentifier == %@ AND serverUrl == %@", self.metadata.fileName, self.metadata.serverUrl)) {
  218. NCEndToEndEncryption.sharedManager()?.decryptFile(self.metadata.fileName, fileNameView: self.metadata.fileNameView, ocId: self.metadata.ocId, key: result.key, initializationVector: result.initializationVector, authenticationTag: result.authenticationTag)
  219. }
  220. }
  221. if CCUtility.fileProviderStorageExists(self.metadata) || self.metadata.isDirectoryE2EE {
  222. let url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  223. if requiredConvert {
  224. #if MFFFLIB
  225. self.convertVideo(withAlert: false)
  226. #endif
  227. } else {
  228. self.openAVPlayer(url: url, autoplay: true)
  229. }
  230. }
  231. }
  232. hud.dismiss()
  233. }
  234. }
  235. }
  236. extension NCPlayer: VLCMediaPlayerDelegate {
  237. func mediaPlayerStateChanged(_ aNotification: Notification) {
  238. guard let player = self.player else { return }
  239. switch player.state {
  240. case .stopped:
  241. videoStop()
  242. print("Played mode: STOPPED")
  243. break
  244. case .opening:
  245. print("Played mode: OPENING")
  246. break
  247. case .buffering:
  248. print("Played mode: BUFFERING")
  249. break
  250. case .ended:
  251. print("Played mode: ENDED")
  252. break
  253. case .error:
  254. print("Played mode: ERROR")
  255. break
  256. case .playing:
  257. print("Played mode: PLAYING")
  258. break
  259. case .paused:
  260. print("Played mode: PAUSED")
  261. break
  262. default: break
  263. }
  264. print(player.state)
  265. }
  266. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  267. self.playerToolBar?.updateToolBar()
  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. }