• 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 <iomanip>
23 #include <iostream>
24 #include <stdint.h>
25 
26 #include <utils/String16.h>
27 #include <media/nbaio/AudioStreamOutSink.h>
28 #include <media/MmapStreamInterface.h>
29 
30 #include "binding/AudioEndpointParcelable.h"
31 #include "utility/AAudioUtilities.h"
32 
33 #include "AAudioServiceEndpointMMAP.h"
34 #include "AAudioServiceStreamBase.h"
35 #include "AAudioServiceStreamMMAP.h"
36 #include "SharedMemoryProxy.h"
37 
38 using android::base::unique_fd;
39 using namespace android;
40 using namespace aaudio;
41 
42 /**
43  * Service Stream that uses an MMAP buffer.
44  */
45 
AAudioServiceStreamMMAP(android::AAudioService & aAudioService,bool inService)46 AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
47                                                  bool inService)
48         : AAudioServiceStreamBase(aAudioService)
49         , mInService(inService) {
50 }
51 
close()52 aaudio_result_t AAudioServiceStreamMMAP::close() {
53     if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
54         return AAUDIO_OK;
55     }
56 
57     stop();
58 
59     return AAudioServiceStreamBase::close();
60 }
61 
62 // Open stream on HAL and pass information about the shared memory buffer back to the client.
open(const aaudio::AAudioStreamRequest & request)63 aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
64 
65     sp<AAudioServiceStreamMMAP> keep(this);
66 
67     aaudio_result_t result = AAudioServiceStreamBase::open(request,
68                                                            AAUDIO_SHARING_MODE_EXCLUSIVE);
69     if (result != AAUDIO_OK) {
70         return result;
71     }
72 
73     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
74     if (endpoint == nullptr) {
75         ALOGE("%s() has no endpoint", __func__);
76         return AAUDIO_ERROR_INVALID_STATE;
77     }
78 
79     result = endpoint->registerStream(keep);
80     if (result != AAUDIO_OK) {
81         return result;
82     }
83 
84     setState(AAUDIO_STREAM_STATE_OPEN);
85 
86     return AAUDIO_OK;
87 }
88 
89 /**
90  * Start the flow of data.
91  */
startDevice()92 aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
93     aaudio_result_t result = AAudioServiceStreamBase::startDevice();
94     if (!mInService && result == AAUDIO_OK) {
95         // Note that this can sometimes take 200 to 300 msec for a cold start!
96         result = startClient(mMmapClient, &mClientHandle);
97     }
98     return result;
99 }
100 
101 /**
102  * Stop the flow of data such that start() can resume with loss of data.
103  */
pause()104 aaudio_result_t AAudioServiceStreamMMAP::pause() {
105     if (!isRunning()) {
106         return AAUDIO_OK;
107     }
108     aaudio_result_t result = AAudioServiceStreamBase::pause();
109     // TODO put before base::pause()?
110     if (!mInService) {
111         (void) stopClient(mClientHandle);
112     }
113     return result;
114 }
115 
stop()116 aaudio_result_t AAudioServiceStreamMMAP::stop() {
117     if (!isRunning()) {
118         return AAUDIO_OK;
119     }
120     aaudio_result_t result = AAudioServiceStreamBase::stop();
121     // TODO put before base::stop()?
122     if (!mInService) {
123         (void) stopClient(mClientHandle);
124     }
125     return result;
126 }
127 
startClient(const android::AudioClient & client,audio_port_handle_t * clientHandle)128 aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
129                                                        audio_port_handle_t *clientHandle) {
130     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
131     if (endpoint == nullptr) {
132         ALOGE("%s() has no endpoint", __func__);
133         return AAUDIO_ERROR_INVALID_STATE;
134     }
135     // Start the client on behalf of the application. Generate a new porthandle.
136     aaudio_result_t result = endpoint->startClient(client, clientHandle);
137     return result;
138 }
139 
stopClient(audio_port_handle_t clientHandle)140 aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
141     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
142     if (endpoint == nullptr) {
143         ALOGE("%s() has no endpoint", __func__);
144         return AAUDIO_ERROR_INVALID_STATE;
145     }
146     aaudio_result_t result = endpoint->stopClient(clientHandle);
147     return result;
148 }
149 
150 // Get free-running DSP or DMA hardware position from the HAL.
getFreeRunningPosition(int64_t * positionFrames,int64_t * timeNanos)151 aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
152                                                                   int64_t *timeNanos) {
153     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
154     if (endpoint == nullptr) {
155         ALOGE("%s() has no endpoint", __func__);
156         return AAUDIO_ERROR_INVALID_STATE;
157     }
158     sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
159             static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
160 
161     aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
162     if (result == AAUDIO_OK) {
163         Timestamp timestamp(*positionFrames, *timeNanos);
164         mAtomicTimestamp.write(timestamp);
165         *positionFrames = timestamp.getPosition();
166         *timeNanos = timestamp.getNanoseconds();
167     } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
168         disconnect();
169     }
170     return result;
171 }
172 
173 // Get timestamp that was written by getFreeRunningPosition()
getHardwareTimestamp(int64_t * positionFrames,int64_t * timeNanos)174 aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
175                                                                 int64_t *timeNanos) {
176 
177     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
178     if (endpoint == nullptr) {
179         ALOGE("%s() has no endpoint", __func__);
180         return AAUDIO_ERROR_INVALID_STATE;
181     }
182     sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
183             static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
184 
185     // TODO Get presentation timestamp from the HAL
186     if (mAtomicTimestamp.isValid()) {
187         Timestamp timestamp = mAtomicTimestamp.read();
188         *positionFrames = timestamp.getPosition();
189         *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
190         return AAUDIO_OK;
191     } else {
192         return AAUDIO_ERROR_UNAVAILABLE;
193     }
194 }
195 
196 /**
197  * Get an immutable description of the data queue from the HAL.
198  */
getAudioDataDescription(AudioEndpointParcelable & parcelable)199 aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
200         AudioEndpointParcelable &parcelable)
201 {
202     sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
203     if (endpoint == nullptr) {
204         ALOGE("%s() has no endpoint", __func__);
205         return AAUDIO_ERROR_INVALID_STATE;
206     }
207     sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
208             static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
209     return serviceEndpointMMAP->getDownDataDescription(parcelable);
210 }
211