All posts by yukuku

Developer

Resolving M4A encoding problem

Background, usage of AAC

Hi-Q uses AAC audio encoding for M4A format. The AAC is the codec for encoding audio, and the M4A is the file extension for MPEG-4 container format.

In Android, there is a built-in encoder for AAC via the class MediaCodec. To start an AAC encoder, we call

MediaCodec.createEncoderByType("audio/mp4a-latm");

Then we need to call configure on the resulting object, providing it with a MediaFormat. We set up MediaFormat as follows:

format = MediaFormat.createAudioFormat("audio/mp4a-latm", 44100, 1);
format.setInteger(KEY_AAC_PROFILE, AACObjectHE);
format.setInteger(KEY_BIT_RATE, 64000);

Then after it is set, we sent the output of the codec to a MediaMuxer, which will format an MPEG-4 container with the AAC-encoded stream as the sole track.

Here is how we get the audio data encoded by the codec, simplified:

  1. We call codec.dequeueInputBuffer() to get an integer, that we pass to codec.getInputBuffer() to get a ByteBuffer.
  2. Put the raw audio data to the buffer.
  3. Call codec.queueInputBuffer(), passing the buffer with additional information such as the current time, size and offset in bytes, etc.
  4. Call codec.dequeueOutputBuffer() to get an integer, that we pass to codec.getOutputBuffer() to get a ByteBuffer.
  5. Read bytes from the buffer, and pass that to the muxer.

This works most of the time. Hi-Q users are able to enjoy a better audio quality and compression in m4a compared to mp3.

The random crash

However, since the first time we release this feature, there has been seemingly random crash as follows:

Fatal Exception: java.lang.IllegalStateException
       at android.media.MediaCodec.native_dequeueInputBuffer(MediaCodec.java)
       at android.media.MediaCodec.dequeueInputBuffer(MediaCodec.java:2635)
Fatal Exception: java.lang.IllegalStateException: Failed to stop the muxer
       at android.media.MediaMuxer.nativeStop(MediaMuxer.java)
       at android.media.MediaMuxer.stop(MediaMuxer.java:454)
Fatal Exception: java.lang.IllegalStateException
       at android.media.MediaCodec.native_dequeueOutputBuffer(MediaCodec.java)
       at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:2698)

The rate of this happening is ~800 users per month, out of more than 200K monthly users that we have.

Unfortunately we can’t find the reason of this happening, we tried multiple ways of reproducing and we never find any reliable way of triggering this error. We suspect that this was a faulty implementation of the codec, but this happened not only on a specific device model but arbitrarily.

Experiment to reliably reproduce crash

Only until one of our customer, Alex, told me that he often get a crash when he sets the Gain to the max while using the M4A format. This might explain the situation!

I tried to do the same myself, setting the gain to the max and recording using the M4A format. I tried it several times. Almost always Hi-Q crashed after several seconds, with similar stack trace as above. This was enlightening!

So we tried to reproduce the crash in a systematic manner. Instead of using audio data taken from real live, we generate the audio data in code:

for (int i = 0, len = block.len; i < len; i++) {
    final short[] data = block.data;
    if (i % 100 == 0) up = !up;
    if (up) {
        data[i] = (short) (32500 + i);
    } else {
        data[i] = (short) (-32500 - i);
    }
}

A block contains 4400 samples. The code above generates a sharp and loud audio signal, having amplitudes 32500~32599 out of max value of 32767. On the third block, the encoding process always crashes.

Hypothesis 1: The loud signals causes the codec to output a lot of data, so we need to repeatedly drain the output by calling dequeueOutputBuffer multiple times until BUFFER_FLAG_END_OF_STREAM is given. I was optimistic when I found that this was not currently done in the code.

This did not work. The same error still happened.

Hypothesis 2: We implemented waiting for the input buffer by calling dequeueInputBuffer with a wrong timeout.

To prevent deadlock, dequeueInputBuffer has a timeout parameter. Probably it was too short or too long. Unfortunately, there is no guideline or clear standard on the number we should put.

Hypothesis 3: We should have used the recommended asynchronous API instead of enqueueing and dequeueing the buffers ourselves.

Since Build.VERSION_CODES.LOLLIPOP, the preferred method is to process data asynchronously by setting a callback before calling configure.

