michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "AppleMP3Reader.h" michael@0: michael@0: #include "nsISeekableStream.h" michael@0: #include "MediaDecoder.h" michael@0: michael@0: // Number of bytes we will read and pass to the audio parser in each michael@0: // |DecodeAudioData| call. michael@0: #define AUDIO_READ_BYTES 4096 michael@0: michael@0: // Maximum number of audio frames we will accept from the audio decoder in one michael@0: // go. Carefully select this to work well with both the mp3 1152 max frames michael@0: // per block and power-of-2 allocation sizes. Since we must pre-allocate the michael@0: // buffer we cannot use AudioCompactor without paying for an additional michael@0: // allocation and copy. Therefore, choosing a value that divides exactly into michael@0: // 1152 is most memory efficient. michael@0: #define MAX_AUDIO_FRAMES 128 michael@0: michael@0: namespace mozilla { michael@0: michael@0: #ifdef PR_LOGGING michael@0: extern PRLogModuleInfo* gMediaDecoderLog; michael@0: #define LOGE(...) PR_LOG(gMediaDecoderLog, PR_LOG_ERROR, (__VA_ARGS__)) michael@0: #define LOGW(...) PR_LOG(gMediaDecoderLog, PR_LOG_WARNING, (__VA_ARGS__)) michael@0: #define LOGD(...) PR_LOG(gMediaDecoderLog, PR_LOG_DEBUG, (__VA_ARGS__)) michael@0: #else michael@0: #define LOGE(...) michael@0: #define LOGW(...) michael@0: #define LOGD(...) michael@0: #endif michael@0: michael@0: #define PROPERTY_ID_FORMAT "%c%c%c%c" michael@0: #define PROPERTY_ID_PRINT(x) ((x) >> 24), \ michael@0: ((x) >> 16) & 0xff, \ michael@0: ((x) >> 8) & 0xff, \ michael@0: (x) & 0xff michael@0: michael@0: AppleMP3Reader::AppleMP3Reader(AbstractMediaDecoder *aDecoder) michael@0: : MediaDecoderReader(aDecoder) michael@0: , mStreamReady(false) michael@0: , mAudioFramesPerCompressedPacket(0) michael@0: , mCurrentAudioFrame(0) michael@0: , mAudioChannels(0) michael@0: , mAudioSampleRate(0) michael@0: , mAudioFileStream(nullptr) michael@0: , mAudioConverter(nullptr) michael@0: , mMP3FrameParser(mDecoder->GetResource()->GetLength()) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread"); michael@0: } michael@0: michael@0: AppleMP3Reader::~AppleMP3Reader() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread"); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * The Apple audio decoding APIs are very callback-happy. When the parser has michael@0: * some metadata, it will call back to here. michael@0: */ michael@0: static void _AudioMetadataCallback(void *aThis, michael@0: AudioFileStreamID aFileStream, michael@0: AudioFileStreamPropertyID aPropertyID, michael@0: UInt32 *aFlags) michael@0: { michael@0: ((AppleMP3Reader*)aThis)->AudioMetadataCallback(aFileStream, aPropertyID, michael@0: aFlags); michael@0: } michael@0: michael@0: /* michael@0: * Similar to above, this is called when the parser has enough data to parse michael@0: * one or more samples. michael@0: */ michael@0: static void _AudioSampleCallback(void *aThis, michael@0: UInt32 aNumBytes, UInt32 aNumPackets, michael@0: const void *aData, michael@0: AudioStreamPacketDescription *aPackets) michael@0: { michael@0: ((AppleMP3Reader*)aThis)->AudioSampleCallback(aNumBytes, aNumPackets, michael@0: aData, aPackets); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * If we're not at end of stream, read |aNumBytes| from the media resource, michael@0: * put it in |aData|, and return true. michael@0: * Otherwise, put as much data as is left into |aData|, set |aNumBytes| to the michael@0: * amount of data we have left, and return false. michael@0: */ michael@0: nsresult michael@0: AppleMP3Reader::Read(uint32_t *aNumBytes, char *aData) michael@0: { michael@0: MediaResource *resource = mDecoder->GetResource(); michael@0: michael@0: // Loop until we have all the data asked for, or we've reached EOS michael@0: uint32_t totalBytes = 0; michael@0: uint32_t numBytes; michael@0: do { michael@0: uint32_t bytesWanted = *aNumBytes - totalBytes; michael@0: nsresult rv = resource->Read(aData + totalBytes, bytesWanted, &numBytes); michael@0: totalBytes += numBytes; michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: *aNumBytes = 0; michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } while(totalBytes < *aNumBytes && numBytes); michael@0: michael@0: *aNumBytes = totalBytes; michael@0: michael@0: // We will have read some data in the last iteration iff we filled the buffer. michael@0: // XXX Maybe return a better value than NS_ERROR_FAILURE? michael@0: return numBytes ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult michael@0: AppleMP3Reader::Init(MediaDecoderReader* aCloneDonor) michael@0: { michael@0: AudioFileTypeID fileType = kAudioFileMP3Type; michael@0: michael@0: OSStatus rv = AudioFileStreamOpen(this, michael@0: _AudioMetadataCallback, michael@0: _AudioSampleCallback, michael@0: fileType, michael@0: &mAudioFileStream); michael@0: michael@0: if (rv) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: struct PassthroughUserData { michael@0: AppleMP3Reader *mReader; michael@0: UInt32 mNumPackets; michael@0: UInt32 mDataSize; michael@0: const void *mData; michael@0: AudioStreamPacketDescription *mPacketDesc; michael@0: bool mDone; michael@0: }; michael@0: michael@0: // Error value we pass through the decoder to signal that nothing has gone wrong michael@0: // during decoding, but more data is needed. michael@0: const UInt32 kNeedMoreData = 'MOAR'; michael@0: michael@0: /* michael@0: * This function is called from |AudioConverterFillComplexBuffer|, which is michael@0: * called from |AudioSampleCallback| below, which in turn is called by michael@0: * |AudioFileStreamParseBytes|, which is called by |DecodeAudioData|. michael@0: * michael@0: * Mercifully, this is all synchronous. michael@0: * michael@0: * This callback is run when the AudioConverter (decoder) wants more MP3 packets michael@0: * to decode. michael@0: */ michael@0: /* static */ OSStatus michael@0: AppleMP3Reader::PassthroughInputDataCallback(AudioConverterRef aAudioConverter, michael@0: UInt32 *aNumDataPackets /* in/out */, michael@0: AudioBufferList *aData /* in/out */, michael@0: AudioStreamPacketDescription **aPacketDesc, michael@0: void *aUserData) michael@0: { michael@0: PassthroughUserData *userData = (PassthroughUserData *)aUserData; michael@0: if (userData->mDone) { michael@0: // We make sure this callback is run _once_, with all the data we received michael@0: // from |AudioFileStreamParseBytes|. When we return an error, the decoder michael@0: // simply passes the return value on to the calling method, michael@0: // |AudioSampleCallback|; and flushes all of the audio frames it had michael@0: // buffered. It does not change the decoder's state. michael@0: LOGD("requested too much data; returning\n"); michael@0: *aNumDataPackets = 0; michael@0: return kNeedMoreData; michael@0: } michael@0: michael@0: userData->mDone = true; michael@0: michael@0: LOGD("AudioConverter wants %u packets of audio data\n", *aNumDataPackets); michael@0: michael@0: *aNumDataPackets = userData->mNumPackets; michael@0: *aPacketDesc = userData->mPacketDesc; michael@0: michael@0: aData->mBuffers[0].mNumberChannels = userData->mReader->mAudioChannels; michael@0: aData->mBuffers[0].mDataByteSize = userData->mDataSize; michael@0: aData->mBuffers[0].mData = const_cast(userData->mData); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * This callback is called when |AudioFileStreamParseBytes| has enough data to michael@0: * extract one or more MP3 packets. michael@0: */ michael@0: void michael@0: AppleMP3Reader::AudioSampleCallback(UInt32 aNumBytes, michael@0: UInt32 aNumPackets, michael@0: const void *aData, michael@0: AudioStreamPacketDescription *aPackets) michael@0: { michael@0: LOGD("got %u bytes, %u packets\n", aNumBytes, aNumPackets); michael@0: michael@0: // 1 frame per packet * num channels * 32-bit float michael@0: uint32_t decodedSize = MAX_AUDIO_FRAMES * mAudioChannels * michael@0: sizeof(AudioDataValue); michael@0: michael@0: // descriptions for _decompressed_ audio packets. ignored. michael@0: nsAutoArrayPtr michael@0: packets(new AudioStreamPacketDescription[MAX_AUDIO_FRAMES]); michael@0: michael@0: // This API insists on having MP3 packets spoon-fed to it from a callback. michael@0: // This structure exists only to pass our state and the result of the parser michael@0: // on to the callback above. michael@0: PassthroughUserData userData = { this, aNumPackets, aNumBytes, aData, aPackets, false }; michael@0: michael@0: do { michael@0: // Decompressed audio buffer michael@0: nsAutoArrayPtr decoded(new uint8_t[decodedSize]); michael@0: michael@0: AudioBufferList decBuffer; michael@0: decBuffer.mNumberBuffers = 1; michael@0: decBuffer.mBuffers[0].mNumberChannels = mAudioChannels; michael@0: decBuffer.mBuffers[0].mDataByteSize = decodedSize; michael@0: decBuffer.mBuffers[0].mData = decoded.get(); michael@0: michael@0: // in: the max number of packets we can handle from the decoder. michael@0: // out: the number of packets the decoder is actually returning. michael@0: UInt32 numFrames = MAX_AUDIO_FRAMES; michael@0: michael@0: OSStatus rv = AudioConverterFillComplexBuffer(mAudioConverter, michael@0: PassthroughInputDataCallback, michael@0: &userData, michael@0: &numFrames /* in/out */, michael@0: &decBuffer, michael@0: packets.get()); michael@0: michael@0: if (rv && rv != kNeedMoreData) { michael@0: LOGE("Error decoding audio stream: %x\n", rv); michael@0: break; michael@0: } michael@0: michael@0: // If we decoded zero frames then AudiOConverterFillComplexBuffer is out michael@0: // of data to provide. We drained its internal buffer completely on the michael@0: // last pass. michael@0: if (numFrames == 0 && rv == kNeedMoreData) { michael@0: LOGD("FillComplexBuffer out of data exactly\n"); michael@0: break; michael@0: } michael@0: michael@0: int64_t time = FramesToUsecs(mCurrentAudioFrame, mAudioSampleRate).value(); michael@0: int64_t duration = FramesToUsecs(numFrames, mAudioSampleRate).value(); michael@0: michael@0: LOGD("pushed audio at time %lfs; duration %lfs\n", michael@0: (double)time / USECS_PER_S, (double)duration / USECS_PER_S); michael@0: michael@0: AudioData *audio = new AudioData(mDecoder->GetResource()->Tell(), michael@0: time, duration, numFrames, michael@0: reinterpret_cast(decoded.forget()), michael@0: mAudioChannels); michael@0: mAudioQueue.Push(audio); michael@0: michael@0: mCurrentAudioFrame += numFrames; michael@0: michael@0: if (rv == kNeedMoreData) { michael@0: // No error; we just need more data. michael@0: LOGD("FillComplexBuffer out of data\n"); michael@0: break; michael@0: } michael@0: } while (true); michael@0: } michael@0: michael@0: bool michael@0: AppleMP3Reader::DecodeAudioData() michael@0: { michael@0: MOZ_ASSERT(mDecoder->OnDecodeThread(), "Should be on decode thread"); michael@0: michael@0: // Read AUDIO_READ_BYTES if we can michael@0: char bytes[AUDIO_READ_BYTES]; michael@0: uint32_t numBytes = AUDIO_READ_BYTES; michael@0: michael@0: nsresult readrv = Read(&numBytes, bytes); michael@0: michael@0: // This function calls |AudioSampleCallback| above, synchronously, when it michael@0: // finds compressed MP3 frame. michael@0: OSStatus rv = AudioFileStreamParseBytes(mAudioFileStream, michael@0: numBytes, michael@0: bytes, michael@0: 0 /* flags */); michael@0: michael@0: if (NS_FAILED(readrv)) { michael@0: mAudioQueue.Finish(); michael@0: return false; michael@0: } michael@0: michael@0: // DataUnavailable just means there wasn't enough data to demux anything. michael@0: // We should have more to push into the demuxer next time we're called. michael@0: if (rv && rv != kAudioFileStreamError_DataUnavailable) { michael@0: LOGE("AudioFileStreamParseBytes returned unknown error %x", rv); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: AppleMP3Reader::DecodeVideoFrame(bool &aKeyframeSkip, michael@0: int64_t aTimeThreshold) michael@0: { michael@0: MOZ_ASSERT(mDecoder->OnDecodeThread(), "Should be on decode thread"); michael@0: return false; michael@0: } michael@0: michael@0: michael@0: bool michael@0: AppleMP3Reader::HasAudio() michael@0: { michael@0: MOZ_ASSERT(mDecoder->OnDecodeThread(), "Should be on decode thread"); michael@0: return mStreamReady; michael@0: } michael@0: michael@0: bool michael@0: AppleMP3Reader::HasVideo() michael@0: { michael@0: MOZ_ASSERT(mDecoder->OnDecodeThread(), "Should be on decode thread"); michael@0: return false; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Query the MP3 parser for a piece of metadata. michael@0: */ michael@0: static nsresult michael@0: GetProperty(AudioFileStreamID aAudioFileStream, michael@0: AudioFileStreamPropertyID aPropertyID, void *aData) michael@0: { michael@0: UInt32 size; michael@0: Boolean writeable; michael@0: OSStatus rv = AudioFileStreamGetPropertyInfo(aAudioFileStream, aPropertyID, michael@0: &size, &writeable); michael@0: michael@0: if (rv) { michael@0: LOGW("Couldn't get property " PROPERTY_ID_FORMAT "\n", michael@0: PROPERTY_ID_PRINT(aPropertyID)); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: rv = AudioFileStreamGetProperty(aAudioFileStream, aPropertyID, michael@0: &size, aData); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: AppleMP3Reader::ReadMetadata(MediaInfo* aInfo, michael@0: MetadataTags** aTags) michael@0: { michael@0: MOZ_ASSERT(mDecoder->OnDecodeThread(), "Should be on decode thread"); michael@0: michael@0: *aTags = nullptr; michael@0: michael@0: /* michael@0: * Feed bytes into the parser until we have all the metadata we need to michael@0: * set up the decoder. When the parser has enough data, it will michael@0: * synchronously call back to |AudioMetadataCallback| below. michael@0: */ michael@0: OSStatus rv; michael@0: nsresult readrv; michael@0: uint32_t offset = 0; michael@0: do { michael@0: char bytes[AUDIO_READ_BYTES]; michael@0: uint32_t numBytes = AUDIO_READ_BYTES; michael@0: readrv = Read(&numBytes, bytes); michael@0: michael@0: rv = AudioFileStreamParseBytes(mAudioFileStream, michael@0: numBytes, michael@0: bytes, michael@0: 0 /* flags */); michael@0: michael@0: mMP3FrameParser.Parse(bytes, numBytes, offset); michael@0: michael@0: offset += numBytes; michael@0: michael@0: // We have to do our decoder setup from the callback. When it's done it will michael@0: // set mStreamReady. michael@0: } while (!mStreamReady && !rv && NS_SUCCEEDED(readrv)); michael@0: michael@0: if (rv) { michael@0: LOGE("Error decoding audio stream metadata\n"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: if (!mAudioConverter) { michael@0: LOGE("Failed to setup the AudioToolbox audio decoder\n"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: if (!mMP3FrameParser.IsMP3()) { michael@0: LOGE("Frame parser failed to parse MP3 stream\n"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: aInfo->mAudio.mRate = mAudioSampleRate; michael@0: aInfo->mAudio.mChannels = mAudioChannels; michael@0: aInfo->mAudio.mHasAudio = mStreamReady; michael@0: michael@0: { michael@0: ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor()); michael@0: mDuration = mMP3FrameParser.GetDuration(); michael@0: mDecoder->SetMediaDuration(mDuration); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: void michael@0: AppleMP3Reader::AudioMetadataCallback(AudioFileStreamID aFileStream, michael@0: AudioFileStreamPropertyID aPropertyID, michael@0: UInt32 *aFlags) michael@0: { michael@0: if (aPropertyID == kAudioFileStreamProperty_ReadyToProducePackets) { michael@0: /* michael@0: * The parser is ready to send us packets of MP3 audio. michael@0: * michael@0: * We need to set the decoder up here, because if michael@0: * |AudioFileStreamParseBytes| has enough audio data, then it will call michael@0: * |AudioSampleCallback| before we get back to |ReadMetadata|. michael@0: */ michael@0: SetupDecoder(); michael@0: mStreamReady = true; michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: AppleMP3Reader::SetupDecoder() michael@0: { michael@0: // Get input format description from demuxer michael@0: AudioStreamBasicDescription inputFormat, outputFormat; michael@0: GetProperty(mAudioFileStream, kAudioFileStreamProperty_DataFormat, &inputFormat); michael@0: michael@0: memset(&outputFormat, 0, sizeof(outputFormat)); michael@0: michael@0: // Set output format michael@0: #if defined(MOZ_SAMPLE_TYPE_FLOAT32) michael@0: outputFormat.mBitsPerChannel = 32; michael@0: outputFormat.mFormatFlags = michael@0: kLinearPCMFormatFlagIsFloat | michael@0: 0; michael@0: #else michael@0: #error Unknown audio sample type michael@0: #endif michael@0: michael@0: mAudioSampleRate = outputFormat.mSampleRate = inputFormat.mSampleRate; michael@0: mAudioChannels michael@0: = outputFormat.mChannelsPerFrame = inputFormat.mChannelsPerFrame; michael@0: mAudioFramesPerCompressedPacket = inputFormat.mFramesPerPacket; michael@0: michael@0: outputFormat.mFormatID = kAudioFormatLinearPCM; michael@0: michael@0: // Set up the decoder so it gives us one sample per frame; this way, it will michael@0: // pass us all the samples it has in one go. Also makes it much easier to michael@0: // deinterlace. michael@0: outputFormat.mFramesPerPacket = 1; michael@0: outputFormat.mBytesPerPacket = outputFormat.mBytesPerFrame michael@0: = outputFormat.mChannelsPerFrame * outputFormat.mBitsPerChannel / 8; michael@0: michael@0: OSStatus rv = AudioConverterNew(&inputFormat, michael@0: &outputFormat, michael@0: &mAudioConverter); michael@0: michael@0: if (rv) { michael@0: LOGE("Error constructing audio format converter: %x\n", rv); michael@0: mAudioConverter = nullptr; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: AppleMP3Reader::Seek(int64_t aTime, michael@0: int64_t aStartTime, michael@0: int64_t aEndTime, michael@0: int64_t aCurrentTime) michael@0: { michael@0: MOZ_ASSERT(mDecoder->OnDecodeThread(), "Should be on decode thread"); michael@0: NS_ASSERTION(aStartTime < aEndTime, michael@0: "Seeking should happen over a positive range"); michael@0: michael@0: // Find the exact frame/packet that contains |aTime|. michael@0: mCurrentAudioFrame = aTime * mAudioSampleRate / USECS_PER_S; michael@0: SInt64 packet = mCurrentAudioFrame / mAudioFramesPerCompressedPacket; michael@0: michael@0: // |AudioFileStreamSeek| will pass back through |byteOffset| the byte offset michael@0: // into the stream it expects next time it reads. michael@0: SInt64 byteOffset; michael@0: UInt32 flags = 0; michael@0: michael@0: OSStatus rv = AudioFileStreamSeek(mAudioFileStream, michael@0: packet, michael@0: &byteOffset, michael@0: &flags); michael@0: michael@0: if (rv) { michael@0: LOGE("Couldn't seek demuxer. Error code %x\n", rv); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: LOGD("computed byte offset = %lld; estimated = %s\n", michael@0: byteOffset, michael@0: (flags & kAudioFileStreamSeekFlag_OffsetIsEstimated) ? "YES" : "NO"); michael@0: michael@0: mDecoder->GetResource()->Seek(nsISeekableStream::NS_SEEK_SET, byteOffset); michael@0: michael@0: ResetDecode(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: AppleMP3Reader::NotifyDataArrived(const char* aBuffer, michael@0: uint32_t aLength, michael@0: int64_t aOffset) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: if (!mMP3FrameParser.NeedsData()) { michael@0: return; michael@0: } michael@0: michael@0: mMP3FrameParser.Parse(aBuffer, aLength, aOffset); michael@0: michael@0: uint64_t duration = mMP3FrameParser.GetDuration(); michael@0: if (duration != mDuration) { michael@0: LOGD("Updating media duration to %lluus\n", duration); michael@0: mDuration = duration; michael@0: ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor()); michael@0: mDecoder->UpdateEstimatedMediaDuration(duration); michael@0: } michael@0: } michael@0: michael@0: } // namespace mozilla