AV Foundation export should allow passthrough from iPod library

Number:rdar://8207641 Date Originated:7/19/10
Status:closed Resolved:9/29/10
Product:iPhone SDK Product Version:4.0.1
Classification:Enhancement Reproducible:N/A
19-Jul-2010 04:46 PM Chris Adamson:
Starting in iOS 4, apps can get a special URL for items in the user's Media Library, of the form ipod-library://..., and create an AVURLAsset with this URL.  This asset can then be added as a track to a composition, or exported directly.  In the latter case, +[AVAssetExportSession supportedFileTypes] returns only a single supported export preset: com.apple.m4a-audio.

Because Core Audio's file APIs (Audio File Services, Extended Audio File Services, etc.) cannot access the iPod library directly (see rdar://8207541 ), this sort of export is necessary for a Core Audio app to work the audio from a file in the media library.  However, since the only supported preset is M4A, items with other encodings (MP3, ALAC) must undergo an expensive and possibly lossy export to M4A, even if Core Audio would be perfectly capable of handling the file format and data format of the original media library item.

If Core Audio cannot be allowed to access the iPod Media Library items directly, the next best thing would be for the AV Foundation to allow a passthrough export of the item.  For example, for an ipod-library:// URL, the AVAssetExportSession could support the AVAssetExportPresetPassthrough, with a new supported file type indicating a .caf (Core Audio Format) file: all the data formats supported by Core Audio can be contained by a .caf, so it is a natural choice for this sort of export.  With passthrough export supported, the export would presumably be much faster, would consume far less CPU, and wouldn't perform a possibly-unwanted lossy resampling and format conversion.

I've written about this stuff in a blog <http://www.subfurther.com/blog/?p=1103>, which also has a code example <http://www.subfurther.com/blog/wp-content/uploads/2010/07/MediaLibraryExportThrowaway1.zip> of performing media library exports with the M4A exporter (since passthrough is unavailable).

Steps to Reproduce:
Create an AVURLAsset from an item in the media library (this code is adapted from the above-mentioned blog):
	NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
	AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
Attempt to set up a passthrough exporter:
	AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
									  initWithAsset: songAsset
									  presetName: AVAssetExportPresetPassthrough];
	exporter.outputFileType = @"public.mpeg-4";
	NSString *exportFile = [myDocumentsDirectory() stringByAppendingPathComponent: @"exported.mp4"];
	[exportURL release];
	exportURL = [[NSURL fileURLWithPath:exportFile] retain];
	exporter.outputURL = exportURL;	
	// do the export
	// (completion handler block omitted) 
	[exporter exportAsynchronouslyWithCompletionHandler:^{ ... }];

Expected Results:
"exported.mp4" file contains audio in its original format from the iPod library (MP3, ALAC, AAC), provided the format can be contained in an .mp4 file.

Actual Results:
The export begins, but the completion handler ultimately gets the AVAssetExportSessionStatusFailed  status with an error indicating that the export failed:
AVAssetExportSessionStatusFailed: Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x182250 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}


One problem that makes this problem somewhat worse is that the MPMediaItem property keys supported by Media Player don't include one that reveals the encoding, so an app using AV Foundation to export the audio cannot know if an M4A export will be particularly expensive. If passthrough were supported, the app might want to know this in order to choose an appropriate output file type (.caf works for anything, but it's possible that you might want to keep MP3 data in .mp3 files, big-endian PCM in .aif, etc.).


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!