| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.IBinder; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Message; |
| 23 | import android.os.RemoteException; |
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 24 | import android.telecom.InCallService.VideoCall; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 25 | import android.view.Surface; |
| 26 | |
| 27 | import com.android.internal.os.SomeArgs; |
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 28 | import com.android.internal.telecom.IVideoCallback; |
| 29 | import com.android.internal.telecom.IVideoProvider; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Implementation of a Video Call, which allows InCallUi to communicate commands to the underlying |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 33 | * {@link Connection.VideoProvider}, and direct callbacks from the |
| 34 | * {@link Connection.VideoProvider} to the appropriate {@link VideoCall.Listener}. |
| 35 | * |
| 36 | * {@hide} |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 37 | */ |
| 38 | public class VideoCallImpl extends VideoCall { |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 39 | |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 40 | private final IVideoProvider mVideoProvider; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 41 | private final VideoCallListenerBinder mBinder; |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 42 | private VideoCall.Callback mCallback; |
| Tyler Gunn | 4538216 | 2015-05-06 08:52:27 -0700 | [diff] [blame] | 43 | private int mVideoQuality = VideoProfile.QUALITY_UNKNOWN; |
| 44 | private Call mCall; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 45 | |
| 46 | private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() { |
| 47 | @Override |
| 48 | public void binderDied() { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 49 | mVideoProvider.asBinder().unlinkToDeath(this, 0); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | /** |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 54 | * IVideoCallback stub implementation. |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 55 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 56 | private final class VideoCallListenerBinder extends IVideoCallback.Stub { |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 57 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 58 | public void receiveSessionModifyRequest(VideoProfile videoProfile) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 59 | mHandler.obtainMessage(MessageHandler.MSG_RECEIVE_SESSION_MODIFY_REQUEST, |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 60 | videoProfile).sendToTarget(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 64 | public void receiveSessionModifyResponse(int status, VideoProfile requestProfile, |
| 65 | VideoProfile responseProfile) { |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 66 | SomeArgs args = SomeArgs.obtain(); |
| 67 | args.arg1 = status; |
| 68 | args.arg2 = requestProfile; |
| 69 | args.arg3 = responseProfile; |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 70 | mHandler.obtainMessage(MessageHandler.MSG_RECEIVE_SESSION_MODIFY_RESPONSE, args) |
| 71 | .sendToTarget(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void handleCallSessionEvent(int event) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 76 | mHandler.obtainMessage(MessageHandler.MSG_HANDLE_CALL_SESSION_EVENT, event) |
| 77 | .sendToTarget(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void changePeerDimensions(int width, int height) { |
| 82 | SomeArgs args = SomeArgs.obtain(); |
| 83 | args.arg1 = width; |
| 84 | args.arg2 = height; |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 85 | mHandler.obtainMessage(MessageHandler.MSG_CHANGE_PEER_DIMENSIONS, args).sendToTarget(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| Rekha Kumar | 0736681 | 2015-03-24 16:42:31 -0700 | [diff] [blame] | 89 | public void changeVideoQuality(int videoQuality) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 90 | mHandler.obtainMessage(MessageHandler.MSG_CHANGE_VIDEO_QUALITY, videoQuality, 0) |
| 91 | .sendToTarget(); |
| Rekha Kumar | 0736681 | 2015-03-24 16:42:31 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void changeCallDataUsage(long dataUsage) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 96 | mHandler.obtainMessage(MessageHandler.MSG_CHANGE_CALL_DATA_USAGE, dataUsage) |
| 97 | .sendToTarget(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| Yorke Lee | 400470f | 2015-05-12 13:31:25 -0700 | [diff] [blame^] | 101 | public void changeCameraCapabilities(VideoProfile.CameraCapabilities cameraCapabilities) { |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 102 | mHandler.obtainMessage(MessageHandler.MSG_CHANGE_CAMERA_CAPABILITIES, |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 103 | cameraCapabilities).sendToTarget(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** Default handler used to consolidate binder method calls onto a single thread. */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 108 | private final class MessageHandler extends Handler { |
| 109 | private static final int MSG_RECEIVE_SESSION_MODIFY_REQUEST = 1; |
| 110 | private static final int MSG_RECEIVE_SESSION_MODIFY_RESPONSE = 2; |
| 111 | private static final int MSG_HANDLE_CALL_SESSION_EVENT = 3; |
| 112 | private static final int MSG_CHANGE_PEER_DIMENSIONS = 4; |
| 113 | private static final int MSG_CHANGE_CALL_DATA_USAGE = 5; |
| 114 | private static final int MSG_CHANGE_CAMERA_CAPABILITIES = 6; |
| 115 | private static final int MSG_CHANGE_VIDEO_QUALITY = 7; |
| 116 | |
| 117 | public MessageHandler(Looper looper) { |
| 118 | super(looper); |
| 119 | } |
| 120 | |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public void handleMessage(Message msg) { |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 123 | if (mCallback == null) { |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | SomeArgs args; |
| 128 | switch (msg.what) { |
| 129 | case MSG_RECEIVE_SESSION_MODIFY_REQUEST: |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 130 | mCallback.onSessionModifyRequestReceived((VideoProfile) msg.obj); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 131 | break; |
| 132 | case MSG_RECEIVE_SESSION_MODIFY_RESPONSE: |
| 133 | args = (SomeArgs) msg.obj; |
| 134 | try { |
| 135 | int status = (int) args.arg1; |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 136 | VideoProfile requestProfile = (VideoProfile) args.arg2; |
| 137 | VideoProfile responseProfile = (VideoProfile) args.arg3; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 138 | |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 139 | mCallback.onSessionModifyResponseReceived( |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 140 | status, requestProfile, responseProfile); |
| 141 | } finally { |
| 142 | args.recycle(); |
| 143 | } |
| 144 | break; |
| 145 | case MSG_HANDLE_CALL_SESSION_EVENT: |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 146 | mCallback.onCallSessionEvent((int) msg.obj); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 147 | break; |
| 148 | case MSG_CHANGE_PEER_DIMENSIONS: |
| 149 | args = (SomeArgs) msg.obj; |
| 150 | try { |
| 151 | int width = (int) args.arg1; |
| 152 | int height = (int) args.arg2; |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 153 | mCallback.onPeerDimensionsChanged(width, height); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 154 | } finally { |
| 155 | args.recycle(); |
| 156 | } |
| 157 | break; |
| 158 | case MSG_CHANGE_CALL_DATA_USAGE: |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 159 | mCallback.onCallDataUsageChanged((long) msg.obj); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 160 | break; |
| 161 | case MSG_CHANGE_CAMERA_CAPABILITIES: |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 162 | mCallback.onCameraCapabilitiesChanged( |
| Yorke Lee | 400470f | 2015-05-12 13:31:25 -0700 | [diff] [blame^] | 163 | (VideoProfile.CameraCapabilities) msg.obj); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 164 | break; |
| Rekha Kumar | 0736681 | 2015-03-24 16:42:31 -0700 | [diff] [blame] | 165 | case MSG_CHANGE_VIDEO_QUALITY: |
| Tyler Gunn | 4538216 | 2015-05-06 08:52:27 -0700 | [diff] [blame] | 166 | mVideoQuality = msg.arg1; |
| Andrew Lee | da80c87 | 2015-04-15 14:09:50 -0700 | [diff] [blame] | 167 | mCallback.onVideoQualityChanged(msg.arg1); |
| Rekha Kumar | 0736681 | 2015-03-24 16:42:31 -0700 | [diff] [blame] | 168 | break; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 169 | default: |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | }; |
| 174 | |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 175 | private Handler mHandler; |
| 176 | |
| Tyler Gunn | 4538216 | 2015-05-06 08:52:27 -0700 | [diff] [blame] | 177 | VideoCallImpl(IVideoProvider videoProvider, Call call) throws RemoteException { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 178 | mVideoProvider = videoProvider; |
| 179 | mVideoProvider.asBinder().linkToDeath(mDeathRecipient, 0); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 180 | |
| 181 | mBinder = new VideoCallListenerBinder(); |
| Tyler Gunn | 7595842 | 2015-04-15 14:23:42 -0700 | [diff] [blame] | 182 | mVideoProvider.addVideoCallback(mBinder); |
| Tyler Gunn | 4538216 | 2015-05-06 08:52:27 -0700 | [diff] [blame] | 183 | mCall = call; |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 186 | public void destroy() { |
| 187 | unregisterCallback(mCallback); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** {@inheritDoc} */ |
| Andrew Lee | 011728f | 2015-04-23 15:47:06 -0700 | [diff] [blame] | 191 | public void registerCallback(VideoCall.Callback callback) { |
| 192 | registerCallback(callback, null); |
| 193 | } |
| 194 | |
| 195 | /** {@inheritDoc} */ |
| 196 | public void registerCallback(VideoCall.Callback callback, Handler handler) { |
| 197 | mCallback = callback; |
| 198 | if (handler == null) { |
| 199 | mHandler = new MessageHandler(Looper.getMainLooper()); |
| 200 | } else { |
| 201 | mHandler = new MessageHandler(handler.getLooper()); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** {@inheritDoc} */ |
| 206 | public void unregisterCallback(VideoCall.Callback callback) { |
| 207 | if (callback != mCallback) { |
| 208 | return; |
| 209 | } |
| 210 | |
| Etan Cohen | 8942724 | 2015-04-23 12:26:37 -0700 | [diff] [blame] | 211 | mCallback = null; |
| Tyler Gunn | 7595842 | 2015-04-15 14:23:42 -0700 | [diff] [blame] | 212 | try { |
| 213 | mVideoProvider.removeVideoCallback(mBinder); |
| 214 | } catch (RemoteException e) { |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** {@inheritDoc} */ |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 219 | public void setCamera(String cameraId) { |
| 220 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 221 | mVideoProvider.setCamera(cameraId); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 222 | } catch (RemoteException e) { |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** {@inheritDoc} */ |
| 227 | public void setPreviewSurface(Surface surface) { |
| 228 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 229 | mVideoProvider.setPreviewSurface(surface); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 230 | } catch (RemoteException e) { |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** {@inheritDoc} */ |
| 235 | public void setDisplaySurface(Surface surface) { |
| 236 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 237 | mVideoProvider.setDisplaySurface(surface); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 238 | } catch (RemoteException e) { |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** {@inheritDoc} */ |
| 243 | public void setDeviceOrientation(int rotation) { |
| 244 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 245 | mVideoProvider.setDeviceOrientation(rotation); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 246 | } catch (RemoteException e) { |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** {@inheritDoc} */ |
| 251 | public void setZoom(float value) { |
| 252 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 253 | mVideoProvider.setZoom(value); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 254 | } catch (RemoteException e) { |
| 255 | } |
| 256 | } |
| 257 | |
| Tyler Gunn | 4538216 | 2015-05-06 08:52:27 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Sends a session modification request to the video provider. |
| 260 | * <p> |
| 261 | * The {@link InCallService} will create the {@code requestProfile} based on the current |
| 262 | * video state (i.e. {@link Call.Details#getVideoState()}). It is, however, possible that the |
| 263 | * video state maintained by the {@link InCallService} could get out of sync with what is known |
| 264 | * by the {@link android.telecom.Connection.VideoProvider}. To remove ambiguity, the |
| 265 | * {@link VideoCallImpl} passes along the pre-modify video profile to the {@code VideoProvider} |
| 266 | * to ensure it has full context of the requested change. |
| 267 | * |
| 268 | * @param requestProfile The requested video profile. |
| 269 | */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 270 | public void sendSessionModifyRequest(VideoProfile requestProfile) { |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 271 | try { |
| Tyler Gunn | 4538216 | 2015-05-06 08:52:27 -0700 | [diff] [blame] | 272 | VideoProfile originalProfile = new VideoProfile(mCall.getDetails().getVideoState(), |
| 273 | mVideoQuality); |
| 274 | |
| 275 | mVideoProvider.sendSessionModifyRequest(originalProfile, requestProfile); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 276 | } catch (RemoteException e) { |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** {@inheritDoc} */ |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 281 | public void sendSessionModifyResponse(VideoProfile responseProfile) { |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 282 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 283 | mVideoProvider.sendSessionModifyResponse(responseProfile); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 284 | } catch (RemoteException e) { |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** {@inheritDoc} */ |
| 289 | public void requestCameraCapabilities() { |
| 290 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 291 | mVideoProvider.requestCameraCapabilities(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 292 | } catch (RemoteException e) { |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** {@inheritDoc} */ |
| 297 | public void requestCallDataUsage() { |
| 298 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 299 | mVideoProvider.requestCallDataUsage(); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 300 | } catch (RemoteException e) { |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** {@inheritDoc} */ |
| 305 | public void setPauseImage(String uri) { |
| 306 | try { |
| Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 307 | mVideoProvider.setPauseImage(uri); |
| Andrew Lee | 50aca23 | 2014-07-22 16:41:54 -0700 | [diff] [blame] | 308 | } catch (RemoteException e) { |
| 309 | } |
| 310 | } |
| Rekha Kumar | 0736681 | 2015-03-24 16:42:31 -0700 | [diff] [blame] | 311 | } |