Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright (C) 2015 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 #ifndef ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_H
     18 #define ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/threads.h>
     24 #include <utils/RefBase.h>
     25 
     26 // for enums
     27 #include "vehicle-network-audio-helper-for-c.h"
     28 
     29 namespace android {
     30 
     31 class VehicleNetworkAudioFocusListener : public virtual RefBase {
     32 public:
     33     virtual void onFocusChange(int32_t activeStreams) = 0;
     34 };
     35 
     36 // ----------------------------------------------------------------------------
     37 
     38 class VehicleNetwork;
     39 class VehicleNetworkListener;
     40 class LocalListener;
     41 
     42 class VehicleNetworkAudioHelper : public VehicleNetworkListener {
     43 public:
     44 
     45     VehicleNetworkAudioHelper(int64_t timeoutNs = FOCUS_WAIT_DEFAULT_TIMEOUT_NS);
     46     VehicleNetworkAudioHelper(int64_t timeoutNs, sp<VehicleNetworkAudioFocusListener> listener);
     47     virtual ~VehicleNetworkAudioHelper();
     48 
     49     status_t init();
     50 
     51     void release();
     52 
     53     void notifyStreamStarted(int32_t stream);
     54     void notifyStreamStopped(int32_t stream);
     55 
     56     vehicle_network_audio_helper_focus_state getStreamFocusState(int32_t stream);
     57 
     58     bool waitForStreamFocus(int32_t stream, nsecs_t waitTimeNs);
     59 
     60     // from VehicleNetworkListener
     61     void onEvents(sp<VehiclePropValueListHolder>& events) override;
     62     void onHalError(int32_t errorCode, int32_t property, int32_t operation) override;
     63     void onHalRestart(bool inMocking) override;
     64     void onPropertySet(const vehicle_prop_value_t& value) override;
     65 private:
     66     void updatePropertiesLocked();
     67 
     68     class StreamState {
     69     public:
     70         int64_t timeoutStartNs;
     71         bool started;
     72         StreamState()
     73          : timeoutStartNs(0),
     74            started(false) { };
     75     };
     76 
     77     StreamState& getStreamStateLocked(int32_t streamNumber);
     78 
     79 private:
     80     const int64_t mTimeoutNs;
     81     sp<VehicleNetworkAudioFocusListener> mListener;
     82     Mutex mLock;
     83     Condition mFocusWait;
     84     sp<VehicleNetwork> mService;
     85     bool mHasFocusProperty;
     86     int32_t mAllowedStreams;
     87     vehicle_prop_value_t mScratchValueFocus;
     88     vehicle_prop_value_t mScratchValueStreamState;
     89     Vector<StreamState> mStreamStates;
     90 };
     91 
     92 }; // namespace android
     93 #endif /*ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_H*/
     94