1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_AppleATDecoder_h
8 #define mozilla_AppleATDecoder_h
9 
10 #include <AudioToolbox/AudioToolbox.h>
11 #include "PlatformDecoderModule.h"
12 #include "mozilla/ReentrantMonitor.h"
13 #include "mozilla/Vector.h"
14 #include "nsIThread.h"
15 #include "AudioConverter.h"
16 
17 namespace mozilla {
18 
19 class TaskQueue;
20 class MediaDataDecoderCallback;
21 
22 class AppleATDecoder : public MediaDataDecoder {
23 public:
24   AppleATDecoder(const AudioInfo& aConfig,
25                  TaskQueue* aTaskQueue,
26                  MediaDataDecoderCallback* aCallback);
27   virtual ~AppleATDecoder();
28 
29   RefPtr<InitPromise> Init() override;
30   void Input(MediaRawData* aSample) override;
31   void Flush() override;
32   void Drain() override;
33   void Shutdown() override;
34 
GetDescriptionName()35   const char* GetDescriptionName() const override
36   {
37     return "apple CoreMedia decoder";
38   }
39 
40   // Callbacks also need access to the config.
41   const AudioInfo& mConfig;
42 
43   // Use to extract magic cookie for HE-AAC detection.
44   nsTArray<uint8_t> mMagicCookie;
45   // Will be set to true should an error occurred while attempting to retrieve
46   // the magic cookie property.
47   bool mFileStreamError;
48 
49 private:
50   const RefPtr<TaskQueue> mTaskQueue;
51   MediaDataDecoderCallback* mCallback;
52   AudioConverterRef mConverter;
53   AudioStreamBasicDescription mOutputFormat;
54   UInt32 mFormatID;
55   AudioFileStreamID mStream;
56   nsTArray<RefPtr<MediaRawData>> mQueuedSamples;
57   UniquePtr<AudioConfig::ChannelLayout> mChannelLayout;
58   UniquePtr<AudioConverter> mAudioConverter;
59   Atomic<bool> mIsFlushing;
60 
61   void ProcessFlush();
62   void ProcessShutdown();
63   void SubmitSample(MediaRawData* aSample);
64   MediaResult DecodeSample(MediaRawData* aSample);
65   MediaResult GetInputAudioDescription(AudioStreamBasicDescription& aDesc,
66                                        const nsTArray<uint8_t>& aExtraData);
67   // Setup AudioConverter once all information required has been gathered.
68   // Will return NS_ERROR_NOT_INITIALIZED if more data is required.
69   MediaResult SetupDecoder(MediaRawData* aSample);
70   nsresult GetImplicitAACMagicCookie(const MediaRawData* aSample);
71   nsresult SetupChannelLayout();
72   uint32_t mParsedFramesForAACMagicCookie;
73   bool mErrored;
74 };
75 
76 } // namespace mozilla
77 
78 #endif // mozilla_AppleATDecoder_h
79