NCVideoViewController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. var videoURLProxy: URL?
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  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 = URL(string: stringURL)
  24. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: videoURL)
  25. guard let authData = (appDelegate.user + ":" + appDelegate.password).data(using: .utf8) else {
  26. return
  27. }
  28. let authValue = "Basic " + authData.base64EncodedString(options: [])
  29. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  30. }
  31. let video = AVPlayer(url: videoURL!)
  32. player = video
  33. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  34. let player = notification.object as! AVPlayerItem
  35. player.seek(to: CMTime.zero, completionHandler: nil)
  36. }
  37. player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  38. player?.play()
  39. }
  40. override func viewDidDisappear(_ animated: Bool) {
  41. super.viewDidDisappear(animated)
  42. }
  43. deinit {
  44. }
  45. //MARK: - Observer
  46. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  47. if keyPath != nil && keyPath == "rate" {
  48. if player?.rate == 1 {
  49. print("start")
  50. } else {
  51. print("stop")
  52. }
  53. // Save cache
  54. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  55. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: self.videoURL) else {
  56. return
  57. }
  58. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  59. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  60. KTVHTTPCache.cacheDelete(with: self.videoURL)
  61. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
  62. }
  63. }
  64. }
  65. @objc func removeObserver() {
  66. player?.removeObserver(self, forKeyPath: "rate", context: nil)
  67. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  68. }
  69. //MARK: - KTVHTTPCache
  70. @objc func setupHTTPCache() {
  71. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  72. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  73. KTVHTTPCache.logSetConsoleLogEnable(true)
  74. }
  75. do {
  76. try KTVHTTPCache.proxyStart()
  77. } catch let error {
  78. print("Proxy Start error : \(error)")
  79. }
  80. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  81. print("URL Filter reviced URL : " + String(describing: url))
  82. return url
  83. }
  84. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  85. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  86. return false
  87. }
  88. }
  89. }