GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Playing audio

Open pgodino opened this issue 14 years ago • 48 comments

When I play a movie with audio, no audio is played. I checked the code and actually it seems that audio is considered only when writing to file.

pgodino avatar Sep 05 '12 08:09 pgodino

You're right, there's nothing in the framework to support audio playback from movies right now. I don't do much work with movie playback myself, so I haven't spent any time looking into this.

I do welcome any contributions you or others might make to add this functionality, though.

BradLarson avatar Sep 05 '12 17:09 BradLarson

I'm trying to implement audio playing but I'm stuck. Since AVAssetReader reads both audio and video I thought to implement audio playing in this way:

  • add to AVAssetReader also the audio track
  • in readNextAudioSampleFromOutput adding a call to a new method processAudioFrame after calling processAudioBuffer (that I think handles writing the audio component to a file)
  • in the new method using CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer to get the AudioBufferList which contains the audio samples
  • paying audio samples using RemoteIO audio units (I think that a low level audio playing is required in order to get opmimal performances) as described for example here: http://atastypixel.com/blog/using-remoteio-audio-unit/.

In this way I get only noise. I think that possible problems are (coding problems. I configured AVAssetReaderTrackOutput with this dictionary of options (in processAsset):

NSMutableDictionary *audioOutputSettings = [NSMutableDictionary dictionary]; [audioOutputSettings setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; [audioOutputSettings setObject:[NSNumber numberWithInt:44100] forKey:AVSampleRateKey]; [audioOutputSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; [audioOutputSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; [audioOutputSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; [audioOutputSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsNonInterleaved];

    // This might need to be extended to handle movies with more than one audio track
    AVAssetTrack* audioTrack = [audioTracks objectAtIndex:0];
    readerAudioTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:audioOutputSettings];
    [reader addOutput:readerAudioTrackOutput];

And in this way audio samples in the buffer I get should be signed int 16 bits.

The second problem I see is the synchronization between reader (the callback required by remoteIO audio unit) ad the writer of the audio samples. I'm working assuming that this is the problem building a buffer queue.

Some help from someone with some experience with playing the audio from an AVAssetReader could be very useful.

pgodino avatar Sep 06 '12 08:09 pgodino

OK I'm almost there. I'm using https://github.com/michaeltyson/TPCircularBuffer for the consumer/producer buffer for AVAssetReader and RemoteIO audio unit.

I have still 2 problems:

  • I'm trying with an 8 secs video and audio processing is much faster that the video one. It seems that the audio processing is not realtime but faster. This has the effect that the circular buffer should be very big in order to store all the audio samples. And I get some acceleration of the audio at the end of the video. This means that it could be worse and worse as the video size increases.
  • I get some noise at the end of the of the audio queue but I think it could be related to the first problem.

I hope to be able to solve this problems next week in order to submit a pull request (but I don't know exactly how to do it, is there some documentations?)

pgodino avatar Sep 07 '12 16:09 pgodino

Actually I wasn't able to solve the two problems I wrote before so I took a completely different direction (I had quite short sequences so I play the image sequence frame by frame especially in order to start and stop the "video" fast on iPad1). Anyway if you wish I can share the code I wrote to play audio but I don't know how I can do it.

pgodino avatar Sep 27 '12 20:09 pgodino

you can copy paste the audio code section and take out the rest.... and put it at https://gist.github.com/

however, i dont have short sequence since my video is 3-4 minutes at least

gordemota avatar Sep 28 '12 09:09 gordemota

Sorry if I did't answer before. I put the complete modified GPUImageMoview with the audio part. Here you have the link: https://gist.github.com/3819907

I hope it could be useful as a starting point.

pgodino avatar Oct 02 '12 15:10 pgodino

I also need this feature. I'm considering adopting a simpler work around. Play the audio of the origin file of the GPUImageMovie. Probable the sync between video and audio will be a problem since GPUImageMovie may not play at the original speed.

linktoming avatar Oct 17 '12 04:10 linktoming

Great library! I also need audio playback and sync to video.

I have also looked at Mo DeJong’s library AVAnimator which seems to have solved similar issues syncing a CAF file to the video images as they decode and adjusting the playback frame to stay in sync.

Do you think any of this code could be applied? I unfortunately cannot get my head around doing the mash up.

scottmcdonnell avatar Nov 29 '12 14:11 scottmcdonnell

There was an issue with the timestep of movie playback in GPUImageMovie. Movie's gradually loose time, which causes audio to desync. Anyway, I modified GPUImageMovie to support basic audio playback, (using an AVAudioPlayer). See here . Play a movie file with audio, and make sure to set the playSound property to true. The code tries to sync the movie playback to the audio. If the movie can't be decoded in realtime or better, than it will still probably fall out of sync. This code hasn't been thoroughly tested, so take it with a grain of salt.

darcy-rayner avatar Mar 07 '13 23:03 darcy-rayner

@darcy-rayner playing audio during recording freezes up the video camera. any idea why?

snofla avatar Mar 12 '13 15:03 snofla

I've only tested it for playing back audio from a movie file. When playSound is turned on, the timestep of the video is tied to the audio track. My guess is it is trying to sync to an audio track with doesn't exist.

darcy-rayner avatar Mar 12 '13 21:03 darcy-rayner

@darcy-rayner Fixed it It's just an audio session setup problem:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
UInt32 enabled = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(enabled), &enabled);
[[AVAudioSession sharedInstance] setActive:YES error:nil];

snofla avatar Mar 13 '13 09:03 snofla

maybe AudioQueue is the right choice. i know a little about the AudioQueue, does anyone who know AudioQueue want to work on this?

notedit avatar May 11 '13 06:05 notedit

Hi. I need some help. I integrated the audio playback following the changes mentioned in the previous comments. I am facing a problem: audio playback is working fine in the simulator but when running my application on a device, it is not working. No error is logged, so I have no idea why this is happening. Has someone encountered the same issue?

JacobDev avatar Jun 26 '13 17:06 JacobDev

@snofla Hi,I want to know where to add your code. I also have the problem. When adding a filter to a sample movie file, the audio goes normally while the video freezes. these are my codes: movieFile.playSound = YES; movieWriter.shouldPassthroughAudio = NO; movieFile.audioEncodingTarget = movieWriter; [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter]; My purpose is processing a movie file with a filter. At beging the result has no sound, and the video has no sound too when processing. But now the video freezes. Thanks! Please help me out of this.

davidgloves avatar Jul 15 '13 07:07 davidgloves

@darcy-rayner I try you GPUImageMovie with the last framework code. It´s working for the .m4v file in the example but I cant do for other video formats. Are you still working on this?

jasaiher avatar Jul 28 '13 13:07 jasaiher

@jasaiher hey, it isn't m4v it's mp4. You can see the profile of the file, it's mp4 file. I think that you can change other file to the format of mp4.

davidgloves avatar Jul 28 '13 13:07 davidgloves

as we encountered the same problem we decided to add audio support to GPUImageMovie using AudioUnits. You can grab our code at: https://github.com/pheed/GPUImage

To use link your project with AudioToolbox and CoreAudio frameworks than just set the playSound to YES on the GPUImageMovie.

few issues:

  1. we rewritten the playAtActualSpeed logic as the old one was out of sync once we added the audio.
  2. only one of playSound/audioEncodingTarget is supported.
  3. the audio player doesn't support pause/play

We are planning to issue a pull request sometime in the future after we test it further.

eluzix avatar Sep 03 '13 11:09 eluzix

Have you tested it? It is safe to use or there are some issues we should be aware of if we intend to use it?

Thanks

oltis avatar Oct 10 '13 07:10 oltis

@oltis we tested it relative to our needs and it seems to be working fine.

We did further changes to the GPUImage project to fit our needs but it shouldn't effect how it behaves under normal circumstances, if it does just grab the sources under GPUImage/Sources/Audio and GPUImageMovie.

eluzix avatar Oct 10 '13 09:10 eluzix