In the asynchronous, we set a callback object to the codec that will receive input buffers to be filled in via onInputBufferAvailable, output buffers from where bytes are extracted via onOutputBufferAvailable, and errors via onError. No more calling dequeue methods, and no more timeout values to be specified!

Sadly, the same error still happens, via onError. The error has code=1101, diagnostic info “android.media.MediaCodec.error_1101”, and isRecoverable=false.

In conclusion, we feel that it is quite likely that the error was not caused by dequeueing and enqueueing the input and output buffers at the wrong order.

Changes made

Hypothesis 4: The codec is configured in a way that does not allow encoding of audio signals outside of a certain limit.

We tried to see more carefully what parameters we passed to the codec, and we realized that we had this:

format.setInteger(KEY_AAC_PROFILE, AACObjectHE);

Then I thought: Why this has to be AACObjectHE? Could I change it to something else and maybe everything would work well? In online examples of encoding audio signals in AAC, people would use AACObjectLC (const value 2) or AACObjectHE (const value 5). I tried to change it to AACObjectLE and… voila! No more crashes.

This change is published starting in Hi-Q 2.8.0. I hope everything goes well and we have much more reliable M4A recording. We apologize for the earlier crashes. 🙇‍♂️

Regarding warnings from anti-virus software such as McAfee

It has come to our attention that certain anti-virus software on Android flag Hi-Q MP3 Voice Recorder, version 2.4 Beta 4, as harmful.

Please take note that other versions, including the most popular publicly available version 2.3.1, is not flagged as such by the anti-virus software.

We have also released 2.4 Beta 5 and 2.4 Beta 6 that is not detected as harmful.

The reason 2.4 Beta 4 was flagged is that we integrated the Tapcore SDK that shows advertisements to people using pirated copy of Hi-Q (e.g. not bought from Google Play). For more information about Tapcore, you can see the website https://tapcore.com/en. However, we found that this is problematic and we have removed completely all things related to Tapcore in our app.

We apologize for this careless integration of 3rd-party SDK into our app. We will be more careful in the future and try to bring you more requested features instead of adding useless things.

Version 2.2 released

It has been so long since we released an update to Hi-Q MP3 Voice Recorder — more than a year ago! We apologize for the lack of updates. Thank you for many feedback that we received via Play Store and help@hiqrecorder.com – we read them all but we could not implement all of them.

Major features of this release since 2.1:

  • Automatic upload to Google Drive
  • Seek back and forward 10s during playback
  • Playback in a loop or “A–B repeat”
  • Longer auto-stop timer up to 100 hours
  • More reliable Dropbox v2 API
  • Fine-grained adjustment of Gain
  • Smoother playback indicator
  • Individual VU-meter values in stereo recording
  • More compatibility when sharing files in Android Nougat

For detailed changes, please take a look at the Updates page.

Another fake Hi-Q MP3 Voice Recorder in the iOS App Store

Not long after we mentioned in our blog about the existence of a fake Hi-Q MP3 Voice Recorder app in the Apple (iOS) App Store, we saw another app that mimics our app.

The app is called “High Quality Audio Recorder Premium” and has the app id of “id1204257992”.

You may ask, “Isn’t it okay to use the phrase ‘High Quality’ as the app title, since it is just a general term?” Yes, it is okay to use that, but consider the description of their app, at the time of writing (July 5, 2017), which is as follows.

We hope you love our app as much as our community and our team do. Download it today and let us know what you think!

Record your conversations and easily listen to them again and again .

Are you looking for a simple application to easily record your conversations and your meetings? Hi-Q Audio Recorder Premium provides the solution through an elegant design and easy-to-use features.

The Hi-Q Audio Recorder takes mobile sound recording to the next level. Feature-packed and loaded with customization options, and with high-fidelity audio sampling, it’s miles ahead of any standard recording app.

Personal voice notes, group discussions, band practice, concerts, lectures, talks, sermons and so much more—if you can hear it, the app can record it

* Simple and Reliable.
Start the app and you are good to go! Press the striking Green button, and you’ll immediately start recording.

* MP3 clips.
Recordings are stored as MP3 files in real-time, which are compact enough and can be played everywhere.

* Keep Recordings Safe.
With automatic upload to Dropbox, your recordings are stored safely and you can free up more space on your device. Sync happens when the preferred connection is available (Wi-Fi only or Wi-Fi and mobile network).

