• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
37 using namespace android;
38 using 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 
dump() const43 std::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.
open(const aaudio::AAudioStreamRequest & request)58 aaudio_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 
close()86 aaudio_result_t AAudioServiceEndpointShared::close() {
87     return getStreamInternal()->close();
88 }
89 
90 // Glue between C and C++ callbacks.
aaudio_endpoint_thread_proc(void * context)91 static void *aaudio_endpoint_thread_proc(void *context) {
92     AAudioServiceEndpointShared *endpoint = (AAudioServiceEndpointShared *) context;
93     if (endpoint != NULL) {
94         return endpoint->callbackLoop();
95     } else {
96         return NULL;
97     }
98 }
99 
startSharingThread_l()100 aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
101     // Launch the callback loop thread.
102     int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
103                           * AAUDIO_NANOS_PER_SECOND
104                           / getSampleRate();
105     mCallbackEnabled.store(true);
106     return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
107 }
108 
stopSharingThread()109 aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
110     mCallbackEnabled.store(false);
111     aaudio_result_t result = getStreamInternal()->joinThread(NULL);
112     return result;
113 }
114 
startStream(sp<AAudioServiceStreamBase> sharedStream,audio_port_handle_t * clientHandle)115 aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream,
116                                                          audio_port_handle_t *clientHandle) {
117     aaudio_result_t result = AAUDIO_OK;
118 
119     {
120         std::lock_guard<std::mutex> lock(mLockStreams);
121         if (++mRunningStreamCount == 1) { // atomic
122             result = getStreamInternal()->requestStart();
123             if (result != AAUDIO_OK) {
124                 --mRunningStreamCount;
125             } else {
126                 result = startSharingThread_l();
127                 if (result != AAUDIO_OK) {
128                     getStreamInternal()->requestStop();
129                     --mRunningStreamCount;
130                 }
131             }
132         }
133     }
134 
135     if (result == AAUDIO_OK) {
136         result = getStreamInternal()->startClient(sharedStream->getAudioClient(), clientHandle);
137         if (result != AAUDIO_OK) {
138             if (--mRunningStreamCount == 0) { // atomic
139                 stopSharingThread();
140                 getStreamInternal()->requestStop();
141             }
142         }
143     }
144 
145     return result;
146 }
147 
stopStream(sp<AAudioServiceStreamBase> sharedStream,audio_port_handle_t clientHandle)148 aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream,
149                                                         audio_port_handle_t clientHandle) {
150     // Don't lock here because the disconnectRegisteredStreams also uses the lock.
151 
152     // Ignore result.
153     (void) getStreamInternal()->stopClient(clientHandle);
154 
155     if (--mRunningStreamCount == 0) { // atomic
156         stopSharingThread();
157         getStreamInternal()->requestStop();
158     }
159     return AAUDIO_OK;
160 }
161 
162 // Get timestamp that was written by the real-time service thread, eg. mixer.
getFreeRunningPosition(int64_t * positionFrames,int64_t * timeNanos)163 aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
164                                                                   int64_t *timeNanos) {
165     if (mAtomicTimestamp.isValid()) {
166         Timestamp timestamp = mAtomicTimestamp.read();
167         *positionFrames = timestamp.getPosition();
168         *timeNanos = timestamp.getNanoseconds();
169         return AAUDIO_OK;
170     } else {
171         return AAUDIO_ERROR_UNAVAILABLE;
172     }
173 }
174 
getTimestamp(int64_t * positionFrames,int64_t * timeNanos)175 aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
176                                                           int64_t *timeNanos) {
177     return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
178 }
179