1 /*
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 package android.media;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.media.VolumeShaper;
22 import android.os.RemoteException;
23 import android.util.Log;
24 
25 import java.lang.IllegalArgumentException;
26 import java.util.Objects;
27 
28 /**
29  * Class to remotely control a player.
30  * @hide
31  */
32 @SystemApi
33 public class PlayerProxy {
34 
35     private final static String TAG = "PlayerProxy";
36     private final static boolean DEBUG = false;
37 
38     private final AudioPlaybackConfiguration mConf; // never null
39 
40     /**
41      * @hide
42      * Constructor. Proxy for this player associated with this AudioPlaybackConfiguration
43      * @param conf the configuration being proxied.
44      */
PlayerProxy(@onNull AudioPlaybackConfiguration apc)45     PlayerProxy(@NonNull AudioPlaybackConfiguration apc) {
46         if (apc == null) {
47             throw new IllegalArgumentException("Illegal null AudioPlaybackConfiguration");
48         }
49         mConf = apc;
50     };
51 
52     //=====================================================================
53     // Methods matching the IPlayer interface
54     /**
55      * @hide
56      */
57     @SystemApi
start()58     public void start() {
59         try {
60             mConf.getIPlayer().start();
61         } catch (NullPointerException|RemoteException e) {
62             throw new IllegalStateException(
63                     "No player to proxy for start operation, player already released?", e);
64         }
65     }
66 
67     /**
68      * @hide
69      */
70     @SystemApi
pause()71     public void pause() {
72         try {
73             mConf.getIPlayer().pause();
74         } catch (NullPointerException|RemoteException e) {
75             throw new IllegalStateException(
76                     "No player to proxy for pause operation, player already released?", e);
77         }
78     }
79 
80     /**
81      * @hide
82      */
83     @SystemApi
stop()84     public void stop() {
85         try {
86             mConf.getIPlayer().stop();
87         } catch (NullPointerException|RemoteException e) {
88             throw new IllegalStateException(
89                     "No player to proxy for stop operation, player already released?", e);
90         }
91     }
92 
93     /**
94      * @hide
95      * @param vol
96      */
97     @SystemApi
setVolume(float vol)98     public void setVolume(float vol) {
99         try {
100             mConf.getIPlayer().setVolume(vol);
101         } catch (NullPointerException|RemoteException e) {
102             throw new IllegalStateException(
103                     "No player to proxy for setVolume operation, player already released?", e);
104         }
105     }
106 
107     /**
108      * @hide
109      * @param pan
110      */
111     @SystemApi
setPan(float pan)112     public void setPan(float pan) {
113         try {
114             mConf.getIPlayer().setPan(pan);
115         } catch (NullPointerException|RemoteException e) {
116             throw new IllegalStateException(
117                     "No player to proxy for setPan operation, player already released?", e);
118         }
119     }
120 
121     /**
122      * @hide
123      * @param delayMs
124      */
125     @SystemApi
setStartDelayMs(int delayMs)126     public void setStartDelayMs(int delayMs) {
127         try {
128             mConf.getIPlayer().setStartDelayMs(delayMs);
129         } catch (NullPointerException|RemoteException e) {
130             throw new IllegalStateException(
131                     "No player to proxy for setStartDelayMs operation, player already released?",
132                     e);
133         }
134     }
135 
136     /**
137      * @hide
138      * @param configuration
139      * @param operation
140      * @return volume shaper id or error
141      */
applyVolumeShaper( @onNull VolumeShaper.Configuration configuration, @NonNull VolumeShaper.Operation operation)142     public void applyVolumeShaper(
143             @NonNull VolumeShaper.Configuration configuration,
144             @NonNull VolumeShaper.Operation operation) {
145         try {
146             mConf.getIPlayer().applyVolumeShaper(configuration, operation);
147         } catch (NullPointerException|RemoteException e) {
148             throw new IllegalStateException(
149                     "No player to proxy for applyVolumeShaper operation,"
150                     + " player already released?", e);
151         }
152     }
153 }
154