VideoURLSenderItemCreator.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. import CocoaLumberjackSwift
  22. import PromiseKit
  23. enum VideoURLSenderItemCreatorError : Error {
  24. case thumbnailCreationFailed
  25. case couldNotCreateExportSession
  26. case generalError
  27. }
  28. @objc protocol VideoConversionProgressDelegate {
  29. func videoExportSession(exportSession : SDAVAssetExportSession)
  30. }
  31. @objc public class VideoURLSenderItemCreator: NSObject {
  32. @objc public static let temporaryDirectory = "tmpVideoCreator"
  33. @objc var encodeProgressDelegate : VideoConversionProgressDelegate?
  34. func getThumbnail(asset : AVAsset) -> Promise<UIImage> {
  35. return Promise { seal in
  36. guard let thumbnail = MediaConverter.getThumbnailForVideo(asset) else {
  37. seal.reject(VideoURLSenderItemCreatorError.thumbnailCreationFailed)
  38. return
  39. }
  40. seal.resolve(thumbnail, nil)
  41. }
  42. }
  43. func getExportSession(asset : AVAsset) -> Promise<SDAVAssetExportSession> {
  44. return Promise { seal in
  45. guard let outputURL = MediaConverter.getAssetOutputURL() else {
  46. seal.reject(VideoURLSenderItemCreatorError.couldNotCreateExportSession)
  47. return
  48. }
  49. guard let exportSession = MediaConverter.getAVAssetExportSession(from: asset, outputURL: outputURL) else {
  50. seal.reject(VideoURLSenderItemCreatorError.couldNotCreateExportSession)
  51. return
  52. }
  53. // exportSession.addObserver(self, forKeyPath: "progress", options: .new, context: nil)
  54. seal.fulfill(exportSession)
  55. }
  56. }
  57. public override func observeValue(forKeyPath keyPath: String?,
  58. of object: Any?,
  59. change: [NSKeyValueChangeKey : Any]?,
  60. context: UnsafeMutableRawPointer?) {
  61. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  62. if let exportSession = object as? SDAVAssetExportSession {
  63. self.encodeProgressDelegate?.videoExportSession(exportSession: exportSession)
  64. }
  65. }
  66. @objc public func getExportSession(for asset : AVAsset) -> SDAVAssetExportSession? {
  67. var exportSession : SDAVAssetExportSession?
  68. let sema = DispatchSemaphore(value: 0)
  69. DispatchQueue.global(qos: .userInitiated).async {
  70. self.getExportSession(asset: asset).done { session in
  71. exportSession = session
  72. sema.signal()
  73. }.catch { error in
  74. sema.signal()
  75. DDLogError("Encountered an error: \(error)")
  76. }
  77. }
  78. sema.wait()
  79. return exportSession
  80. }
  81. func convertVideo(asset : AVAsset) -> Promise<URL> {
  82. getExportSession(asset: asset).then { exportSession in
  83. return self.convertVideo(on: exportSession, asset: asset)
  84. }
  85. }
  86. func convertVideo(on exportSession : SDAVAssetExportSession, asset : AVAsset) -> Promise<URL> {
  87. return Promise { seal in
  88. MediaConverter.convertVideoAsset(asset, with: exportSession, onCompletion: { completionURL in
  89. guard let url = completionURL else {
  90. seal.reject(VideoURLSenderItemCreatorError.generalError)
  91. return
  92. }
  93. return seal.fulfill(url)
  94. }, onError: { completionError in
  95. guard let error = completionError else {
  96. seal.reject(VideoURLSenderItemCreatorError.generalError)
  97. return
  98. }
  99. seal.reject(error)
  100. })
  101. }
  102. }
  103. public func senderItem(from asset : AVAsset) -> Promise<URLSenderItem> {
  104. let bgq = DispatchQueue.global(qos: .userInitiated)
  105. return self.getExportSession(asset: asset).then(on: bgq) { exportSession in
  106. self.convertVideo(on: exportSession, asset: asset)
  107. }.compactMap(on: bgq) { url in
  108. URLSenderItem.init(url: url, type: kUTTypeMPEG4 as String, renderType: 1, sendAsFile: true)
  109. }
  110. }
  111. @objc public func senderItem(from videoUrl : URL) -> URLSenderItem? {
  112. guard let scheme = videoUrl.scheme else {
  113. return nil
  114. }
  115. if (scheme != "file"), !FileManager.default.fileExists(atPath: videoUrl.absoluteString) {
  116. return nil
  117. }
  118. let asset = AVURLAsset(url: videoUrl)
  119. if !asset.isExportable {
  120. return nil
  121. }
  122. return senderItem(fromAsset: asset)
  123. }
  124. @objc public func senderItem(fromAsset : AVAsset) -> URLSenderItem? {
  125. var senderItem : URLSenderItem?
  126. let sema = DispatchSemaphore(value: 0)
  127. let bgq = DispatchQueue.global(qos: .userInitiated)
  128. firstly {
  129. self.senderItem(from: fromAsset)
  130. }.done(on: bgq) { item in
  131. senderItem = item
  132. sema.signal()
  133. }.catch { error in
  134. DDLogError("Error: \(error)")
  135. }
  136. sema.wait()
  137. return senderItem
  138. }
  139. @objc func senderItem(from asset : AVAsset, on exportSession : SDAVAssetExportSession) -> URLSenderItem? {
  140. var senderItem : URLSenderItem?
  141. let sema = DispatchSemaphore(value: 0)
  142. firstly {
  143. self.convertVideo(on: exportSession, asset: asset)
  144. }.then { url in
  145. self.senderItem(from: asset)
  146. }.done { item in
  147. senderItem = item
  148. sema.signal()
  149. }.catch { error in
  150. sema.signal()
  151. DDLogError("Error: \(error)")
  152. }
  153. sema.wait()
  154. return senderItem
  155. }
  156. @objc static public func writeToTemporaryDirectory(data : Data) -> URL? {
  157. let fileManager = FileManager.default
  158. let tmpDirectory = fileManager.temporaryDirectory
  159. let tmpFolder = tmpDirectory.appendingPathComponent(VideoURLSenderItemCreator.temporaryDirectory)
  160. let fileName = SwiftUtils.pseudoRandomString(length: 10)
  161. let fileUrl = tmpFolder.appendingPathComponent(fileName).appendingPathExtension("mp4")
  162. do {
  163. try fileManager.createDirectory(at: tmpFolder, withIntermediateDirectories: true, attributes: nil)
  164. } catch {
  165. DDLogError("Could not create temporary directory \(error)")
  166. return nil
  167. }
  168. do {
  169. try data.write(to: fileUrl)
  170. }
  171. catch {
  172. DDLogError("Could not write file \(error)")
  173. return nil
  174. }
  175. return fileUrl
  176. }
  177. @objc static public func cleanTemporaryDirectory() -> Bool {
  178. let fileManager = FileManager.default
  179. let tmpDirectory = fileManager.temporaryDirectory
  180. let tmpFolder = tmpDirectory.appendingPathComponent(VideoURLSenderItemCreator.temporaryDirectory)
  181. do {
  182. try fileManager.removeItem(at: tmpFolder)
  183. } catch {
  184. DDLogError("Could not clean temporary directory \(error)")
  185. return false
  186. }
  187. return true
  188. }
  189. /// Returns a pseudorandom string
  190. /// - Parameter length: the length of the returned String
  191. /// - Returns: A pseudorandom string of the given length
  192. private static func pseudoRandomString(length: Int) -> String {
  193. let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  194. return String((0..<length).map{ _ in letters.randomElement()! })
  195. }
  196. }