blob: b15043d6660cbd8dda7c781b7014769f174eaa67 [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() {
66 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
67 mState = AAUDIO_STREAM_STATE_STARTED;
68 mThreadEnabled.store(true);
69 return mAAudioThread.start(this);
70}
71
72aaudio_result_t AAudioServiceStreamBase::pause() {
73
74 sendCurrentTimestamp();
75 mThreadEnabled.store(false);
76 aaudio_result_t result = mAAudioThread.stop();
77 if (result != AAUDIO_OK) {
78 processError();
79 return result;
80 }
81 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
82 mState = AAUDIO_STREAM_STATE_PAUSED;
83 return result;
84}
85
86// implement Runnable
87void AAudioServiceStreamBase::run() {
88 ALOGD("AAudioServiceStreamMMAP::run() entering ----------------");
89 TimestampScheduler timestampScheduler;
90 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
91 timestampScheduler.start(AudioClock::getNanoseconds());
92 int64_t nextTime = timestampScheduler.nextAbsoluteTime();
93 while(mThreadEnabled.load()) {
94 if (AudioClock::getNanoseconds() >= nextTime) {
95 aaudio_result_t result = sendCurrentTimestamp();
96 if (result != AAUDIO_OK) {
97 break;
98 }
99 nextTime = timestampScheduler.nextAbsoluteTime();
100 } else {
101 // Sleep until it is time to send the next timestamp.
102 AudioClock::sleepUntilNanoTime(nextTime);
103 }
104 }
105 ALOGD("AAudioServiceStreamMMAP::run() exiting ----------------");
106}
107
108void AAudioServiceStreamBase::processError() {
109 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
110}
111
112aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event,
113 double dataDouble,
114 int64_t dataLong) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800115 AAudioServiceMessage command;
116 command.what = AAudioServiceMessage::code::EVENT;
Phil Burk2355edb2016-12-26 13:54:02 -0800117 command.event.event = event;
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 command.event.dataDouble = dataDouble;
119 command.event.dataLong = dataLong;
120 return writeUpMessageQueue(&command);
121}
122
123aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) {
124 std::lock_guard<std::mutex> lock(mLockUpMessageQueue);
125 int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1);
126 if (count != 1) {
127 ALOGE("writeUpMessageQueue(): Queue full. Did client die?");
128 return AAUDIO_ERROR_WOULD_BLOCK;
129 } else {
130 return AAUDIO_OK;
131 }
132}
133
134aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() {
135 AAudioServiceMessage command;
136 aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position,
137 &command.timestamp.timestamp);
138 if (result == AAUDIO_OK) {
139 command.what = AAudioServiceMessage::code::TIMESTAMP;
140 result = writeUpMessageQueue(&command);
141 }
142 return result;
Phil Burk2355edb2016-12-26 13:54:02 -0800143}
144
145
Phil Burkc0c70e32017-02-09 13:18:38 -0800146/**
147 * Get an immutable description of the in-memory queues
148 * used to communicate with the underlying HAL or Service.
149 */
150aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) {
151 // Gather information on the message queue.
152 mUpMessageQueue->fillParcelable(parcelable,
153 parcelable.mUpMessageQueueParcelable);
154 return getDownDataDescription(parcelable);
155}