1 /* AudioUsbALSA.h
2 
3 Copyright (c) 2012, Code Aurora Forum. All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above
11       copyright notice, this list of conditions and the following
12       disclaimer in the documentation and/or other materials provided
13       with the distribution.
14     * Neither the name of Code Aurora Forum, Inc. nor the names of its
15       contributors may be used to endorse or promote products derived
16       from this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
21 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
29 
30 #ifndef ANDROID_AUDIO_USB_ALSA_H
31 #define ANDROID_AUDIO_USB_ALSA_H
32 
33 #include <utils/List.h>
34 #include <hardware_legacy/AudioHardwareBase.h>
35 
36 #include <hardware_legacy/AudioHardwareInterface.h>
37 #include <hardware_legacy/AudioSystemLegacy.h>
38 #include <system/audio.h>
39 #include <hardware/audio.h>
40 #include <utils/threads.h>
41 
42 #define DEFAULT_BUFFER_SIZE   2048
43 #define POLL_TIMEOUT   3000
44 #define DEFAULT_CHANNEL_MODE  2
45 #define CHANNEL_MODE_ONE  1
46 #define PROXY_DEFAULT_SAMPLING_RATE 48000
47 #define SIGNAL_EVENT_TIMEOUT 1
48 #define SIGNAL_EVENT_KILLTHREAD 2
49 
50 #define BUFFSIZE 1000000
51 
52 #define PATH "/proc/asound/card1/stream0"
53 
54 extern "C" {
55    #include <sound/asound.h>
56    #include "alsa_audio.h"
57    #include "msm8960_use_cases.h"
58 }
59 
60 #include <hardware/hardware.h>
61 
62 namespace android_audio_legacy
63 {
64 using android::List;
65 using android::Mutex;
66 class AudioUsbALSA;
67 
68 class AudioUsbALSA
69 {
70 private:
71     int mproxypfdPlayback;
72     int musbpfdPlayback;
73     int mnfdsPlayback;
74     int mnfdsRecording;
75     int mtimeOut;
76     int mtimeOutRecording;
77     struct pcm *mproxyRecordingHandle;
78     struct pcm *musbRecordingHandle;
79     struct pcm *mproxyPlaybackHandle;
80     struct pcm *musbPlaybackHandle;
81     u_int8_t *mdstUsb_addr;
82     u_int8_t *msrcProxy_addr;
83     bool mkillPlayBackThread;
84     bool mkillRecordingThread;
85     pthread_t mPlaybackUsb;
86     pthread_t mRecordingUsb;
87     snd_use_case_mgr_t *mUcMgr;
88 
89     //Helper functions
90     struct pcm * configureDevice(unsigned flags, char* hw, int sampleRate, int channelCount, int periodSize, bool playback);
91     status_t syncPtr(struct pcm *handle, bool *killThread);
92 
93     //playback
94     void pollForProxyData();
95     void pollForUsbData();
96 
97     //recording
98     void pollForUsbDataForRecording();
99     void pollForProxyDataForRecording();
100 
101     status_t startDevice(pcm *handle, bool *killThread);
102 
103     void PlaybackThreadEntry();
104     static void *PlaybackThreadWrapper(void *me);
105 
106     void RecordingThreadEntry();
107     static void *RecordingThreadWrapper(void *me);
108 
109     status_t setHardwareParams(pcm *local_handle, uint32_t sampleRate, uint32_t channels, int periodSize);
110 
111     status_t setSoftwareParams(pcm *pcm, bool playback);
112 
113     status_t closeDevice(pcm *handle);
114 
115     status_t getCap(char * type, int &channels, int &sampleRate);
116     int         getnumOfRates(char *rateStr);
117     int         mchannelsPlayback;
118     int         msampleRatePlayback;
119     int         mchannelsCapture;
120     int         msampleRateCapture;
121 
122 public:
123     AudioUsbALSA();
124     virtual            ~AudioUsbALSA();
125 
126     void exitPlaybackThread(uint64_t writeVal);
127     void exitRecordingThread(uint64_t writeVal);
128     void setkillUsbRecordingThread(bool val);
getkillUsbPlaybackThread()129     bool getkillUsbPlaybackThread() {
130         return mkillPlayBackThread;
131     }
getkillUsbRecordingThread()132     bool getkillUsbRecordingThread() {
133         return mkillRecordingThread;
134     }
135     //Playback
136     void startPlayback();
137 
138     //Capture
139     void startRecording();
140 };
141 
142 };        // namespace android_audio_legacy
143 #endif    // ANDROID_AUDIO_USB_ALSA_H
144