blob: 533e5a8ad00a05b1a02d0de8d1370ad143effff0 [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 Laurenta54f1282017-07-01 19:39:32 -070046 AAudioServiceStreamMMAP(const android::AudioClient& serviceClient, bool inService);
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
Eric Laurenta54f1282017-07-01 19:39:32 -070080 virtual aaudio_result_t startClient(const android::AudioClient& client,
81 audio_port_handle_t *clientHandle);
82
83 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle);
84
Phil Burkc0c70e32017-02-09 13:18:38 -080085 /**
86 * Send a MMAP/NOIRQ buffer timestamp to the client.
87 */
88 aaudio_result_t sendCurrentTimestamp();
89
90 // -------------- Callback functions ---------------------
91 void onTearDown() override;
92
93 void onVolumeChanged(audio_channel_mask_t channels,
94 android::Vector<float> values) override;
95
96 void onRoutingChanged(audio_port_handle_t deviceId) override;
97
98protected:
99
100 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
101
102 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
103
104private:
105 // This proxy class was needed to prevent a crash in AudioFlinger
106 // when the stream was closed.
107 class MyMmapStreamCallback : public android::MmapStreamCallback {
108 public:
109 explicit MyMmapStreamCallback(android::MmapStreamCallback &serviceCallback)
110 : mServiceCallback(serviceCallback){}
111 virtual ~MyMmapStreamCallback() = default;
112
113 void onTearDown() override {
114 mServiceCallback.onTearDown();
115 };
116
117 void onVolumeChanged(audio_channel_mask_t channels, android::Vector<float> values) override
118 {
119 mServiceCallback.onVolumeChanged(channels, values);
120 };
121
122 void onRoutingChanged(audio_port_handle_t deviceId) override {
123 mServiceCallback.onRoutingChanged(deviceId);
124 };
125
126 private:
127 android::MmapStreamCallback &mServiceCallback;
128 };
129
130 android::sp<MyMmapStreamCallback> mMmapStreamCallback;
131 MonotonicCounter mFramesWritten;
132 MonotonicCounter mFramesRead;
133 int32_t mPreviousFrameCounter = 0; // from HAL
Phil Burk942bdc02017-05-03 11:36:50 -0700134 int mAudioDataFileDescriptor = -1;
Phil Burkc0c70e32017-02-09 13:18:38 -0800135
136 // Interface to the AudioFlinger MMAP support.
137 android::sp<android::MmapStreamInterface> mMmapStream;
138 struct audio_mmap_buffer_info mMmapBufferinfo;
Eric Laurenta54f1282017-07-01 19:39:32 -0700139 audio_port_handle_t mPortHandle = AUDIO_PORT_HANDLE_NONE;
140 audio_port_handle_t mDeviceId = AUDIO_PORT_HANDLE_NONE;
141 android::AudioClient mServiceClient;
142 bool mInService = false;
Phil Burkc0c70e32017-02-09 13:18:38 -0800143};
144
145} // namespace aaudio
146
147#endif //AAUDIO_AAUDIO_SERVICE_STREAM_MMAP_H