NCViewerMedia.swift 5.9 KB

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