blob: 2de537a7f1d4eb0aaa26784c2d761eb14f665d82 [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
Phil Burka62fb952018-01-16 12:44:06 -080063 copyFrom(configuration);
Phil Burk39f02dd2017-08-04 09:13:31 -070064 mRequestedDeviceId = configuration.getDeviceId();
Phil Burk39f02dd2017-08-04 09:13:31 -070065
66 AudioStreamBuilder builder;
Phil Burka62fb952018-01-16 12:44:06 -080067 builder.copyFrom(configuration);
68
Phil Burk39f02dd2017-08-04 09:13:31 -070069 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
70 // Don't fall back to SHARED because that would cause recursion.
71 builder.setSharingModeMatchRequired(true);
Phil Burka62fb952018-01-16 12:44:06 -080072
Phil Burk39f02dd2017-08-04 09:13:31 -070073 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
74
75 result = mStreamInternal->open(builder);
76
77 setSampleRate(mStreamInternal->getSampleRate());
78 setSamplesPerFrame(mStreamInternal->getSamplesPerFrame());
79 setDeviceId(mStreamInternal->getDeviceId());
80 mFramesPerBurst = mStreamInternal->getFramesPerBurst();
81
82 return result;
83}
84
85aaudio_result_t AAudioServiceEndpointShared::close() {
86 return getStreamInternal()->close();
87}
88
89// Glue between C and C++ callbacks.
90static void *aaudio_endpoint_thread_proc(void *context) {
91 AAudioServiceEndpointShared *endpoint = (AAudioServiceEndpointShared *) context;
92 if (endpoint != NULL) {
Phil Burkbe0a5b62017-10-12 15:56:00 -070093 void *result = endpoint->callbackLoop();
94 // Close now so that the HW resource is freed and we can open a new device.
95 if (!endpoint->isConnected()) {
96 endpoint->close();
97 }
98 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -070099 } else {
100 return NULL;
101 }
102}
103
104aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
105 // Launch the callback loop thread.
106 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
107 * AAUDIO_NANOS_PER_SECOND
108 / getSampleRate();
109 mCallbackEnabled.store(true);
110 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
111}
112
113aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
114 mCallbackEnabled.store(false);
115 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
116 return result;
117}
118
119aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream,
120 audio_port_handle_t *clientHandle) {
121 aaudio_result_t result = AAUDIO_OK;
Phil Burkb92c9882017-09-15 11:39:15 -0700122
123 {
Phil Burk39f02dd2017-08-04 09:13:31 -0700124 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkb92c9882017-09-15 11:39:15 -0700125 if (++mRunningStreamCount == 1) { // atomic
126 result = getStreamInternal()->requestStart();
127 if (result != AAUDIO_OK) {
128 --mRunningStreamCount;
129 } else {
130 result = startSharingThread_l();
131 if (result != AAUDIO_OK) {
132 getStreamInternal()->requestStop();
133 --mRunningStreamCount;
134 }
135 }
136 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700137 }
Phil Burkb92c9882017-09-15 11:39:15 -0700138
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 if (result == AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700140 result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle);
Phil Burkb92c9882017-09-15 11:39:15 -0700141 if (result != AAUDIO_OK) {
142 if (--mRunningStreamCount == 0) { // atomic
143 stopSharingThread();
144 getStreamInternal()->requestStop();
145 }
146 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 }
Phil Burkb92c9882017-09-15 11:39:15 -0700148
Phil Burk39f02dd2017-08-04 09:13:31 -0700149 return result;
150}
151
152aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream,
153 audio_port_handle_t clientHandle) {
154 // Don't lock here because the disconnectRegisteredStreams also uses the lock.
155
156 // Ignore result.
157 (void) getStreamInternal()->stopClient(clientHandle);
158
159 if (--mRunningStreamCount == 0) { // atomic
160 stopSharingThread();
161 getStreamInternal()->requestStop();
162 }
163 return AAUDIO_OK;
164}
165
Phil Burk39f02dd2017-08-04 09:13:31 -0700166// Get timestamp that was written by the real-time service thread, eg. mixer.
167aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
168 int64_t *timeNanos) {
169 if (mAtomicTimestamp.isValid()) {
170 Timestamp timestamp = mAtomicTimestamp.read();
171 *positionFrames = timestamp.getPosition();
172 *timeNanos = timestamp.getNanoseconds();
173 return AAUDIO_OK;
174 } else {
175 return AAUDIO_ERROR_UNAVAILABLE;
176 }
177}
178
179aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
180 int64_t *timeNanos) {
181 return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
182}