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     virtual void onEvents(sp<VehiclePropValueListHolder>& events) ;
62     virtual void onHalError(int32_t errorCode, int32_t property, int32_t operation);
63     virtual void onHalRestart(bool inMocking);
64 private:
65     void updatePropertiesLocked();
66 
67     class StreamState {
68     public:
69         int64_t timeoutStartNs;
70         bool started;
StreamState()71         StreamState()
72          : timeoutStartNs(0),
73            started(false) { };
74     };
75 
76     StreamState& getStreamStateLocked(int32_t streamNumber);
77 
78 private:
79     const int64_t mTimeoutNs;
80     sp<VehicleNetworkAudioFocusListener> mListener;
81     Mutex mLock;
82     Condition mFocusWait;
83     sp<VehicleNetwork> mService;
84     bool mHasFocusProperty;
85     int32_t mAllowedStreams;
86     vehicle_prop_value_t mScratchValueFocus;
87     vehicle_prop_value_t mScratchValueStreamState;
88     Vector<StreamState> mStreamStates;
89 };
90 
91 }; // namespace android
92 #endif /*ANDROID_VEHICLE_NETWORK_AUDIO_HELPER_H*/
93