blob: b69dc462bbf02cef31212ae9af28cd6c0a713d74 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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_MMAP_H
18#define AAUDIO_AAUDIO_SERVICE_STREAM_MMAP_H
19
20#include <atomic>
21
Phil Burk37417522017-08-08 13:20:45 -070022#include <android-base/unique_fd.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <media/audiohal/StreamHalInterface.h>
24#include <media/MmapStreamCallback.h>
25#include <media/MmapStreamInterface.h>
26#include <utils/RefBase.h>
27#include <utils/String16.h>
28#include <utils/Vector.h>
29
30#include "binding/AAudioServiceMessage.h"
31#include "AAudioServiceStreamBase.h"
32#include "binding/AudioEndpointParcelable.h"
33#include "SharedMemoryProxy.h"
34#include "TimestampScheduler.h"
35#include "utility/MonotonicCounter.h"
36
Phil Burk37417522017-08-08 13:20:45 -070037
Phil Burkc0c70e32017-02-09 13:18:38 -080038namespace aaudio {
39
40 /**
41 * Manage one memory mapped buffer that originated from a HAL.
42 */
43class AAudioServiceStreamMMAP
44 : public AAudioServiceStreamBase
45 , public android::MmapStreamCallback {
46
47public:
Eric Laurentcb4dae22017-07-01 19:39:32 -070048 AAudioServiceStreamMMAP(const android::AudioClient& serviceClient, bool inService);
Phil Burk98d6d922017-07-06 11:52:45 -070049 virtual ~AAudioServiceStreamMMAP() = default;
Phil Burkc0c70e32017-02-09 13:18:38 -080050
51 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
52 aaudio::AAudioStreamConfiguration &configurationOutput) override;
53
54 /**
55 * Start the flow of audio data.
56 *
57 * This is not guaranteed to be synchronous but it currently is.
58 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
59 */
60 aaudio_result_t start() override;
61
62 /**
63 * Stop the flow of data so that start() can resume without loss of data.
64 *
65 * This is not guaranteed to be synchronous but it currently is.
66 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
67 */
68 aaudio_result_t pause() override;
69
Phil Burk71f35bb2017-04-13 16:05:07 -070070 aaudio_result_t stop() override;
71
Phil Burkc0c70e32017-02-09 13:18:38 -080072 /**
73 * Discard any data held by the underlying HAL or Service.
74 *
75 * This is not guaranteed to be synchronous but it currently is.
76 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
77 */
78 aaudio_result_t flush() override;
79
80 aaudio_result_t close() override;
81
Eric Laurentcb4dae22017-07-01 19:39:32 -070082 virtual aaudio_result_t startClient(const android::AudioClient& client,
83 audio_port_handle_t *clientHandle);
84
85 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle);
86
Phil Burkc0c70e32017-02-09 13:18:38 -080087 /**
88 * Send a MMAP/NOIRQ buffer timestamp to the client.
89 */
90 aaudio_result_t sendCurrentTimestamp();
91
92 // -------------- Callback functions ---------------------
93 void onTearDown() override;
94
95 void onVolumeChanged(audio_channel_mask_t channels,
96 android::Vector<float> values) override;
97
98 void onRoutingChanged(audio_port_handle_t deviceId) override;
99
100protected:
101
102 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
103
104 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
105
106private:
107 // This proxy class was needed to prevent a crash in AudioFlinger
108 // when the stream was closed.
109 class MyMmapStreamCallback : public android::MmapStreamCallback {
110 public:
111 explicit MyMmapStreamCallback(android::MmapStreamCallback &serviceCallback)
112 : mServiceCallback(serviceCallback){}
113 virtual ~MyMmapStreamCallback() = default;
114
115 void onTearDown() override {
116 mServiceCallback.onTearDown();
117 };
118
119 void onVolumeChanged(audio_channel_mask_t channels, android::Vector<float> values) override
120 {
121 mServiceCallback.onVolumeChanged(channels, values);
122 };
123
124 void onRoutingChanged(audio_port_handle_t deviceId) override {
125 mServiceCallback.onRoutingChanged(deviceId);
126 };
127
128 private:
129 android::MmapStreamCallback &mServiceCallback;
130 };
131
132 android::sp<MyMmapStreamCallback> mMmapStreamCallback;
133 MonotonicCounter mFramesWritten;
134 MonotonicCounter mFramesRead;
135 int32_t mPreviousFrameCounter = 0; // from HAL
Phil Burkc0c70e32017-02-09 13:18:38 -0800136
137 // Interface to the AudioFlinger MMAP support.
138 android::sp<android::MmapStreamInterface> mMmapStream;
139 struct audio_mmap_buffer_info mMmapBufferinfo;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700140 audio_port_handle_t mPortHandle = AUDIO_PORT_HANDLE_NONE;
141 audio_port_handle_t mDeviceId = AUDIO_PORT_HANDLE_NONE;
142 android::AudioClient mServiceClient;
143 bool mInService = false;
Phil Burk37417522017-08-08 13:20:45 -0700144 android::base::unique_fd mAudioDataFileDescriptor;
Phil Burkc0c70e32017-02-09 13:18:38 -0800145};
146
147} // namespace aaudio
148
149#endif //AAUDIO_AAUDIO_SERVICE_STREAM_MMAP_H