NCViewerVideo.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // NCViewerVideo.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/10/2020.
  6. // Copyright © 2020 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. protocol NCViewerVideoDelegate {
  25. func stopPictureInPicture(metadata: tableMetadata)
  26. func startPictureInPicture(metadata: tableMetadata)
  27. func playerCurrentTime(_ time: CMTime?)
  28. }
  29. @objc class NCViewerVideo: AVPlayerViewController {
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. var metadata = tableMetadata()
  32. var seekTime: CMTime?
  33. var pictureInPicture: Bool = false
  34. var delegateViewerVideo: NCViewerVideoDelegate?
  35. private var rateObserverToken: Any?
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. delegate = self
  39. }
  40. override func viewDidAppear(_ animated: Bool) {
  41. super.viewDidAppear(animated)
  42. var videoURL: URL?
  43. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  44. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  45. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  46. } else {
  47. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  48. return
  49. }
  50. allowsPictureInPicturePlayback = false
  51. videoURL = NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
  52. }
  53. if let url = videoURL {
  54. player = AVPlayer(url: url)
  55. // At end go back to start
  56. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  57. self.player?.seek(to: CMTime.zero)
  58. }
  59. rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  60. player?.play()
  61. if seekTime != nil {
  62. player?.seek(to: seekTime!)
  63. seekTime = nil
  64. }
  65. player?.isMuted = CCUtility.getAudioMute()
  66. }
  67. }
  68. override func viewWillDisappear(_ animated: Bool) {
  69. super.viewWillDisappear(animated)
  70. if let player = self.player {
  71. CCUtility.setAudioMute(player.isMuted)
  72. }
  73. if !pictureInPicture {
  74. player?.pause()
  75. if rateObserverToken != nil {
  76. player?.removeObserver(self, forKeyPath: "rate")
  77. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  78. NCKTVHTTPCache.shared.stopProxy()
  79. self.delegateViewerVideo?.playerCurrentTime(player?.currentTime())
  80. self.rateObserverToken = nil
  81. }
  82. }
  83. }
  84. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  85. if keyPath != nil && keyPath == "rate" {
  86. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  87. }
  88. }
  89. }
  90. extension NCViewerVideo: AVPlayerViewControllerDelegate {
  91. func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
  92. false
  93. }
  94. func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
  95. pictureInPicture = true
  96. delegateViewerVideo?.startPictureInPicture(metadata: metadata)
  97. }
  98. func playerViewControllerDidStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
  99. pictureInPicture = false
  100. delegateViewerVideo?.stopPictureInPicture(metadata: metadata)
  101. }
  102. }