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 #if !defined(GMPAudioDecoder_h_)
8 #define GMPAudioDecoder_h_
9 
10 #include "GMPAudioDecoderProxy.h"
11 #include "MediaDataDecoderProxy.h"
12 #include "PlatformDecoderModule.h"
13 #include "mozIGeckoMediaPluginService.h"
14 #include "nsAutoPtr.h"
15 
16 namespace mozilla {
17 
18 class AudioCallbackAdapter : public GMPAudioDecoderCallbackProxy {
19 public:
AudioCallbackAdapter(MediaDataDecoderCallbackProxy * aCallback)20   explicit AudioCallbackAdapter(MediaDataDecoderCallbackProxy* aCallback)
21    : mCallback(aCallback)
22    , mLastStreamOffset(0)
23    , mAudioFrameSum(0)
24    , mAudioFrameOffset(0)
25    , mMustRecaptureAudioPosition(true)
26   {}
27 
Callback()28   MediaDataDecoderCallbackProxy* Callback() const { return mCallback; }
29 
30   // GMPAudioDecoderCallbackProxy
31   void Decoded(const nsTArray<int16_t>& aPCM, uint64_t aTimeStamp, uint32_t aChannels, uint32_t aRate) override;
32   void InputDataExhausted() override;
33   void DrainComplete() override;
34   void ResetComplete() override;
35   void Error(GMPErr aErr) override;
36   void Terminated() override;
37 
SetLastStreamOffset(int64_t aStreamOffset)38   void SetLastStreamOffset(int64_t aStreamOffset) {
39     mLastStreamOffset = aStreamOffset;
40   }
41 
42 private:
43   MediaDataDecoderCallbackProxy* mCallback;
44   int64_t mLastStreamOffset;
45 
46   int64_t mAudioFrameSum;
47   int64_t mAudioFrameOffset;
48   bool mMustRecaptureAudioPosition;
49 };
50 
51 struct GMPAudioDecoderParams {
52   explicit GMPAudioDecoderParams(const CreateDecoderParams& aParams);
53   GMPAudioDecoderParams& WithCallback(MediaDataDecoderProxy* aWrapper);
54   GMPAudioDecoderParams& WithAdapter(AudioCallbackAdapter* aAdapter);
55 
56   const AudioInfo& mConfig;
57   TaskQueue* mTaskQueue;
58   MediaDataDecoderCallbackProxy* mCallback;
59   AudioCallbackAdapter* mAdapter;
60   RefPtr<GMPCrashHelper> mCrashHelper;
61 };
62 
63 class GMPAudioDecoder : public MediaDataDecoder {
64 public:
65   explicit GMPAudioDecoder(const GMPAudioDecoderParams& aParams);
66 
67   RefPtr<InitPromise> Init() override;
68   void Input(MediaRawData* aSample) override;
69   void Flush() override;
70   void Drain() override;
71   void Shutdown() override;
GetDescriptionName()72   const char* GetDescriptionName() const override
73   {
74     return "GMP audio decoder";
75   }
76 
77 protected:
78   virtual void InitTags(nsTArray<nsCString>& aTags);
79   virtual nsCString GetNodeId();
80 
81 private:
82 
83   class GMPInitDoneCallback : public GetGMPAudioDecoderCallback
84   {
85   public:
GMPInitDoneCallback(GMPAudioDecoder * aDecoder)86     explicit GMPInitDoneCallback(GMPAudioDecoder* aDecoder)
87       : mDecoder(aDecoder)
88     {
89     }
90 
Done(GMPAudioDecoderProxy * aGMP)91     void Done(GMPAudioDecoderProxy* aGMP) override
92     {
93       mDecoder->GMPInitDone(aGMP);
94     }
95 
96   private:
97     RefPtr<GMPAudioDecoder> mDecoder;
98   };
99   void GMPInitDone(GMPAudioDecoderProxy* aGMP);
100 
101   const AudioInfo mConfig;
102   MediaDataDecoderCallbackProxy* mCallback;
103   nsCOMPtr<mozIGeckoMediaPluginService> mMPS;
104   GMPAudioDecoderProxy* mGMP;
105   nsAutoPtr<AudioCallbackAdapter> mAdapter;
106   MozPromiseHolder<InitPromise> mInitPromise;
107   RefPtr<GMPCrashHelper> mCrashHelper;
108 };
109 
110 } // namespace mozilla
111 
112 #endif // GMPAudioDecoder_h_
113