Downloading multiple HLS audio files simultaneously fails - ios

I have been trying to multiple HLS audio files together in iOS. The files are encrypted with a key. I need to fetch the key and store it locally for offline use. When I download a small number (2 or 3 files) of files simultaneously it works fine, but if I start downloading 10-15 files simultaneously most of them fails with the error message-
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could
not be completed" UserInfo={NSLocalizedFailureReason=An unknown error
occurred (-17377), NSLocalizedDescription=The operation could not be
completed}
I am getting error message from NSURLErrorDomain as well but they are rare.
The link of the approach which I am using to fetch the key for offline use is below -
Playing Offline HLS with AES-128 encryption iOS
Any help would be appreciated.
class AudioDownloader {
var productKey: String
var downloadUrl: URL
var downloadSession: AVAssetDownloadURLSession?
var fakeDownloadUrl: URL?
var downloadTask: AVAssetDownloadTask?
func downloadAudio() {
if downloadSession == nil {
let configuration = URLSessionConfiguration.background(withIdentifier: self.productKey)
downloadSession = AVAssetDownloadURLSession(configuration: configuration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)
configuration.shouldUseExtendedBackgroundIdleMode = true
configuration.httpShouldSetCookies = true
configuration.httpShouldUsePipelining = false
configuration.allowsCellularAccess = true
configuration.isDiscretionary = true
}
self.fakeDownloadUrl = self.convertToScheme(url: self.downloadUrl, scheme: "fakehttp")
let asset = AVURLAsset(url: self.fakeDownloadUrl!)
let loader = asset.resourceLoader
loader.setDelegate(self, queue: DispatchQueue(label: "dispatch2"))
self.downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset, assetTitle: "assetTitle \(self.productKey)", assetArtworkData: nil, options: nil)!
self.downloadTask?.taskDescription = self.productKey
self.downloadTask?.resume()
}
}

Related

How to set AWS Appsync request timeout limit || AWSAppSync Client not giving callback

I'm using AWS Appsync for the current App I'm developing and facing a serious issue that is Whenever I fire queries in Appsync client, when there is slow internet connection the request never end with a callback. I checked over internet there is limited source of information on this topic and also found this issue that is still open.
This is the code I used to get the response
func getAllApi(completion:#escaping DataCallback){
guard isInternetAvailabele() else {
completion(nil)
return
}
// AppSyncManager.Client() is AWSAppSyncClient Object
AppSyncManager.Client().fetch(query: GetlAllPostQuery(input: allInputs), cachePolicy:.fetchIgnoringCacheData) {
(result, error) in
var haveError:Bool = error != nil
if let _ = result?.data?.getAllPostings?.responseCode {haveError = false} else {haveError = true}
if haveError {
print(error?.localizedDescription ?? "")
completion(nil)
return
}
if result != nil{
completion(result)
}else{
completion(nil)
}
}
}
The code works fine with internet connection and I have already checked at the top if there is no internet but when there is slow internet connection or the wifi is connected to a hotspot that I created with my mobile with internet data disabled the request doesn't return any callback it should give failed alert like we get in other apis when the request time out.
Is there any support for request for request time out or did I miss something?
Note : I recieved these logs in Terminal
Task <06E9BBF4-5731-471B-9B7D-19E5E504E57F>.<45> HTTP load failed (error code: -1001 [1:60])
Task <D91CA952-DBB5-4DBD-9A90-98E2069DBE2D>.<46> HTTP load failed (error code: -1001 [1:60])
Task <06E9BBF4-5731-471B-9B7D-19E5E504E57F>.<45> finished with error - code: -1001
Task <D91CA952-DBB5-4DBD-9A90-98E2069DBE2D>.<46> finished with error - code: -1001
Actually there could be two possible ways to fix the issue,
1) While configuring AWSAppSyncClientConfiguration, provide a custom URLSessionConfiguration and set the request timeout to your needs,
extension URLSessionConfiguration {
/// A `URLSessionConfiguration` to have a request timeout of 1 minutes.
static let customDelayed: URLSessionConfiguration = {
let secondsInOneMinute = 60
let numberOfMinutesForTimeout = 1
let timoutInterval = TimeInterval(numberOfMinutesForTimeout * secondsInOneMinute)
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timoutInterval
configuration.timeoutIntervalForResource = timoutInterval
return configuration
}()
}
And pass this session configuration i.e URLSessionConfiguration.customDelayed when initializing AWSAppSyncClientConfiguration as it accepts the URLSessionConfiguration in the below constructor,
public convenience init(url: URL,
serviceRegion: AWSRegionType,
credentialsProvider: AWSCredentialsProvider,
urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default,
databaseURL: URL? = nil,
connectionStateChangeHandler: ConnectionStateChangeHandler? = nil,
s3ObjectManager: AWSS3ObjectManager? = nil,
presignedURLClient: AWSS3ObjectPresignedURLGenerator? = nil) throws {
2) If the first doesn't work then you have another option to edit/unlock the pod files directly. There is a class AWSAppSyncRetryHandler where you can change the logic for retrying request. If you are able to fix the issue then you can fork the original repo, clone your repo, make changes in your repo and in pods file point this pod to use your repository. This should be done as changing the pod files directly is absolutely wrong until you are really stuck and want to find some solution.
Update: This issue has been fixed with AppSync SDK 2.7.0

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost

