blob: 34ddb4bcc8a4f14b4042b8dffc4ca0d5fbdcf063 [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 Burkfbf031e2017-10-12 15:58:31 -070084// Start the flow of data.
Phil Burkbcc36742017-08-31 17:24:51 -070085aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
86 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070087 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070088 // Note that this can sometimes take 200 to 300 msec for a cold start!
Phil Burk81ad5ec2017-09-01 10:45:41 -070089 result = startClient(mMmapClient, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -080090 }
91 return result;
92}
93
Phil Burkfbf031e2017-10-12 15:58:31 -070094// Stop the flow of data such that start() can resume with loss of data.
Phil Burkc0c70e32017-02-09 13:18:38 -080095aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070096 if (!isRunning()) {
97 return AAUDIO_OK;
98 }
Phil Burk39f02dd2017-08-04 09:13:31 -070099 aaudio_result_t result = AAudioServiceStreamBase::pause();
100 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700101 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700102 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700103 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700104 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800105}
106
Phil Burk71f35bb2017-04-13 16:05:07 -0700107aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700108 if (!isRunning()) {
109 return AAUDIO_OK;
110 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 aaudio_result_t result = AAudioServiceStreamBase::stop();
112 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700113 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700114 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700115 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800117}
118
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 audio_port_handle_t *clientHandle) {
Phil Burkbcc36742017-08-31 17:24:51 -0700121 // Start the client on behalf of the application. Generate a new porthandle.
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 aaudio_result_t result = mServiceEndpoint->startClient(client, clientHandle);
123 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700124}
125
126aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700127 aaudio_result_t result = mServiceEndpoint->stopClient(clientHandle);
128 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700129}
130
Phil Burk97350f92017-07-21 15:59:44 -0700131// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800132aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 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);
Phil Burk97350f92017-07-21 15:59:44 -0700139 mAtomicTimestamp.write(timestamp);
140 *positionFrames = timestamp.getPosition();
141 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
143 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 }
Phil Burk940083c2017-07-17 17:00:02 -0700145 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800146}
147
Phil Burk97350f92017-07-21 15:59:44 -0700148// Get timestamp that was written by getFreeRunningPosition()
149aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
150 int64_t *timeNanos) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700151 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
152 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
Phil Burk97350f92017-07-21 15:59:44 -0700153 // TODO Get presentation timestamp from the HAL
154 if (mAtomicTimestamp.isValid()) {
155 Timestamp timestamp = mAtomicTimestamp.read();
156 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700158 return AAUDIO_OK;
159 } else {
160 return AAUDIO_ERROR_UNAVAILABLE;
161 }
162}
163
Phil Burkfbf031e2017-10-12 15:58:31 -0700164// Get an immutable description of the data queue from the HAL.
Phil Burk523b3042017-09-13 13:03:08 -0700165aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
166 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800167{
Phil Burk39f02dd2017-08-04 09:13:31 -0700168 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{
169 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())};
170 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700171}