NCViewerVideo.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 startPictureInPicture(metadata: tableMetadata)
  26. func stopPictureInPicture(metadata: tableMetadata, playing: Bool)
  27. }
  28. @objc class NCViewerVideo: AVPlayerViewController {
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. var metadata = tableMetadata()
  31. var pictureInPicture: Bool = false
  32. var delegateViewerVideo: NCViewerVideoDelegate?
  33. private var rateObserverToken: Any?
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. delegate = self
  37. }
  38. override func viewDidAppear(_ animated: Bool) {
  39. super.viewDidAppear(animated)
  40. NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
  41. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  42. player = AVPlayer(url: url)
  43. // At end go back to start
  44. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main) { (notification) in
  45. if let item = notification.object as? AVPlayerItem, let currentItem = self.player?.currentItem, item == currentItem {
  46. self.player?.seek(to: CMTime.zero)
  47. }
  48. }
  49. rateObserverToken = player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  50. player?.play()
  51. if let time = NCManageDatabase.shared.getVideoTime(metadata: self.metadata) {
  52. player?.seek(to: time)
  53. }
  54. player?.isMuted = CCUtility.getAudioMute()
  55. }
  56. }
  57. override func viewWillDisappear(_ animated: Bool) {
  58. super.viewWillDisappear(animated)
  59. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  60. if let player = self.player {
  61. CCUtility.setAudioMute(player.isMuted)
  62. }
  63. if !pictureInPicture {
  64. player?.pause()
  65. if rateObserverToken != nil {
  66. player?.removeObserver(self, forKeyPath: "rate")
  67. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  68. NCKTVHTTPCache.shared.stopProxy()
  69. self.rateObserverToken = nil
  70. }
  71. }
  72. }
  73. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  74. if keyPath != nil && keyPath == "rate" {
  75. NCKTVHTTPCache.shared.saveCache(metadata: metadata)
  76. }
  77. }
  78. }
  79. extension NCViewerVideo: AVPlayerViewControllerDelegate {
  80. func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
  81. true
  82. }
  83. func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
  84. pictureInPicture = true
  85. delegateViewerVideo?.startPictureInPicture(metadata: metadata)
  86. }
  87. func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
  88. NCManageDatabase.shared.addVideoTime(metadata: metadata, time: player?.currentTime())
  89. pictureInPicture = false
  90. let playing = player?.timeControlStatus == .playing
  91. delegateViewerVideo?.stopPictureInPicture(metadata: metadata, playing: playing)
  92. }
  93. }