blob: d8882c98f9cc4e104f0030023befbfbf73eb19f1 [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 Burk2355edb2016-12-26 13:54:02 -080045}
46
Phil Burkc0c70e32017-02-09 13:18:38 -080047aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request,
48 aaudio::AAudioStreamConfiguration &configurationOutput) {
49 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
50 if (mUpMessageQueue != nullptr) {
51 return AAUDIO_ERROR_INVALID_STATE;
52 } else {
53 mUpMessageQueue = new SharedRingBuffer();
54 return mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), QUEUE_UP_CAPACITY_COMMANDS);
55 }
56}
Phil Burkdec33ab2017-01-17 14:48:16 -080057
Phil Burkc0c70e32017-02-09 13:18:38 -080058aaudio_result_t AAudioServiceStreamBase::close() {
59 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
60 delete mUpMessageQueue;
61 mUpMessageQueue = nullptr;
62 return AAUDIO_OK;
63}
64
65aaudio_result_t AAudioServiceStreamBase::start() {
Phil Burk71f35bb2017-04-13 16:05:07 -070066 ALOGD("AAudioServiceStreamBase::start() send AAUDIO_SERVICE_EVENT_STARTED");
Phil Burkc0c70e32017-02-09 13:18:38 -080067 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
68 mState = AAUDIO_STREAM_STATE_STARTED;
69 mThreadEnabled.store(true);
70 return mAAudioThread.start(this);
71}
72
73aaudio_result_t AAudioServiceStreamBase::pause() {
74
75 sendCurrentTimestamp();
76 mThreadEnabled.store(false);
77 aaudio_result_t result = mAAudioThread.stop();
78 if (result != AAUDIO_OK) {
79 processError();
80 return result;
81 }
Phil Burk71f35bb2017-04-13 16:05:07 -070082 ALOGD("AAudioServiceStreamBase::pause() send AAUDIO_SERVICE_EVENT_PAUSED");
Phil Burkc0c70e32017-02-09 13:18:38 -080083 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
84 mState = AAUDIO_STREAM_STATE_PAUSED;
85 return result;
86}
87
Phil Burk71f35bb2017-04-13 16:05:07 -070088aaudio_result_t AAudioServiceStreamBase::stop() {
89 // TODO wait for data to be played out
90 sendCurrentTimestamp();
91 mThreadEnabled.store(false);
92 aaudio_result_t result = mAAudioThread.stop();
93 if (result != AAUDIO_OK) {
94 processError();
95 return result;
96 }
97 ALOGD("AAudioServiceStreamBase::stop() send AAUDIO_SERVICE_EVENT_STOPPED");
98 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
99 mState = AAUDIO_STREAM_STATE_STOPPED;
100 return result;
101}
102
103aaudio_result_t AAudioServiceStreamBase::flush() {
104 ALOGD("AAudioServiceStreamBase::flush() send AAUDIO_SERVICE_EVENT_FLUSHED");
105 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
106 mState = AAUDIO_STREAM_STATE_FLUSHED;
107 return AAUDIO_OK;
108}
109
Phil Burkc0c70e32017-02-09 13:18:38 -0800110// implement Runnable
111void AAudioServiceStreamBase::run() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700112 ALOGD("AAudioServiceStreamBase::run() entering ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800113 TimestampScheduler timestampScheduler;
114 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
115 timestampScheduler.start(AudioClock::getNanoseconds());
116 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
117 while(mThreadEnabled.load()) {
118 if (AudioClock::getNanoseconds() >= nextTime) {
119 aaudio_result_t result = sendCurrentTimestamp();
120 if (result != AAUDIO_OK) {
121 break;
122 }
123 nextTime = timestampScheduler.nextAbsoluteTime();
124 } else {
125 // Sleep until it is time to send the next timestamp.
126 AudioClock::sleepUntilNanoTime(nextTime);
127 }
128 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700129 ALOGD("AAudioServiceStreamBase::run() exiting ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800130}
131
132void AAudioServiceStreamBase::processError() {
133 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
134}
135
136aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
137 double dataDouble,
138 int64_t dataLong) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800139 AAudioServiceMessage command;
140 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800141 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 command.event.dataDouble = dataDouble;
143 command.event.dataLong = dataLong;
144 return writeUpMessageQueue(&command);
145}
146
147aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
148 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
Phil Burk71f35bb2017-04-13 16:05:07 -0700149 if (mUpMessageQueue == nullptr) {
150 ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open");
151 return AAUDIO_ERROR_NULL;
152 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
154 if (count != 1) {
155 ALOGE("writeUpMessageQueue(): Queue full. Did client die?");
156 return AAUDIO_ERROR_WOULD_BLOCK;
157 } else {
158 return AAUDIO_OK;
159 }
160}
161
162aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
163 AAudioServiceMessage command;
Phil Burk71f35bb2017-04-13 16:05:07 -0700164 //ALOGD("sendCurrentTimestamp() called");
Phil Burkc0c70e32017-02-09 13:18:38 -0800165 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
166 &command.timestamp.timestamp);
167 if (result == AAUDIO_OK) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700168 //ALOGD("sendCurrentTimestamp(): position %d", (int) command.timestamp.position);
Phil Burkc0c70e32017-02-09 13:18:38 -0800169 command.what = AAudioServiceMessage::code::TIMESTAMP;
170 result = writeUpMessageQueue(&command);
171 }
172 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800173}
174
175
Phil Burkc0c70e32017-02-09 13:18:38 -0800176/**
177 * Get an immutable description of the in-memory queues
178 * used to communicate with the underlying HAL or Service.
179 */
180aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
181 // Gather information on the message queue.
182 mUpMessageQueue->fillParcelable(parcelable,
183 parcelable.mUpMessageQueueParcelable);
184 return getDownDataDescription(parcelable);
185}