Home | History | Annotate | Download | only in itf
      1 /*
      2  * Copyright (C) 2010 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 
     17 /* PlaybackRate implementation */
     18 
     19 #include "sles_allinclusive.h"
     20 
     21 
     22 static SLresult IPlaybackRate_SetRate(SLPlaybackRateItf self, SLpermille rate)
     23 {
     24     SL_ENTER_INTERFACE
     25 
     26     IPlaybackRate *thiz = (IPlaybackRate *) self;
     27     // const, so no lock needed
     28     if (!(thiz->mMinRate <= rate && rate <= thiz->mMaxRate)) {
     29         result = SL_RESULT_PARAMETER_INVALID;
     30     } else {
     31         interface_lock_exclusive(thiz);
     32 #ifdef ANDROID
     33         CAudioPlayer *ap = (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) ?
     34                 (CAudioPlayer *) thiz->mThis : NULL;
     35         if (NULL != ap) {
     36             result = android_audioPlayer_setPlaybackRateAndConstraints(ap, rate, thiz->mProperties);
     37         } else {
     38             result = SL_RESULT_FEATURE_UNSUPPORTED;
     39         }
     40 #else
     41         result = SL_RESULT_SUCCESS;
     42 #endif
     43         if (SL_RESULT_SUCCESS == result) {
     44             thiz->mRate = rate;
     45         }
     46         interface_unlock_exclusive(thiz);
     47     }
     48 
     49     SL_LEAVE_INTERFACE
     50 }
     51 
     52 
     53 static SLresult IPlaybackRate_GetRate(SLPlaybackRateItf self, SLpermille *pRate)
     54 {
     55     SL_ENTER_INTERFACE
     56 
     57     if (NULL == pRate) {
     58         result = SL_RESULT_PARAMETER_INVALID;
     59     } else {
     60         IPlaybackRate *thiz = (IPlaybackRate *) self;
     61         interface_lock_shared(thiz);
     62         SLpermille rate = thiz->mRate;
     63         interface_unlock_shared(thiz);
     64         *pRate = rate;
     65         result = SL_RESULT_SUCCESS;
     66     }
     67 
     68     SL_LEAVE_INTERFACE
     69 }
     70 
     71 
     72 static SLresult IPlaybackRate_SetPropertyConstraints(SLPlaybackRateItf self, SLuint32 constraints)
     73 {
     74     SL_ENTER_INTERFACE
     75 
     76     IPlaybackRate *thiz = (IPlaybackRate *) self;
     77     if (constraints & ~(SL_RATEPROP_SILENTAUDIO | SL_RATEPROP_STAGGEREDAUDIO |
     78             SL_RATEPROP_NOPITCHCORAUDIO | SL_RATEPROP_PITCHCORAUDIO)) {
     79         result = SL_RESULT_PARAMETER_INVALID;
     80     // const, so no lock needed
     81     } else if (!(thiz->mCapabilities & constraints)) {
     82         result = SL_RESULT_FEATURE_UNSUPPORTED;
     83     } else {
     84         interface_lock_exclusive(thiz);
     85 #ifdef ANDROID
     86         // verify property support before storing
     87         CAudioPlayer *ap = (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) ?
     88                 (CAudioPlayer *) thiz->mThis : NULL;
     89         if (NULL != ap) {
     90             result = android_audioPlayer_setPlaybackRateAndConstraints(ap, thiz->mRate,
     91                     constraints);
     92         } else {
     93             result = SL_RESULT_FEATURE_UNSUPPORTED;
     94         }
     95 #else
     96         result = SL_RESULT_SUCCESS;
     97 #endif
     98         if (result == SL_RESULT_SUCCESS) {
     99             thiz->mProperties = constraints;
    100         }
    101         interface_unlock_exclusive(thiz);
    102     }
    103 
    104     SL_LEAVE_INTERFACE
    105 }
    106 
    107 
    108 static SLresult IPlaybackRate_GetProperties(SLPlaybackRateItf self, SLuint32 *pProperties)
    109 {
    110     SL_ENTER_INTERFACE
    111 
    112     if (NULL == pProperties) {
    113         result = SL_RESULT_PARAMETER_INVALID;
    114     } else {
    115         IPlaybackRate *thiz = (IPlaybackRate *) self;
    116         interface_lock_shared(thiz);
    117         SLuint32 properties = thiz->mProperties;
    118         interface_unlock_shared(thiz);
    119         *pProperties = properties;
    120         result = SL_RESULT_SUCCESS;
    121     }
    122 
    123     SL_LEAVE_INTERFACE
    124 }
    125 
    126 
    127 static SLresult IPlaybackRate_GetCapabilitiesOfRate(SLPlaybackRateItf self,
    128     SLpermille rate, SLuint32 *pCapabilities)
    129 {
    130     SL_ENTER_INTERFACE
    131 
    132     if (NULL == pCapabilities) {
    133         result = SL_RESULT_PARAMETER_INVALID;
    134     } else {
    135         IPlaybackRate *thiz = (IPlaybackRate *) self;
    136         SLuint32 capabilities;
    137         // const, so no lock needed
    138         if (!(thiz->mMinRate <= rate && rate <= thiz->mMaxRate)) {
    139             capabilities = 0;
    140             result = SL_RESULT_PARAMETER_INVALID;
    141         } else {
    142             capabilities = thiz->mCapabilities;
    143             result = SL_RESULT_SUCCESS;
    144         }
    145         *pCapabilities = capabilities;
    146     }
    147 
    148     SL_LEAVE_INTERFACE
    149 }
    150 
    151 
    152 static SLresult IPlaybackRate_GetRateRange(SLPlaybackRateItf self, SLuint8 index,
    153     SLpermille *pMinRate, SLpermille *pMaxRate, SLpermille *pStepSize, SLuint32 *pCapabilities)
    154 {
    155     SL_ENTER_INTERFACE
    156 
    157     // only one range
    158     if (NULL == pMinRate || NULL == pMaxRate || NULL == pStepSize || NULL == pCapabilities ||
    159         (0 < index)) {
    160         result = SL_RESULT_PARAMETER_INVALID;
    161     } else {
    162         IPlaybackRate *thiz = (IPlaybackRate *) self;
    163         // const, so no lock needed
    164         SLpermille minRate = thiz->mMinRate;
    165         SLpermille maxRate = thiz->mMaxRate;
    166         SLpermille stepSize = thiz->mStepSize;
    167         SLuint32 capabilities = thiz->mCapabilities;
    168         *pMinRate = minRate;
    169         *pMaxRate = maxRate;
    170         *pStepSize = stepSize;
    171         *pCapabilities = capabilities;
    172         result = SL_RESULT_SUCCESS;
    173     }
    174 
    175     SL_LEAVE_INTERFACE
    176 }
    177 
    178 
    179 static const struct SLPlaybackRateItf_ IPlaybackRate_Itf = {
    180     IPlaybackRate_SetRate,
    181     IPlaybackRate_GetRate,
    182     IPlaybackRate_SetPropertyConstraints,
    183     IPlaybackRate_GetProperties,
    184     IPlaybackRate_GetCapabilitiesOfRate,
    185     IPlaybackRate_GetRateRange
    186 };
    187 
    188 void IPlaybackRate_init(void *self)
    189 {
    190     IPlaybackRate *thiz = (IPlaybackRate *) self;
    191     thiz->mItf = &IPlaybackRate_Itf;
    192     thiz->mProperties = SL_RATEPROP_NOPITCHCORAUDIO;
    193     thiz->mRate = 1000;
    194     // const after initialization; these are default values which may be overwritten
    195     // during object creation but will not be modified after that
    196     // (e.g. for an Android AudioPlayer, see sles_to_android_audioPlayerCreate)
    197     thiz->mMinRate = 1000;
    198     thiz->mMaxRate = 1000;
    199     thiz->mStepSize = 0;
    200     thiz->mCapabilities = SL_RATEPROP_NOPITCHCORAUDIO;
    201 }
    202