• 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     virtual ~AAudioServiceStreamShared() = 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 
55     void writeDataIfRoom(int64_t mmapFramesRead, const void *buffer, int32_t numFrames);
56 
57     /**
58      * This must only be called under getAudioDataQueueLock().
59      * @return
60      */
getAudioDataQueue_l()61     std::shared_ptr<SharedRingBuffer> getAudioDataQueue_l()
62             REQUIRES(audioDataQueueLock) {
63         return mAudioDataQueue;
64     }
65 
66     /* Keep a record of when a buffer transfer completed.
67      * This allows for a more accurate timing model.
68      */
69     void markTransferTime(Timestamp &timestamp);
70 
setTimestampPositionOffset(int64_t deltaFrames)71     void setTimestampPositionOffset(int64_t deltaFrames) {
72         mTimestampPositionOffset.store(deltaFrames);
73     }
74 
incrementXRunCount()75     void incrementXRunCount() {
76         sendXRunCount(++mXRunCount);
77     }
78 
getXRunCount()79     int32_t getXRunCount() const {
80         return mXRunCount.load();
81     }
82 
getTypeText()83     const char *getTypeText() const override { return "Shared"; }
84 
85     // This is public so that the thread safety annotation, GUARDED_BY(),
86     // Can work when another object takes the lock.
87     mutable std::mutex   audioDataQueueLock;
88 
89 protected:
90 
91     aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override;
92 
93     aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
94 
95     aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
96 
97     /**
98      * @param requestedCapacityFrames
99      * @param framesPerBurst
100      * @return capacity or negative error
101      */
102     static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
103                                             int32_t framesPerBurst);
104 
105 private:
106 
107     std::shared_ptr<SharedRingBuffer> mAudioDataQueue GUARDED_BY(audioDataQueueLock);
108 
109     std::atomic<int64_t>     mTimestampPositionOffset;
110     std::atomic<int32_t>     mXRunCount;
111 
112 };
113 
114 } /* namespace aaudio */
115 
116 #endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
117