I am getting this error when using SwiftyBeaver logger, which tries to send data to the cloud via this code:
func sendToServerAsync(str: String?, complete: (ok: Bool, status: Int) -> ()) {
if let payload = str, let queue = self.queue {
// create operation queue which uses current serial queue of destination
let operationQueue = NSOperationQueue()
operationQueue.underlyingQueue = queue
let session = NSURLSession(configuration:
NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: nil, delegateQueue: operationQueue)
// assemble request
let request = NSMutableURLRequest(URL: serverURL)
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
// basic auth header
let credentials = "\(appID):\(appSecret)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentials.base64EncodedStringWithOptions([])
request.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")
// POST parameters
let params = ["payload": payload]
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch let error as NSError {
toNSLog("Error! Could not create JSON for server payload. \(error)")
}
//toNSLog("sending params: \(params)")
//toNSLog("\n\nbefore sendToServer on thread '\(threadName())'")
sendingInProgress = true
// send request async to server on destination queue
let task = session.dataTaskWithRequest(request) {
_, response, error in
var ok = false
var status = 0
//toNSLog("callback of sendToServer on thread '\(self.threadName())'")
if let error = error {
// an error did occur
self.toNSLog("Error! Could not send entries to server. \(error)")
} else {
if let response = response as? NSHTTPURLResponse {
status = response.statusCode
if status == 200 {
// all went well, entries were uploaded to server
ok = true
} else {
// status code was not 200
var msg = "Error! Sending entries to server failed "
msg += "with status code \(status)"
self.toNSLog(msg)
}
}
}
return complete(ok: ok, status: status)
}
task.resume()
}
}
The strange thing is it works for the first two or three log entries, and then stops due to the above error. I tried to reset content and settings on the simulator and reboot my simulator (as suggested in Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost.") but that just fixes it temporarily--after the first 2-3 log entries, it starts failing again.
I tried debugging this for hours with the creator of SwiftBeaver last night, but we couldn't get it to work. Seems like not many people are seeing this issue.
I tried removing my Wifi connection and reconnecting, but that didn't work either.
Any guidance on this would be much appreciated.
FYI, I'm using Swift 2 and XCode 7.3.
This is probably caused by HTTP keep-alive support being seriously buggy in the iOS simulator. See:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
for more details, but the short answer is to disable keep-alive on the server that you use when doing simulator testing, or better yet, add some logic that immediately retries the request if it sees that particular error.

Firebase Storage download to local file error

I'm trying to download the image f5bd8360.jpeg from my Firebase Storage.When I download this image to memory using dataWithMaxSize:completion, I'm able to download it.
My problem comes when I try to download the image to a local file using the writeToFile: instance method. I'm getting the following error:
Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown
error occurred, please check the server response."
UserInfo={object=images/f5bd8360.jpeg,
bucket=fir-test-3d9a6.appspot.com, NSLocalizedDescription=An unknown
error occurred, please check the server response.,
ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/Documents/images,
NSUnderlyingError=0x1700562c0 {Error Domain=NSPOSIXErrorDomain Code=1
"Operation not permitted"}, ResponseErrorCode=513}"
Here is a snippet of my Swift code:
#IBAction func buttonClicked(_ sender: UIButton) {
// Get a reference to the storage service, using the default Firebase App
let storage = FIRStorage.storage()
// Get reference to the image on Firebase Storage
let imageRef = storage.reference(forURL: "gs://fir-test-3d9a6.appspot.com/images/f5bd8360.jpeg")
// Create local filesystem URL
let localURL: URL! = URL(string: "file:///Documents/images/f5bd8360.jpeg")
// Download to the local filesystem
let downloadTask = imageRef.write(toFile: localURL) { (URL, error) -> Void in
if (error != nil) {
print("Uh-oh, an error occurred!")
print(error)
} else {
print("Local file URL is returned")
}
}
}
I found another question with the same error I'm getting but it was never answered in full. I think the proposal is right. I don't have permissions to write in the file. However, I don't know how gain permissions. Any ideas?
The problem is that at the moment when you write this line:
let downloadTask = imageRef.write(toFile: localURL) { (URL, error) -> Void in
etc.
you don't yet have permission to write to that (localURL) location. To get the permission you need to write the following code before trying to write anything to localURL
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let localURL = documentsURL.appendingPathComponent("filename")
By doing it you will write the file into the following path on your device (if you are testing on the real device):
file:///var/mobile/Containers/Data/Application/XXXXXXXetc.etc./Documents/filename
If you are testing on the simulator, the path will obviously be somewhere on the computer.

Error 'The request timed out' when uploading files in parallel on AWSS3

I have a problem with the parallel upload in AWSS3
I'm using
AWSTask(forCompletionOfAllTasks:tasks).continueWithBlock({task -> AWSTask in
to upload files in parallel on S3. It works well with small amount of file, but when I'm sending more than 50 files in parallel, I have a lot of timeout error and I don't understand why.
I tried several time to load 150 files in parallel and I have a timeout after the 60th file was uploaded with success. There is around 50 timeout error which take around 1 or 2 minutes, after that, the upload continue and at the end all the files are uploaded successfully but these timeout slow a lot the upload...
I see the errors in the logs but I don't know how to catch it in the code, the upload request (AWSS3TransferManagerUploadRequest) didn't return an error, so I cannot display an error to inform the user that something goes wrong.
Has anybody an idea how to solve the problem? And if there is no solution for the timeout, do you know how to catch the error for at least display an error on the screen?
I already tried to change the timeoutIntervalForRequest/timeoutIntervalForResource but it doesn't change anything.
The error is (fifty times or more):
20 AWSiOSSDK v2.4.8 [Error] AWSURLSessionManager.m line:212 | -[AWSURLSessionManager URLSession:task:didCompleteWithError:] | Session task failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=https://s3.amazonaws.com/***/user-72358/project-***/***.jpg?partNumber=1&uploadId=***, _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLKey=https://s3.amazonaws.com/***/user-***/project-***/***.jpg?partNumber=1&uploadId=***, NSLocalizedDescription=The request timed out., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x1dc69d80 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102}}}
And here is my code :
func uploadAllFileRequests(imageArray:[String], completion:(filedataReturned:FileData?, uploadOfFileSucceeded: Bool, error: NSError?)-> Void,progress:(totalSent: Int64, totalExpect: Int64)-> Void) -> Void
{
var tasks = [AWSTask]()
let keyCred = s3CredentialsInfo?.key
AWSS3TransferManager.registerS3TransferManagerWithConfiguration(serviceConfiguration, forKey: keyCred)
let transferManager:AWSS3TransferManager = AWSS3TransferManager.S3TransferManagerForKey(keyCred)
for imageFilePath:String in imageArray {
...
let keyOnS3 = "\(keyCred!)/\(filename)"
// url image to upload to s3
let url:NSURL = NSURL(fileURLWithPath: filedata.getFullpath())
// next we set up the S3 upload request manager
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = s3CredentialsInfo?.bucket
uploadRequest?.key = keyOnS3
uploadRequest?.contentType = type
uploadRequest?.body = url
// Track Upload Progress through AWSNetworkingUploadProgressBlock
uploadRequest?.uploadProgress = {(bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
progress(totalSent: currentTotal, totalExpect: totalSize)
})
}
tasks.append(transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock:{task -> AWSTask in
if(task.result != nil){
// upload success
print("-> upload successfull \(keyOnS3) on S3")
completion(filedataReturned:filedata, uploadOfFileSucceeded: true, error: nil)
}else{
print("->task error upload on S3 : \(task.error)")
}
return task
}))
}
AWSTask(forCompletionOfAllTasks:tasks).continueWithBlock({task -> AWSTask in
print("-> parallel task is \(task)")
if(!task.cancelled && !task.faulted){
// upload success
print("-> upload successfull on S3")
}else{
print("->error of parallel upload on S3")
completion(filedataReturned:nil, uploadOfFileSucceeded: false, error: task.error)
}
return task
})
}
I'm using AWSCore and AWSS3 cocoapods version 2.4.8
Thanks in advance for your help
Stéphanie
The error of time out comes once your network is slow, for this you just have to increate time out in configuration.
In your case
let serviceConfiguration = AWSServiceConfiguration(region: convertedRegion, credentialsProvider: credentialsProvider)
configuration?.timeoutIntervalForRequest = 60 // in seconds

AVAssetExportSession only works first 6 times

Updated code sample, console output and information
I'm trying to get the total duration of a video composition, divide it by 10, round up, then loop through and splice the video composition at those intervals.
It works, but after the 'currentDuration' becomes 60+, it throws a "The requested URL was not found on this server."
Basically if it determines it needs to produce 9 clips, it succeeds the first 6 and fails the other 3. Also my numLoop isn't working as expected, it almost seems the while loop finishes before any of the 9 clips are attempted.
Would love some help/insight into getting all 9 clips exporting.
I've noticed if the video is less than 60 seconds long, it has 100% success rate. Any video I choose over 60 seconds will fails on the 7th clip.
Here's my method:
func splitVideo(videoComposition: AVMutableVideoComposition) {
let fileManager = NSFileManager.defaultManager()
let documentsPath : String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
//grab total duration/number of splits
let exporter: AVAssetExportSession = AVAssetExportSession(asset: asset!, presetName:AVAssetExportPresetHighestQuality)!
let totalDuration = Float64(CMTimeGetSeconds(exporter.asset.duration))
let totalDurationPieces = (totalDuration/10)
var numberOfSplits=Int(ceil(Double(totalDurationPieces)))
//prepare for loop
var loopNum : Int = 0
var currentDuration : Float64 = 0
var sessionNumber = (arc4random()%1000)
let opQueue = NSOperationQueue()
opQueue.maxConcurrentOperationCount = 1
while loopNum < numberOfSplits {
//new exporter
var destinationPath: String = documentsPath + "/splitVideo-"+String(sessionNumber)
destinationPath+="-"+String(Int(loopNum))+".mp4"
let new_exporter = AVAssetExportSession(asset: asset!, presetName:AVAssetExportPresetHighestQuality)!
new_exporter.outputURL = NSURL(fileURLWithPath: destinationPath as String)
new_exporter.videoComposition = videoComposition
new_exporter.outputFileType = AVFileTypeMPEG4
new_exporter.shouldOptimizeForNetworkUse = false
new_exporter.timeRange = CMTimeRangeMake(
CMTimeMakeWithSeconds(currentDuration, framesPerSecond!),CMTimeMakeWithSeconds(Float64(10),framesPerSecond!))
// Set up the exporter, then:
opQueue.addOperationWithBlock { () -> Void in
new_exporter.exportAsynchronouslyWithCompletionHandler({
dispatch_async(dispatch_get_main_queue(),{
print("Exporting... \(loopNum)")
self.exportDidFinish(new_exporter, loopNum: loopNum)
})
}) // end completion handler
} // end block
//prepare for next loop
loopNum = loopNum+1
currentDuration = currentDuration+10
if(loopNum>=numberOfSplits){
self.allExportsDone(Int(numberOfSplits))
}
} // end while
}
Here's the exportDidFinish method:
func exportDidFinish(session: AVAssetExportSession, loopNum: Int) {
let outputURL: NSURL = session.outputURL!
let library: ALAssetsLibrary = ALAssetsLibrary()
if(library.videoAtPathIsCompatibleWithSavedPhotosAlbum(outputURL)) {
library.writeVideoAtPathToSavedPhotosAlbum(outputURL, completionBlock: {(url, error) in
//done
print("Success on \(Int(loopNum))")
})
}
}
Here's the console output:
Exporting... 9
2016-08-20 13:39:27.980 TrimVideo[4776:1576022] Video /var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-6.mp4 cannot be saved to the saved photos album: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x1457f43f0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSErrorFailingURLStringKey=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-6.mp4, NSErrorFailingURLKey=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-6.mp4, NSURL=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-6.mp4, NSLocalizedDescription=The requested URL was not found on this server.}
Exporting... 9
2016-08-20 13:39:27.984 TrimVideo[4776:1576022] Video /var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-7.mp4 cannot be saved to the saved photos album: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x1457f88c0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSErrorFailingURLStringKey=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-7.mp4, NSErrorFailingURLKey=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-7.mp4, NSURL=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-7.mp4, NSLocalizedDescription=The requested URL was not found on this server.}
Exporting... 9
2016-08-20 13:39:27.988 TrimVideo[4776:1576022] Video /var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-8.mp4 cannot be saved to the saved photos album: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x14687cb30 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSErrorFailingURLStringKey=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-8.mp4, NSErrorFailingURLKey=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-8.mp4, NSURL=file:///var/mobile/Containers/Data/Application/BE125EB7-AFC4-48B5-95C3-941B420BB71F/Documents/splitVideo-972-8.mp4, NSLocalizedDescription=The requested URL was not found on this server.}
Exporting... 9
Success on 9
Exporting... 9
Success on 9
Exporting... 9
Success on 9
Exporting... 9
Success on 9
Exporting... 9
Ok, some news.
ALAssetsLibrary is deprecated since iOS 8, with Apple moving everyone off to the Photos Framework to do this. Good news is that AVAssetExportSession is not deprecated. While you could carry on with the deprecated API, it might be a good idea to rewrite that exportDidFinish function to use the new API.
The while loop in the splitVideo function is throwing out ten concurrent export operations. Its a bit of a guess to be honest, but I suspect that there is some resource contention kicking in once you get to clip 6.
So that needs to be redesigned to be a little more friendly. Best bet is to use an NSOperationQueue with the maxConcurrentOperationsCount set to one (i.e. a serial queue).
Something like:
let opQueue = NSOperationQueue()
opQueue.maxConcurrentOperationsCount = 1
for loopNum in 0..<numberOfSplits {
// Set up the exporter, then:
opQueue.addOperationWithBlock { () -> Void in
new_exporter.exportAsynchronouslyWithCompletionHandler({
dispatch_async(dispatch_get_main_queue(),{
print("Exporting... \(loopNum)")
self.exportDidFinish(new_exporter, loopNum: loopNum)
})
} // end completion handler
} // end block
} // end while
The purpose of this is to ensure that the export operations get run one at a time rather than an attempt at all at once. If that succeeds, you can experiment with upping the maxConcurrentOperationsCount to get some multi-threading going on.

Resources