• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "AAudioServiceStreamMMAP"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include <atomic>
22 #include <inttypes.h>
23 #include <iomanip>
24 #include <iostream>
25 #include <stdint.h>
26 
27 #include <utils/String16.h>
28 #include <media/nbaio/AudioStreamOutSink.h>
29 #include <media/MmapStreamInterface.h>
30 
31 #include "binding/AudioEndpointParcelable.h"
32 #include "utility/AAudioUtilities.h"
33 
34 #include "AAudioServiceEndpointMMAP.h"
35 #include "AAudioServiceStreamBase.h"
36 #include "AAudioServiceStreamMMAP.h"
37 #include "SharedMemoryProxy.h"
38 
39 using android::base::unique_fd;
40 using namespace android;
41 using namespace aaudio;
42 
43 /**
44  * Service Stream that uses an MMAP buffer.
45  */
46 
AAudioServiceStreamMMAP(android::AAudioService & aAudioService,bool inService)47 AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
48                                                  bool inService)
49         : AAudioServiceStreamBase(aAudioService)
50         , mInService(inService) {
51 }
52 
53 // Open stream on HAL and pass information about the shared memory buffer back to the client.
open(const aaudio::AAudioStreamRequest & request)54 aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
55 
56     sp<AAudioServiceStreamMMAP> keep(this);
57 
58     if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) {
59         ALOGE("%s() sharingMode mismatch %d", __func__,
60               request.getConstantConfiguration().getSharingMode());
61         return AAUDIO_ERROR_INTERNAL;
62     }
63 
64     aaudio_result_t result = AAudioServiceStreamBase::open(request);
65     if (result != AAUDIO_OK) {
66         return result;
67     }
68 
69     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
70     if (endpoint == nullptr) {
71         ALOGE("%s() has no endpoint", __func__);
72         return AAUDIO_ERROR_INVALID_STATE;
73     }
74 
75     result = endpoint->registerStream(keep);
76     if (result != AAUDIO_OK) {
77         return result;
78     }
79 
80     setState(AAUDIO_STREAM_STATE_OPEN);
81 
82     return AAUDIO_OK;
83 }
84 
85 // Start the flow of data.
startDevice()86 aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
87     aaudio_result_t result = AAudioServiceStreamBase::startDevice();
88     if (!mInService && result == AAUDIO_OK) {
89         // Note that this can sometimes take 200 to 300 msec for a cold start!
90         result = startClient(mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle);
91     }
92     return result;
93 }
94 
95 // Stop the flow of data such that start() can resume with loss of data.
pause_l()96 aaudio_result_t AAudioServiceStreamMMAP::pause_l() {
97     if (!isRunning()) {
98         return AAUDIO_OK;
99     }
100     aaudio_result_t result = AAudioServiceStreamBase::pause_l();
101     // TODO put before base::pause()?
102     if (!mInService) {
103         (void) stopClient(mClientHandle);
104     }
105     return result;
106 }
107 
stop_l()108 aaudio_result_t AAudioServiceStreamMMAP::stop_l() {
109     if (!isRunning()) {
110         return AAUDIO_OK;
111     }
112     aaudio_result_t result = AAudioServiceStreamBase::stop_l();
113     // TODO put before base::stop()?
114     if (!mInService) {
115         (void) stopClient(mClientHandle);
116     }
117     return result;
118 }
119 
standby_l()120 aaudio_result_t AAudioServiceStreamMMAP::standby_l() {
121     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
122     if (endpoint == nullptr) {
123         ALOGE("%s() has no endpoint", __func__);
124         return AAUDIO_ERROR_INVALID_STATE;
125     }
126     aaudio_result_t result = endpoint->standby();
127     if (result == AAUDIO_OK) {
128         setStandby_l(true);
129     }
130     return result;
131 }
132 
exitStandby_l(AudioEndpointParcelable * parcelable)133 aaudio_result_t AAudioServiceStreamMMAP::exitStandby_l(AudioEndpointParcelable* parcelable) {
134     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
135     if (endpoint == nullptr) {
136         ALOGE("%s() has no endpoint", __func__);
137         return AAUDIO_ERROR_INVALID_STATE;
138     }
139     aaudio_result_t result = endpoint->exitStandby(parcelable);
140     if (result == AAUDIO_OK) {
141         setStandby_l(false);
142     } else {
143         ALOGE("%s failed, result %d, disconnecting stream.", __func__, result);
144         disconnect_l();
145     }
146     return result;
147 }
148 
startClient(const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * clientHandle)149 aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
150                                                      const audio_attributes_t *attr,
151                                                      audio_port_handle_t *clientHandle) {
152     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
153     if (endpoint == nullptr) {
154         ALOGE("%s() has no endpoint", __func__);
155         return AAUDIO_ERROR_INVALID_STATE;
156     }
157     // Start the client on behalf of the application. Generate a new porthandle.
158     aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
159     return result;
160 }
161 
stopClient(audio_port_handle_t clientHandle)162 aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
163     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
164     if (endpoint == nullptr) {
165         ALOGE("%s() has no endpoint", __func__);
166         return AAUDIO_ERROR_INVALID_STATE;
167     }
168     aaudio_result_t result = endpoint->stopClient(clientHandle);
169     return result;
170 }
171 
172 // Get free-running DSP or DMA hardware position from the HAL.
getFreeRunningPosition_l(int64_t * positionFrames,int64_t * timeNanos)173 aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition_l(int64_t *positionFrames,
174                                                                   int64_t *timeNanos) {
175     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
176     if (endpoint == nullptr) {
177         ALOGE("%s() has no endpoint", __func__);
178         return AAUDIO_ERROR_INVALID_STATE;
179     }
180     sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
181             static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
182 
183     aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
184     if (result == AAUDIO_OK) {
185         Timestamp timestamp(*positionFrames, *timeNanos);
186         mAtomicStreamTimestamp.write(timestamp);
187         *positionFrames = timestamp.getPosition();
188         *timeNanos = timestamp.getNanoseconds();
189     } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
190         disconnect_l();
191     }
192     return result;
193 }
194 
195 // Get timestamp from presentation position.
196 // If it fails, get timestamp that was written by getFreeRunningPosition()
getHardwareTimestamp_l(int64_t * positionFrames,int64_t * timeNanos)197 aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp_l(int64_t *positionFrames,
198                                                                 int64_t *timeNanos) {
199     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
200     if (endpoint == nullptr) {
201         ALOGE("%s() has no endpoint", __func__);
202         return AAUDIO_ERROR_INVALID_STATE;
203     }
204     sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
205             static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
206 
207     uint64_t position;
208     aaudio_result_t result = serviceEndpointMMAP->getExternalPosition(&position, timeNanos);
209     if (result == AAUDIO_OK) {
210         ALOGV("%s() getExternalPosition() says pos = %" PRIi64 ", time = %" PRIi64,
211                 __func__, position, *timeNanos);
212         *positionFrames = (int64_t) position;
213         return AAUDIO_OK;
214     } else {
215         ALOGV("%s() getExternalPosition() returns error %d", __func__, result);
216     }
217 
218     if (mAtomicStreamTimestamp.isValid()) {
219         Timestamp timestamp = mAtomicStreamTimestamp.read();
220         *positionFrames = timestamp.getPosition();
221         *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
222         return AAUDIO_OK;
223     } else {
224         return AAUDIO_ERROR_UNAVAILABLE;
225     }
226 }
227 
228 // Get an immutable description of the data queue from the HAL.
getAudioDataDescription_l(AudioEndpointParcelable * parcelable)229 aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription_l(
230         AudioEndpointParcelable* parcelable)
231 {
232     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
233     if (endpoint == nullptr) {
234         ALOGE("%s() has no endpoint", __func__);
235         return AAUDIO_ERROR_INVALID_STATE;
236     }
237     sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
238             static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
239     return serviceEndpointMMAP->getDownDataDescription(parcelable);
240 }
241