Is there a way to intercept byte data and perform a XOR operation to each byte prior to playing audio in AVPlayer?
I'm building an audio streaming app and use a little script written in C to add a simple layer of encryption to the MP3 files. In Android it gets decoded in real time like this:
@Override
public int read(byte[] buffer, int offset, int readLength) throws FileDataSourceException {
// ...
if (readLength == 1 && offset >= 1 && offset <= 123) {
buffer[offset] = (byte)(buffer[offset] ^ 11);
}
return bytesRead;
}
}
As you can see above reversing the XOR encryption is fairly easy in Android as I use ExoPlayer and override the read() method in its datasource classes. Is there any chance to perform the same thing using AVPlayer with Swift?
Here's a flowchart of the whole idea:
AVPlayer encryption flowchart
Thank you.
You'll want to use MTAudioProcessingTap
for this, which is a property of AVPlayer
's AVAudioMix
. In your tap process callback, use MTAudioProcessingTapGetSourceAudio
to grab your buffers. Once you have your buffer reference you can xor the data.
There's some boilerplate needed to get the AVAudioMix
and MTAudioProcessingTap
setup properly. The sample code that Apple has is pretty old, but still should work.
https://developer.apple.com/library/archive/samplecode/AudioTapProcessor/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012324
Also note that it will be easier to do this in Objective C, for several reasons. The interop with your C file will be easier, and it is much more straightforward reading/writing to the buffer in Objc. It will also run faster than in swift. If you are interested to seeing what this would look like in swift, there is a sample project here: https://github.com/gchilds/MTAudioProcessingTap-in-Swift
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With