* Manage Clips.
Share, sort, rename and delete recordings as you please.During playback you can move fast forward or fast backward at a selected period.

* During recording.
You can reject incoming calls while recording. Background recording is also supported.

With the Hi-Q Audio Recorder you won’t ever have to fuss with your phone to get the best recording quality. Record yourself and practice for a presentation, record song ideas and brainstorming sessions, record anything !

Buy now and you will get free upgrades with many new features!!!

Record your world with Hi-Q Audio Recorder Premium !
Thank you about using the app.
Good luck !

Our original Hi-Q MP3 Voice Recorder description says:

The Hi-Q MP3 Voice Recorder takes mobile sound recording to the next level. Feature-packed and loaded with customization options, and with high-fidelity 44 kHz audio sampling, it’s miles ahead of any standard recording app.

Personal voice notes, group discussions, band practice, concerts, lectures, talks, sermons and so much more—if you can hear it, the app can record it.

Simple and Reliable. Start the app and you are good to go! Press the striking Red button, and you’ll immediately start recording.

MP3 clips. Recordings are stored as MP3 files in real-time, which are compact enough and can be played almost everywhere.

Keep Recordings Safe. With automatic upload to Dropbox, your recordings are stored safely and you can free up more space on your device. Sync happens when the preferred connection is available (Wi-Fi only or Wi-Fi and mobile network).

Home Screen Widgets. Start, pause and resume recording in a flash with home screen widget.

Quality Settings. Customize audio quality with changeable bit rate up to 320 kbps. You can also record in WAV, OGG, M4A, and FLAC (experimental) formats.

Input Selection. Select the more sensitive front microphone, or the clearer back microphone as you wish (depending on individual device).

Shhh! Switch the recording icon for discretion.

Manage Clips. Share, sort, rename and delete recordings as you please.

Wi-Fi Transfer. Transfer recordings to your PC wirelessly with the built-in Wi-Fi Transfer, utilizing your home Wi-Fi or your device’s Wi-Fi Hotspot capability.

Gain. Specify input gain settings in real-time for optimum recording in various noise levels.

With the Hi-Q MP3 Voice Recorder you won’t ever have to fuss with your phone to get the best recording quality. Record yourself and practice for a presentation, record song ideas and brainstorming sessions, record anything!

I have highlighted the copied text in orange. You can easily notice that they are copycat of our app. Full diff can be seen on this PDF report.

You may notice several things:

  • They use the name “Hi-Q Audio Recorder”, which is actually the name of our app that we publish in the Amazon Appstore.
  • They use “Hi-Q” even though the name of their app is not “Hi-Q”. I think this is such that a search on “Hi-Q” on the Apple App Store results in their app.
  • They changed the phrase “press the striking Red button” to “press the striking Green button”. It’s funny how lazy they are to make their own app description.

As an Android developer, I thought the quality of applications in iTunes App Store is much better because of their strict approval process. I was wrong. This is the 3rd time a copycat of our app has appeared in App Store, and I will file a complaint again to Apple. We do not have any Hi-Q Recorder apps on the App Store. Please be careful and do not get deceived.

Beware of fake “Hi-Q MP3 Voice Recorder” on iTunes/iOS App Store

It has come to our attention that there is a fake and unauthorized Hi-Q MP3 Voice Recorder on the iOS platform on the following Apple App Store page:

https://itunes.apple.com/us/app/hi-q-mp3-voice-recorder-pro/id1149183454?mt=8

As of November 2, 2016, the page looks like this:

As you can notice from the screenshots above:

  • The name of the app is taken from ours: Hi-Q MP3 Voice Recorder Pro.
  • The icon is exactly the same as ours.
  • The publisher is “heon Song Seung”, a name of a Korean actor.
  • The UI is completely different than ours and very simplistic.

We have contacted Apple for removal of the aforementioned app since it infringes our trademark and copyright.

Please contact Apple if you have bought that app from the iTunes Store for cancellation or refund.

The official Hi-Q MP3 Voice Recorder can be obtained only for Android at the following links: Free trial version, Pro version.

Note: It is disappointing to know that Apple App Store approval process did not filter out such submission. This is the second time this happened. The first one happened on Aug 2016 when the same fake app appeared (same name, icon, and screenshots) and published by “Prisma bible Nguyen”, Apple ID of the app “1120553159”.