NCViewerVideo.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. private var videoLayer: AVPlayerLayer?
  28. private var view: UIView?
  29. private var timeObserver: Any?
  30. private var rateObserver: Any?
  31. private var metadata: tableMetadata?
  32. public var viewerVideoToolBar: NCViewerVideoToolBar?
  33. public var player: AVPlayer?
  34. public var pictureInPictureOcId: String = ""
  35. init(view: UIView?, viewerVideoToolBar: NCViewerVideoToolBar?) {
  36. super.init()
  37. self.view = view
  38. self.viewerVideoToolBar = viewerVideoToolBar
  39. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  40. }
  41. deinit {
  42. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  43. }
  44. //MARK: - NotificationCenter
  45. @objc func applicationDidEnterBackground(_ notification: NSNotification) {
  46. if metadata?.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  47. player?.pause()
  48. }
  49. }
  50. func videoPlay(metadata: tableMetadata) {
  51. self.metadata = metadata
  52. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  53. func play(url: URL) {
  54. self.player = AVPlayer(url: url)
  55. self.player?.isMuted = CCUtility.getAudioMute()
  56. self.videoLayer = AVPlayerLayer(player: self.player)
  57. if let view = view {
  58. self.videoLayer!.frame = view.bounds
  59. self.videoLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
  60. view.layer.addSublayer(self.videoLayer!)
  61. // At end go back to start
  62. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { (notification) in
  63. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  64. self.player?.seek(to: .zero)
  65. if metadata.livePhoto {
  66. NCManageDatabase.shared.deleteVideoTime(metadata: metadata)
  67. }
  68. }
  69. }
  70. self.rateObserver = self.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  71. if self.pictureInPictureOcId != metadata.ocId {
  72. self.player?.play()
  73. }
  74. }
  75. viewerVideoToolBar?.setBarPlayer(player: player)
  76. }
  77. //NCNetworking.shared.getVideoUrl(metadata: metadata) { url in
  78. // if let url = url {
  79. //}
  80. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  81. play(url: url)
  82. }
  83. }
  84. func videoStop() {
  85. guard let metadata = self.metadata else { return }
  86. player?.pause()
  87. player?.seek(to: CMTime.zero)
  88. if let timeObserver = timeObserver {
  89. player?.removeTimeObserver(timeObserver)
  90. self.timeObserver = nil
  91. }
  92. if rateObserver != nil {
  93. player?.removeObserver(self, forKeyPath: "rate")
  94. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  95. NCKTVHTTPCache.shared.stopProxy(metadata: metadata)
  96. self.rateObserver = nil
  97. }
  98. videoLayer?.removeFromSuperlayer()
  99. }
  100. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  101. guard let metadata = self.metadata else { return }
  102. if keyPath != nil && keyPath == "rate" {
  103. self.viewerVideoToolBar?.setToolBar()
  104. if ((player?.rate) == 1) {
  105. if let time = NCManageDatabase.shared.getVideoTime(metadata: metadata) {
  106. player?.seek(to: time)
  107. player?.isMuted = CCUtility.getAudioMute()
  108. }
  109. } else if !metadata.livePhoto {
  110. if let time = player?.currentTime(), let duration = self.player?.currentItem?.asset.duration {
  111. let timeSecond = Double(CMTimeGetSeconds(time))
  112. let durationSeconds = Double(CMTimeGetSeconds(duration))
  113. if timeSecond < durationSeconds {
  114. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }