AudioTrack acquisition process to modify the StreamType of the specified app

In order to specifically differentiate the application scenarios of the android car device from the mobile terminal device, some special audio streams need to be forcibly converted at the system framework layer. In response to this demand, we can force changes in the audio data stream production.

First look at the
source code system/media/audio/include/system/audio-base.h of how many Audio StreamTypes the system defines

typedef enum {
    
    
    AUDIO_STREAM_DEFAULT = -1, // (-1)
    AUDIO_STREAM_MIN = 0,
    AUDIO_STREAM_VOICE_CALL = 0,
    AUDIO_STREAM_SYSTEM = 1,
    AUDIO_STREAM_RING = 2,
    AUDIO_STREAM_MUSIC = 3,
    AUDIO_STREAM_ALARM = 4,
    AUDIO_STREAM_NOTIFICATION = 5,
    AUDIO_STREAM_BLUETOOTH_SCO = 6,
    AUDIO_STREAM_ENFORCED_AUDIBLE = 7,
    AUDIO_STREAM_DTMF = 8,
    AUDIO_STREAM_TTS = 9,
    AUDIO_STREAM_ACCESSIBILITY = 10,
    AUDIO_STREAM_REROUTING = 11,
    AUDIO_STREAM_PATCH = 12,
    AUDIO_STREAM_PUBLIC_CNT = 11, // (ACCESSIBILITY + 1)
    AUDIO_STREAM_FOR_POLICY_CNT = 12, // PATCH
    AUDIO_STREAM_CNT = 13, // (PATCH + 1)
} audio_stream_type_t;

Let’s take the navigation app as an example. The data stream level of the navigation application scene in the car is higher than ordinary media playback, so we force the music type set by the navigation app’s default setting to be an alarm stream, so as to prepare for the subsequent car audio strategy Implementation.

AudioTrack, the producer of audio data, can help us. Simply describe the idea and directly upload the code, and directly get the process of the corresponding navigation app. The current process is active (navigation is at the time of tts voice broadcast), and it is forcibly modified to AUDIO_STREAM_ALARM.

frameworks\av\media\libaudioclient\AudioTrack.cpp
increase function acquisition

static int get_pack_from_cmdline(int pid, char *buf, int len) {
    
    
   char filename[256];
   int n = 0;
   int fd;
   if (pid < 1 || buf == NULL || len < 256) {
    
    
       return -1;
   }
   memset(filename, 0, 256);
   sprintf(filename, "/proc/%d/cmdline", pid);
   fd = open(filename, O_RDONLY);
   if (fd < 0) {
    
    
       perror("open:");
       return -1;
   }
   int ret = read(fd, buf, len);
   close(fd);
   memset(filename, 0, 256);
   while (1) {
    
    
       if (buf[n] == ' '||buf[n] == ':')
           break;
       filename[n] = buf[n];
       n++;
       if (n == ret)
           break;
   }
   memset(buf, 0, len);
   memcpy(buf, filename, n + 1);
   return ret;
}

AudioTrack initially sets the set function of the playback parameter to implement the change of stream type

  status_t AudioTrack::set(
        audio_stream_type_t streamType,
        uint32_t sampleRate,
        audio_format_t format,
        audio_channel_mask_t channelMask,
        size_t frameCount,
        audio_output_flags_t flags,
        callback_t cbf,
        void* user,
        int32_t notificationFrames,
        const sp<IMemory>& sharedBuffer,
        bool threadCanCallJava,
        audio_session_t sessionId,
        transfer_type transferType,
        const audio_offload_info_t *offloadInfo,
        uid_t uid,
        pid_t pid,
        const audio_attributes_t* pAttributes,
        bool doNotReconnect,
        float maxRequiredSpeed)
{
    
    
    MTK_ALOGI("set(): %p, streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
          "flags #%x, notificationFrames %d, sessionId %d, transferType %d, uid %d, pid %d",
          this, streamType, sampleRate, format, channelMask, frameCount, flags,
          notificationFrames, sessionId, transferType, uid, pid);
    ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
          "flags #%x, notificationFrames %d, sessionId %d, transferType %d, uid %d, pid %d",
          streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
          sessionId, transferType, uid, pid);
...
...

   //获取进程
	char pidName[256];
	memset(pidName, 0, 256);
	get_pack_from_cmdline(getpid(), pidName, 256);
	if (strcmp(pidName,"具体的导航app进程名,eg.高德com.autonavi.amapauto")== 0){
    
    
		mSteamType = AUDIO_STREAM_ALARM ;
	}

In this way, the audio data stream of navigation can be distinguished from the data stream of media playback, so as to realize the audio strategy on the car.

Guess you like

Origin blog.csdn.net/jeephao/article/details/107302285