blob: 257bea96cf7e0d8f9989625a484ddafabc1bab6f [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
22#include <media/audiohal/StreamHalInterface.h>
23#include <media/MmapStreamCallback.h>
24#include <media/MmapStreamInterface.h>
25#include <utils/RefBase.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
28
29#include "binding/AAudioServiceMessage.h"
30#include "AAudioServiceStreamBase.h"
31#include "binding/AudioEndpointParcelable.h"
32#include "SharedMemoryProxy.h"
33#include "TimestampScheduler.h"
34#include "utility/MonotonicCounter.h"
35
36namespace aaudio {
37
38 /**
39 * Manage one memory mapped buffer that originated from a HAL.
40 */
41class AAudioServiceStreamMMAP
42 : public AAudioServiceStreamBase
43 , public android::MmapStreamCallback {
44
45public:
Eric Laurentd51329e2017-06-30 16:06:16 -070046 AAudioServiceStreamMMAP(uid_t serviceUid);
Phil Burk98d6d922017-07-06 11:52:45 -070047 virtual ~AAudioServiceStreamMMAP() = default;
Phil Burkc0c70e32017-02-09 13:18:38 -080048
49 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
50 aaudio::AAudioStreamConfiguration &configurationOutput) override;
51
52 /**
53 * Start the flow of audio data.
54 *
55 * This is not guaranteed to be synchronous but it currently is.
56 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
57 */
58 aaudio_result_t start() override;
59
60 /**
61 * Stop the flow of data so that start() can resume without loss of data.
62 *
63 * This is not guaranteed to be synchronous but it currently is.
64 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
65 */
66 aaudio_result_t pause() override;
67
Phil Burk71f35bb2017-04-13 16:05:07 -070068 aaudio_result_t stop() override;
69
Phil Burkc0c70e32017-02-09 13:18:38 -080070 /**
71 * Discard any data held by the underlying HAL or Service.
72 *
73 * This is not guaranteed to be synchronous but it currently is.
74 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
75 */
76 aaudio_result_t flush() override;
77
78 aaudio_result_t close() override;
79
80 /**
81 * Send a MMAP/NOIRQ buffer timestamp to the client.
82 */
83 aaudio_result_t sendCurrentTimestamp();
84
85 // -------------- Callback functions ---------------------
86 void onTearDown() override;
87
88 void onVolumeChanged(audio_channel_mask_t channels,
89 android::Vector<float> values) override;
90
91 void onRoutingChanged(audio_port_handle_t deviceId) override;
92
93protected:
94
95 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
96
97 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
98
99private:
100 // This proxy class was needed to prevent a crash in AudioFlinger
101 // when the stream was closed.
102 class MyMmapStreamCallback : public android::MmapStreamCallback {
103 public:
104 explicit MyMmapStreamCallback(android::MmapStreamCallback &serviceCallback)
105 : mServiceCallback(serviceCallback){}
106 virtual ~MyMmapStreamCallback() = default;
107
108 void onTearDown() override {
109 mServiceCallback.onTearDown();
110 };
111
112 void onVolumeChanged(audio_channel_mask_t channels, android::Vector<float> values) override
113 {
114 mServiceCallback.onVolumeChanged(channels, values);
115 };
116
117 void onRoutingChanged(audio_port_handle_t deviceId) override {
118 mServiceCallback.onRoutingChanged(deviceId);
119 };
120
121 private:
122 android::MmapStreamCallback &mServiceCallback;
123 };
124
125 android::sp<MyMmapStreamCallback> mMmapStreamCallback;
126 MonotonicCounter mFramesWritten;
127 MonotonicCounter mFramesRead;
128 int32_t mPreviousFrameCounter = 0; // from HAL
Phil Burk942bdc02017-05-03 11:36:50 -0700129 int mAudioDataFileDescriptor = -1;
Phil Burkc0c70e32017-02-09 13:18:38 -0800130
131 // Interface to the AudioFlinger MMAP support.
132 android::sp<android::MmapStreamInterface> mMmapStream;
133 struct audio_mmap_buffer_info mMmapBufferinfo;
134 android::MmapStreamInterface::Client mMmapClient;
135 audio_port_handle_t mPortHandle = -1; // TODO review best default
Eric Laurentd51329e2017-06-30 16:06:16 -0700136 uid_t mCachedUserId = -1;
Phil Burkc0c70e32017-02-09 13:18:38 -0800137};
138
139} // namespace aaudio
140
141#endif //AAUDIO_AAUDIO_SERVICE_STREAM_MMAP_H