blob: ee0e7ed047a01af5ffd4a455ab123df61f859429 [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;
Phil Burk942bdc02017-05-03 11:36:50 -070062
Phil Burkc0c70e32017-02-09 13:18:38 -080063 return AAUDIO_OK;
64}
65
66aaudio_result_t AAudioServiceStreamBase::start() {
67 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() {
Phil Burkc0c70e32017-02-09 13:18:38 -080074 sendCurrentTimestamp();
75 mThreadEnabled.store(false);
76 aaudio_result_t result = mAAudioThread.stop();
77 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -070078 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -080079 return result;
80 }
81 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
82 mState = AAUDIO_STREAM_STATE_PAUSED;
83 return result;
84}
85
Phil Burk71f35bb2017-04-13 16:05:07 -070086aaudio_result_t AAudioServiceStreamBase::stop() {
87 // TODO wait for data to be played out
88 sendCurrentTimestamp();
89 mThreadEnabled.store(false);
90 aaudio_result_t result = mAAudioThread.stop();
91 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -070092 processFatalError();
Phil Burk71f35bb2017-04-13 16:05:07 -070093 return result;
94 }
Phil Burk71f35bb2017-04-13 16:05:07 -070095 sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED);
96 mState = AAUDIO_STREAM_STATE_STOPPED;
97 return result;
98}
99
100aaudio_result_t AAudioServiceStreamBase::flush() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700101 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
102 mState = AAUDIO_STREAM_STATE_FLUSHED;
103 return AAUDIO_OK;
104}
105
Phil Burkcf5f6d22017-05-26 12:35:07 -0700106// implement Runnable, periodically send timestamps to client
Phil Burkc0c70e32017-02-09 13:18:38 -0800107void AAudioServiceStreamBase::run() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700108 ALOGD("AAudioServiceStreamBase::run() entering ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 TimestampScheduler timestampScheduler;
110 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
111 timestampScheduler.start(AudioClock::getNanoseconds());
112 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
113 while(mThreadEnabled.load()) {
114 if (AudioClock::getNanoseconds() >= nextTime) {
115 aaudio_result_t result = sendCurrentTimestamp();
116 if (result != AAUDIO_OK) {
117 break;
118 }
119 nextTime = timestampScheduler.nextAbsoluteTime();
120 } else {
121 // Sleep until it is time to send the next timestamp.
122 AudioClock::sleepUntilNanoTime(nextTime);
123 }
124 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700125 ALOGD("AAudioServiceStreamBase::run() exiting ----------------");
Phil Burkc0c70e32017-02-09 13:18:38 -0800126}
127
Phil Burkec89b2e2017-06-20 15:05:06 -0700128void AAudioServiceStreamBase::processFatalError() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
130}
131
132aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
133 double dataDouble,
134 int64_t dataLong) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800135 AAudioServiceMessage command;
136 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800137 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 command.event.dataDouble = dataDouble;
139 command.event.dataLong = dataLong;
140 return writeUpMessageQueue(&command);
141}
142
143aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
144 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
Phil Burk71f35bb2017-04-13 16:05:07 -0700145 if (mUpMessageQueue == nullptr) {
146 ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open");
147 return AAUDIO_ERROR_NULL;
148 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
150 if (count != 1) {
151 ALOGE("writeUpMessageQueue(): Queue full. Did client die?");
152 return AAUDIO_ERROR_WOULD_BLOCK;
153 } else {
154 return AAUDIO_OK;
155 }
156}
157
158aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
159 AAudioServiceMessage command;
160 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
161 &command.timestamp.timestamp);
162 if (result == AAUDIO_OK) {
Phil Burk87c9f642017-05-17 07:22:39 -0700163 // ALOGD("sendCurrentTimestamp(): position = %lld, nanos = %lld",
164 // (long long) command.timestamp.position,
165 // (long long) command.timestamp.timestamp);
Phil Burkc0c70e32017-02-09 13:18:38 -0800166 command.what = AAudioServiceMessage::code::TIMESTAMP;
167 result = writeUpMessageQueue(&command);
168 }
169 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800170}
171
Phil Burkc0c70e32017-02-09 13:18:38 -0800172/**
173 * Get an immutable description of the in-memory queues
174 * used to communicate with the underlying HAL or Service.
175 */
176aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
177 // Gather information on the message queue.
178 mUpMessageQueue->fillParcelable(parcelable,
179 parcelable.mUpMessageQueueParcelable);
180 return getDownDataDescription(parcelable);
181}