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