1 /*
2  * Copyright (C) 2015 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.telecom.cts;
18 
19 import android.net.Uri;
20 import android.os.RemoteException;
21 import android.telecom.Connection;
22 import android.telecom.RemoteConnection;
23 import android.telecom.VideoProfile;
24 import android.view.Surface;
25 
26 import android.telecom.Connection.VideoProvider;
27 
28 /**
29  * Implements a mock video provider implementation.
30  */
31 public class MockVideoProvider extends VideoProvider {
32     public static final String CAMERA_NONE = "none";
33     public static final String CAMERA_FRONT = "front";
34     public static final String CAMERA_BACK = "back";
35     public static final int CAMERA_FRONT_DIMENSIONS = 1024;
36     public static final int CAMERA_BACK_DIMENSIONS = 2048;
37     public static final long DATA_USAGE = 1024;
38     public static final long DATA_USAGE_UNDEFINED = -1;
39     public static final int VIDEO_QUALITY_UNDEFINED = -1;
40     public static final int SESSION_EVENT_UNDEFINED = -1;
41     public static final int PEER_WIDTH_UNDEFINED = -1;
42     public static final int DEVICE_ORIENTATION_UNDEFINED = -1;
43     public static final float ZOOM_UNDEFINED = -1.0f;
44 
45     private Uri mPauseImageUri;
46     private String mCameraId = CAMERA_NONE;
47     private MockConnection mMockConnection;
48     private int mDeviceOrientation = DEVICE_ORIENTATION_UNDEFINED;
49     private float mZoom = ZOOM_UNDEFINED;
50     private Surface mPreviewSurface = null;
51     private Surface mDisplaySurface = null;
52     private VideoProfile mSessionModifyResponse = null;
53     private BaseTelecomTestWithMockServices.InvokeCounter mVideoProviderHandlerTracker;
54 
MockVideoProvider(MockConnection mockConnection)55     public MockVideoProvider(MockConnection mockConnection) {
56         mMockConnection = mockConnection;
57     }
58 
59     @Override
onSetCamera(String cameraId)60     public void onSetCamera(String cameraId) {
61         handleCameraChange(cameraId);
62     }
63 
64     @Override
onSetPreviewSurface(Surface surface)65     public void onSetPreviewSurface(Surface surface) {
66         mPreviewSurface = surface;
67     }
68 
69     @Override
onSetDisplaySurface(Surface surface)70     public void onSetDisplaySurface(Surface surface) {
71         mDisplaySurface = surface;
72     }
73 
74     @Override
onSetDeviceOrientation(int rotation)75     public void onSetDeviceOrientation(int rotation) {
76         mDeviceOrientation = rotation;
77     }
78 
79     @Override
onSetZoom(float value)80     public void onSetZoom(float value) {
81         if (mVideoProviderHandlerTracker != null) {
82             mVideoProviderHandlerTracker.invoke();
83             return;
84         }
85         mZoom = value;
86     }
87 
88     /**
89      * Handles a session modification request from the {@link MockInCallService}. Assumes the peer
90      * has accepted the proposed video profile.
91      *
92      * @param fromProfile The video properties prior to the request.
93      * @param toProfile The video properties with the requested changes made.
94      */
95     @Override
onSendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile)96     public void onSendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile) {
97         super.receiveSessionModifyResponse(Connection.VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS,
98                 toProfile, toProfile);
99         mMockConnection.setVideoState(toProfile.getVideoState());
100     }
101 
102     @Override
onSendSessionModifyResponse(VideoProfile responseProfile)103     public void onSendSessionModifyResponse(VideoProfile responseProfile) {
104         mSessionModifyResponse = responseProfile;
105     }
106 
107     /**
108      * Responds with the current camera capabilities.
109      */
110     @Override
onRequestCameraCapabilities()111     public void onRequestCameraCapabilities() {
112         handleCameraChange(mCameraId);
113     }
114 
115     /**
116      * Handles requests to retrieve the connection data usage by returning a fixed usage amount of
117      * {@code 1024} bytes.
118      */
119     @Override
onRequestConnectionDataUsage()120     public void onRequestConnectionDataUsage() {
121         super.setCallDataUsage(DATA_USAGE);
122     }
123 
124     @Override
onSetPauseImage(Uri uri)125     public void onSetPauseImage(Uri uri) {
126         mPauseImageUri = uri;
127     }
128 
129     /**
130      * Handles a change to the current camera selection.  Responds by reporting the capabilities of
131      * the camera.
132      */
handleCameraChange(String cameraId)133     private void handleCameraChange(String cameraId) {
134         mCameraId = cameraId;
135         if (CAMERA_FRONT.equals(mCameraId)) {
136             super.changeCameraCapabilities(new VideoProfile.CameraCapabilities(
137                     CAMERA_FRONT_DIMENSIONS, CAMERA_FRONT_DIMENSIONS));
138         } else if (CAMERA_BACK.equals(mCameraId)) {
139             super.changeCameraCapabilities(new VideoProfile.CameraCapabilities(
140                     CAMERA_BACK_DIMENSIONS, CAMERA_BACK_DIMENSIONS));
141         }
142     }
143 
144     /**
145      * Waits until all messages in the VideoProvider message handler up to the point of this call
146      * have been cleared and processed. Use this to wait for the callback to actually register.
147      */
waitForVideoProviderHandler(RemoteConnection.VideoProvider remoteVideoProvider)148     public void waitForVideoProviderHandler(RemoteConnection.VideoProvider remoteVideoProvider) {
149         mVideoProviderHandlerTracker =
150                 new BaseTelecomTestWithMockServices.InvokeCounter("WaitForHandler");
151         remoteVideoProvider.setZoom(0);
152         mVideoProviderHandlerTracker.waitForCount(1);
153         mVideoProviderHandlerTracker = null;
154     }
155     /**
156      * Sends a mock video quality value from the provider.
157      *
158      * @param videoQuality The video quality.
159      */
sendMockVideoQuality(int videoQuality)160     public void sendMockVideoQuality(int videoQuality) {
161         super.changeVideoQuality(videoQuality);
162     }
163 
164     /**
165      * Sends a mock call session event from the provider.
166      *
167      * @param event The call session event.
168      */
sendMockCallSessionEvent(int event)169     public void sendMockCallSessionEvent(int event) {
170         super.handleCallSessionEvent(event);
171     }
172 
173     /**
174      * Sends a mock peer width from the provider.
175      *
176      * @param width The peer width.
177      */
sendMockPeerWidth(int width)178     public void sendMockPeerWidth(int width) {
179         super.changePeerDimensions(width, width);
180     }
181 
182     /**
183      * Sends a mock session modify request from the provider.
184      *
185      * @param request The requested profile.
186      */
sendMockSessionModifyRequest(VideoProfile request)187     public void sendMockSessionModifyRequest(VideoProfile request) {
188         super.receiveSessionModifyRequest(request);
189     }
190 
191     /**
192      * Sends a mock session modify response from the provider.
193      *
194      * @param status The response status.
195      * @param requestProfile The request video profile.
196      * @param responseProfile The response video profile.
197      */
sendMockSessionModifyResponse(int status, VideoProfile requestProfile, VideoProfile responseProfile)198     public void sendMockSessionModifyResponse(int status, VideoProfile requestProfile,
199             VideoProfile responseProfile) {
200         super.receiveSessionModifyResponse(status, requestProfile, responseProfile);
201     }
202 
getDeviceOrientation()203     public int getDeviceOrientation() {
204         return mDeviceOrientation;
205     }
206 
getZoom()207     public float getZoom() {
208         return mZoom;
209     }
210 
getPreviewSurface()211     public Surface getPreviewSurface() {
212         return mPreviewSurface;
213     }
214 
getDisplaySurface()215     public Surface getDisplaySurface() {
216         return mDisplaySurface;
217     }
218 
getSessionModifyResponse()219     public VideoProfile getSessionModifyResponse() {
220         return mSessionModifyResponse;
221     }
222 
getPauseImageUri()223     public Uri getPauseImageUri() {
224         return mPauseImageUri;
225     }
226 }
227