blob: 6af9e7e99ce975805b1c63dd2f4b631eb56b84af [file] [log] [blame]
Phil Burk39f02dd2017-08-04 09:13:31 -07001/*
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
18#define LOG_TAG "AAudioServiceEndpointShared"
19//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
22#include <iomanip>
23#include <iostream>
24#include <sstream>
25
26#include "binding/AAudioServiceMessage.h"
27#include "client/AudioStreamInternal.h"
28#include "client/AudioStreamInternalPlay.h"
29#include "core/AudioStreamBuilder.h"
30
31#include "AAudioServiceEndpointShared.h"
32#include "AAudioServiceStreamShared.h"
33#include "AAudioServiceStreamMMAP.h"
34#include "AAudioMixer.h"
35#include "AAudioService.h"
36
37using namespace android;
38using namespace aaudio;
39
40// This is the maximum size in frames. The effective size can be tuned smaller at runtime.
41#define DEFAULT_BUFFER_CAPACITY (48 * 8)
42
43std::string AAudioServiceEndpointShared::dump() const {
44 std::stringstream result;
45
46 result << " SHARED: sharing exclusive stream with handle = 0x"
47 << std::setfill('0') << std::setw(8)
48 << std::hex << mStreamInternal->getServiceHandle()
49 << std::dec << std::setfill(' ');
Phil Burk23296382017-11-20 15:45:11 -080050 result << ", XRuns = " << mStreamInternal->getXRunCount();
Phil Burk39f02dd2017-08-04 09:13:31 -070051 result << "\n";
52 result << " Running Stream Count: " << mRunningStreamCount << "\n";
53
54 result << AAudioServiceEndpoint::dump();
55 return result.str();
56}
57
58// Share an AudioStreamInternal.
59aaudio_result_t AAudioServiceEndpointShared::open(const aaudio::AAudioStreamRequest &request) {
60 aaudio_result_t result = AAUDIO_OK;
61 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
62
63 mRequestedDeviceId = configuration.getDeviceId();
64 setDirection(configuration.getDirection());
65
66 AudioStreamBuilder builder;
67 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
68 // Don't fall back to SHARED because that would cause recursion.
69 builder.setSharingModeMatchRequired(true);
70 builder.setDeviceId(mRequestedDeviceId);
71 builder.setFormat(configuration.getFormat());
72 builder.setSampleRate(configuration.getSampleRate());
73 builder.setSamplesPerFrame(configuration.getSamplesPerFrame());
74 builder.setDirection(configuration.getDirection());
75 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
76
77 result = mStreamInternal->open(builder);
78
79 setSampleRate(mStreamInternal->getSampleRate());
80 setSamplesPerFrame(mStreamInternal->getSamplesPerFrame());
81 setDeviceId(mStreamInternal->getDeviceId());
82 mFramesPerBurst = mStreamInternal->getFramesPerBurst();
83
84 return result;
85}
86
87aaudio_result_t AAudioServiceEndpointShared::close() {
88 return getStreamInternal()->close();
89}
90
91// Glue between C and C++ callbacks.
92static void *aaudio_endpoint_thread_proc(void *context) {
93 AAudioServiceEndpointShared *endpoint = (AAudioServiceEndpointShared *) context;
94 if (endpoint != NULL) {
Phil Burkbe0a5b62017-10-12 15:56:00 -070095 void *result = endpoint->callbackLoop();
96 // Close now so that the HW resource is freed and we can open a new device.
97 if (!endpoint->isConnected()) {
98 endpoint->close();
99 }
100 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700101 } else {
102 return NULL;
103 }
104}
105
106aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
107 // Launch the callback loop thread.
108 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
109 * AAUDIO_NANOS_PER_SECOND
110 / getSampleRate();
111 mCallbackEnabled.store(true);
112 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
113}
114
115aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
116 mCallbackEnabled.store(false);
117 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
118 return result;
119}
120
121aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream,
122 audio_port_handle_t *clientHandle) {
123 aaudio_result_t result = AAUDIO_OK;
Phil Burkb92c9882017-09-15 11:39:15 -0700124
125 {
Phil Burk39f02dd2017-08-04 09:13:31 -0700126 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkb92c9882017-09-15 11:39:15 -0700127 if (++mRunningStreamCount == 1) { // atomic
128 result = getStreamInternal()->requestStart();
129 if (result != AAUDIO_OK) {
130 --mRunningStreamCount;
131 } else {
132 result = startSharingThread_l();
133 if (result != AAUDIO_OK) {
134 getStreamInternal()->requestStop();
135 --mRunningStreamCount;
136 }
137 }
138 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 }
Phil Burkb92c9882017-09-15 11:39:15 -0700140
Phil Burk39f02dd2017-08-04 09:13:31 -0700141 if (result == AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle);
Phil Burkb92c9882017-09-15 11:39:15 -0700143 if (result != AAUDIO_OK) {
144 if (--mRunningStreamCount == 0) { // atomic
145 stopSharingThread();
146 getStreamInternal()->requestStop();
147 }
148 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700149 }
Phil Burkb92c9882017-09-15 11:39:15 -0700150
Phil Burk39f02dd2017-08-04 09:13:31 -0700151 return result;
152}
153
154aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream,
155 audio_port_handle_t clientHandle) {
156 // Don't lock here because the disconnectRegisteredStreams also uses the lock.
157
158 // Ignore result.
159 (void) getStreamInternal()->stopClient(clientHandle);
160
161 if (--mRunningStreamCount == 0) { // atomic
162 stopSharingThread();
163 getStreamInternal()->requestStop();
164 }
165 return AAUDIO_OK;
166}
167
Phil Burk39f02dd2017-08-04 09:13:31 -0700168// Get timestamp that was written by the real-time service thread, eg. mixer.
169aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
170 int64_t *timeNanos) {
171 if (mAtomicTimestamp.isValid()) {
172 Timestamp timestamp = mAtomicTimestamp.read();
173 *positionFrames = timestamp.getPosition();
174 *timeNanos = timestamp.getNanoseconds();
175 return AAUDIO_OK;
176 } else {
177 return AAUDIO_ERROR_UNAVAILABLE;
178 }
179}
180
181aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
182 int64_t *timeNanos) {
183 return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
184}