Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #ifndef AUDIO_POLICY_HELPER_H_
     17 #define AUDIO_POLICY_HELPER_H_
     18 
     19 #include <system/audio.h>
     20 
     21 static inline
     22 audio_stream_type_t audio_attributes_to_stream_type(const audio_attributes_t *attr)
     23 {
     24     // flags to stream type mapping
     25     if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
     26         return AUDIO_STREAM_ENFORCED_AUDIBLE;
     27     }
     28     if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
     29         return AUDIO_STREAM_BLUETOOTH_SCO;
     30     }
     31 
     32     // usage to stream type mapping
     33     switch (attr->usage) {
     34     case AUDIO_USAGE_MEDIA:
     35     case AUDIO_USAGE_GAME:
     36     case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
     37     case AUDIO_USAGE_ASSISTANT:
     38         return AUDIO_STREAM_MUSIC;
     39     case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
     40         return AUDIO_STREAM_ACCESSIBILITY;
     41     case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
     42         return AUDIO_STREAM_SYSTEM;
     43     case AUDIO_USAGE_VOICE_COMMUNICATION:
     44         return AUDIO_STREAM_VOICE_CALL;
     45 
     46     case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
     47         return AUDIO_STREAM_DTMF;
     48 
     49     case AUDIO_USAGE_ALARM:
     50         return AUDIO_STREAM_ALARM;
     51     case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
     52         return AUDIO_STREAM_RING;
     53 
     54     case AUDIO_USAGE_NOTIFICATION:
     55     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
     56     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
     57     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
     58     case AUDIO_USAGE_NOTIFICATION_EVENT:
     59         return AUDIO_STREAM_NOTIFICATION;
     60 
     61     case AUDIO_USAGE_UNKNOWN:
     62     default:
     63         return AUDIO_STREAM_MUSIC;
     64     }
     65 }
     66 
     67 static inline
     68 void stream_type_to_audio_attributes(audio_stream_type_t streamType,
     69                                      audio_attributes_t *attr) {
     70     memset(attr, 0, sizeof(audio_attributes_t));
     71 
     72     switch (streamType) {
     73     case AUDIO_STREAM_DEFAULT:
     74     case AUDIO_STREAM_MUSIC:
     75         attr->content_type = AUDIO_CONTENT_TYPE_MUSIC;
     76         attr->usage = AUDIO_USAGE_MEDIA;
     77         break;
     78     case AUDIO_STREAM_VOICE_CALL:
     79         attr->content_type = AUDIO_CONTENT_TYPE_SPEECH;
     80         attr->usage = AUDIO_USAGE_VOICE_COMMUNICATION;
     81         break;
     82     case AUDIO_STREAM_ENFORCED_AUDIBLE:
     83         attr->flags  |= AUDIO_FLAG_AUDIBILITY_ENFORCED;
     84         // intended fall through, attributes in common with STREAM_SYSTEM
     85     case AUDIO_STREAM_SYSTEM:
     86         attr->content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
     87         attr->usage = AUDIO_USAGE_ASSISTANCE_SONIFICATION;
     88         break;
     89     case AUDIO_STREAM_RING:
     90         attr->content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
     91         attr->usage = AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE;
     92         break;
     93     case AUDIO_STREAM_ALARM:
     94         attr->content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
     95         attr->usage = AUDIO_USAGE_ALARM;
     96         break;
     97     case AUDIO_STREAM_NOTIFICATION:
     98         attr->content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
     99         attr->usage = AUDIO_USAGE_NOTIFICATION;
    100         break;
    101     case AUDIO_STREAM_BLUETOOTH_SCO:
    102         attr->content_type = AUDIO_CONTENT_TYPE_SPEECH;
    103         attr->usage = AUDIO_USAGE_VOICE_COMMUNICATION;
    104         attr->flags |= AUDIO_FLAG_SCO;
    105         break;
    106     case AUDIO_STREAM_DTMF:
    107         attr->content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
    108         attr->usage = AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING;
    109         break;
    110     case AUDIO_STREAM_TTS:
    111         attr->content_type = AUDIO_CONTENT_TYPE_SPEECH;
    112         attr->usage = AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY;
    113         break;
    114     default:
    115         ALOGE("invalid stream type %d when converting to attributes", streamType);
    116     }
    117 }
    118 
    119 #endif //AUDIO_POLICY_HELPER_H_
    120