blob: 01a02f1d9f057d01e59490c2484b5232303f12d4 [file] [log] [blame]
Rahul Ravikumar05336002019-10-14 15:04:32 -07001/*
2 * Copyright (C) 2019 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
17package android.media;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
22
23import com.android.internal.util.Preconditions;
24
25/**
26 * The HwAudioSource represents the audio playback directly from a source audio device.
27 * It currently supports {@link HwAudioSource#start()} and {@link HwAudioSource#stop()} only
28 * corresponding to {@link AudioSystem#startAudioSource(AudioPortConfig, AudioAttributes)}
29 * and {@link AudioSystem#stopAudioSource(int)}.
30 *
31 * @hide
32 */
33@SystemApi
34public class HwAudioSource extends PlayerBase {
35 private final AudioDeviceInfo mAudioDeviceInfo;
36 private final AudioAttributes mAudioAttributes;
37
38 private int mNativeHandle;
39
40 /**
41 * Class constructor for a hardware audio source based player.
42 *
43 * Use the {@link Builder} class to construct a {@link HwAudioSource} instance.
44 *
45 * @param device {@link AudioDeviceInfo} instance of the source audio device.
46 * @param attributes {@link AudioAttributes} instance for this player.
47 */
48 private HwAudioSource(@NonNull AudioDeviceInfo device, @NonNull AudioAttributes attributes) {
49 super(attributes, AudioPlaybackConfiguration.PLAYER_TYPE_HW_SOURCE);
50 Preconditions.checkNotNull(device);
51 Preconditions.checkNotNull(attributes);
52 Preconditions.checkArgument(device.isSource(), "Requires a source device");
53 mAudioDeviceInfo = device;
54 mAudioAttributes = attributes;
55 baseRegisterPlayer();
56 }
57
58 /**
59 * TODO: sets the gain on {@link #mAudioDeviceInfo}.
60 *
61 * @param muting if true, the player is to be muted, and the volume values can be ignored
62 * @param leftVolume the left volume to use if muting is false
63 * @param rightVolume the right volume to use if muting is false
64 */
65 @Override
66 void playerSetVolume(boolean muting, float leftVolume, float rightVolume) {
67 }
68
69 /**
70 * TODO: applies {@link VolumeShaper} on {@link #mAudioDeviceInfo}.
71 *
72 * @param configuration a {@code VolumeShaper.Configuration} object
73 * created by {@link VolumeShaper.Configuration.Builder} or
74 * an created from a {@code VolumeShaper} id
75 * by the {@link VolumeShaper.Configuration} constructor.
76 * @param operation a {@code VolumeShaper.Operation}.
77 * @return
78 */
79 @Override
80 int playerApplyVolumeShaper(
81 @NonNull VolumeShaper.Configuration configuration,
82 @NonNull VolumeShaper.Operation operation) {
83 return 0;
84 }
85
86 /**
87 * TODO: gets the {@link VolumeShaper} by a given id.
88 *
89 * @param id the {@code VolumeShaper} id returned from
90 * sending a fully specified {@code VolumeShaper.Configuration}
91 * through {@link #playerApplyVolumeShaper}
92 * @return
93 */
94 @Override
95 @Nullable
96 VolumeShaper.State playerGetVolumeShaperState(int id) {
97 return new VolumeShaper.State(1f, 1f);
98 }
99
100 /**
101 * TODO: sets the level on {@link #mAudioDeviceInfo}.
102 *
103 * @param muting
104 * @param level
105 * @return
106 */
107 @Override
108 int playerSetAuxEffectSendLevel(boolean muting, float level) {
109 return AudioSystem.SUCCESS;
110 }
111
112 @Override
113 void playerStart() {
114 start();
115 }
116
117 @Override
118 void playerPause() {
119 // Pause is equivalent to stop for hardware audio source based players.
120 stop();
121 }
122
123 @Override
124 void playerStop() {
125 stop();
126 }
127
128 /**
129 * Starts the playback from {@link AudioDeviceInfo}.
130 */
131 public void start() {
132 Preconditions.checkState(!isPlaying(), "HwAudioSource is currently playing");
133 baseStart();
134 mNativeHandle = AudioSystem.startAudioSource(
135 mAudioDeviceInfo.getPort().activeConfig(),
136 mAudioAttributes);
137 }
138
139 /**
140 * Checks whether the HwAudioSource player is playing.
141 * @return true if currently playing, false otherwise
142 */
143 public boolean isPlaying() {
144 return mNativeHandle != 0;
145 }
146
147 /**
148 * Stops the playback from {@link AudioDeviceInfo}.
149 */
150 public void stop() {
151 baseStop();
152 if (mNativeHandle > 0) {
153 AudioSystem.stopAudioSource(mNativeHandle);
154 mNativeHandle = 0;
155 }
156 }
157
158 /**
159 * Builder class for {@link HwAudioSource} objects.
160 * Use this class to configure and create a <code>HwAudioSource</code> instance.
161 * <p>Here is an example where <code>Builder</code> is used to specify an audio
162 * playback directly from a source device as media usage, to be used by a new
163 * <code>HwAudioSource</code> instance:
164 *
165 * <pre class="prettyprint">
166 * HwAudioSource player = new HwAudioSource.Builder()
167 * .setAudioAttributes(new AudioAttributes.Builder()
168 * .setUsage(AudioAttributes.USAGE_MEDIA)
169 * .build())
170 * .setAudioDeviceInfo(device)
171 * .build()
172 * </pre>
173 * <p>
174 * If the audio attributes are not set with {@link #setAudioAttributes(AudioAttributes)},
175 * attributes comprising {@link AudioAttributes#USAGE_MEDIA} will be used.
176 */
177 public static final class Builder {
178 private AudioAttributes mAudioAttributes;
179 private AudioDeviceInfo mAudioDeviceInfo;
180
181 /**
182 * Constructs a new Builder with default values.
183 */
184 public Builder() {
185 }
186
187 /**
188 * Sets the {@link AudioAttributes}.
189 * @param attributes a non-null {@link AudioAttributes} instance that describes the audio
190 * data to be played.
191 * @return the same Builder instance.
192 */
193 public @NonNull Builder setAudioAttributes(@NonNull AudioAttributes attributes) {
194 Preconditions.checkNotNull(attributes);
195 mAudioAttributes = attributes;
196 return this;
197 }
198
199 /**
200 * Sets the {@link AudioDeviceInfo}.
201 * @param info a non-null {@link AudioDeviceInfo} instance that describes the audio
202 * data come from.
203 * @return the same Builder instance.
204 */
205 public @NonNull Builder setAudioDeviceInfo(@NonNull AudioDeviceInfo info) {
206 Preconditions.checkNotNull(info);
207 Preconditions.checkArgument(info.isSource());
208 mAudioDeviceInfo = info;
209 return this;
210 }
211
212 /**
213 * Builds an {@link HwAudioSource} instance initialized with all the parameters set
214 * on this <code>Builder</code>.
215 * @return a new successfully initialized {@link HwAudioSource} instance.
216 */
217 public @NonNull HwAudioSource build() {
218 Preconditions.checkNotNull(mAudioDeviceInfo);
219 if (mAudioAttributes == null) {
220 mAudioAttributes = new AudioAttributes.Builder()
221 .setUsage(AudioAttributes.USAGE_MEDIA)
222 .build();
223 }
224 return new HwAudioSource(mAudioDeviceInfo, mAudioAttributes);
225 }
226 }
227}