• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "WAVExtractor"
19 #include <utils/Log.h>
20 
21 #include "WAVExtractor.h"
22 
23 #include <android-base/properties.h>
24 #include <android/binder_ibinder.h> // for AIBinder_getCallingUid
25 #include <audio_utils/primitives.h>
26 #include <media/stagefright/foundation/ADebug.h>
27 #include <media/stagefright/MediaDefs.h>
28 #include <media/stagefright/MediaErrors.h>
29 #include <media/stagefright/MetaData.h>
30 #include <private/android_filesystem_config.h> // for AID_MEDIA
31 #include <system/audio.h>
32 #include <utils/String8.h>
33 #include <cutils/bitops.h>
34 
35 #define CHANNEL_MASK_USE_CHANNEL_ORDER 0
36 
37 // NOTE: This code assumes the device processor is little endian.
38 
39 namespace android {
40 
41 // MediaServer is capable of handling float extractor output, but general processes
42 // may not be able to do so.
43 // TODO: Improve API to set extractor float output.
44 // (Note: duplicated with FLACExtractor.cpp)
shouldExtractorOutputFloat(int bitsPerSample)45 static inline bool shouldExtractorOutputFloat(int bitsPerSample)
46 {
47     return bitsPerSample > 16 && AIBinder_getCallingUid() == AID_MEDIA
48                               && android::base::GetBoolProperty("media.extractor.float", true);
49 }
50 
51 enum {
52     WAVE_FORMAT_PCM        = 0x0001,
53     WAVE_FORMAT_IEEE_FLOAT = 0x0003,
54     WAVE_FORMAT_ALAW       = 0x0006,
55     WAVE_FORMAT_MULAW      = 0x0007,
56     WAVE_FORMAT_MSGSM      = 0x0031,
57     WAVE_FORMAT_EXTENSIBLE = 0xFFFE
58 };
59 
60 static const char* WAVEEXT_SUBFORMAT = "\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71";
61 static const char* AMBISONIC_SUBFORMAT = "\x00\x00\x21\x07\xD3\x11\x86\x44\xC8\xC1\xCA\x00\x00\x00";
62 
U32_LE_AT(const uint8_t * ptr)63 static uint32_t U32_LE_AT(const uint8_t *ptr) {
64     return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0];
65 }
66 
U16_LE_AT(const uint8_t * ptr)67 static uint16_t U16_LE_AT(const uint8_t *ptr) {
68     return ptr[1] << 8 | ptr[0];
69 }
70 
71 struct WAVSource : public MediaTrackHelper {
72     WAVSource(
73             DataSourceHelper *dataSource,
74             AMediaFormat *meta,
75             uint16_t waveFormat,
76             bool outputFloat,
77             off64_t offset, size_t size);
78 
79     virtual media_status_t start();
80     virtual media_status_t stop();
81     virtual media_status_t getFormat(AMediaFormat *meta);
82 
83     virtual media_status_t read(
84             MediaBufferHelper **buffer, const ReadOptions *options = NULL);
85 
supportsNonBlockingReadandroid::WAVSource86     bool supportsNonBlockingRead() override { return false; }
87 
88 protected:
89     virtual ~WAVSource();
90 
91 private:
92     static const size_t kMaxFrameSize;
93 
94     DataSourceHelper *mDataSource;
95     AMediaFormat *mMeta;
96     uint16_t mWaveFormat;
97     const bool mOutputFloat;
98     uint32_t mSampleRate;
99     uint32_t mNumChannels;
100     uint32_t mBitsPerSample;
101     off64_t mOffset;
102     size_t mSize;
103     bool mStarted;
104     off64_t mCurrentPos;
105 
106     WAVSource(const WAVSource &);
107     WAVSource &operator=(const WAVSource &);
108 };
109 
WAVExtractor(DataSourceHelper * source)110 WAVExtractor::WAVExtractor(DataSourceHelper *source)
111     : mDataSource(source),
112       mValidFormat(false),
113       mChannelMask(CHANNEL_MASK_USE_CHANNEL_ORDER) {
114     mTrackMeta = AMediaFormat_new();
115     mInitCheck = init();
116 }
117 
~WAVExtractor()118 WAVExtractor::~WAVExtractor() {
119     delete mDataSource;
120     AMediaFormat_delete(mTrackMeta);
121 }
122 
getMetaData(AMediaFormat * meta)123 media_status_t WAVExtractor::getMetaData(AMediaFormat *meta) {
124     AMediaFormat_clear(meta);
125     if (mInitCheck == OK) {
126         AMediaFormat_setString(meta, AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_CONTAINER_WAV);
127     }
128 
129     return AMEDIA_OK;
130 }
131 
countTracks()132 size_t WAVExtractor::countTracks() {
133     return mInitCheck == OK ? 1 : 0;
134 }
135 
getTrack(size_t index)136 MediaTrackHelper *WAVExtractor::getTrack(size_t index) {
137     if (mInitCheck != OK || index > 0) {
138         return NULL;
139     }
140 
141     return new WAVSource(
142             mDataSource, mTrackMeta,
143             mWaveFormat, shouldExtractorOutputFloat(mBitsPerSample), mDataOffset, mDataSize);
144 }
145 
getTrackMetaData(AMediaFormat * meta,size_t index,uint32_t)146 media_status_t WAVExtractor::getTrackMetaData(
147         AMediaFormat *meta,
148         size_t index, uint32_t /* flags */) {
149     if (mInitCheck != OK || index > 0) {
150         return AMEDIA_ERROR_UNKNOWN;
151     }
152 
153     const media_status_t status = AMediaFormat_copy(meta, mTrackMeta);
154     if (status == OK) {
155         AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_PCM_ENCODING,
156                 shouldExtractorOutputFloat(mBitsPerSample)
157                         ? kAudioEncodingPcmFloat : kAudioEncodingPcm16bit);
158     }
159     return status;
160 }
161 
init()162 status_t WAVExtractor::init() {
163     uint8_t header[12];
164     if (mDataSource->readAt(
165                 0, header, sizeof(header)) < (ssize_t)sizeof(header)) {
166         return NO_INIT;
167     }
168 
169     if (memcmp(header, "RIFF", 4) || memcmp(&header[8], "WAVE", 4)) {
170         return NO_INIT;
171     }
172 
173     size_t totalSize = U32_LE_AT(&header[4]);
174 
175     off64_t offset = 12;
176     size_t remainingSize = totalSize;
177     while (remainingSize >= 8) {
178         uint8_t chunkHeader[8];
179         if (mDataSource->readAt(offset, chunkHeader, 8) < 8) {
180             return NO_INIT;
181         }
182 
183         remainingSize -= 8;
184         offset += 8;
185 
186         uint32_t chunkSize = U32_LE_AT(&chunkHeader[4]);
187 
188         if (chunkSize > remainingSize) {
189             return NO_INIT;
190         }
191 
192         if (!memcmp(chunkHeader, "fmt ", 4)) {
193             if (chunkSize < 16) {
194                 return NO_INIT;
195             }
196 
197             uint8_t formatSpec[40];
198             if (mDataSource->readAt(offset, formatSpec, 2) < 2) {
199                 return NO_INIT;
200             }
201 
202             mWaveFormat = U16_LE_AT(formatSpec);
203             if (mWaveFormat != WAVE_FORMAT_PCM
204                     && mWaveFormat != WAVE_FORMAT_IEEE_FLOAT
205                     && mWaveFormat != WAVE_FORMAT_ALAW
206                     && mWaveFormat != WAVE_FORMAT_MULAW
207                     && mWaveFormat != WAVE_FORMAT_MSGSM
208                     && mWaveFormat != WAVE_FORMAT_EXTENSIBLE) {
209                 return AMEDIA_ERROR_UNSUPPORTED;
210             }
211 
212             uint8_t fmtSize = 16;
213             if (mWaveFormat == WAVE_FORMAT_EXTENSIBLE) {
214                 fmtSize = 40;
215             }
216             if (mDataSource->readAt(offset, formatSpec, fmtSize) < fmtSize) {
217                 return NO_INIT;
218             }
219 
220             mNumChannels = U16_LE_AT(&formatSpec[2]);
221 
222             if (mNumChannels < 1 || mNumChannels > FCC_8) {
223                 ALOGE("Unsupported number of channels (%d)", mNumChannels);
224                 return AMEDIA_ERROR_UNSUPPORTED;
225             }
226 
227             if (mWaveFormat != WAVE_FORMAT_EXTENSIBLE) {
228                 if (mNumChannels != 1 && mNumChannels != FCC_2) {
229                     ALOGW("More than 2 channels (%d) in non-WAVE_EXT, unknown channel mask",
230                             mNumChannels);
231                 }
232             }
233 
234             mSampleRate = U32_LE_AT(&formatSpec[4]);
235 
236             if (mSampleRate == 0) {
237                 return ERROR_MALFORMED;
238             }
239 
240             mBitsPerSample = U16_LE_AT(&formatSpec[14]);
241 
242             if (mWaveFormat == WAVE_FORMAT_EXTENSIBLE) {
243                 uint16_t validBitsPerSample = U16_LE_AT(&formatSpec[18]);
244                 if (validBitsPerSample != mBitsPerSample) {
245                     if (validBitsPerSample != 0) {
246                         ALOGE("validBits(%d) != bitsPerSample(%d) are not supported",
247                                 validBitsPerSample, mBitsPerSample);
248                         return AMEDIA_ERROR_UNSUPPORTED;
249                     } else {
250                         // we only support valitBitsPerSample == bitsPerSample but some WAV_EXT
251                         // writers don't correctly set the valid bits value, and leave it at 0.
252                         ALOGW("WAVE_EXT has 0 valid bits per sample, ignoring");
253                     }
254                 }
255 
256                 mChannelMask = U32_LE_AT(&formatSpec[20]);
257                 ALOGV("numChannels=%d channelMask=0x%x", mNumChannels, mChannelMask);
258                 if ((mChannelMask >> 18) != 0) {
259                     ALOGE("invalid channel mask 0x%x", mChannelMask);
260                     return ERROR_MALFORMED;
261                 }
262 
263                 if ((mChannelMask != CHANNEL_MASK_USE_CHANNEL_ORDER)
264                         && (popcount(mChannelMask) != mNumChannels)) {
265                     ALOGE("invalid number of channels (%d) in channel mask (0x%x)",
266                             popcount(mChannelMask), mChannelMask);
267                     return ERROR_MALFORMED;
268                 }
269 
270                 // In a WAVE_EXT header, the first two bytes of the GUID stored at byte 24 contain
271                 // the sample format, using the same definitions as a regular WAV header
272                 mWaveFormat = U16_LE_AT(&formatSpec[24]);
273                 if (memcmp(&formatSpec[26], WAVEEXT_SUBFORMAT, 14) &&
274                     memcmp(&formatSpec[26], AMBISONIC_SUBFORMAT, 14)) {
275                     ALOGE("unsupported GUID");
276                     return ERROR_UNSUPPORTED;
277                 }
278             }
279 
280             if (mWaveFormat == WAVE_FORMAT_PCM) {
281                 if (mBitsPerSample != 8 && mBitsPerSample != 16
282                     && mBitsPerSample != 24 && mBitsPerSample != 32) {
283                     return ERROR_UNSUPPORTED;
284                 }
285             } else if (mWaveFormat == WAVE_FORMAT_IEEE_FLOAT) {
286                 if (mBitsPerSample != 32) {  // TODO we don't support double
287                     return ERROR_UNSUPPORTED;
288                 }
289             }
290             else if (mWaveFormat == WAVE_FORMAT_MSGSM) {
291                 if (mBitsPerSample != 0) {
292                     return ERROR_UNSUPPORTED;
293                 }
294             } else if (mWaveFormat == WAVE_FORMAT_MULAW || mWaveFormat == WAVE_FORMAT_ALAW) {
295                 if (mBitsPerSample != 8) {
296                     return ERROR_UNSUPPORTED;
297                 }
298             } else {
299                 return ERROR_UNSUPPORTED;
300             }
301 
302             mValidFormat = true;
303         } else if (!memcmp(chunkHeader, "data", 4)) {
304             if (mValidFormat) {
305                 mDataOffset = offset;
306                 mDataSize = chunkSize;
307 
308                 AMediaFormat_clear(mTrackMeta);
309 
310                 switch (mWaveFormat) {
311                     case WAVE_FORMAT_PCM:
312                     case WAVE_FORMAT_IEEE_FLOAT:
313                         AMediaFormat_setString(mTrackMeta,
314                                 AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_RAW);
315                         break;
316                     case WAVE_FORMAT_ALAW:
317                         AMediaFormat_setString(mTrackMeta,
318                                 AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_G711_ALAW);
319                         break;
320                     case WAVE_FORMAT_MSGSM:
321                         AMediaFormat_setString(mTrackMeta,
322                                 AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_MSGSM);
323                         break;
324                     default:
325                         CHECK_EQ(mWaveFormat, (uint16_t)WAVE_FORMAT_MULAW);
326                         AMediaFormat_setString(mTrackMeta,
327                                 AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_G711_MLAW);
328                         break;
329                 }
330 
331                 AMediaFormat_setInt32(mTrackMeta, AMEDIAFORMAT_KEY_CHANNEL_COUNT, mNumChannels);
332                 AMediaFormat_setInt32(mTrackMeta, AMEDIAFORMAT_KEY_CHANNEL_MASK, mChannelMask);
333                 AMediaFormat_setInt32(mTrackMeta, AMEDIAFORMAT_KEY_SAMPLE_RATE, mSampleRate);
334                 AMediaFormat_setInt32(mTrackMeta, AMEDIAFORMAT_KEY_BITS_PER_SAMPLE, mBitsPerSample);
335                 int64_t durationUs = 0;
336                 if (mWaveFormat == WAVE_FORMAT_MSGSM) {
337                     // 65 bytes decode to 320 8kHz samples
338                     durationUs =
339                         1000000LL * (mDataSize / 65 * 320) / 8000;
340                 } else {
341                     size_t bytesPerSample = mBitsPerSample >> 3;
342 
343                     if (!bytesPerSample || !mNumChannels)
344                         return AMEDIA_ERROR_MALFORMED;
345 
346                     size_t num_samples = mDataSize / (mNumChannels * bytesPerSample);
347 
348                     if (!mSampleRate)
349                         return AMEDIA_ERROR_MALFORMED;
350 
351                     durationUs =
352                         1000000LL * num_samples / mSampleRate;
353                 }
354 
355                 AMediaFormat_setInt64(mTrackMeta, AMEDIAFORMAT_KEY_DURATION, durationUs);
356 
357                 return OK;
358             }
359         }
360 
361         offset += chunkSize;
362     }
363 
364     return NO_INIT;
365 }
366 
367 const size_t WAVSource::kMaxFrameSize = 32768;
368 
WAVSource(DataSourceHelper * dataSource,AMediaFormat * meta,uint16_t waveFormat,bool outputFloat,off64_t offset,size_t size)369 WAVSource::WAVSource(
370         DataSourceHelper *dataSource,
371         AMediaFormat *meta,
372         uint16_t waveFormat,
373         bool outputFloat,
374         off64_t offset, size_t size)
375     : mDataSource(dataSource),
376       mMeta(meta),
377       mWaveFormat(waveFormat),
378       mOutputFloat(outputFloat),
379       mOffset(offset),
380       mSize(size),
381       mStarted(false) {
382     CHECK(AMediaFormat_getInt32(mMeta, AMEDIAFORMAT_KEY_SAMPLE_RATE, (int32_t*) &mSampleRate));
383     CHECK(AMediaFormat_getInt32(mMeta, AMEDIAFORMAT_KEY_CHANNEL_COUNT, (int32_t*) &mNumChannels));
384     CHECK(AMediaFormat_getInt32(mMeta, AMEDIAFORMAT_KEY_BITS_PER_SAMPLE, (int32_t*) &mBitsPerSample));
385 }
386 
~WAVSource()387 WAVSource::~WAVSource() {
388     if (mStarted) {
389         stop();
390     }
391 }
392 
start()393 media_status_t WAVSource::start() {
394     ALOGV("WAVSource::start");
395 
396     CHECK(!mStarted);
397 
398     // some WAV files may have large audio buffers that use shared memory transfer.
399     if (!mBufferGroup->init(4 /* buffers */, kMaxFrameSize)) {
400         return AMEDIA_ERROR_UNKNOWN;
401     }
402 
403     mCurrentPos = mOffset;
404 
405     mStarted = true;
406 
407     return AMEDIA_OK;
408 }
409 
stop()410 media_status_t WAVSource::stop() {
411     ALOGV("WAVSource::stop");
412 
413     CHECK(mStarted);
414 
415     mStarted = false;
416 
417     return AMEDIA_OK;
418 }
419 
getFormat(AMediaFormat * meta)420 media_status_t WAVSource::getFormat(AMediaFormat *meta) {
421     ALOGV("WAVSource::getFormat");
422 
423     const media_status_t status = AMediaFormat_copy(meta, mMeta);
424     if (status == OK) {
425         AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, kMaxFrameSize);
426         AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_PCM_ENCODING,
427                 mOutputFloat ? kAudioEncodingPcmFloat : kAudioEncodingPcm16bit);
428     }
429     return status;
430 }
431 
read(MediaBufferHelper ** out,const ReadOptions * options)432 media_status_t WAVSource::read(
433         MediaBufferHelper **out, const ReadOptions *options) {
434     *out = NULL;
435 
436     if (options != nullptr && options->getNonBlocking() && !mBufferGroup->has_buffers()) {
437         return AMEDIA_ERROR_WOULD_BLOCK;
438     }
439 
440     int64_t seekTimeUs;
441     ReadOptions::SeekMode mode;
442     if (options != NULL && options->getSeekTo(&seekTimeUs, &mode)) {
443         int64_t pos;
444         int64_t sampleNumber;
445         bool overflowed = __builtin_mul_overflow(seekTimeUs, mSampleRate, &sampleNumber);
446         sampleNumber /= 1000000;
447         if (mWaveFormat == WAVE_FORMAT_MSGSM) {
448             // 65 bytes decode to 320 8kHz samples
449             pos = sampleNumber / 320 * 65;
450         } else {
451             int64_t bytesPerFrame;
452             overflowed |= __builtin_mul_overflow(mNumChannels, mBitsPerSample >> 3, &bytesPerFrame);
453             overflowed |= __builtin_mul_overflow(bytesPerFrame, sampleNumber, &pos);
454         }
455         if (overflowed) {
456           return AMEDIA_ERROR_MALFORMED;
457         }
458         pos = std::clamp(pos, (int64_t) 0, (int64_t) mSize);
459         mCurrentPos = pos + mOffset;
460     }
461 
462     MediaBufferHelper *buffer;
463     media_status_t err = mBufferGroup->acquire_buffer(&buffer);
464     if (err != OK) {
465         return err;
466     }
467 
468     // maxBytesToRead may be reduced so that in-place data conversion will fit in buffer size.
469     const size_t bufferSize = std::min(buffer->size(), kMaxFrameSize);
470     size_t maxBytesToRead;
471     if (mOutputFloat) { // destination is float at 4 bytes per sample, source may be less.
472         maxBytesToRead = (mBitsPerSample / 8) * (bufferSize / 4);
473     } else { // destination is int16_t at 2 bytes per sample, only source of 8 bits is less.
474         maxBytesToRead = mBitsPerSample == 8 ? bufferSize / 2 : bufferSize;
475     }
476 
477     const size_t maxBytesAvailable =
478         (mCurrentPos < mOffset || mCurrentPos - mOffset >= (off64_t)mSize)
479             ? 0 : mSize - (mCurrentPos - mOffset);
480 
481     if (maxBytesToRead > maxBytesAvailable) {
482         maxBytesToRead = maxBytesAvailable;
483     }
484 
485     if (mWaveFormat == WAVE_FORMAT_MSGSM) {
486         // Microsoft packs 2 frames into 65 bytes, rather than using separate 33-byte frames,
487         // so read multiples of 65, and use smaller buffers to account for ~10:1 expansion ratio
488         if (maxBytesToRead > 1024) {
489             maxBytesToRead = 1024;
490         }
491         maxBytesToRead = (maxBytesToRead / 65) * 65;
492     } else {
493         // read only integral amounts of audio unit frames.
494         const size_t inputUnitFrameSize = mNumChannels * mBitsPerSample / 8;
495         maxBytesToRead -= maxBytesToRead % inputUnitFrameSize;
496     }
497 
498     ssize_t n = mDataSource->readAt(
499             mCurrentPos, buffer->data(),
500             maxBytesToRead);
501 
502     if (n <= 0) {
503         buffer->release();
504         buffer = NULL;
505 
506         return AMEDIA_ERROR_END_OF_STREAM;
507     }
508 
509     buffer->set_range(0, n);
510 
511     // TODO: add capability to return data as float PCM instead of 16 bit PCM.
512     if (mWaveFormat == WAVE_FORMAT_PCM) {
513         const size_t bytesPerFrame = (mBitsPerSample >> 3) * mNumChannels;
514         const size_t numFrames = n / bytesPerFrame;
515         const size_t numSamples = numFrames * mNumChannels;
516         if (mOutputFloat) {
517             float *fdest = (float *)buffer->data();
518             buffer->set_range(0, 4 * numSamples);
519             switch (mBitsPerSample) {
520             case 8: {
521                 memcpy_to_float_from_u8(fdest, (const uint8_t *)buffer->data(), numSamples);
522             } break;
523             case 16: {
524                 memcpy_to_float_from_i16(fdest, (const int16_t *)buffer->data(), numSamples);
525             } break;
526             case 24: {
527                 memcpy_to_float_from_p24(fdest, (const uint8_t *)buffer->data(), numSamples);
528             } break;
529             case 32: { // buffer range is correct
530                 memcpy_to_float_from_i32(fdest, (const int32_t *)buffer->data(), numSamples);
531             } break;
532             }
533         } else {
534             int16_t *idest = (int16_t *)buffer->data();
535             buffer->set_range(0, 2 * numSamples);
536             switch (mBitsPerSample) {
537             case 8: {
538                 memcpy_to_i16_from_u8(idest, (const uint8_t *)buffer->data(), numSamples);
539             } break;
540             case 16:
541                 // no conversion needed
542                 break;
543             case 24: {
544                 memcpy_to_i16_from_p24(idest, (const uint8_t *)buffer->data(), numSamples);
545             } break;
546             case 32: {
547                 memcpy_to_i16_from_i32(idest, (const int32_t *)buffer->data(), numSamples);
548             } break;
549             }
550         }
551     } else if (mWaveFormat == WAVE_FORMAT_IEEE_FLOAT) {
552         if (!mOutputFloat) { // mBitsPerSample == 32
553             int16_t *idest = (int16_t *)buffer->data();
554             const size_t numSamples = n / 4;
555             memcpy_to_i16_from_float(idest, (const float *)buffer->data(), numSamples);
556             buffer->set_range(0, 2 * numSamples);
557         }
558         // Note: if output encoding is float, no need to convert if source is float.
559     }
560 
561     int64_t timeStampUs = 0;
562 
563     if (mWaveFormat == WAVE_FORMAT_MSGSM) {
564         timeStampUs = 1000000LL * (mCurrentPos - mOffset) * 320 / 65 / mSampleRate;
565     } else {
566         size_t bytesPerSample = mBitsPerSample >> 3;
567         timeStampUs = 1000000LL * (mCurrentPos - mOffset)
568                 / (mNumChannels * bytesPerSample) / mSampleRate;
569     }
570 
571     AMediaFormat *meta = buffer->meta_data();
572     AMediaFormat_setInt64(meta, AMEDIAFORMAT_KEY_TIME_US, timeStampUs);
573     AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_IS_SYNC_FRAME, 1);
574 
575     mCurrentPos += n;
576 
577     *out = buffer;
578 
579     return AMEDIA_OK;
580 }
581 
582 ////////////////////////////////////////////////////////////////////////////////
583 
CreateExtractor(CDataSource * source,void *)584 static CMediaExtractor* CreateExtractor(
585         CDataSource *source,
586         void *) {
587     return wrap(new WAVExtractor(new DataSourceHelper(source)));
588 }
589 
Sniff(CDataSource * source,float * confidence,void **,FreeMetaFunc *)590 static CreatorFunc Sniff(
591         CDataSource *source,
592         float *confidence,
593         void **,
594         FreeMetaFunc *) {
595     DataSourceHelper *helper = new DataSourceHelper(source);
596     char header[12];
597     if (helper->readAt(0, header, sizeof(header)) < (ssize_t)sizeof(header)) {
598         delete helper;
599         return NULL;
600     }
601 
602     if (memcmp(header, "RIFF", 4) || memcmp(&header[8], "WAVE", 4)) {
603         delete helper;
604         return NULL;
605     }
606 
607     WAVExtractor *extractor = new WAVExtractor(helper); // extractor owns the helper
608     int numTracks = extractor->countTracks();
609     delete extractor;
610     if (numTracks == 0) {
611         return NULL;
612     }
613 
614     *confidence = 0.3f;
615 
616     return CreateExtractor;
617 }
618 
619 static const char *extensions[] = {
620     "wav",
621     NULL
622 };
623 
624 extern "C" {
625 // This is the only symbol that needs to be exported
626 __attribute__ ((visibility ("default")))
GETEXTRACTORDEF()627 ExtractorDef GETEXTRACTORDEF() {
628     return {
629         EXTRACTORDEF_VERSION,
630         UUID("7d613858-5837-4a38-84c5-332d1cddee27"),
631         1, // version
632         "WAV Extractor",
633         { .v3 = {Sniff, extensions} },
634     };
635 }
636 
637 } // extern "C"
638 
639 } // namespace android
640