blob: f121c5c5422d2153d0499a711472de35f1ed816f [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:
46 AAudioServiceStreamMMAP();
47 virtual ~AAudioServiceStreamMMAP();
48
49
50 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
51 aaudio::AAudioStreamConfiguration &configurationOutput) override;
52
53 /**
54 * Start the flow of audio data.
55 *
56 * This is not guaranteed to be synchronous but it currently is.
57 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
58 */
59 aaudio_result_t start() override;
60
61 /**
62 * Stop the flow of data so that start() can resume without loss of data.
63 *
64 * This is not guaranteed to be synchronous but it currently is.
65 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
66 */
67 aaudio_result_t pause() override;
68
69 /**
70 * Discard any data held by the underlying HAL or Service.
71 *
72 * This is not guaranteed to be synchronous but it currently is.
73 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
74 */
75 aaudio_result_t flush() override;
76
77 aaudio_result_t close() override;
78
79 /**
80 * Send a MMAP/NOIRQ buffer timestamp to the client.
81 */
82 aaudio_result_t sendCurrentTimestamp();
83
84 // -------------- Callback functions ---------------------
85 void onTearDown() override;
86
87 void onVolumeChanged(audio_channel_mask_t channels,
88 android::Vector<float> values) override;
89
90 void onRoutingChanged(audio_port_handle_t deviceId) override;
91
92protected:
93
94 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
95
96 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
97
98private:
99 // This proxy class was needed to prevent a crash in AudioFlinger
100 // when the stream was closed.
101 class MyMmapStreamCallback : public android::MmapStreamCallback {
102 public:
103 explicit MyMmapStreamCallback(android::MmapStreamCallback &serviceCallback)
104 : mServiceCallback(serviceCallback){}
105 virtual ~MyMmapStreamCallback() = default;
106
107 void onTearDown() override {
108 mServiceCallback.onTearDown();
109 };
110
111 void onVolumeChanged(audio_channel_mask_t channels, android::Vector<float> values) override
112 {
113 mServiceCallback.onVolumeChanged(channels, values);
114 };
115
116 void onRoutingChanged(audio_port_handle_t deviceId) override {
117 mServiceCallback.onRoutingChanged(deviceId);
118 };
119
120 private:
121 android::MmapStreamCallback &mServiceCallback;
122 };
123
124 android::sp<MyMmapStreamCallback> mMmapStreamCallback;
125 MonotonicCounter mFramesWritten;
126 MonotonicCounter mFramesRead;
127 int32_t mPreviousFrameCounter = 0; // from HAL
128
129 // Interface to the AudioFlinger MMAP support.
130 android::sp<android::MmapStreamInterface> mMmapStream;
131 struct audio_mmap_buffer_info mMmapBufferinfo;
132 android::MmapStreamInterface::Client mMmapClient;
133 audio_port_handle_t mPortHandle = -1; // TODO review best default
134};
135
136} // namespace aaudio
137
138#endif //AAUDIO_AAUDIO_SERVICE_STREAM_MMAP_H