blob: 52b1801a12e703e353a8e9a9c804bf26374f8ff7 [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
2 * Copyright (C) 2016 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
Phil Burk5ed503c2017-02-01 09:38:15 -080017#define LOG_TAG "AAudioService"
Phil Burk2355edb2016-12-26 13:54:02 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burkc0c70e32017-02-09 13:18:38 -080021#include <mutex>
Phil Burk2355edb2016-12-26 13:54:02 -080022
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include "binding/IAAudioService.h"
24#include "binding/AAudioServiceMessage.h"
25#include "utility/AudioClock.h"
26
27#include "AAudioServiceStreamBase.h"
28#include "TimestampScheduler.h"
29
30using namespace android; // TODO just import names needed
31using namespace aaudio; // TODO just import names needed
Phil Burk2355edb2016-12-26 13:54:02 -080032
33/**
Phil Burkc0c70e32017-02-09 13:18:38 -080034 * Base class for streams in the service.
35 * @return
Phil Burk2355edb2016-12-26 13:54:02 -080036 */
37
Phil Burk5ed503c2017-02-01 09:38:15 -080038AAudioServiceStreamBase::AAudioServiceStreamBase()
Phil Burk2355edb2016-12-26 13:54:02 -080039 : mUpMessageQueue(nullptr)
Phil Burkc0c70e32017-02-09 13:18:38 -080040 , mAAudioThread() {
Phil Burk2355edb2016-12-26 13:54:02 -080041}
42
Phil Burk5ed503c2017-02-01 09:38:15 -080043AAudioServiceStreamBase::~AAudioServiceStreamBase() {
Phil Burk98d6d922017-07-06 11:52:45 -070044 ALOGD("AAudioServiceStreamBase::~AAudioServiceStreamBase() destroying %p", this);
Phil Burk5a26e662017-07-07 12:44:48 -070045 // If the stream is deleted when OPEN or in use then audio resources will leak.
46 // This would indicate an internal error. So we want to find this ASAP.
47 LOG_ALWAYS_FATAL_IF(!(mState == AAUDIO_STREAM_STATE_CLOSED
48 || mState == AAUDIO_STREAM_STATE_UNINITIALIZED),
49 "service stream still open, state = %d", mState);
Phil Burk2355edb2016-12-26 13:54:02 -080050}
51
Phil Burk4501b352017-06-29 18:12:36 -070052std::string AAudioServiceStreamBase::dump() const {
53 std::stringstream result;
54
55 result << " -------- handle = 0x" << std::hex << mHandle << std::dec << "\n";
56 result << " state = " << AAudio_convertStreamStateToText(mState) << "\n";
57 result << " format = " << mAudioFormat << "\n";
58 result << " framesPerBurst = " << mFramesPerBurst << "\n";
59 result << " channelCount = " << mSamplesPerFrame << "\n";
60 result << " capacityFrames = " << mCapacityInFrames << "\n";
61 result << " owner uid = " << mOwnerUserId << "\n";
62
63 return result.str();
64}
65
Phil Burkc0c70e32017-02-09 13:18:38 -080066aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request,
67 aaudio::AAudioStreamConfiguration &configurationOutput) {
68 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
69 if (mUpMessageQueue != nullptr) {
70 return AAUDIO_ERROR_INVALID_STATE;
71 } else {
72 mUpMessageQueue = new SharedRingBuffer();
73 return mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), QUEUE_UP_CAPACITY_COMMANDS);
74 }
75}
Phil Burkdec33ab2017-01-17 14:48:16 -080076
Phil Burkc0c70e32017-02-09 13:18:38 -080077aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burk98d6d922017-07-06 11:52:45 -070078 if (mState != AAUDIO_STREAM_STATE_CLOSED) {
79 stopTimestampThread();
80 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
81 delete mUpMessageQueue;
82 mUpMessageQueue = nullptr;
83 mState = AAUDIO_STREAM_STATE_CLOSED;
84 }
Phil Burkc0c70e32017-02-09 13:18:38 -080085 return AAUDIO_OK;
86}
87
88aaudio_result_t AAudioServiceStreamBase::start() {
89 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
90 mState = AAUDIO_STREAM_STATE_STARTED;
91 mThreadEnabled.store(true);
92 return mAAudioThread.start(this);
93}
94
95aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burk11e8d332017-05-24 09:59:02 -070096 aaudio_result_t result = AAUDIO_OK;
97 if (isRunning()) {
98 sendCurrentTimestamp();
99 mThreadEnabled.store(false);
100 result = mAAudioThread.stop();
101 if (result != AAUDIO_OK) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700102 disconnect();
Phil Burk11e8d332017-05-24 09:59:02 -0700103 return result;
104 }
105 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800106 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800107 mState = AAUDIO_STREAM_STATE_PAUSED;
108 return result;
109}
110
Phil Burk71f35bb2017-04-13 16:05:07 -0700111aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burk11e8d332017-05-24 09:59:02 -0700112 aaudio_result_t result = AAUDIO_OK;
113 if (isRunning()) {
114 // TODO wait for data to be played out
Phil Burk98d6d922017-07-06 11:52:45 -0700115 sendCurrentTimestamp(); // warning - this calls a virtual function
116 result = stopTimestampThread();
Phil Burk11e8d332017-05-24 09:59:02 -0700117 if (result != AAUDIO_OK) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700118 disconnect();
Phil Burk11e8d332017-05-24 09:59:02 -0700119 return result;
120 }
121 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700122 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700123 mState = AAUDIO_STREAM_STATE_STOPPED;
124 return result;
125}
126
Phil Burk98d6d922017-07-06 11:52:45 -0700127aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() {
128 aaudio_result_t result = AAUDIO_OK;
129 // clear flag that tells thread to loop
130 if (mThreadEnabled.exchange(false)) {
131 result = mAAudioThread.stop();
132 }
133 return result;
134}
135
Phil Burk71f35bb2017-04-13 16:05:07 -0700136aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700137 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
138 mState = AAUDIO_STREAM_STATE_FLUSHED;
139 return AAUDIO_OK;
140}
141
Phil Burkcf5f6d22017-05-26 12:35:07 -0700142// implement Runnable, periodically send timestamps to client
Phil Burkc0c70e32017-02-09 13:18:38 -0800143void AAudioServiceStreamBase::run() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700144 ALOGD("AAudioServiceStreamBase::run() entering ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 TimestampScheduler timestampScheduler;
146 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
147 timestampScheduler.start(AudioClock::getNanoseconds());
148 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
149 while(mThreadEnabled.load()) {
150 if (AudioClock::getNanoseconds() >= nextTime) {
151 aaudio_result_t result = sendCurrentTimestamp();
152 if (result != AAUDIO_OK) {
153 break;
154 }
155 nextTime = timestampScheduler.nextAbsoluteTime();
156 } else {
157 // Sleep until it is time to send the next timestamp.
Phil Burk98d6d922017-07-06 11:52:45 -0700158 // TODO Wait for a signal with a timeout so that we can stop more quickly.
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 AudioClock::sleepUntilNanoTime(nextTime);
160 }
161 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700162 ALOGD("AAudioServiceStreamBase::run() exiting ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800163}
164
Phil Burk5ef003b2017-06-30 11:43:37 -0700165void AAudioServiceStreamBase::disconnect() {
166 if (mState != AAUDIO_STREAM_STATE_DISCONNECTED) {
167 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
168 mState = AAUDIO_STREAM_STATE_DISCONNECTED;
169 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800170}
171
172aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
173 double dataDouble,
174 int64_t dataLong) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800175 AAudioServiceMessage command;
176 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800177 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 command.event.dataDouble = dataDouble;
179 command.event.dataLong = dataLong;
180 return writeUpMessageQueue(&command);
181}
182
183aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
184 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
Phil Burk71f35bb2017-04-13 16:05:07 -0700185 if (mUpMessageQueue == nullptr) {
186 ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open");
187 return AAUDIO_ERROR_NULL;
188 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800189 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
190 if (count != 1) {
191 ALOGE("writeUpMessageQueue(): Queue full. Did client die?");
192 return AAUDIO_ERROR_WOULD_BLOCK;
193 } else {
194 return AAUDIO_OK;
195 }
196}
197
198aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
199 AAudioServiceMessage command;
200 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
201 &command.timestamp.timestamp);
202 if (result == AAUDIO_OK) {
Phil Burk87c9f642017-05-17 07:22:39 -0700203 // ALOGD("sendCurrentTimestamp(): position = %lld, nanos = %lld",
204 // (long long) command.timestamp.position,
205 // (long long) command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 command.what = AAudioServiceMessage::code::TIMESTAMP;
207 result = writeUpMessageQueue(&command);
208 }
209 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800210}
211
Phil Burkc0c70e32017-02-09 13:18:38 -0800212/**
213 * Get an immutable description of the in-memory queues
214 * used to communicate with the underlying HAL or Service.
215 */
216aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
217 // Gather information on the message queue.
218 mUpMessageQueue->fillParcelable(parcelable,
219 parcelable.mUpMessageQueueParcelable);
220 return getDownDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700221}