NCViewerMedia.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/09/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import KTVHTTPCache
  10. class NCViewerMedia: NSObject {
  11. var detail: CCDetail!
  12. var metadata: tableMetadata!
  13. var videoURL: URL!
  14. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  15. var safeAreaBottom: Int = 0
  16. @objc static let sharedInstance: NCViewerMedia = {
  17. let viewMedia = NCViewerMedia()
  18. viewMedia.setupHTTPCache()
  19. return viewMedia
  20. }()
  21. @objc func viewMedia(_ metadata: tableMetadata, detail: CCDetail) {
  22. var videoURLProxy: URL!
  23. self.detail = detail
  24. self.metadata = metadata
  25. guard let rootView = UIApplication.shared.keyWindow else {
  26. return
  27. }
  28. if #available(iOS 11.0, *) {
  29. safeAreaBottom = Int(rootView.safeAreaInsets.bottom)
  30. }
  31. if CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  32. self.videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  33. videoURLProxy = videoURL
  34. } else {
  35. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  36. return
  37. }
  38. self.videoURL = URL(string: stringURL)
  39. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: self.videoURL)
  40. guard let authData = (appDelegate.activeUser + ":" + appDelegate.activePassword).data(using: .utf8) else {
  41. return
  42. }
  43. let authValue = "Basic " + authData.base64EncodedString(options: [])
  44. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  45. // Disable Button Action (the file is in download via Proxy Server)
  46. detail.buttonAction.isEnabled = false
  47. }
  48. appDelegate.player = AVPlayer(url: videoURLProxy)
  49. appDelegate.playerController = AVPlayerViewController()
  50. appDelegate.playerController.player = appDelegate.player
  51. appDelegate.playerController.view.frame = CGRect(x: 0, y: 0, width: Int(rootView.bounds.size.width), height: Int(rootView.bounds.size.height) - Int(k_detail_Toolbar_Height) - safeAreaBottom - 1)
  52. appDelegate.playerController.allowsPictureInPicturePlayback = false
  53. detail.addChild(appDelegate.playerController)
  54. detail.view.addSubview(appDelegate.playerController.view)
  55. appDelegate.playerController.didMove(toParent: detail)
  56. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  57. let player = notification.object as! AVPlayerItem
  58. player.seek(to: CMTime.zero)
  59. }
  60. appDelegate.player.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  61. detail.isMediaObserver = true
  62. appDelegate.player.play()
  63. }
  64. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  65. if keyPath != nil && keyPath == "rate" {
  66. if appDelegate.player.rate == 1 {
  67. print("start")
  68. } else {
  69. print("stop")
  70. }
  71. // Save cache
  72. if !CCUtility.fileProviderStorageExists(self.metadata.fileID, fileNameView:self.metadata.fileNameView) {
  73. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: self.videoURL) else {
  74. return
  75. }
  76. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageFileID(self.metadata.fileID, fileNameView: self.metadata.fileNameView))
  77. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  78. KTVHTTPCache.cacheDelete(with: self.videoURL)
  79. // reload Data Source
  80. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl:self.metadata.serverUrl, fileID: self.metadata.fileID, action: k_action_MOD)
  81. // Enabled Button Action (the file is in local)
  82. self.detail.buttonAction.isEnabled = true
  83. }
  84. }
  85. }
  86. @objc func removeObserver() {
  87. appDelegate.player.removeObserver(self, forKeyPath: "rate", context: nil)
  88. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  89. }
  90. @objc func setupHTTPCache() {
  91. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  92. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  93. KTVHTTPCache.logSetConsoleLogEnable(true)
  94. }
  95. do {
  96. try KTVHTTPCache.proxyStart()
  97. } catch let error {
  98. print("Proxy Start error : \(error)")
  99. }
  100. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  101. print("URL Filter reviced URL : " + String(describing: url))
  102. return url
  103. }
  104. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  105. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  106. return false
  107. }
  108. }
  109. }