blob: c88038f92806c56d4d4039aa6abf2363607061f3 [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 Burkc0c70e32017-02-09 13:18:38 -080044 close();
Phil Burk11e8d332017-05-24 09:59:02 -070045 ALOGD("AAudioServiceStreamBase::~AAudioServiceStreamBase() destroyed %p", this);
Phil Burk2355edb2016-12-26 13:54:02 -080046}
47
Phil Burk4501b352017-06-29 18:12:36 -070048std::string AAudioServiceStreamBase::dump() const {
49 std::stringstream result;
50
51 result << " -------- handle = 0x" << std::hex << mHandle << std::dec << "\n";
52 result << " state = " << AAudio_convertStreamStateToText(mState) << "\n";
53 result << " format = " << mAudioFormat << "\n";
54 result << " framesPerBurst = " << mFramesPerBurst << "\n";
55 result << " channelCount = " << mSamplesPerFrame << "\n";
56 result << " capacityFrames = " << mCapacityInFrames << "\n";
57 result << " owner uid = " << mOwnerUserId << "\n";
58
59 return result.str();
60}
61
Phil Burkc0c70e32017-02-09 13:18:38 -080062aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request,
63 aaudio::AAudioStreamConfiguration &configurationOutput) {
64 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
65 if (mUpMessageQueue != nullptr) {
66 return AAUDIO_ERROR_INVALID_STATE;
67 } else {
68 mUpMessageQueue = new SharedRingBuffer();
69 return mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), QUEUE_UP_CAPACITY_COMMANDS);
70 }
71}
Phil Burkdec33ab2017-01-17 14:48:16 -080072
Phil Burkc0c70e32017-02-09 13:18:38 -080073aaudio_result_t AAudioServiceStreamBase::close() {
Phil Burk11e8d332017-05-24 09:59:02 -070074 stop();
Phil Burkc0c70e32017-02-09 13:18:38 -080075 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
76 delete mUpMessageQueue;
77 mUpMessageQueue = nullptr;
Phil Burk942bdc02017-05-03 11:36:50 -070078
Phil Burkc0c70e32017-02-09 13:18:38 -080079 return AAUDIO_OK;
80}
81
82aaudio_result_t AAudioServiceStreamBase::start() {
83 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
84 mState = AAUDIO_STREAM_STATE_STARTED;
85 mThreadEnabled.store(true);
86 return mAAudioThread.start(this);
87}
88
89aaudio_result_t AAudioServiceStreamBase::pause() {
Phil Burk11e8d332017-05-24 09:59:02 -070090 aaudio_result_t result = AAUDIO_OK;
91 if (isRunning()) {
92 sendCurrentTimestamp();
93 mThreadEnabled.store(false);
94 result = mAAudioThread.stop();
95 if (result != AAUDIO_OK) {
Phil Burk5ef003b2017-06-30 11:43:37 -070096 disconnect();
Phil Burk11e8d332017-05-24 09:59:02 -070097 return result;
98 }
99 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 mState = AAUDIO_STREAM_STATE_PAUSED;
102 return result;
103}
104
Phil Burk71f35bb2017-04-13 16:05:07 -0700105aaudio_result_t AAudioServiceStreamBase::stop() {
Phil Burk11e8d332017-05-24 09:59:02 -0700106 aaudio_result_t result = AAUDIO_OK;
107 if (isRunning()) {
108 // TODO wait for data to be played out
109 sendCurrentTimestamp();
110 mThreadEnabled.store(false);
111 result = mAAudioThread.stop();
112 if (result != AAUDIO_OK) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700113 disconnect();
Phil Burk11e8d332017-05-24 09:59:02 -0700114 return result;
115 }
116 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
Phil Burk71f35bb2017-04-13 16:05:07 -0700117 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700118 mState = AAUDIO_STREAM_STATE_STOPPED;
119 return result;
120}
121
122aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700123 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
124 mState = AAUDIO_STREAM_STATE_FLUSHED;
125 return AAUDIO_OK;
126}
127
Phil Burkcf5f6d22017-05-26 12:35:07 -0700128// implement Runnable, periodically send timestamps to client
Phil Burkc0c70e32017-02-09 13:18:38 -0800129void AAudioServiceStreamBase::run() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700130 ALOGD("AAudioServiceStreamBase::run() entering ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 TimestampScheduler timestampScheduler;
132 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
133 timestampScheduler.start(AudioClock::getNanoseconds());
134 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
135 while(mThreadEnabled.load()) {
136 if (AudioClock::getNanoseconds() >= nextTime) {
137 aaudio_result_t result = sendCurrentTimestamp();
138 if (result != AAUDIO_OK) {
139 break;
140 }
141 nextTime = timestampScheduler.nextAbsoluteTime();
142 } else {
143 // Sleep until it is time to send the next timestamp.
144 AudioClock::sleepUntilNanoTime(nextTime);
145 }
146 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700147 ALOGD("AAudioServiceStreamBase::run() exiting ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800148}
149
Phil Burk5ef003b2017-06-30 11:43:37 -0700150void AAudioServiceStreamBase::disconnect() {
151 if (mState != AAUDIO_STREAM_STATE_DISCONNECTED) {
152 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
153 mState = AAUDIO_STREAM_STATE_DISCONNECTED;
154 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800155}
156
157aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
158 double dataDouble,
159 int64_t dataLong) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800160 AAudioServiceMessage command;
161 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800162 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800163 command.event.dataDouble = dataDouble;
164 command.event.dataLong = dataLong;
165 return writeUpMessageQueue(&command);
166}
167
168aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
169 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
Phil Burk71f35bb2017-04-13 16:05:07 -0700170 if (mUpMessageQueue == nullptr) {
171 ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open");
172 return AAUDIO_ERROR_NULL;
173 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800174 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
175 if (count != 1) {
176 ALOGE("writeUpMessageQueue(): Queue full. Did client die?");
177 return AAUDIO_ERROR_WOULD_BLOCK;
178 } else {
179 return AAUDIO_OK;
180 }
181}
182
183aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
184 AAudioServiceMessage command;
185 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
186 &command.timestamp.timestamp);
187 if (result == AAUDIO_OK) {
Phil Burk87c9f642017-05-17 07:22:39 -0700188 // ALOGD("sendCurrentTimestamp(): position = %lld, nanos = %lld",
189 // (long long) command.timestamp.position,
190 // (long long) command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800191 command.what = AAudioServiceMessage::code::TIMESTAMP;
192 result = writeUpMessageQueue(&command);
193 }
194 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800195}
196
Phil Burkc0c70e32017-02-09 13:18:38 -0800197/**
198 * Get an immutable description of the in-memory queues
199 * used to communicate with the underlying HAL or Service.
200 */
201aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
202 // Gather information on the message queue.
203 mUpMessageQueue->fillParcelable(parcelable,
204 parcelable.mUpMessageQueueParcelable);
205 return getDownDataDescription(parcelable);
Phil Burk11e8d332017-05-24 09:59:02 -0700206}