blob: 7a82c1bb3e3d200c9da74974a0e62e5b5a16d841 [file] [log] [blame]
Andrew Lee50aca232014-07-22 16:41:54 -07001/*
2 * Copyright (C) 2014 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
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Andrew Lee50aca232014-07-22 16:41:54 -070018
19import android.os.Handler;
20import android.os.IBinder;
21import android.os.Looper;
22import android.os.Message;
23import android.os.RemoteException;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070024import android.telecom.InCallService.VideoCall;
Andrew Lee50aca232014-07-22 16:41:54 -070025import android.view.Surface;
26
27import com.android.internal.os.SomeArgs;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070028import com.android.internal.telecom.IVideoCallback;
29import com.android.internal.telecom.IVideoProvider;
Andrew Lee50aca232014-07-22 16:41:54 -070030
31/**
32 * Implementation of a Video Call, which allows InCallUi to communicate commands to the underlying
Ihab Awadb19a0bc2014-08-07 19:46:01 -070033 * {@link Connection.VideoProvider}, and direct callbacks from the
34 * {@link Connection.VideoProvider} to the appropriate {@link VideoCall.Listener}.
35 *
36 * {@hide}
Andrew Lee50aca232014-07-22 16:41:54 -070037 */
38public class VideoCallImpl extends VideoCall {
Andrew Lee50aca232014-07-22 16:41:54 -070039
Ihab Awadb19a0bc2014-08-07 19:46:01 -070040 private final IVideoProvider mVideoProvider;
Andrew Lee50aca232014-07-22 16:41:54 -070041 private final VideoCallListenerBinder mBinder;
Andrew Leeda80c872015-04-15 14:09:50 -070042 private VideoCall.Callback mCallback;
Andrew Lee50aca232014-07-22 16:41:54 -070043
44 private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
45 @Override
46 public void binderDied() {
Ihab Awadb19a0bc2014-08-07 19:46:01 -070047 mVideoProvider.asBinder().unlinkToDeath(this, 0);
Andrew Lee50aca232014-07-22 16:41:54 -070048 }
49 };
50
51 /**
Ihab Awadb19a0bc2014-08-07 19:46:01 -070052 * IVideoCallback stub implementation.
Andrew Lee50aca232014-07-22 16:41:54 -070053 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070054 private final class VideoCallListenerBinder extends IVideoCallback.Stub {
Andrew Lee50aca232014-07-22 16:41:54 -070055 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -070056 public void receiveSessionModifyRequest(VideoProfile videoProfile) {
Andrew Lee011728f2015-04-23 15:47:06 -070057 mHandler.obtainMessage(MessageHandler.MSG_RECEIVE_SESSION_MODIFY_REQUEST,
Ihab Awadb19a0bc2014-08-07 19:46:01 -070058 videoProfile).sendToTarget();
Andrew Lee50aca232014-07-22 16:41:54 -070059 }
60
61 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -070062 public void receiveSessionModifyResponse(int status, VideoProfile requestProfile,
63 VideoProfile responseProfile) {
Andrew Lee50aca232014-07-22 16:41:54 -070064 SomeArgs args = SomeArgs.obtain();
65 args.arg1 = status;
66 args.arg2 = requestProfile;
67 args.arg3 = responseProfile;
Andrew Lee011728f2015-04-23 15:47:06 -070068 mHandler.obtainMessage(MessageHandler.MSG_RECEIVE_SESSION_MODIFY_RESPONSE, args)
69 .sendToTarget();
Andrew Lee50aca232014-07-22 16:41:54 -070070 }
71
72 @Override
73 public void handleCallSessionEvent(int event) {
Andrew Lee011728f2015-04-23 15:47:06 -070074 mHandler.obtainMessage(MessageHandler.MSG_HANDLE_CALL_SESSION_EVENT, event)
75 .sendToTarget();
Andrew Lee50aca232014-07-22 16:41:54 -070076 }
77
78 @Override
79 public void changePeerDimensions(int width, int height) {
80 SomeArgs args = SomeArgs.obtain();
81 args.arg1 = width;
82 args.arg2 = height;
Andrew Lee011728f2015-04-23 15:47:06 -070083 mHandler.obtainMessage(MessageHandler.MSG_CHANGE_PEER_DIMENSIONS, args).sendToTarget();
Andrew Lee50aca232014-07-22 16:41:54 -070084 }
85
86 @Override
Rekha Kumar07366812015-03-24 16:42:31 -070087 public void changeVideoQuality(int videoQuality) {
Andrew Lee011728f2015-04-23 15:47:06 -070088 mHandler.obtainMessage(MessageHandler.MSG_CHANGE_VIDEO_QUALITY, videoQuality, 0)
89 .sendToTarget();
Rekha Kumar07366812015-03-24 16:42:31 -070090 }
91
92 @Override
93 public void changeCallDataUsage(long dataUsage) {
Andrew Lee011728f2015-04-23 15:47:06 -070094 mHandler.obtainMessage(MessageHandler.MSG_CHANGE_CALL_DATA_USAGE, dataUsage)
95 .sendToTarget();
Andrew Lee50aca232014-07-22 16:41:54 -070096 }
97
98 @Override
Ihab Awadb19a0bc2014-08-07 19:46:01 -070099 public void changeCameraCapabilities(CameraCapabilities cameraCapabilities) {
Andrew Lee011728f2015-04-23 15:47:06 -0700100 mHandler.obtainMessage(MessageHandler.MSG_CHANGE_CAMERA_CAPABILITIES,
Andrew Lee50aca232014-07-22 16:41:54 -0700101 cameraCapabilities).sendToTarget();
102 }
103 }
104
105 /** Default handler used to consolidate binder method calls onto a single thread. */
Andrew Lee011728f2015-04-23 15:47:06 -0700106 private final class MessageHandler extends Handler {
107 private static final int MSG_RECEIVE_SESSION_MODIFY_REQUEST = 1;
108 private static final int MSG_RECEIVE_SESSION_MODIFY_RESPONSE = 2;
109 private static final int MSG_HANDLE_CALL_SESSION_EVENT = 3;
110 private static final int MSG_CHANGE_PEER_DIMENSIONS = 4;
111 private static final int MSG_CHANGE_CALL_DATA_USAGE = 5;
112 private static final int MSG_CHANGE_CAMERA_CAPABILITIES = 6;
113 private static final int MSG_CHANGE_VIDEO_QUALITY = 7;
114
115 public MessageHandler(Looper looper) {
116 super(looper);
117 }
118
Andrew Lee50aca232014-07-22 16:41:54 -0700119 @Override
120 public void handleMessage(Message msg) {
Andrew Leeda80c872015-04-15 14:09:50 -0700121 if (mCallback == null) {
Andrew Lee50aca232014-07-22 16:41:54 -0700122 return;
123 }
124
125 SomeArgs args;
126 switch (msg.what) {
127 case MSG_RECEIVE_SESSION_MODIFY_REQUEST:
Andrew Leeda80c872015-04-15 14:09:50 -0700128 mCallback.onSessionModifyRequestReceived((VideoProfile) msg.obj);
Andrew Lee50aca232014-07-22 16:41:54 -0700129 break;
130 case MSG_RECEIVE_SESSION_MODIFY_RESPONSE:
131 args = (SomeArgs) msg.obj;
132 try {
133 int status = (int) args.arg1;
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700134 VideoProfile requestProfile = (VideoProfile) args.arg2;
135 VideoProfile responseProfile = (VideoProfile) args.arg3;
Andrew Lee50aca232014-07-22 16:41:54 -0700136
Andrew Leeda80c872015-04-15 14:09:50 -0700137 mCallback.onSessionModifyResponseReceived(
Andrew Lee50aca232014-07-22 16:41:54 -0700138 status, requestProfile, responseProfile);
139 } finally {
140 args.recycle();
141 }
142 break;
143 case MSG_HANDLE_CALL_SESSION_EVENT:
Andrew Leeda80c872015-04-15 14:09:50 -0700144 mCallback.onCallSessionEvent((int) msg.obj);
Andrew Lee50aca232014-07-22 16:41:54 -0700145 break;
146 case MSG_CHANGE_PEER_DIMENSIONS:
147 args = (SomeArgs) msg.obj;
148 try {
149 int width = (int) args.arg1;
150 int height = (int) args.arg2;
Andrew Leeda80c872015-04-15 14:09:50 -0700151 mCallback.onPeerDimensionsChanged(width, height);
Andrew Lee50aca232014-07-22 16:41:54 -0700152 } finally {
153 args.recycle();
154 }
155 break;
156 case MSG_CHANGE_CALL_DATA_USAGE:
Andrew Leeda80c872015-04-15 14:09:50 -0700157 mCallback.onCallDataUsageChanged((long) msg.obj);
Andrew Lee50aca232014-07-22 16:41:54 -0700158 break;
159 case MSG_CHANGE_CAMERA_CAPABILITIES:
Andrew Leeda80c872015-04-15 14:09:50 -0700160 mCallback.onCameraCapabilitiesChanged(
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700161 (CameraCapabilities) msg.obj);
Andrew Lee50aca232014-07-22 16:41:54 -0700162 break;
Rekha Kumar07366812015-03-24 16:42:31 -0700163 case MSG_CHANGE_VIDEO_QUALITY:
Andrew Leeda80c872015-04-15 14:09:50 -0700164 mCallback.onVideoQualityChanged(msg.arg1);
Rekha Kumar07366812015-03-24 16:42:31 -0700165 break;
Andrew Lee50aca232014-07-22 16:41:54 -0700166 default:
167 break;
168 }
169 }
170 };
171
Andrew Lee011728f2015-04-23 15:47:06 -0700172 private Handler mHandler;
173
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700174 VideoCallImpl(IVideoProvider videoProvider) throws RemoteException {
175 mVideoProvider = videoProvider;
176 mVideoProvider.asBinder().linkToDeath(mDeathRecipient, 0);
Andrew Lee50aca232014-07-22 16:41:54 -0700177
178 mBinder = new VideoCallListenerBinder();
Tyler Gunn75958422015-04-15 14:23:42 -0700179 mVideoProvider.addVideoCallback(mBinder);
Andrew Lee50aca232014-07-22 16:41:54 -0700180 }
181
Andrew Lee011728f2015-04-23 15:47:06 -0700182 public void destroy() {
183 unregisterCallback(mCallback);
Andrew Lee50aca232014-07-22 16:41:54 -0700184 }
185
186 /** {@inheritDoc} */
Andrew Lee011728f2015-04-23 15:47:06 -0700187 public void registerCallback(VideoCall.Callback callback) {
188 registerCallback(callback, null);
189 }
190
191 /** {@inheritDoc} */
192 public void registerCallback(VideoCall.Callback callback, Handler handler) {
193 mCallback = callback;
194 if (handler == null) {
195 mHandler = new MessageHandler(Looper.getMainLooper());
196 } else {
197 mHandler = new MessageHandler(handler.getLooper());
198 }
199 }
200
201 /** {@inheritDoc} */
202 public void unregisterCallback(VideoCall.Callback callback) {
203 if (callback != mCallback) {
204 return;
205 }
206
Etan Cohen89427242015-04-23 12:26:37 -0700207 mCallback = null;
Tyler Gunn75958422015-04-15 14:23:42 -0700208 try {
209 mVideoProvider.removeVideoCallback(mBinder);
210 } catch (RemoteException e) {
211 }
212 }
213
214 /** {@inheritDoc} */
Andrew Lee50aca232014-07-22 16:41:54 -0700215 public void setCamera(String cameraId) {
216 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700217 mVideoProvider.setCamera(cameraId);
Andrew Lee50aca232014-07-22 16:41:54 -0700218 } catch (RemoteException e) {
219 }
220 }
221
222 /** {@inheritDoc} */
223 public void setPreviewSurface(Surface surface) {
224 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700225 mVideoProvider.setPreviewSurface(surface);
Andrew Lee50aca232014-07-22 16:41:54 -0700226 } catch (RemoteException e) {
227 }
228 }
229
230 /** {@inheritDoc} */
231 public void setDisplaySurface(Surface surface) {
232 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700233 mVideoProvider.setDisplaySurface(surface);
Andrew Lee50aca232014-07-22 16:41:54 -0700234 } catch (RemoteException e) {
235 }
236 }
237
238 /** {@inheritDoc} */
239 public void setDeviceOrientation(int rotation) {
240 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700241 mVideoProvider.setDeviceOrientation(rotation);
Andrew Lee50aca232014-07-22 16:41:54 -0700242 } catch (RemoteException e) {
243 }
244 }
245
246 /** {@inheritDoc} */
247 public void setZoom(float value) {
248 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700249 mVideoProvider.setZoom(value);
Andrew Lee50aca232014-07-22 16:41:54 -0700250 } catch (RemoteException e) {
251 }
252 }
253
254 /** {@inheritDoc} */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700255 public void sendSessionModifyRequest(VideoProfile requestProfile) {
Andrew Lee50aca232014-07-22 16:41:54 -0700256 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700257 mVideoProvider.sendSessionModifyRequest(requestProfile);
Andrew Lee50aca232014-07-22 16:41:54 -0700258 } catch (RemoteException e) {
259 }
260 }
261
262 /** {@inheritDoc} */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700263 public void sendSessionModifyResponse(VideoProfile responseProfile) {
Andrew Lee50aca232014-07-22 16:41:54 -0700264 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700265 mVideoProvider.sendSessionModifyResponse(responseProfile);
Andrew Lee50aca232014-07-22 16:41:54 -0700266 } catch (RemoteException e) {
267 }
268 }
269
270 /** {@inheritDoc} */
271 public void requestCameraCapabilities() {
272 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700273 mVideoProvider.requestCameraCapabilities();
Andrew Lee50aca232014-07-22 16:41:54 -0700274 } catch (RemoteException e) {
275 }
276 }
277
278 /** {@inheritDoc} */
279 public void requestCallDataUsage() {
280 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700281 mVideoProvider.requestCallDataUsage();
Andrew Lee50aca232014-07-22 16:41:54 -0700282 } catch (RemoteException e) {
283 }
284 }
285
286 /** {@inheritDoc} */
287 public void setPauseImage(String uri) {
288 try {
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700289 mVideoProvider.setPauseImage(uri);
Andrew Lee50aca232014-07-22 16:41:54 -0700290 } catch (RemoteException e) {
291 }
292 }
Rekha Kumar07366812015-03-24 16:42:31 -0700293}