android.media.AudioRecordingConfiguration Java Examples

The following examples show how to use android.media.AudioRecordingConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MediaRecorderMonitor.java    From talkback with Apache License 2.0 6 votes vote down vote up
public MediaRecorderMonitor(Context context) {
  audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  if (BuildVersionUtils.isAtLeastN()) {
    audioRecordingCallback =
        new AudioRecordingCallback() {
          @Override
          public void onRecordingConfigChanged(List<AudioRecordingConfiguration> configs) {
            super.onRecordingConfigChanged(configs);
            isVoiceRecognitionActive =
                containsAudioSource(configs, AudioSource.VOICE_RECOGNITION);
            final boolean isRecording = containsAudioSources(configs);
            if (!MediaRecorderMonitor.this.isRecording && isRecording) {
              listener.onMicrophoneActivated();
            }
            MediaRecorderMonitor.this.isRecording = isRecording;
          }
        };
  } else {
    audioRecordingCallback = null;
  }
}
 
Example #2
Source File: MediaRecorderMonitor.java    From talkback with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
private boolean containsAudioSources(List<AudioRecordingConfiguration> configs) {
  if (configs == null) {
    return false;
  }
  // Try to find a target audio source in the recording configurations.
  for (AudioSource source : AudioSource.values()) {
    int sourceId = source.getId();
    for (AudioRecordingConfiguration config : configs) {
      if (sourceId == config.getClientAudioSource()) {
        return true;
      }
    }
  }
  return false;
}
 
Example #3
Source File: RecordingActivityMonitor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of android.media.AudioSystem.AudioRecordingCallback
 */
public void onRecordingConfigurationChanged(int event, int uid, int session, int source,
        int[] recordingInfo, String packName) {
    if (MediaRecorder.isSystemOnlyAudioSource(source)) {
        return;
    }
    final List<AudioRecordingConfiguration> configsSystem =
            updateSnapshot(event, uid, session, source, recordingInfo);
    if (configsSystem != null){
        synchronized (mClients) {
            // list of recording configurations for "public consumption". It is only computed if
            // there are non-system recording activity listeners.
            final List<AudioRecordingConfiguration> configsPublic = mHasPublicClients ?
                    anonymizeForPublicConsumption(configsSystem) :
                        new ArrayList<AudioRecordingConfiguration>();
            final Iterator<RecMonitorClient> clientIterator = mClients.iterator();
            while (clientIterator.hasNext()) {
                final RecMonitorClient rmc = clientIterator.next();
                try {
                    if (rmc.mIsPrivileged) {
                        rmc.mDispatcherCb.dispatchRecordingConfigChange(configsSystem);
                    } else {
                        rmc.mDispatcherCb.dispatchRecordingConfigChange(configsPublic);
                    }
                } catch (RemoteException e) {
                    Log.w(TAG, "Could not call dispatchRecordingConfigChange() on client", e);
                }
            }
        }
    }
}
 
Example #4
Source File: RecordingActivityMonitor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
protected void dump(PrintWriter pw) {
    // players
    pw.println("\nRecordActivityMonitor dump time: "
            + DateFormat.getTimeInstance().format(new Date()));
    synchronized(mRecordConfigs) {
        for (AudioRecordingConfiguration conf : mRecordConfigs.values()) {
            conf.dump(pw);
        }
    }
    pw.println("\n");
    // log
    sEventLogger.dump(pw);
}
 
Example #5
Source File: RecordingActivityMonitor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private ArrayList<AudioRecordingConfiguration> anonymizeForPublicConsumption(
        List<AudioRecordingConfiguration> sysConfigs) {
    ArrayList<AudioRecordingConfiguration> publicConfigs =
            new ArrayList<AudioRecordingConfiguration>();
    // only add active anonymized configurations,
    for (AudioRecordingConfiguration config : sysConfigs) {
        publicConfigs.add(AudioRecordingConfiguration.anonymizedCopy(config));
    }
    return publicConfigs;
}
 
Example #6
Source File: RecordingActivityMonitor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
List<AudioRecordingConfiguration> getActiveRecordingConfigurations(boolean isPrivileged) {
    synchronized(mRecordConfigs) {
        if (isPrivileged) {
            return new ArrayList<AudioRecordingConfiguration>(mRecordConfigs.values());
        } else {
            final List<AudioRecordingConfiguration> configsPublic =
                    anonymizeForPublicConsumption(
                        new ArrayList<AudioRecordingConfiguration>(mRecordConfigs.values()));
            return configsPublic;
        }
    }
}
 
