NCVideoViewController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. 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. }
  41. override func viewDidDisappear(_ animated: Bool) {
  42. super.viewDidDisappear(animated)
  43. player?.pause()
  44. removeObserver()
  45. if KTVHTTPCache.proxyIsRunning() {
  46. KTVHTTPCache.proxyStop()
  47. }
  48. }
  49. //MARK: - Observer
  50. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  51. if keyPath != nil && keyPath == "rate" {
  52. if player?.rate == 1 {
  53. print("start")
  54. } else {
  55. print("stop")
  56. }
  57. // Save cache
  58. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  59. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: self.videoURL) else {
  60. return
  61. }
  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. }