At the time I worked hard to minimize all allocations during recording, and that
quite fixed the problem. But now that I moved the audio engine into a background
service, GC seems not to have such effect anymore... It does occur from time to
time, but dropouts are gone.
Olivier
So what happens if I create new pthread in native code. I can run
audio decoding in it but to play decoded PCM samples I still need to
make call into Java layer to use AudioTrack class since Android does
not provide native access to do this. What happens to this native
thread that calls into Java? Will it be GC'ed?
On May 28, 5:52 pm, fadden <fad...@android.com> wrote:
> On May 28, 5:54 am, ls02 <agal...@audible.com> wrote:
>
> > I assume that native code called from Java layer runs in Java thread
> > and is interrupted by GC. What happens if I spawn a native thread in
> > JNI code and call Java objects method from that native JNI code
> > running in that thread? Does the native thread in this case get
> > attached to Java and GC will suspend it to run GC on it?
>
> Your initial assumption is incorrect.
>
> All threads are Linux pthreads. There is no such thing as a "Java
> thread". Some threads are visible to the VM, either because the VM
> created them or they were attached with the AttachCurrentThread JNI
> call.
>
> The stop-the-world GC mechanism uses safe pointing (i.e. each thread
> periodically checks to see if it needs to suspend). Native code is
> considered "safe", in that it has no way of doing anything that can
> confuse the GC without making a JNI call. The entry point of all JNI
> calls has an explicit check for thread suspension.
>
> So, if you have a thread that is either not known to the VM, or is
> known but is quietly executing without making any JNI calls, it will
> continue to run while the GC does its thing.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
What might be better would be to have your native thread drop messages in a
native data structure that can be picked up by a JNI thread and posted back
to the JVM code. That way the native thread remains entirely in native code,
but the communication back to the JVM still happens (albeit in a more
convoluted fashion).
Ted Neward
Java, .NET, XML Services
Consulting, Teaching, Speaking, Writing
http://www.tedneward.com
> --
> You received this message because you are subscribed to the Google
> Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-
> ndk+uns...@googlegroups.com.
So I'd suggest avoid creating/destroying objects unnecessarily, and
perhaps crank up your audio buffer size to avoid underruns while the
cpu is distracted.
- Gus
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
I'm getting near zero dropouts here, it works quite fine. I access the
AudioTrack in a dedicated thread, and I've squashed all allocations on sight.
> Clearly there's *some* way to do this, or the music player would have
> the same problem. I don't know anything about what media APIs are
> public, though, so I don't have an answer for you here.
I think that MediaPlayer bypasses the Java calls which are involved when using
AudioTrack.
> Doesn't seem like thread priorities are at all relevant here. The
> real issue is that you need to keep the sound device fed, and if you
> can't give it enough data to span a GC pause you're going to have a
> hiccup. How much allocating does the Java code do as part of writing
> your data to the device? The less that gets allocated, the longer you
> can go without a GC.
Yeah, don't allocate anything if you can. Also, my engine is now in a background
Service running in a separate process, and it seems like it has helped solving
dropouts when recording with AudioRecord.
> Can you feed stuff to android.media.MediaPlayer? (Again, I possess
> near-total ignorance of media stuff on the device.)
No you can't. Currently, the media C++ API is 100% private.
--
Olivier
Sorry, forget this last sentence, I misread what you said. And AFAIK, no the
only way to play live raw audio data is AudioTrack.
--
Olivier