OSDN Git Service

Write correct number of bytes per attempt.
[android-x86/hardware-alsa_sound.git] / AudioStreamOutALSA.cpp
1 /* AudioStreamOutALSA.cpp
2  **
3  ** Copyright 2008-2009 Wind River Systems
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17
18 #include <errno.h>
19 #include <stdarg.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <dlfcn.h>
25
26 #define LOG_TAG "AudioHardwareALSA"
27 #include <utils/Log.h>
28 #include <utils/String8.h>
29
30 #include <cutils/properties.h>
31 #include <media/AudioRecord.h>
32 #include <hardware_legacy/power.h>
33
34 #include "AudioHardwareALSA.h"
35
36 #ifndef ALSA_DEFAULT_SAMPLE_RATE
37 #define ALSA_DEFAULT_SAMPLE_RATE 44100 // in Hz
38 #endif
39
40 namespace android
41 {
42
43 // ----------------------------------------------------------------------------
44
45 static const int DEFAULT_SAMPLE_RATE = ALSA_DEFAULT_SAMPLE_RATE;
46
47 // ----------------------------------------------------------------------------
48
49 AudioStreamOutALSA::AudioStreamOutALSA(AudioHardwareALSA *parent, alsa_handle_t *handle) :
50     ALSAStreamOps(parent, handle)
51 {
52 }
53
54 AudioStreamOutALSA::~AudioStreamOutALSA()
55 {
56     close();
57 }
58
59 uint32_t AudioStreamOutALSA::channels() const
60 {
61     int c = ALSAStreamOps::channels();
62     return c;
63 }
64
65 status_t AudioStreamOutALSA::setVolume(float left, float right)
66 {
67     return mixer()->setVolume (mHandle->curDev, left, right);
68 }
69
70 ssize_t AudioStreamOutALSA::write(const void *buffer, size_t bytes)
71 {
72     AutoMutex lock(mLock);
73
74     if (!mPowerLock) {
75         acquire_wake_lock (PARTIAL_WAKE_LOCK, "AudioOutLock");
76         mPowerLock = true;
77     }
78
79     acoustic_device_t *aDev = acoustics();
80
81     // For output, we will pass the data on to the acoustics module, but the actual
82     // data is expected to be sent to the audio device directly as well.
83     if (aDev && aDev->write)
84         aDev->write(aDev, buffer, bytes);
85
86     snd_pcm_sframes_t n;
87     size_t            sent = 0;
88     status_t          err;
89
90     do {
91         n = snd_pcm_writei(mHandle->handle,
92                            (char *)buffer + sent,
93                            snd_pcm_bytes_to_frames(mHandle->handle, bytes - sent));
94         if (n == -EBADFD) {
95             // Somehow the stream is in a bad state. The driver probably
96             // has a bug and snd_pcm_recover() doesn't seem to handle this.
97             mHandle->module->open(mHandle, mHandle->curDev, mHandle->curMode);
98
99             if (aDev && aDev->recover) aDev->recover(aDev, n);
100         }
101         else if (n < 0) {
102             if (mHandle->handle) {
103                 // snd_pcm_recover() will return 0 if successful in recovering from
104                 // an error, or -errno if the error was unrecoverable.
105                 n = snd_pcm_recover(mHandle->handle, n, 1);
106
107                 if (aDev && aDev->recover) aDev->recover(aDev, n);
108
109                 if (n) return static_cast<ssize_t>(n);
110             }
111         }
112         else
113             sent += static_cast<ssize_t>(snd_pcm_frames_to_bytes(mHandle->handle, n));
114
115     } while (mHandle->handle && sent < bytes);
116
117     return sent;
118 }
119
120 status_t AudioStreamOutALSA::dump(int fd, const Vector<String16>& args)
121 {
122     return NO_ERROR;
123 }
124
125 status_t AudioStreamOutALSA::open(int mode)
126 {
127     AutoMutex lock(mLock);
128
129     return ALSAStreamOps::open(mode);
130 }
131
132 status_t AudioStreamOutALSA::close()
133 {
134     AutoMutex lock(mLock);
135
136     snd_pcm_drain (mHandle->handle);
137     close();
138
139     if (mPowerLock) {
140         release_wake_lock ("AudioOutLock");
141         mPowerLock = false;
142     }
143
144     return NO_ERROR;
145 }
146
147 status_t AudioStreamOutALSA::standby()
148 {
149     AutoMutex lock(mLock);
150
151     snd_pcm_drain (mHandle->handle);
152
153     if (mPowerLock) {
154         release_wake_lock ("AudioOutLock");
155         mPowerLock = false;
156     }
157
158     return NO_ERROR;
159 }
160
161 #define USEC_TO_MSEC(x) ((x + 999) / 1000)
162
163 uint32_t AudioStreamOutALSA::latency() const
164 {
165     // Android wants latency in milliseconds.
166     return USEC_TO_MSEC (mHandle->latency);
167 }
168
169 }       // namespace android