NCViewerVideoAudio.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // NCViewerVideoAudio.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. import KTVHTTPCache
  25. class NCViewerVideoAudio: AVPlayerViewController {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var metadata = tableMetadata()
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. var videoURL: URL?
  31. setupHTTPCache()
  32. saveCache()
  33. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  34. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  35. } else {
  36. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  37. return
  38. }
  39. videoURL = KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  40. guard let authData = (appDelegate.user + ":" + appDelegate.password).data(using: .utf8) else {
  41. return
  42. }
  43. let authValue = "Basic " + authData.base64EncodedString(options: [])
  44. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  45. }
  46. if let url = videoURL {
  47. let video = AVPlayer(url: url)
  48. player = video
  49. // At end go back to start
  50. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  51. let player = notification.object as! AVPlayerItem
  52. player.seek(to: CMTime.zero, completionHandler: nil)
  53. }
  54. player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  55. player?.play()
  56. }
  57. }
  58. override func viewDidDisappear(_ animated: Bool) {
  59. super.viewDidDisappear(animated)
  60. player?.pause()
  61. removeObserver()
  62. if KTVHTTPCache.proxyIsRunning() {
  63. KTVHTTPCache.proxyStop()
  64. }
  65. }
  66. //MARK: -
  67. func saveCache() {
  68. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  69. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  70. let videoURL = URL(string: stringURL)
  71. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  72. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  73. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  74. KTVHTTPCache.cacheDelete(with: videoURL)
  75. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
  76. }
  77. }
  78. //MARK: - Observer
  79. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  80. if keyPath != nil && keyPath == "rate" {
  81. if player?.rate == 1 {
  82. print("start")
  83. } else {
  84. print("stop")
  85. }
  86. saveCache()
  87. }
  88. }
  89. @objc func removeObserver() {
  90. player?.removeObserver(self, forKeyPath: "rate", context: nil)
  91. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  92. }
  93. //MARK: - KTVHTTPCache
  94. @objc func setupHTTPCache() {
  95. if KTVHTTPCache.proxyIsRunning() {
  96. KTVHTTPCache.proxyStop()
  97. }
  98. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  99. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  100. KTVHTTPCache.logSetConsoleLogEnable(true)
  101. }
  102. do {
  103. try KTVHTTPCache.proxyStart()
  104. } catch let error {
  105. print("Proxy Start error : \(error)")
  106. }
  107. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  108. print("URL Filter reviced URL : " + String(describing: url))
  109. return url
  110. }
  111. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  112. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  113. return false
  114. }
  115. }
  116. }