AAudioServiceStreamMMAP.cpp revision 6e2770ef771b7a3042d606c37f9c3af8a933439b
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
38using android::base::unique_fd;
39using namespace android;
40using namespace aaudio;
41
42/**
43 * Service Stream that uses an MMAP buffer.
44 */
45
46AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
47                                                 bool inService)
48        : AAudioServiceStreamBase(aAudioService)
49        , mInService(inService) {
50}
51
52aaudio_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.
63aaudio_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// Start the flow of data.
90aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
91    aaudio_result_t result = AAudioServiceStreamBase::startDevice();
92    if (!mInService && result == AAUDIO_OK) {
93        // Note that this can sometimes take 200 to 300 msec for a cold start!
94        result = startClient(mMmapClient, &mClientHandle);
95    }
96    return result;
97}
98
99// Stop the flow of data such that start() can resume with loss of data.
100aaudio_result_t AAudioServiceStreamMMAP::pause() {
101    if (!isRunning()) {
102        return AAUDIO_OK;
103    }
104    aaudio_result_t result = AAudioServiceStreamBase::pause();
105    // TODO put before base::pause()?
106    if (!mInService) {
107        (void) stopClient(mClientHandle);
108    }
109    return result;
110}
111
112aaudio_result_t AAudioServiceStreamMMAP::stop() {
113    if (!isRunning()) {
114        return AAUDIO_OK;
115    }
116    aaudio_result_t result = AAudioServiceStreamBase::stop();
117    // TODO put before base::stop()?
118    if (!mInService) {
119        (void) stopClient(mClientHandle);
120    }
121    return result;
122}
123
124aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
125                                                       audio_port_handle_t *clientHandle) {
126    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
127    if (endpoint == nullptr) {
128        ALOGE("%s() has no endpoint", __func__);
129        return AAUDIO_ERROR_INVALID_STATE;
130    }
131    // Start the client on behalf of the application. Generate a new porthandle.
132    aaudio_result_t result = endpoint->startClient(client, clientHandle);
133    return result;
134}
135
136aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
137    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
138    if (endpoint == nullptr) {
139        ALOGE("%s() has no endpoint", __func__);
140        return AAUDIO_ERROR_INVALID_STATE;
141    }
142    aaudio_result_t result = endpoint->stopClient(clientHandle);
143    return result;
144}
145
146// Get free-running DSP or DMA hardware position from the HAL.
147aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
148                                                                  int64_t *timeNanos) {
149    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
150    if (endpoint == nullptr) {
151        ALOGE("%s() has no endpoint", __func__);
152        return AAUDIO_ERROR_INVALID_STATE;
153    }
154    sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
155            static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
156
157    aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
158    if (result == AAUDIO_OK) {
159        Timestamp timestamp(*positionFrames, *timeNanos);
160        mAtomicTimestamp.write(timestamp);
161        *positionFrames = timestamp.getPosition();
162        *timeNanos = timestamp.getNanoseconds();
163    } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
164        disconnect();
165    }
166    return result;
167}
168
169// Get timestamp that was written by getFreeRunningPosition()
170aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
171                                                                int64_t *timeNanos) {
172
173    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
174    if (endpoint == nullptr) {
175        ALOGE("%s() has no endpoint", __func__);
176        return AAUDIO_ERROR_INVALID_STATE;
177    }
178    sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
179            static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
180
181    // TODO Get presentation timestamp from the HAL
182    if (mAtomicTimestamp.isValid()) {
183        Timestamp timestamp = mAtomicTimestamp.read();
184        *positionFrames = timestamp.getPosition();
185        *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
186        return AAUDIO_OK;
187    } else {
188        return AAUDIO_ERROR_UNAVAILABLE;
189    }
190}
191
192// Get an immutable description of the data queue from the HAL.
193aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
194        AudioEndpointParcelable &parcelable)
195{
196    sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
197    if (endpoint == nullptr) {
198        ALOGE("%s() has no endpoint", __func__);
199        return AAUDIO_ERROR_INVALID_STATE;
200    }
201    sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
202            static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
203    return serviceEndpointMMAP->getDownDataDescription(parcelable);
204}
205