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 #include "MediaCodecProxy.h"
7 #include <OMX_IVCommon.h>
8 #include <gui/Surface.h>
9 #include <ICrypto.h>
10 #include "GonkAudioDecoderManager.h"
11 #include "MediaDecoderReader.h"
12 #include "VideoUtils.h"
13 #include "nsTArray.h"
14 #include "mozilla/Logging.h"
15 #include "stagefright/MediaBuffer.h"
16 #include "stagefright/MetaData.h"
17 #include "stagefright/MediaErrors.h"
18 #include <stagefright/foundation/AMessage.h>
19 #include <stagefright/foundation/ALooper.h>
20 #include "media/openmax/OMX_Audio.h"
21 #include "MediaData.h"
22 #include "MediaInfo.h"
23 
24 #define CODECCONFIG_TIMEOUT_US 10000LL
25 #define READ_OUTPUT_BUFFER_TIMEOUT_US  0LL
26 
27 #include <android/log.h>
28 #define GADM_LOG(...) __android_log_print(ANDROID_LOG_DEBUG, "GonkAudioDecoderManager", __VA_ARGS__)
29 
30 #define LOG(...) MOZ_LOG(sPDMLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
31 
32 using namespace android;
33 typedef android::MediaCodecProxy MediaCodecProxy;
34 
35 namespace mozilla {
36 
GonkAudioDecoderManager(const AudioInfo & aConfig)37 GonkAudioDecoderManager::GonkAudioDecoderManager(const AudioInfo& aConfig)
38   : mAudioChannels(aConfig.mChannels)
39   , mAudioRate(aConfig.mRate)
40   , mAudioProfile(aConfig.mProfile)
41   , mAudioCompactor(mAudioQueue)
42 {
43   MOZ_COUNT_CTOR(GonkAudioDecoderManager);
44   MOZ_ASSERT(mAudioChannels);
45   mCodecSpecificData = aConfig.mCodecSpecificConfig;
46   mMimeType = aConfig.mMimeType;
47 }
48 
~GonkAudioDecoderManager()49 GonkAudioDecoderManager::~GonkAudioDecoderManager()
50 {
51   MOZ_COUNT_DTOR(GonkAudioDecoderManager);
52 }
53 
54 RefPtr<MediaDataDecoder::InitPromise>
Init()55 GonkAudioDecoderManager::Init()
56 {
57   if (InitMediaCodecProxy()) {
58     return InitPromise::CreateAndResolve(TrackType::kAudioTrack, __func__);
59   } else {
60     return InitPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_FATAL_ERR, __func__);
61   }
62 }
63 
64 bool
InitMediaCodecProxy()65 GonkAudioDecoderManager::InitMediaCodecProxy()
66 {
67   status_t rv = OK;
68   if (!InitLoopers(MediaData::AUDIO_DATA)) {
69     return false;
70   }
71 
72   mDecoder = MediaCodecProxy::CreateByType(mDecodeLooper, mMimeType.get(), false);
73   if (!mDecoder.get()) {
74     return false;
75   }
76   if (!mDecoder->AllocateAudioMediaCodec())
77   {
78     mDecoder = nullptr;
79     return false;
80   }
81   sp<AMessage> format = new AMessage;
82   // Fixed values
83   GADM_LOG("Configure audio mime type:%s, chan no:%d, sample-rate:%d, profile:%d",
84            mMimeType.get(), mAudioChannels, mAudioRate, mAudioProfile);
85   format->setString("mime", mMimeType.get());
86   format->setInt32("channel-count", mAudioChannels);
87   format->setInt32("sample-rate", mAudioRate);
88   format->setInt32("aac-profile", mAudioProfile);
89   status_t err = mDecoder->configure(format, nullptr, nullptr, 0);
90   if (err != OK || !mDecoder->Prepare()) {
91     return false;
92   }
93 
94   if (mMimeType.EqualsLiteral("audio/mp4a-latm")) {
95     rv = mDecoder->Input(mCodecSpecificData->Elements(), mCodecSpecificData->Length(), 0,
96                          android::MediaCodec::BUFFER_FLAG_CODECCONFIG,
97                          CODECCONFIG_TIMEOUT_US);
98   }
99 
100   if (rv == OK) {
101     return true;
102   } else {
103     GADM_LOG("Failed to input codec specific data!");
104     return false;
105   }
106 }
107 
108 nsresult
CreateAudioData(MediaBuffer * aBuffer,int64_t aStreamOffset)109 GonkAudioDecoderManager::CreateAudioData(MediaBuffer* aBuffer, int64_t aStreamOffset)
110 {
111   if (!(aBuffer != nullptr && aBuffer->data() != nullptr)) {
112     GADM_LOG("Audio Buffer is not valid!");
113     return NS_ERROR_UNEXPECTED;
114   }
115 
116   int64_t timeUs;
117   if (!aBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) {
118     return NS_ERROR_UNEXPECTED;
119   }
120 
121   if (aBuffer->range_length() == 0) {
122     // Some decoders may return spurious empty buffers that we just want to ignore
123     // quoted from Android's AwesomePlayer.cpp
124     return NS_ERROR_NOT_AVAILABLE;
125   }
126 
127   if (mLastTime > timeUs) {
128     GADM_LOG("Output decoded sample time is revert. time=%lld", timeUs);
129     MOZ_ASSERT(false);
130     return NS_ERROR_NOT_AVAILABLE;
131   }
132   mLastTime = timeUs;
133 
134   const uint8_t *data = static_cast<const uint8_t*>(aBuffer->data());
135   size_t dataOffset = aBuffer->range_offset();
136   size_t size = aBuffer->range_length();
137 
138   uint32_t frames = size / (2 * mAudioChannels);
139 
140   CheckedInt64 duration = FramesToUsecs(frames, mAudioRate);
141   if (!duration.isValid()) {
142     return NS_ERROR_UNEXPECTED;
143   }
144 
145   typedef AudioCompactor::NativeCopy OmxCopy;
146   mAudioCompactor.Push(aStreamOffset,
147                        timeUs,
148                        mAudioRate,
149                        frames,
150                        mAudioChannels,
151                        OmxCopy(data+dataOffset,
152                                size,
153                                mAudioChannels));
154   return NS_OK;
155 }
156 
157 nsresult
Output(int64_t aStreamOffset,RefPtr<MediaData> & aOutData)158 GonkAudioDecoderManager::Output(int64_t aStreamOffset,
159                                 RefPtr<MediaData>& aOutData)
160 {
161   aOutData = nullptr;
162   if (mAudioQueue.GetSize() > 0) {
163     aOutData = mAudioQueue.PopFront();
164     return mAudioQueue.AtEndOfStream() ? NS_ERROR_ABORT : NS_OK;
165   }
166 
167   status_t err;
168   MediaBuffer* audioBuffer = nullptr;
169   err = mDecoder->Output(&audioBuffer, READ_OUTPUT_BUFFER_TIMEOUT_US);
170   AutoReleaseMediaBuffer a(audioBuffer, mDecoder.get());
171 
172   switch (err) {
173     case OK:
174     {
175       nsresult rv = CreateAudioData(audioBuffer, aStreamOffset);
176       NS_ENSURE_SUCCESS(rv, rv);
177       break;
178     }
179     case android::INFO_FORMAT_CHANGED:
180     {
181       // If the format changed, update our cached info.
182       GADM_LOG("Decoder format changed");
183       sp<AMessage> audioCodecFormat;
184 
185       if (mDecoder->getOutputFormat(&audioCodecFormat) != OK ||
186         audioCodecFormat == nullptr) {
187         return NS_ERROR_UNEXPECTED;
188       }
189 
190       int32_t codec_channel_count = 0;
191       int32_t codec_sample_rate = 0;
192 
193       if (!audioCodecFormat->findInt32("channel-count", &codec_channel_count) ||
194         !audioCodecFormat->findInt32("sample-rate", &codec_sample_rate)) {
195         return NS_ERROR_UNEXPECTED;
196       }
197 
198       // Update AudioInfo
199       AudioConfig::ChannelLayout layout(codec_channel_count);
200       if (!layout.IsValid()) {
201         return NS_ERROR_FAILURE;
202       }
203       mAudioChannels = codec_channel_count;
204       mAudioRate = codec_sample_rate;
205 
206       return Output(aStreamOffset, aOutData);
207     }
208     case android::INFO_OUTPUT_BUFFERS_CHANGED:
209     {
210       GADM_LOG("Info Output Buffers Changed");
211       if (mDecoder->UpdateOutputBuffers()) {
212         return Output(aStreamOffset, aOutData);
213       }
214       return NS_ERROR_FAILURE;
215     }
216     case -EAGAIN:
217     {
218       return NS_ERROR_NOT_AVAILABLE;
219     }
220     case android::ERROR_END_OF_STREAM:
221     {
222       GADM_LOG("Got EOS frame!");
223       nsresult rv = CreateAudioData(audioBuffer, aStreamOffset);
224       NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
225       MOZ_ASSERT(mAudioQueue.GetSize() > 0);
226       mAudioQueue.Finish();
227       break;
228     }
229     case -ETIMEDOUT:
230     {
231       GADM_LOG("Timeout. can try again next time");
232       return NS_ERROR_UNEXPECTED;
233     }
234     default:
235     {
236       GADM_LOG("Decoder failed, err=%d", err);
237       return NS_ERROR_UNEXPECTED;
238     }
239   }
240 
241   if (mAudioQueue.GetSize() > 0) {
242     aOutData = mAudioQueue.PopFront();
243     // Return NS_ERROR_ABORT at the last sample.
244     return mAudioQueue.AtEndOfStream() ? NS_ERROR_ABORT : NS_OK;
245   }
246 
247   return NS_ERROR_NOT_AVAILABLE;
248 }
249 
250 void
ProcessFlush()251 GonkAudioDecoderManager::ProcessFlush()
252 {
253   GADM_LOG("FLUSH<<<");
254   mAudioQueue.Reset();
255   GADM_LOG(">>>FLUSH");
256   GonkDecoderManager::ProcessFlush();
257 }
258 
259 void
ResetEOS()260 GonkAudioDecoderManager::ResetEOS()
261 {
262   GADM_LOG("ResetEOS(<<<");
263   mAudioQueue.Reset();
264   GADM_LOG(">>>ResetEOS(");
265   GonkDecoderManager::ResetEOS();
266 }
267 
268 } // namespace mozilla
269