NCViewerVideo.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // NCViewerVideo.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 NCCommunication
  25. class NCViewerVideo: NSObject {
  26. @objc static let shared: NCViewerVideo = {
  27. let instance = NCViewerVideo()
  28. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  29. return instance
  30. }()
  31. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. private var imageView: UIImageView?
  33. private var timeObserver: Any?
  34. private var rateObserver: Any?
  35. private var durationSeconds: Double = 0
  36. private var viewerVideoToolBar: NCViewerVideoToolBar?
  37. public var metadata: tableMetadata?
  38. public var videoLayer: AVPlayerLayer?
  39. public var player: AVPlayer?
  40. //MARK: - NotificationCenter
  41. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  42. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  43. self.player?.pause()
  44. }
  45. }
  46. func initVideoPlayer(imageView: UIImageView?, viewerVideoToolBar: NCViewerVideoToolBar?, metadata: tableMetadata) {
  47. guard let imageView = imageView else { return }
  48. if self.imageView == imageView { return }
  49. self.imageView = imageView
  50. self.viewerVideoToolBar = viewerVideoToolBar
  51. self.metadata = metadata
  52. func initPlayer(url: URL) {
  53. self.player = AVPlayer(url: url)
  54. self.player?.isMuted = CCUtility.getAudioMute()
  55. self.player?.seek(to: .zero)
  56. self.videoLayer = AVPlayerLayer(player: self.player)
  57. self.videoLayer!.frame = imageView.bounds
  58. self.videoLayer!.videoGravity = .resizeAspect
  59. imageView.layer.addSublayer(videoLayer!)
  60. // At end go back to start & show toolbar
  61. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { (notification) in
  62. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  63. self.player?.seek(to: .zero)
  64. self.viewerVideoToolBar?.showToolBar()
  65. }
  66. }
  67. rateObserver = self.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  68. // save durationSeconds on database
  69. if let duration: CMTime = (player?.currentItem?.asset.duration) {
  70. durationSeconds = CMTimeGetSeconds(duration)
  71. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: nil, durationSeconds: durationSeconds)
  72. }
  73. // seek to datamebase ti
  74. if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
  75. self.player?.seek(to: time)
  76. }
  77. viewerVideoToolBar?.setBarPlayer()
  78. }
  79. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  80. initPlayer(url: url)
  81. }
  82. }
  83. func videoPlay() {
  84. guard let metadata = self.metadata else { return }
  85. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  86. self.player?.play()
  87. }
  88. func videoPause() {
  89. guard let metadata = self.metadata else { return }
  90. self.player?.pause()
  91. NCKTVHTTPCache.shared.stopProxy(metadata: metadata)
  92. }
  93. func videoSeek(time: CMTime) {
  94. guard let metadata = self.metadata else { return }
  95. self.player?.seek(to: time)
  96. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: time, durationSeconds: nil)
  97. }
  98. func videoRemoved() {
  99. guard let metadata = self.metadata else { return }
  100. self.player?.pause()
  101. self.player?.seek(to: CMTime.zero)
  102. if let timeObserver = timeObserver {
  103. self.player?.removeTimeObserver(timeObserver)
  104. self.timeObserver = nil
  105. }
  106. if rateObserver != nil {
  107. self.player?.removeObserver(self, forKeyPath: "rate")
  108. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  109. NCKTVHTTPCache.shared.stopProxy(metadata: metadata)
  110. self.rateObserver = nil
  111. }
  112. self.videoLayer?.removeFromSuperlayer()
  113. }
  114. func getVideoCurrentSeconds() -> Float64 {
  115. if let currentTime = NCViewerVideo.shared.player?.currentTime() {
  116. return CMTimeGetSeconds(currentTime)
  117. }
  118. return 0
  119. }
  120. func getVideoDurationSeconds() -> Float64 {
  121. return self.durationSeconds
  122. }
  123. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  124. guard let metadata = self.metadata else { return }
  125. if keyPath != nil && keyPath == "rate" {
  126. self.viewerVideoToolBar?.updateToolBar()
  127. if self.player?.rate == 1 {
  128. if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
  129. self.player?.seek(to: time)
  130. self.player?.isMuted = CCUtility.getAudioMute()
  131. let timeSecond = Double(CMTimeGetSeconds(time))
  132. print("Play video at: \(timeSecond)")
  133. }
  134. } else if !metadata.livePhoto {
  135. if let time = self.player?.currentTime(), let duration = self.player?.currentItem?.asset.duration {
  136. let timeSecond = Double(CMTimeGetSeconds(time))
  137. let durationSeconds = Double(CMTimeGetSeconds(duration))
  138. if timeSecond < durationSeconds {
  139. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: self.player?.currentTime(), durationSeconds: nil)
  140. if let time = self.player?.currentTime() {
  141. let timeSecond = Double(CMTimeGetSeconds(time))
  142. print("Stop video at: \(timeSecond)")
  143. }
  144. } else {
  145. NCManageDatabase.shared.deleteVideoTime(metadata: metadata)
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }