NCVideoViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. setupHTTPCache()
  18. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  19. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  20. } else {
  21. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  22. return
  23. }
  24. videoURL = URL(string: stringURL)
  25. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: videoURL)
  26. guard let authData = (appDelegate.user + ":" + appDelegate.password).data(using: .utf8) else {
  27. return
  28. }
  29. let authValue = "Basic " + authData.base64EncodedString(options: [])
  30. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  31. }
  32. let video = AVPlayer(url: videoURL!)
  33. player = video
  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. 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. }
  97. /*
  98. @IBAction func touchUpInsidecloseButton(_ sender: Any) {
  99. if NCVideoCommon.shared.player != nil && NCVideoCommon.shared.player.rate != 0 {
  100. NCVideoCommon.shared.player.pause()
  101. }
  102. if NCVideoCommon.shared.isMediaObserver {
  103. NCVideoCommon.shared.isMediaObserver = false
  104. NCVideoCommon.shared.removeObserver()
  105. }
  106. dismiss(animated: false) { }
  107. }
  108. */