ImsVideoCallProviderWrapper.java revision 48b9589b8e85d74af982c2aee173da433d54a77d
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
17package com.android.ims.internal;
18
19import android.os.Handler;
20import android.os.IBinder;
21import android.os.Looper;
22import android.os.Message;
23import android.os.RemoteException;
24import android.telecom.CameraCapabilities;
25import android.telecom.Connection;
26import android.telecom.VideoProfile;
27import android.view.Surface;
28
29import com.android.internal.os.SomeArgs;
30
31/**
32 * Subclass implementation of {@link Connection.VideoProvider}. This intermediates and
33 * communicates with the actual implementation of the video call provider in the IMS service; it is
34 * in essence, a wrapper around the IMS's video call provider implementation.
35 *
36 * This class maintains a binder by which the ImsVideoCallProvider's implementation can communicate
37 * its intent to invoke callbacks. In this class, the message across this binder is handled, and
38 * the superclass's methods are used to execute the callbacks.
39 *
40 * @hide
41 */
42public class ImsVideoCallProviderWrapper extends Connection.VideoProvider {
43    private static final int MSG_RECEIVE_SESSION_MODIFY_REQUEST = 1;
44    private static final int MSG_RECEIVE_SESSION_MODIFY_RESPONSE = 2;
45    private static final int MSG_HANDLE_CALL_SESSION_EVENT = 3;
46    private static final int MSG_CHANGE_PEER_DIMENSIONS = 4;
47    private static final int MSG_CHANGE_CALL_DATA_USAGE = 5;
48    private static final int MSG_CHANGE_CAMERA_CAPABILITIES = 6;
49    private static final int MSG_CHANGE_VIDEO_QUALITY = 7;
50
51    private final IImsVideoCallProvider mVideoCallProvider;
52    private final ImsVideoCallCallback mBinder;
53
54    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
55        @Override
56        public void binderDied() {
57            mVideoCallProvider.asBinder().unlinkToDeath(this, 0);
58        }
59    };
60
61    /**
62     * IImsVideoCallCallback stub implementation.
63     */
64    private final class ImsVideoCallCallback extends IImsVideoCallCallback.Stub {
65        @Override
66        public void receiveSessionModifyRequest(VideoProfile VideoProfile) {
67            mHandler.obtainMessage(MSG_RECEIVE_SESSION_MODIFY_REQUEST,
68                    VideoProfile).sendToTarget();
69        }
70
71        @Override
72        public void receiveSessionModifyResponse(
73                int status, VideoProfile requestProfile, VideoProfile responseProfile) {
74            SomeArgs args = SomeArgs.obtain();
75            args.arg1 = status;
76            args.arg2 = requestProfile;
77            args.arg3 = responseProfile;
78            mHandler.obtainMessage(MSG_RECEIVE_SESSION_MODIFY_RESPONSE, args).sendToTarget();
79        }
80
81        @Override
82        public void handleCallSessionEvent(int event) {
83            mHandler.obtainMessage(MSG_HANDLE_CALL_SESSION_EVENT, event).sendToTarget();
84        }
85
86        @Override
87        public void changePeerDimensions(int width, int height) {
88            SomeArgs args = SomeArgs.obtain();
89            args.arg1 = width;
90            args.arg2 = height;
91            mHandler.obtainMessage(MSG_CHANGE_PEER_DIMENSIONS, args).sendToTarget();
92        }
93
94        @Override
95        public void changeVideoQuality(int videoQuality) {
96            mHandler.obtainMessage(MSG_CHANGE_VIDEO_QUALITY, videoQuality, 0).sendToTarget();
97        }
98
99        @Override
100        public void changeCallDataUsage(long dataUsage) {
101            mHandler.obtainMessage(MSG_CHANGE_CALL_DATA_USAGE, dataUsage).sendToTarget();
102        }
103
104        @Override
105        public void changeCameraCapabilities(CameraCapabilities cameraCapabilities) {
106            mHandler.obtainMessage(MSG_CHANGE_CAMERA_CAPABILITIES,
107                    cameraCapabilities).sendToTarget();
108        }
109    }
110
111    /** Default handler used to consolidate binder method calls onto a single thread. */
112    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
113        @Override
114        public void handleMessage(Message msg) {
115            SomeArgs args;
116            switch (msg.what) {
117                case MSG_RECEIVE_SESSION_MODIFY_REQUEST:
118                    receiveSessionModifyRequest((VideoProfile) msg.obj);
119                    break;
120                case MSG_RECEIVE_SESSION_MODIFY_RESPONSE:
121                    args = (SomeArgs) msg.obj;
122                    try {
123                        int status = (int) args.arg1;
124                        VideoProfile requestProfile = (VideoProfile) args.arg2;
125                        VideoProfile responseProfile = (VideoProfile) args.arg3;
126
127                        receiveSessionModifyResponse(status, requestProfile, responseProfile);
128                    } finally {
129                        args.recycle();
130                    }
131                    break;
132                case MSG_HANDLE_CALL_SESSION_EVENT:
133                    handleCallSessionEvent((int) msg.obj);
134                    break;
135                case MSG_CHANGE_PEER_DIMENSIONS:
136                    args = (SomeArgs) msg.obj;
137                    try {
138                        int width = (int) args.arg1;
139                        int height = (int) args.arg2;
140                        changePeerDimensions(width, height);
141                    } finally {
142                        args.recycle();
143                    }
144                    break;
145                case MSG_CHANGE_CALL_DATA_USAGE:
146                    changeCallDataUsage((long) msg.obj);
147                    break;
148                case MSG_CHANGE_CAMERA_CAPABILITIES:
149                    changeCameraCapabilities((CameraCapabilities) msg.obj);
150                    break;
151                case MSG_CHANGE_VIDEO_QUALITY:
152                    changeVideoQuality(msg.arg1);
153                    break;
154                default:
155                    break;
156            }
157        }
158    };
159
160    /**
161     * Instantiates an instance of the ImsVideoCallProvider, taking in the binder for IMS's video
162     * call provider implementation.
163     *
164     * @param VideoProvider
165     */
166    public ImsVideoCallProviderWrapper(IImsVideoCallProvider VideoProvider)
167            throws RemoteException {
168        mVideoCallProvider = VideoProvider;
169        mVideoCallProvider.asBinder().linkToDeath(mDeathRecipient, 0);
170
171        mBinder = new ImsVideoCallCallback();
172        mVideoCallProvider.setCallback(mBinder);
173    }
174
175    /** @inheritDoc */
176    public void onSetCamera(String cameraId) {
177        try {
178            mVideoCallProvider.setCamera(cameraId);
179        } catch (RemoteException e) {
180        }
181    }
182
183    /** @inheritDoc */
184    public void onSetPreviewSurface(Surface surface) {
185        try {
186            mVideoCallProvider.setPreviewSurface(surface);
187        } catch (RemoteException e) {
188        }
189    }
190
191    /** @inheritDoc */
192    public void onSetDisplaySurface(Surface surface) {
193        try {
194            mVideoCallProvider.setDisplaySurface(surface);
195        } catch (RemoteException e) {
196        }
197    }
198
199    /** @inheritDoc */
200    public void onSetDeviceOrientation(int rotation) {
201        try {
202            mVideoCallProvider.setDeviceOrientation(rotation);
203        } catch (RemoteException e) {
204        }
205    }
206
207    /** @inheritDoc */
208    public void onSetZoom(float value) {
209        try {
210            mVideoCallProvider.setZoom(value);
211        } catch (RemoteException e) {
212        }
213    }
214
215    /** @inheritDoc */
216    public void onSendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile) {
217        try {
218            mVideoCallProvider.sendSessionModifyRequest(fromProfile, toProfile);
219        } catch (RemoteException e) {
220        }
221    }
222
223    /** @inheritDoc */
224    public void onSendSessionModifyResponse(VideoProfile responseProfile) {
225        try {
226            mVideoCallProvider.sendSessionModifyResponse(responseProfile);
227        } catch (RemoteException e) {
228        }
229    }
230
231    /** @inheritDoc */
232    public void onRequestCameraCapabilities() {
233        try {
234            mVideoCallProvider.requestCameraCapabilities();
235        } catch (RemoteException e) {
236        }
237    }
238
239    /** @inheritDoc */
240    public void onRequestConnectionDataUsage() {
241        try {
242            mVideoCallProvider.requestCallDataUsage();
243        } catch (RemoteException e) {
244        }
245    }
246
247    /** @inheritDoc */
248    public void onSetPauseImage(String uri) {
249        try {
250            mVideoCallProvider.setPauseImage(uri);
251        } catch (RemoteException e) {
252        }
253    }
254}
255