• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace 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 
34 class AAudioEndpointManager;
35 class AAudioServiceEndpoint;
36 class 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  */
43 class AAudioServiceStreamShared : public AAudioServiceStreamBase {
44 
45 public:
46     explicit AAudioServiceStreamShared(android::AAudioService &aAudioService);
47     ~AAudioServiceStreamShared() override = default;
48 
49     static std::string dumpHeader();
50 
51     std::string dump() const override;
52 
53     aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override
54             EXCLUDES(mUpMessageQueueLock);
55 
56     void writeDataIfRoom(int64_t mmapFramesRead, const void *buffer, int32_t numFrames);
57 
58     /**
59      * This must only be called under getAudioDataQueueLock().
60      * @return
61      */
getAudioDataQueue_l()62     std::shared_ptr<SharedRingBuffer> getAudioDataQueue_l()
63             REQUIRES(audioDataQueueLock) {
64         return mAudioDataQueue;
65     }
66 
67     /* Keep a record of when a buffer transfer completed.
68      * This allows for a more accurate timing model.
69      */
70     void markTransferTime(Timestamp &timestamp);
71 
setTimestampPositionOffset(int64_t deltaFrames)72     void setTimestampPositionOffset(int64_t deltaFrames) {
73         mTimestampPositionOffset.store(deltaFrames);
74     }
75 
incrementXRunCount()76     void incrementXRunCount() {
77         sendXRunCount(++mXRunCount);
78     }
79 
getXRunCount()80     int32_t getXRunCount() const {
81         return mXRunCount.load();
82     }
83 
getTypeText()84     const char *getTypeText() const override { return "Shared"; }
85 
86     // This is public so that the thread safety annotation, GUARDED_BY(),
87     // Can work when another object takes the lock.
88     mutable std::mutex   audioDataQueueLock;
89 
90 protected:
91 
92     aaudio_result_t getAudioDataDescription_l(
93             AudioEndpointParcelable* parcelable) REQUIRES(mLock) override;
94 
95     aaudio_result_t getFreeRunningPosition_l(
96             int64_t *positionFrames, int64_t *timeNanos) REQUIRES(mLock) override;
97 
98     aaudio_result_t getHardwareTimestamp_l(
99             int64_t *positionFrames, int64_t *timeNanos) REQUIRES(mLock) override;
100 
101     /**
102      * @param requestedCapacityFrames
103      * @param framesPerBurst
104      * @return capacity or negative error
105      */
106     static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
107                                             int32_t framesPerBurst);
108 
109 private:
110 
111     std::shared_ptr<SharedRingBuffer> mAudioDataQueue PT_GUARDED_BY(audioDataQueueLock);
112 
113     std::atomic<int64_t>     mTimestampPositionOffset;
114     std::atomic<int32_t>     mXRunCount;
115 
116 };
117 
118 } /* namespace aaudio */
119 
120 #endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
121