NCVideoViewController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // NCVideo.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import KTVHTTPCache
  10. class NCVideoViewController: AVPlayerViewController {
  11. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  12. var metadata = tableMetadata()
  13. var videoURL: URL?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. setupHTTPCache()
  17. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  18. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  19. } else {
  20. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  21. return
  22. }
  23. videoURL = KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  24. guard let authData = (appDelegate.user + ":" + appDelegate.password).data(using: .utf8) else {
  25. return
  26. }
  27. let authValue = "Basic " + authData.base64EncodedString(options: [])
  28. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  29. }
  30. if let videoURL = videoURL {
  31. let video = AVPlayer(url: videoURL)
  32. player = video
  33. // At end go back to start
  34. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  35. let player = notification.object as! AVPlayerItem
  36. player.seek(to: CMTime.zero, completionHandler: nil)
  37. }
  38. player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  39. player?.play()
  40. }
  41. }
  42. override func viewDidDisappear(_ animated: Bool) {
  43. super.viewDidDisappear(animated)
  44. player?.pause()
  45. removeObserver()
  46. if KTVHTTPCache.proxyIsRunning() {
  47. KTVHTTPCache.proxyStop()
  48. }
  49. }
  50. //MARK: - Observer
  51. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  52. if keyPath != nil && keyPath == "rate" {
  53. if player?.rate == 1 {
  54. print("start")
  55. } else {
  56. print("stop")
  57. }
  58. // Save cache
  59. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  60. guard let videoURL = self.videoURL else { return }
  61. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  62. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  63. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  64. KTVHTTPCache.cacheDelete(with: self.videoURL)
  65. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
  66. }
  67. }
  68. }
  69. @objc func removeObserver() {
  70. player?.removeObserver(self, forKeyPath: "rate", context: nil)
  71. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  72. }
  73. //MARK: - KTVHTTPCache
  74. @objc func setupHTTPCache() {
  75. if KTVHTTPCache.proxyIsRunning() {
  76. KTVHTTPCache.proxyStop()
  77. }
  78. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  79. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  80. KTVHTTPCache.logSetConsoleLogEnable(true)
  81. }
  82. do {
  83. try KTVHTTPCache.proxyStart()
  84. } catch let error {
  85. print("Proxy Start error : \(error)")
  86. }
  87. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  88. print("URL Filter reviced URL : " + String(describing: url))
  89. return url
  90. }
  91. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  92. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  93. return false
  94. }
  95. }
  96. }