• 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_SERVICE_ENDPOINT_MMAP_H
18 #define AAUDIO_SERVICE_ENDPOINT_MMAP_H
19 
20 #include <atomic>
21 #include <functional>
22 #include <mutex>
23 #include <vector>
24 
25 #include "client/AudioStreamInternal.h"
26 #include "client/AudioStreamInternalPlay.h"
27 #include "binding/AAudioServiceMessage.h"
28 #include "AAudioServiceEndpointShared.h"
29 #include "AAudioServiceStreamShared.h"
30 #include "AAudioServiceStreamMMAP.h"
31 #include "AAudioMixer.h"
32 #include "AAudioService.h"
33 #include "SharedMemoryWrapper.h"
34 
35 namespace aaudio {
36 
37 /**
38  * This is used by AAudioServiceStreamMMAP to access the MMAP devices
39  * through AudioFlinger.
40  */
41 class AAudioServiceEndpointMMAP
42         : public AAudioServiceEndpoint
43         , public android::MmapStreamCallback {
44 
45 public:
46     explicit AAudioServiceEndpointMMAP(android::AAudioService &audioService);
47 
48     ~AAudioServiceEndpointMMAP() override = default;
49 
50     std::string dump() const override;
51 
52     aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override;
53 
54     void close() override;
55 
56     aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream,
57                                 audio_port_handle_t *clientHandle) override;
58 
59     aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream,
60                                audio_port_handle_t clientHandle) override;
61 
62     aaudio_result_t startClient(const android::AudioClient& client,
63                                 const audio_attributes_t *attr,
64                                 audio_port_handle_t *clientHandle)  override;
65 
66     aaudio_result_t stopClient(audio_port_handle_t clientHandle)  override;
67 
68     aaudio_result_t standby() override;
69 
70     aaudio_result_t exitStandby(AudioEndpointParcelable* parcelable) override;
71 
72     aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
73 
74     aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
75 
76     void handleTearDownAsync(audio_port_handle_t portHandle);
77 
78     // -------------- Callback functions for MmapStreamCallback ---------------------
79     void onTearDown(audio_port_handle_t portHandle) override;
80 
81     void onVolumeChanged(float volume) override;
82 
83     void onRoutingChanged(audio_port_handle_t portHandle) override;
84     // ------------------------------------------------------------------------------
85 
86     aaudio_result_t getDownDataDescription(AudioEndpointParcelable* parcelable);
87 
getHardwareTimeOffsetNanos()88     int64_t getHardwareTimeOffsetNanos() const {
89         return mHardwareTimeOffsetNanos;
90     }
91 
92     aaudio_result_t getExternalPosition(uint64_t *positionFrames, int64_t *timeNanos);
93 
94     int64_t nextDataReportTime();
95 
96     void reportData();
97 
98 private:
99 
100     aaudio_result_t openWithFormat(audio_format_t audioFormat, audio_format_t* nextFormatToTry);
101 
102     aaudio_result_t createMmapBuffer();
103 
104     MonotonicCounter                          mFramesTransferred;
105 
106     // Interface to the AudioFlinger MMAP support.
107     android::sp<android::MmapStreamInterface> mMmapStream;
108     struct audio_mmap_buffer_info             mMmapBufferinfo;
109 
110     // There is only one port associated with an MMAP endpoint.
111     audio_port_handle_t                       mPortHandle = AUDIO_PORT_HANDLE_NONE;
112 
113     android::AAudioService                    &mAAudioService;
114 
115     std::unique_ptr<SharedMemoryWrapper>      mAudioDataWrapper;
116 
117     int64_t                                   mHardwareTimeOffsetNanos = 0; // TODO get from HAL
118 
119     aaudio_result_t                           mHalExternalPositionStatus = AAUDIO_OK;
120     uint64_t                                  mLastPositionFrames = 0;
121     int64_t                                   mTimestampNanosForLastPosition = 0;
122     int32_t                                   mTimestampGracePeriodMs;
123     int32_t                                   mFrozenPositionCount = 0;
124     int32_t                                   mFrozenTimestampCount = 0;
125     int64_t                                   mDataReportOffsetNanos = 0;
126 
127 };
128 
129 } /* namespace aaudio */
130 
131 #endif //AAUDIO_SERVICE_ENDPOINT_MMAP_H
132 
133