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