NCVideoCommon.swift 5.7 KB

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