blob: e631fd36143182762a3a0c3fa0ccd0a3ae039d0a [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 Burke72481c2017-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 Burke72481c2017-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 Laurenta54f1282017-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 Laurenta54f1282017-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;
Phil Burk97350f92017-07-21 15:59:44 -0700105 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames,
106 int64_t *timeNanos) override;
Phil Burkc0c70e32017-02-09 13:18:38 -0800107
108private:
109 // This proxy class was needed to prevent a crash in AudioFlinger
110 // when the stream was closed.
111 class MyMmapStreamCallback : public android::MmapStreamCallback {
112 public:
113 explicit MyMmapStreamCallback(android::MmapStreamCallback &serviceCallback)
114 : mServiceCallback(serviceCallback){}
115 virtual ~MyMmapStreamCallback() = default;
116
117 void onTearDown() override {
118 mServiceCallback.onTearDown();
119 };
120
121 void onVolumeChanged(audio_channel_mask_t channels, android::Vector<float> values) override
122 {
123 mServiceCallback.onVolumeChanged(channels, values);
124 };
125
126 void onRoutingChanged(audio_port_handle_t deviceId) override {
127 mServiceCallback.onRoutingChanged(deviceId);
128 };
129
130 private:
131 android::MmapStreamCallback &mServiceCallback;
132 };
133
134 android::sp<MyMmapStreamCallback> mMmapStreamCallback;
135 MonotonicCounter mFramesWritten;
136 MonotonicCounter mFramesRead;
137 int32_t mPreviousFrameCounter = 0; // from HAL
Phil Burk97350f92017-07-21 15:59:44 -0700138 int64_t mHardwareTimeOffsetNanos = 0; // TODO get from HAL
Phil Burkc0c70e32017-02-09 13:18:38 -0800139
Phil Burke72481c2017-08-08 13:20:45 -0700140
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 // Interface to the AudioFlinger MMAP support.
142 android::sp<android::MmapStreamInterface> mMmapStream;
143 struct audio_mmap_buffer_info mMmapBufferinfo;
Eric Laurenta54f1282017-07-01 19:39:32 -0700144 audio_port_handle_t mPortHandle = AUDIO_PORT_HANDLE_NONE;
145 audio_port_handle_t mDeviceId = AUDIO_PORT_HANDLE_NONE;
146 android::AudioClient mServiceClient;
147 bool mInService = false;
Phil Burke72481c2017-08-08 13:20:45 -0700148 android::base::unique_fd mAudioDataFileDescriptor;
Phil Burkc0c70e32017-02-09 13:18:38 -0800149};
150
151} // namespace aaudio
152
153#endif //AAUDIO_AAUDIO_SERVICE_STREAM_MMAP_H