NCViewerVideo.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // NCViewerVideo.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/09/18.
  6. // Copyright © 2018 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 NCViewerVideo: NSObject {
  26. var metadata: tableMetadata!
  27. var videoURL: URL?
  28. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. var safeAreaBottom: Int = 0
  30. @objc static let sharedInstance: NCViewerVideo = {
  31. let viewVideo = NCViewerVideo()
  32. viewVideo.setupHTTPCache()
  33. return viewVideo
  34. }()
  35. @objc func viewMedia(_ metadata: tableMetadata, view: UIView, frame: CGRect) {
  36. var videoURLProxy: URL!
  37. self.metadata = metadata
  38. guard let rootView = UIApplication.shared.keyWindow else {
  39. return
  40. }
  41. if #available(iOS 11.0, *) {
  42. safeAreaBottom = Int(rootView.safeAreaInsets.bottom)
  43. }
  44. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  45. self.videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  46. videoURLProxy = videoURL
  47. } else {
  48. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  49. return
  50. }
  51. self.videoURL = URL(string: stringURL)
  52. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: self.videoURL)
  53. guard let authData = (appDelegate.activeUser + ":" + appDelegate.activePassword).data(using: .utf8) else {
  54. return
  55. }
  56. let authValue = "Basic " + authData.base64EncodedString(options: [])
  57. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  58. }
  59. appDelegate.player = AVPlayer(url: videoURLProxy)
  60. appDelegate.playerController = AVPlayerViewController()
  61. appDelegate.playerController.player = appDelegate.player
  62. appDelegate.playerController.view.frame = frame
  63. appDelegate.playerController.allowsPictureInPicturePlayback = false
  64. view.addSubview(appDelegate.playerController.view)
  65. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  66. let player = notification.object as! AVPlayerItem
  67. player.seek(to: CMTime.zero)
  68. }
  69. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  70. self.appDelegate.player.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  71. self.appDelegate.isMediaObserver = true
  72. self.appDelegate.player.play()
  73. }
  74. }
  75. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  76. if keyPath != nil && keyPath == "rate" {
  77. if appDelegate.player.rate == 1 {
  78. print("start")
  79. } else {
  80. print("stop")
  81. }
  82. // Save cache
  83. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  84. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: self.videoURL) else {
  85. return
  86. }
  87. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  88. _ = NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  89. KTVHTTPCache.cacheDelete(with: self.videoURL)
  90. // reload Data Source
  91. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl:self.metadata.serverUrl, ocId: self.metadata.ocId, action: k_action_MOD)
  92. }
  93. }
  94. }
  95. @objc func removeObserver() {
  96. appDelegate.player.removeObserver(self, forKeyPath: "rate", context: nil)
  97. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  98. }
  99. @objc func setupHTTPCache() {
  100. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  101. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  102. KTVHTTPCache.logSetConsoleLogEnable(true)
  103. }
  104. do {
  105. try KTVHTTPCache.proxyStart()
  106. } catch let error {
  107. print("Proxy Start error : \(error)")
  108. }
  109. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  110. print("URL Filter reviced URL : " + String(describing: url))
  111. return url
  112. }
  113. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  114. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  115. return false
  116. }
  117. }
  118. }