NCViewerVideo.swift 5.7 KB

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