AAudioServiceStreamMMAP.cpp revision 81ad5ecd211d0f30225714ae8ee61c4d76dbcf56
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 (mState == 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        ALOGE("AAudioServiceStreamBase open returned %d", result);
71        return result;
72    }
73
74    result = mServiceEndpoint->registerStream(keep);
75    if (result != AAUDIO_OK) {
76        goto error;
77    }
78
79    setState(AAUDIO_STREAM_STATE_OPEN);
80
81error:
82    return AAUDIO_OK;
83}
84
85/**
86 * Start the flow of data.
87 */
88aaudio_result_t AAudioServiceStreamMMAP::start() {
89    if (isRunning()) {
90        return AAUDIO_OK;
91    }
92
93    aaudio_result_t result = AAudioServiceStreamBase::start();
94    if (!mInService && result == AAUDIO_OK) {
95        result = startClient(mMmapClient, &mClientHandle);
96    }
97    return result;
98}
99
100/**
101 * Stop the flow of data such that start() can resume with loss of data.
102 */
103aaudio_result_t AAudioServiceStreamMMAP::pause() {
104    if (!isRunning()) {
105        return AAUDIO_OK;
106    }
107    aaudio_result_t result = AAudioServiceStreamBase::pause();
108    // TODO put before base::pause()?
109    if (!mInService) {
110        (void) stopClient(mClientHandle);
111    }
112    return result;
113}
114
115aaudio_result_t AAudioServiceStreamMMAP::stop() {
116    if (!isRunning()) {
117        return AAUDIO_OK;
118    }
119    aaudio_result_t result = AAudioServiceStreamBase::stop();
120    // TODO put before base::stop()?
121    if (!mInService) {
122        (void) stopClient(mClientHandle);
123    }
124    return result;
125}
126
127aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
128                                                       audio_port_handle_t *clientHandle) {
129    aaudio_result_t result = mServiceEndpoint->startClient(client, clientHandle);
130    return result;
131}
132
133aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
134    aaudio_result_t result = mServiceEndpoint->stopClient(clientHandle);
135    return result;
136}
137
138// Get free-running DSP or DMA hardware position from the HAL.
139aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
140                                                                  int64_t *timeNanos) {
141    sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
142            static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
143    aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
144    if (result == AAUDIO_OK) {
145        Timestamp timestamp(*positionFrames, *timeNanos);
146        mAtomicTimestamp.write(timestamp);
147        *positionFrames = timestamp.getPosition();
148        *timeNanos = timestamp.getNanoseconds();
149    } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
150        disconnect();
151    }
152    return result;
153}
154
155// Get timestamp that was written by getFreeRunningPosition()
156aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
157                                                                int64_t *timeNanos) {
158    sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
159            static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
160    // TODO Get presentation timestamp from the HAL
161    if (mAtomicTimestamp.isValid()) {
162        Timestamp timestamp = mAtomicTimestamp.read();
163        *positionFrames = timestamp.getPosition();
164        *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
165        return AAUDIO_OK;
166    } else {
167        return AAUDIO_ERROR_UNAVAILABLE;
168    }
169}
170
171/**
172 * Get an immutable description of the data queue from the HAL.
173 */
174aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
175{
176    sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
177            static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
178    return serviceEndpointMMAP->getDownDataDescription(parcelable);
179}
180