Audio Framework: stream, strategies, devices - android

I am trying to get a better understanding of android audio framework (froyo onwards in particular).
I've almost got the distinction into streams. The AudioFlinger maps each stream into a "strategy" and then routes a strategy to the proper "device" (BT, Headset, Speaker..)
However I still struggle to understand:
1) How multiple apps play onto the same stream? can they just 'play' or do they have to handle the focus (request, abandon and listen for changes)?
2) When multiple streams are playing...what is the priority of one stream respect to another? surely the in-call stream has top priority but are there any "rules" to understand how things really work.
If anyone with more confidence on this matter has got any doc/resource to study.. it would be great.
Thanks!
Marco

The answer to this question
Concurrent Sound on Android Device
may help you.
Also, here
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
is written how to initialize the SoundManager with the number of concurrent streams you want to play.

If you need to understand the default behavoiur you can see the Cplusplus code at
/hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp
However each vendor can make changes and ship a different flavour

Related

How to record 4th input channel audio

I have an audiocard with 4 input channels: mono, stereo, 3 , 4. Is there any sdk way to record 4th channel data?
Now I just can record only mono/stereo by AudioRecord.
According to the Android docs for AudioRecord, the only channel input configuration's available specified are AudioFormat.CHANNEL_IN_MONO or AudioFormat.CHANNEL_IN_STEREO.
However, it seems that much higher number of channel outputs are supported according to the AudioFormat docs (or are at least planned to be implemented if not already).
I think that this would be a challenge on its own to implement it yourself, but could be worthwhile doing (I didn't manage to see any obvious solutions for this or any open source code yet). However, an example on the app store does exist, so it is possible via the USB interface according to the USB Audio Recorder Pro App.
JUCE (a mostly audio based C++ library that can compile directly to Android .apk s) seems to be working on this as well but I haven't seen a solution to this yet (maybe in the very near future).
I think you would have to go directly into OpenSL (C++ with JNI) to get the raw audio being received and then pass this back into Java to do whatever you wanted to do with it. Probably worth investigating OpenSL recording through USB devices with something like this to get started.
I know this is a lot of links, but hopefully it will get you started if you did want to implement this functionality (comment a link if you ever did start it).
Otherwise I hope this helped anyways!
Have recorded 4th channel audio. I use tinyalsa through jni. github.com/tinyalsa/tinyalsa
In this way, data is recorded from /dev/snd/pcmC1D0c, but it's needed root access or a+rw rights on pcmC1D0c.

How to play audio by a specific audio device in Android?

I've looked up around for a way to achieve mainly 3 different things with Android:
How to getting listed all the audio output and input devices?
I've found out that there's a method added since API 23 on AudioManager instances according to Android documentation which does this, but there's no documentation about a solution for previous versions. This question remains unanswered here at StackOverflow and several places online.
How to pick up a specific audio output device to stream audio?
The purpose I want to acomplish with this is to select one of the available output devices and stream out the audio by it. Is there a way to do so?
How to record by a specific input device?
The same that before, specific device usage, but in the other way around, I mean, to select one of the available microphones and use it to record audio.
Please help me with some guidence, source code or idea, all of them will be very welcome.

Live Streaming and conversation in the stream

I intend to realise an app and my first trouble is : is it even possible ? Let me explain to you.
The app must allow people to stream live channel that are broadcasting audios. Nothing hard here. The channel's owner can choose which specific people are allowed to listen to his channel (still nothing hard) but also which people can react among them. Yes it must allow people to talk in the audio stream too. In this part it will be like a hangout or skype-like. The stream also has a limited time (1-hour maybe).
So basically the app allow to make live audio stream and people can react in that stream.
Do you guys have any direction or even intuition in the feasibility of such a thing ? Thanks you.

Why it is not possible to play an audio file on a voice call in android

This question might seem to be a repetition of the questions such as following:
How to play an audio file on a voice call in android
Background Audio for a Call in Progress - Possible?
The answers of these questions suggests that it is not possible to play a pre-recorded audio on a voice call in android. I want to know why it is not possible? What is the limitation (hardware/software)? Is it really a limitation or done purposely? Can we alter the source code of android to make it possible?
I think this is a limitation, imposed for security reasons and restricted at the OS level.
Let's analyze the security threat, first of all. If you were able to play custom audio files to the callee, a whole world of cons opens up: you could trick customer supports, you could pretend to be someone else, you could give unauthorized purchase confirmations, and so on. For this reason, neither Android nor iOS allows this functionality.
On Android, you won't be able to do so in a programmatic way, simply because the current APIs won't allow you to do so. It is stated in the official documentation as well, as pointed out here. If you dig into the source code, you can probably enable this feature by accessing the microphone output during a phone call, but that would require running your custom version of Android. A good starting point would be the AudioTrack source, available here.
EDIT: a good example of an audio mod involves enabling the Nexus 5 earpiece as a second loudspeaker (requires root). Can be found here.
After a thorough research, what I have come to know is that there are more than one limitations/hurdles to make it possible. These limitations/hurdles are at three different levels.
First limitation is at API level, because there is no high-level API to play sound files in the conversation audio during a call as mentioned in Android official documentation.
Second limitation is at Radio Interface Layer (RIL). RIL passes on complete control of the call to Radio Daemon (rild) of the Linux library which then further passes the control to the vendor RIL. That means we cannot manipulate voice call in android source code.
Even if we are able to remove these two limitations, we may still not be able to play audio file to an ongoing voice call. Because there is a third limitation. Every vendor has their own library of RIL that communicates with Radio Daemon (rild). This requires that vendor RIL to be open source which is not actually. Hardware vendors do not usually make their device drivers code available.
Detail discussion on this topic is present at this link.
This is software related due to the prioritization of audio routing in Android.
Take a look into the CallManager where you can dig into the method setAudioMode(). After the audio mode was set to MODE_IN_COMMUNICATION the following code is called
audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
From this point on the telephony service has the highest priority and won't let any other audio play in parallel.
Note: You can play back the audio data only to the standard output device. Currently, that is the mobile device speaker or a Bluetooth headset. You cannot play sound files in the conversation audio during a call.
See official link
http://developer.android.com/guide/topics/media/mediaplayer.html
By implementing the AudioManager.OnAudioFocusChangeListener you can get the state of the audiomanager. so by this if any music is playing in the background you can get the AudioManager states(playing and pausing is completely in developer hands) similarly......
Some of the native music players in android device where handling this, they restrict the music when call is in TelephonyManager.EXTRA_STATE_OFFHOOK.so this scenario is also completely in developer hand (whether to handle or not) if he is not handling both will play parallel y

Read an audio stream during (GSM) phone call

Is it possible to read an audio stream during (GSM) phone call? I would like to write an encoding application, and I do not want to go with SIP&VoIP. Thank you.
This will be phone and OS dependent and there are several apps that claim they record audio (Total Recall, Record my call on Android) but they generally seem to record via the microphone meaning the far end sound is poor.
I don't believe either the apple or android api's support access to the raw voice stream today.
Something to be aware of also is that it is not always legal to do this without informing the other party (i.e. the person on the other end of the call that you are planning to 'capture' the voice stream somehow) in many places - this may not be relevant for your particular plans but worth mentioning anyway.
If you have the option of doing the work in the network or on a PABX then you can create a basic (if not very efficient) solution by simply creating a three way (or conference) call.

Categories

Resources