blob: a629ed60121a4eef28f1ea3de5f8928c49c420aa [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Eric Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceStreamMMAP"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <atomic>
Phil Burk39f02dd2017-08-04 09:13:31 -070022#include <iomanip>
23#include <iostream>
Phil Burkc0c70e32017-02-09 13:18:38 -080024#include <stdint.h>
25
26#include <utils/String16.h>
27#include <media/nbaio/AudioStreamOutSink.h>
28#include <media/MmapStreamInterface.h>
29
Phil Burk39f02dd2017-08-04 09:13:31 -070030#include "binding/AudioEndpointParcelable.h"
31#include "utility/AAudioUtilities.h"
32
33#include "AAudioServiceEndpointMMAP.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080034#include "AAudioServiceStreamBase.h"
35#include "AAudioServiceStreamMMAP.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080036#include "SharedMemoryProxy.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080037
Phil Burke72481c2017-08-08 13:20:45 -070038using android::base::unique_fd;
Phil Burkc0c70e32017-02-09 13:18:38 -080039using namespace android;
40using namespace aaudio;
41
Phil Burkc0c70e32017-02-09 13:18:38 -080042/**
Phil Burk11e8d332017-05-24 09:59:02 -070043 * Service Stream that uses an MMAP buffer.
Phil Burkc0c70e32017-02-09 13:18:38 -080044 */
45
Phil Burk39f02dd2017-08-04 09:13:31 -070046AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
Eric Laurentcb4dae22017-07-01 19:39:32 -070047 bool inService)
Phil Burk39f02dd2017-08-04 09:13:31 -070048 : AAudioServiceStreamBase(aAudioService)
Eric Laurentcb4dae22017-07-01 19:39:32 -070049 , mInService(inService) {
Phil Burkc0c70e32017-02-09 13:18:38 -080050}
51
Phil Burkc0c70e32017-02-09 13:18:38 -080052aaudio_result_t AAudioServiceStreamMMAP::close() {
Phil Burkbcc36742017-08-31 17:24:51 -070053 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk98d6d922017-07-06 11:52:45 -070054 return AAUDIO_OK;
55 }
Phil Burk39f02dd2017-08-04 09:13:31 -070056
Eric Laurentcb4dae22017-07-01 19:39:32 -070057 stop();
Phil Burk71f35bb2017-04-13 16:05:07 -070058
Phil Burkc0c70e32017-02-09 13:18:38 -080059 return AAudioServiceStreamBase::close();
60}
61
62// Open stream on HAL and pass information about the shared memory buffer back to the client.
Phil Burk39f02dd2017-08-04 09:13:31 -070063aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -080064
Phil Burk39f02dd2017-08-04 09:13:31 -070065 sp<AAudioServiceStreamMMAP> keep(this);
66
67 aaudio_result_t result = AAudioServiceStreamBase::open(request,
68 AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burkc0c70e32017-02-09 13:18:38 -080069 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -080070 return result;
71 }
72
Phil Burk39f02dd2017-08-04 09:13:31 -070073 result = mServiceEndpoint->registerStream(keep);
74 if (result != AAUDIO_OK) {
75 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -080076 }
Phil Burkc0c70e32017-02-09 13:18:38 -080077
Phil Burk5a26e662017-07-07 12:44:48 -070078 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk39f02dd2017-08-04 09:13:31 -070079
80error:
Phil Burkc0c70e32017-02-09 13:18:38 -080081 return AAUDIO_OK;
82}
83
Phil Burkc0c70e32017-02-09 13:18:38 -080084/**
85 * Start the flow of data.
86 */
Phil Burkbcc36742017-08-31 17:24:51 -070087aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
88 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070089 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070090 // Note that this can sometimes take 200 to 300 msec for a cold start!
Phil Burk81ad5ec2017-09-01 10:45:41 -070091 result = startClient(mMmapClient, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -080092 }
93 return result;
94}
95
96/**
97 * Stop the flow of data such that start() can resume with loss of data.
98 */
99aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700100 if (!isRunning()) {
101 return AAUDIO_OK;
102 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700103 aaudio_result_t result = AAudioServiceStreamBase::pause();
104 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700105 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700106 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700107 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700108 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800109}
110
Phil Burk71f35bb2017-04-13 16:05:07 -0700111aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700112 if (!isRunning()) {
113 return AAUDIO_OK;
114 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700115 aaudio_result_t result = AAudioServiceStreamBase::stop();
116 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700117 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700118 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800121}
122
Eric Laurentcb4dae22017-07-01 19:39:32 -0700123aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
Phil Burk39f02dd2017-08-04 09:13:31 -0700124 audio_port_handle_t *clientHandle) {
Phil Burkbcc36742017-08-31 17:24:51 -0700125 // Start the client on behalf of the application. Generate a new porthandle.
Phil Burk39f02dd2017-08-04 09:13:31 -0700126 aaudio_result_t result = mServiceEndpoint->startClient(client, clientHandle);
127 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700128}
129
130aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700131 aaudio_result_t result = mServiceEndpoint->stopClient(clientHandle);
132 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700133}
134
Phil Burk97350f92017-07-21 15:59:44 -0700135// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800136aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700137 int64_t *timeNanos) {
138 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
139 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
140 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
141 if (result == AAUDIO_OK) {
142 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burk97350f92017-07-21 15:59:44 -0700143 mAtomicTimestamp.write(timestamp);
144 *positionFrames = timestamp.getPosition();
145 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700146 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
147 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 }
Phil Burk940083c2017-07-17 17:00:02 -0700149 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800150}
151
Phil Burk97350f92017-07-21 15:59:44 -0700152// Get timestamp that was written by getFreeRunningPosition()
153aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
154 int64_t *timeNanos) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700155 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
156 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
Phil Burk97350f92017-07-21 15:59:44 -0700157 // TODO Get presentation timestamp from the HAL
158 if (mAtomicTimestamp.isValid()) {
159 Timestamp timestamp = mAtomicTimestamp.read();
160 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700161 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700162 return AAUDIO_OK;
163 } else {
164 return AAUDIO_ERROR_UNAVAILABLE;
165 }
166}
167
Phil Burkc0c70e32017-02-09 13:18:38 -0800168/**
169 * Get an immutable description of the data queue from the HAL.
170 */
171aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
172{
Phil Burk39f02dd2017-08-04 09:13:31 -0700173 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
174 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
175 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700176}