NCPlayer.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 MediaPlayer
  27. import MobileVLCKit
  28. class NCPlayer: NSObject {
  29. internal let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. internal var url: URL?
  31. internal weak var playerToolBar: NCPlayerToolBar?
  32. internal weak var viewController: UIViewController?
  33. private weak var imageVideoContainer: imageVideoContainerView?
  34. private weak var detailView: NCViewerMediaDetailView?
  35. private weak var viewerMediaPage: NCViewerMediaPage?
  36. var player: VLCMediaPlayer?
  37. var thumbnailer: VLCMediaThumbnailer?
  38. var metadata: tableMetadata
  39. var singleTapGestureRecognizer: UITapGestureRecognizer!
  40. var width: Int64?
  41. var height: Int64?
  42. let fileNamePreviewLocalPath: String
  43. let fileNameIconLocalPath: String
  44. // MARK: - View Life Cycle
  45. init(imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, detailView: NCViewerMediaDetailView?, viewController: UIViewController, viewerMediaPage: NCViewerMediaPage?) {
  46. self.imageVideoContainer = imageVideoContainer
  47. self.playerToolBar = playerToolBar
  48. self.metadata = metadata
  49. self.detailView = detailView
  50. self.viewController = viewController
  51. self.viewerMediaPage = viewerMediaPage
  52. fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  53. fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  54. super.init()
  55. do {
  56. try AVAudioSession.sharedInstance().setCategory(.playback)
  57. try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.none)
  58. try AVAudioSession.sharedInstance().setActive(true)
  59. } catch {
  60. print(error)
  61. }
  62. }
  63. deinit {
  64. print("deinit NCPlayer with ocId \(metadata.ocId)")
  65. player?.stop()
  66. if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
  67. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  68. }
  69. }
  70. func openAVPlayer(url: URL) {
  71. var position: Float = 0
  72. self.url = url
  73. self.singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  74. print("Play URL: \(url)")
  75. player = VLCMediaPlayer()
  76. player?.media = VLCMedia(url: url)
  77. player?.delegate = self
  78. player?.media?.addOption("--network-caching=5000")
  79. let volume = CCUtility.getAudioVolume()
  80. if metadata.livePhoto {
  81. player?.audio?.volume = 0
  82. } else if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue {
  83. player?.audio?.volume = Int32(volume)
  84. } else {
  85. player?.audio?.volume = Int32(volume)
  86. if let result = NCManageDatabase.shared.getVideoPosition(metadata: metadata) {
  87. position = result
  88. player?.position = position
  89. }
  90. }
  91. player?.drawable = self.imageVideoContainer
  92. if let view = player?.drawable as? UIView {
  93. view.isUserInteractionEnabled = true
  94. view.addGestureRecognizer(singleTapGestureRecognizer)
  95. }
  96. playerToolBar?.setBarPlayer(ncplayer: self, position: position)
  97. playerToolBar?.setMetadata(self.metadata)
  98. playerToolBar?.show(enableTimerAutoHide: false)
  99. if let media = player?.media {
  100. thumbnailer = VLCMediaThumbnailer(media: media, andDelegate: self)
  101. }
  102. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  103. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  104. NotificationCenter.default.addObserver(self, selector: #selector(playerPause), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
  105. NotificationCenter.default.addObserver(self, selector: #selector(playerPlay), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
  106. }
  107. func closeAVPlayer() {
  108. playerPause(withSnapshot: false)
  109. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  110. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  111. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPauseMedia), object: nil)
  112. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterPlayMedia), object: nil)
  113. }
  114. // MARK: - UIGestureRecognizerDelegate
  115. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  116. viewerMediaPage?.didSingleTapWith(gestureRecognizer: gestureRecognizer)
  117. }
  118. // MARK: - NotificationCenter
  119. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  120. if metadata.classFile == NKCommon.TypeClassFile.video.rawValue, let playerToolBar = self.playerToolBar {
  121. if !playerToolBar.isPictureInPictureActive() {
  122. playerPause()
  123. }
  124. }
  125. }
  126. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  127. playerToolBar?.update(position: player?.position)
  128. }
  129. // MARK: -
  130. func isPlay() -> Bool {
  131. return player?.isPlaying ?? false
  132. }
  133. @objc func playerPlay() {
  134. player?.play()
  135. if let position = NCManageDatabase.shared.getVideoPosition(metadata: self.metadata) {
  136. player?.position = position
  137. playerToolBar?.update(position: position)
  138. } else {
  139. playerToolBar?.update(position: player?.position)
  140. }
  141. }
  142. @objc func playerPause(withSnapshot: Bool = true) {
  143. if let width = width, let height = height, withSnapshot {
  144. player?.saveVideoSnapshot(at: fileNamePreviewLocalPath, withWidth: Int32(width), andHeight: Int32(height))
  145. }
  146. player?.pause()
  147. playerToolBar?.update(position: player?.position)
  148. if let playerToolBar = self.playerToolBar, playerToolBar.isPictureInPictureActive() {
  149. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  150. }
  151. }
  152. func videoSeek(position: Float) {
  153. player?.position = position
  154. playerToolBar?.update(position: position)
  155. }
  156. func savePosition(_ position: Float) {
  157. if metadata.classFile == NKCommon.TypeClassFile.audio.rawValue { return }
  158. let length = Int(player?.media?.length.intValue ?? 0)
  159. NCManageDatabase.shared.addVideo(metadata: metadata, position: position, length: length)
  160. }
  161. func snapshot() {
  162. if let player = player, let width = width, let height = height {
  163. player.saveVideoSnapshot(at: fileNamePreviewLocalPath, withWidth: Int32(width), andHeight: Int32(height))
  164. }
  165. }
  166. }
  167. extension NCPlayer: VLCMediaPlayerDelegate {
  168. func mediaPlayerStateChanged(_ aNotification: Notification) {
  169. guard let player = self.player else { return }
  170. switch player.state {
  171. case .stopped:
  172. if let url = self.url {
  173. NCManageDatabase.shared.addVideo(metadata: metadata, position: 0)
  174. if !(self.detailView?.isShow() ?? false) {
  175. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": self.metadata.ocId, "enableTimerAutoHide": false])
  176. }
  177. self.thumbnailer?.fetchThumbnail()
  178. self.openAVPlayer(url: url)
  179. }
  180. print("Played mode: STOPPED")
  181. break
  182. case .opening:
  183. playerToolBar?.buffering()
  184. print("Played mode: OPENING")
  185. break
  186. case .buffering:
  187. playerToolBar?.buffering()
  188. print("Played mode: BUFFERING")
  189. break
  190. case .ended:
  191. print("Played mode: ENDED")
  192. playerToolBar?.update(position: player.position)
  193. break
  194. case .error:
  195. print("Played mode: ERROR")
  196. break
  197. case .playing:
  198. if let tracksInformation = player.media?.tracksInformation {
  199. for case let track as [String:Any] in tracksInformation {
  200. if track["type"] as? String == "video" {
  201. width = track["width"] as? Int64
  202. height = track["height"] as? Int64
  203. }
  204. }
  205. }
  206. // let metaDictionary = player.media?.metaData
  207. print("Played mode: PLAYING")
  208. break
  209. case .paused:
  210. print("Played mode: PAUSED")
  211. playerToolBar?.update(position: player.position)
  212. break
  213. default: break
  214. }
  215. print(player.state)
  216. }
  217. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  218. self.playerToolBar?.update(position: player?.position)
  219. }
  220. func mediaPlayerTitleChanged(_ aNotification: Notification) {
  221. print(".")
  222. }
  223. func mediaPlayerChapterChanged(_ aNotification: Notification) {
  224. print(".")
  225. }
  226. func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
  227. print(".")
  228. }
  229. func mediaPlayerSnapshot(_ aNotification: Notification) {
  230. if let data = NSData(contentsOfFile: fileNamePreviewLocalPath),
  231. let image = UIImage(data: data as Data),
  232. let image = image.resizeImage(size: CGSize(width: NCGlobal.shared.sizeIcon, height: NCGlobal.shared.sizeIcon)),
  233. let data = image.jpegData(compressionQuality: 0.5) {
  234. try? data.write(to: URL(fileURLWithPath: fileNameIconLocalPath))
  235. }
  236. print("Snapshot saved")
  237. }
  238. func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
  239. // Handle other states...
  240. }
  241. func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
  242. // Handle other states...
  243. }
  244. }
  245. extension NCPlayer: VLCMediaThumbnailerDelegate {
  246. func mediaThumbnailerDidTimeOut(_ mediaThumbnailer: VLCMediaThumbnailer) { }
  247. func mediaThumbnailer(_ mediaThumbnailer: VLCMediaThumbnailer, didFinishThumbnail thumbnail: CGImage) {
  248. var image: UIImage?
  249. do {
  250. image = UIImage(cgImage: thumbnail)
  251. // Update Playing Info Center
  252. let mediaItemPropertyTitle = MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle] as? String
  253. if let image = image, mediaItemPropertyTitle == metadata.fileNameView {
  254. MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  255. return image
  256. }
  257. }
  258. // Preview
  259. if let data = image?.jpegData(compressionQuality: 0.5) {
  260. try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
  261. }
  262. // Icon
  263. if let data = image?.jpegData(compressionQuality: 0.5) {
  264. try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  265. }
  266. } catch let error as NSError {
  267. print("GeneratorImagePreview localized error:")
  268. print(error.localizedDescription)
  269. }
  270. }
  271. }