AAudioServiceStreamShared.h revision 71f35bb687476694882a617ba4a810a0bb56fe23
1/*
2 * Copyright (C) 2017 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
17#ifndef AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
18#define AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
19
20#include "fifo/FifoBuffer.h"
21#include "binding/AAudioServiceMessage.h"
22#include "binding/AAudioStreamRequest.h"
23#include "binding/AAudioStreamConfiguration.h"
24
25#include "AAudioService.h"
26#include "AAudioServiceStreamBase.h"
27
28namespace aaudio {
29
30// We expect the queue to only have a few commands.
31// This should be way more than we need.
32#define QUEUE_UP_CAPACITY_COMMANDS (128)
33
34class AAudioEndpointManager;
35class AAudioServiceEndpoint;
36class SharedRingBuffer;
37
38/**
39 * One of these is created for every MODE_SHARED stream in the AAudioService.
40 *
41 * Each Shared stream will register itself with an AAudioServiceEndpoint when it is opened.
42 */
43class AAudioServiceStreamShared : public AAudioServiceStreamBase {
44
45public:
46    AAudioServiceStreamShared(android::AAudioService &aAudioService);
47    virtual ~AAudioServiceStreamShared();
48
49    aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
50                         aaudio::AAudioStreamConfiguration &configurationOutput) override;
51
52    /**
53     * Start the flow of audio data.
54     *
55     * This is not guaranteed to be synchronous but it currently is.
56     * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
57     */
58    aaudio_result_t start() override;
59
60    /**
61     * Stop the flow of data so that start() can resume without loss of data.
62     *
63     * This is not guaranteed to be synchronous but it currently is.
64     * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
65    */
66    aaudio_result_t pause() override;
67
68    /**
69     * Stop the flow of data after data in buffer has played.
70     */
71    aaudio_result_t stop() override;
72
73    /**
74     *  Discard any data held by the underlying HAL or Service.
75     *
76     * This is not guaranteed to be synchronous but it currently is.
77     * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
78     */
79    aaudio_result_t flush() override;
80
81    aaudio_result_t close() override;
82
83    android::FifoBuffer *getDataFifoBuffer() { return mAudioDataQueue->getFifoBuffer(); }
84
85    /* Keep a record of when a buffer transfer completed.
86     * This allows for a more accurate timing model.
87     */
88    void markTransferTime(int64_t nanoseconds);
89
90    void onStop();
91
92    void onDisconnect();
93
94protected:
95
96    aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
97
98    aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
99
100private:
101    android::AAudioService  &mAudioService;
102    AAudioServiceEndpoint   *mServiceEndpoint = nullptr;
103    SharedRingBuffer        *mAudioDataQueue;
104
105    int64_t                  mMarkedPosition = 0;
106    int64_t                  mMarkedTime = 0;
107};
108
109} /* namespace aaudio */
110
111#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
112