blob: 820ed289723276465f29e1640d0fc102e08582b7 [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(' ');
50 result << "\n";
51 result << " Running Stream Count: " << mRunningStreamCount << "\n";
52
53 result << AAudioServiceEndpoint::dump();
54 return result.str();
55}
56
57// Share an AudioStreamInternal.
58aaudio_result_t AAudioServiceEndpointShared::open(const aaudio::AAudioStreamRequest &request) {
59 aaudio_result_t result = AAUDIO_OK;
60 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
61
62 mRequestedDeviceId = configuration.getDeviceId();
63 setDirection(configuration.getDirection());
64
65 AudioStreamBuilder builder;
66 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
67 // Don't fall back to SHARED because that would cause recursion.
68 builder.setSharingModeMatchRequired(true);
69 builder.setDeviceId(mRequestedDeviceId);
70 builder.setFormat(configuration.getFormat());
71 builder.setSampleRate(configuration.getSampleRate());
72 builder.setSamplesPerFrame(configuration.getSamplesPerFrame());
73 builder.setDirection(configuration.getDirection());
74 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
75
76 result = mStreamInternal->open(builder);
77
78 setSampleRate(mStreamInternal->getSampleRate());
79 setSamplesPerFrame(mStreamInternal->getSamplesPerFrame());
80 setDeviceId(mStreamInternal->getDeviceId());
81 mFramesPerBurst = mStreamInternal->getFramesPerBurst();
82
83 return result;
84}
85
86aaudio_result_t AAudioServiceEndpointShared::close() {
87 return getStreamInternal()->close();
88}
89
90// Glue between C and C++ callbacks.
91static void *aaudio_endpoint_thread_proc(void *context) {
92 AAudioServiceEndpointShared *endpoint = (AAudioServiceEndpointShared *) context;
93 if (endpoint != NULL) {
Phil Burkbe0a5b62017-10-12 15:56:00 -070094 void *result = endpoint->callbackLoop();
95 // Close now so that the HW resource is freed and we can open a new device.
96 if (!endpoint->isConnected()) {
97 endpoint->close();
98 }
99 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700100 } else {
101 return NULL;
102 }
103}
104
105aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
106 // Launch the callback loop thread.
107 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
108 * AAUDIO_NANOS_PER_SECOND
109 / getSampleRate();
110 mCallbackEnabled.store(true);
111 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
112}
113
114aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
115 mCallbackEnabled.store(false);
116 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
117 return result;
118}
119
120aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream,
121 audio_port_handle_t *clientHandle) {
122 aaudio_result_t result = AAUDIO_OK;
Phil Burkb92c9882017-09-15 11:39:15 -0700123
124 {
Phil Burk39f02dd2017-08-04 09:13:31 -0700125 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkb92c9882017-09-15 11:39:15 -0700126 if (++mRunningStreamCount == 1) { // atomic
127 result = getStreamInternal()->requestStart();
128 if (result != AAUDIO_OK) {
129 --mRunningStreamCount;
130 } else {
131 result = startSharingThread_l();
132 if (result != AAUDIO_OK) {
133 getStreamInternal()->requestStop();
134 --mRunningStreamCount;
135 }
136 }
137 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 }
Phil Burkb92c9882017-09-15 11:39:15 -0700139
Phil Burk39f02dd2017-08-04 09:13:31 -0700140 if (result == AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700141 result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle);
Phil Burkb92c9882017-09-15 11:39:15 -0700142 if (result != AAUDIO_OK) {
143 if (--mRunningStreamCount == 0) { // atomic
144 stopSharingThread();
145 getStreamInternal()->requestStop();
146 }
147 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700148 }
Phil Burkb92c9882017-09-15 11:39:15 -0700149
Phil Burk39f02dd2017-08-04 09:13:31 -0700150 return result;
151}
152
153aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream,
154 audio_port_handle_t clientHandle) {
155 // Don't lock here because the disconnectRegisteredStreams also uses the lock.
156
157 // Ignore result.
158 (void) getStreamInternal()->stopClient(clientHandle);
159
160 if (--mRunningStreamCount == 0) { // atomic
161 stopSharingThread();
162 getStreamInternal()->requestStop();
163 }
164 return AAUDIO_OK;
165}
166
Phil Burk39f02dd2017-08-04 09:13:31 -0700167// Get timestamp that was written by the real-time service thread, eg. mixer.
168aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
169 int64_t *timeNanos) {
170 if (mAtomicTimestamp.isValid()) {
171 Timestamp timestamp = mAtomicTimestamp.read();
172 *positionFrames = timestamp.getPosition();
173 *timeNanos = timestamp.getNanoseconds();
174 return AAUDIO_OK;
175 } else {
176 return AAUDIO_ERROR_UNAVAILABLE;
177 }
178}
179
180aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
181 int64_t *timeNanos) {
182 return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
183}