Example #7
Source File: SpeechControllerImpl.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Called when transitioning from an idle state to a speaking state, e.g. the queue was empty,
 * there was no current speech, a speech item was added to the queue, and {@link #resume()} is
 * called when status is {@link #STATUS_PAUSE} .
 *
 * @see #handleSpeechCompleted(int status)
 */
@TargetApi(Build.VERSION_CODES.N)
private void handleSpeechStarting() {
  for (SpeechController.Observer observer : mObservers) {
    observer.onSpeechStarting();
  }

  boolean useAudioFocus = mUseAudioFocus;
  if (BuildVersionUtils.isAtLeastN()) {
    List<AudioRecordingConfiguration> recordConfigurations =
        mAudioManager.getActiveRecordingConfigurations();
    if (recordConfigurations.size() != 0) {
      useAudioFocus = false;
    }
  }

  if (useAudioFocus) {
    if (BuildVersionUtils.isAtLeastO()) {
      mAudioManager.requestAudioFocus(mAudioFocusRequest);
    } else {
      mAudioManager.requestAudioFocus(
          mAudioFocusListener, DEFAULT_STREAM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
    }
  }

  if (mIsSpeaking) {
    LogUtils.e(TAG, "Started speech while already speaking!");
  }

  mIsSpeaking = true;
}
 
Example #8
Source File: MediaRecorderMonitor.java    From talkback with Apache License 2.0 5 votes vote down vote up
private boolean containsAudioSource(
    List<AudioRecordingConfiguration> configs, AudioSource targetSource) {
  if (configs == null || targetSource == null) {
    return false;
  }
  for (AudioRecordingConfiguration config : configs) {
    if (config.getClientAudioSource() == targetSource.getId()) {
      return true;
    }
  }
  return false;
}
 
Example #9
Source File: RecordingActivityMonitor.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Update the internal "view" of the active recording sessions
 * @param event
 * @param session
 * @param source
 * @param recordingFormat see
 *     {@link AudioSystem.AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int[])}
 *     for the definition of the contents of the array
 * @return null if the list of active recording sessions has not been modified, a list
 *     with the current active configurations otherwise.
 */
private List<AudioRecordingConfiguration> updateSnapshot(int event, int uid, int session,
        int source, int[] recordingInfo) {
    final boolean configChanged;
    final ArrayList<AudioRecordingConfiguration> configs;
    synchronized(mRecordConfigs) {
        switch (event) {
        case AudioManager.RECORD_CONFIG_EVENT_STOP:
            // return failure if an unknown recording session stopped
            configChanged = (mRecordConfigs.remove(new Integer(session)) != null);
            if (configChanged) {
                sEventLogger.log(new RecordingEvent(event, uid, session, source, null));
            }
            break;
        case AudioManager.RECORD_CONFIG_EVENT_START:
            final AudioFormat clientFormat = new AudioFormat.Builder()
                    .setEncoding(recordingInfo[0])
                    // FIXME this doesn't support index-based masks
                    .setChannelMask(recordingInfo[1])
                    .setSampleRate(recordingInfo[2])
                    .build();
            final AudioFormat deviceFormat = new AudioFormat.Builder()
                    .setEncoding(recordingInfo[3])
                    // FIXME this doesn't support index-based masks
                    .setChannelMask(recordingInfo[4])
                    .setSampleRate(recordingInfo[5])
                    .build();
            final int patchHandle = recordingInfo[6];
            final Integer sessionKey = new Integer(session);

            final String[] packages = mPackMan.getPackagesForUid(uid);
            final String packageName;
            if (packages != null && packages.length > 0) {
                packageName = packages[0];
            } else {
                packageName = "";
            }
            final AudioRecordingConfiguration updatedConfig =
                    new AudioRecordingConfiguration(uid, session, source,
                            clientFormat, deviceFormat, patchHandle, packageName);

            if (mRecordConfigs.containsKey(sessionKey)) {
                if (updatedConfig.equals(mRecordConfigs.get(sessionKey))) {
                    configChanged = false;
                } else {
                    // config exists but has been modified
                    mRecordConfigs.remove(sessionKey);
                    mRecordConfigs.put(sessionKey, updatedConfig);
                    configChanged = true;
                }
            } else {
                mRecordConfigs.put(sessionKey, updatedConfig);
                configChanged = true;
            }
            if (configChanged) {
                sEventLogger.log(new RecordingEvent(event, uid, session, source, packageName));
            }
            break;
        default:
            Log.e(TAG, String.format("Unknown event %d for session %d, source %d",
                    event, session, source));
            configChanged = false;
        }
        if (configChanged) {
            configs = new ArrayList<AudioRecordingConfiguration>(mRecordConfigs.values());
        } else {
            configs = null;
        }
    }
    return configs;
}