summaryrefslogtreecommitdiff
path: root/InCallUI/src/com/android/incallui/VideoCallPresenter.java
blob: 12c95a3b9839989d57d2c2fbb7985b5888578f79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.incallui;

import com.google.common.base.Preconditions;

import com.android.incallui.CallVideoClientNotifier.SurfaceChangeListener;
import com.android.incallui.CallVideoClientNotifier.VideoEventListener;
import com.android.incallui.InCallPresenter.InCallDetailsListener;
import com.android.incallui.InCallPresenter.InCallStateListener;
import com.android.incallui.InCallPresenter.IncomingCallListener;

import android.content.Context;
import android.telecomm.RemoteCallVideoProvider;
import android.view.Surface;

import java.util.Objects;

/**
 * Logic related to the {@link VideoCallFragment} and for managing changes to the video calling
 * surfaces based on other user interface events and incoming events from the
 * {@class CallVideoClient}.
 */
public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi>  implements
        IncomingCallListener, InCallStateListener,
        InCallDetailsListener, SurfaceChangeListener, VideoEventListener {

    /**
     * The current context.
     */
    private Context mContext;

    /**
     * The call the video surfaces are currently related to
     */
    private Call mCall;

    /**
     * The {@link RemoteCallVideoProvider} used to inform the video telephony layer of changes
     * to the video surfaces.
     */
    private RemoteCallVideoProvider mCallVideoProvider;

    /**
     * Determines if the current UI state represents a video call.
     */
    private boolean mIsVideoCall;

    /**
     * Initializes the presenter.
     *
     * @param context The current context.
     */
    public void init(Context context) {
        mContext = Preconditions.checkNotNull(context);
    }

    /**
     * Called when the user interface is ready to be used.
     *
     * @param ui The Ui implementation that is now ready to be used.
     */
    @Override
    public void onUiReady(VideoCallUi ui) {
        super.onUiReady(ui);

        // Register for call state changes last
        InCallPresenter.getInstance().addListener(this);
        InCallPresenter.getInstance().addIncomingCallListener(this);

        // Register for surface and video events from {@link InCallVideoProvider}s.
        CallVideoClientNotifier.getInstance().addSurfaceChangeListener(this);
        CallVideoClientNotifier.getInstance().addVideoEventListener(this);

        mIsVideoCall = false;
    }

    /**
     * Called when the user interface is no longer ready to be used.
     *
     * @param ui The Ui implementation that is no longer ready to be used.
     */
    public void unUiUnready(VideoCallUi ui) {
        super.onUiUnready(ui);

        InCallPresenter.getInstance().removeListener(this);
        InCallPresenter.getInstance().removeIncomingCallListener(this);
        CallVideoClientNotifier.getInstance().removeSurfaceChangeListener(this);
        CallVideoClientNotifier.getInstance().removeVideoEventListener(this);
    }

    /**
     * @return The {@link RemoteCallVideoProvider}.
     */
    private RemoteCallVideoProvider getCallVideoProvider() {
        return mCallVideoProvider;
    }

    /**
     * Handles the creation of a surface in the {@link VideoCallFragment}.
     *
     * @param surface The surface which was created.
     */
    public void onSurfaceCreated(int surface) {
        final VideoCallUi ui = getUi();
        final RemoteCallVideoProvider callVideoProvider = getCallVideoProvider();

        if (ui == null || callVideoProvider == null) {
            return;
        }

        if (surface == VideoCallFragment.SURFACE_DISPLAY) {
            mCallVideoProvider.setDisplaySurface(ui.getDisplayVideoSurface());
        } else if (surface == VideoCallFragment.SURFACE_PREVIEW) {
            mCallVideoProvider.setPreviewSurface(ui.getPreviewVideoSurface());
        }
    }

    /**
     * Handles structural changes (format or size) to a surface.
     *
     * @param surface The surface which changed.
     * @param format The new PixelFormat of the surface.
     * @param width The new width of the surface.
     * @param height The new height of the surface.
     */
    public void onSurfaceChanged(int surface, int format, int width, int height) {
        //Do stuff
    }

    /**
     * Handles the destruction of a surface in the {@link VideoCallFragment}.
     *
     * @param surface The surface which was destroyed.
     */
    public void onSurfaceDestroyed(int surface) {
        final VideoCallUi ui = getUi();
        final RemoteCallVideoProvider callVideoProvider = getCallVideoProvider();

        if (ui == null || callVideoProvider == null) {
            return;
        }

        if (surface == VideoCallFragment.SURFACE_DISPLAY) {
            mCallVideoProvider.setDisplaySurface(null);
        } else if (surface == VideoCallFragment.SURFACE_PREVIEW) {
            mCallVideoProvider.setPreviewSurface(null);
        }
    }

    /**
     * Handles incoming calls.
     *
     * @param state The in call state.
     * @param call The call.
     */
    @Override
    public void onIncomingCall(InCallPresenter.InCallState state, Call call) {
        // same logic should happen as with onStateChange()
        onStateChange(state, CallList.getInstance());
    }

    /**
     * Handles state changes (including incoming calls)
     *
     * @param state The in call state.
     * @param callList The call list.
     */
    @Override
    public void onStateChange(InCallPresenter.InCallState state, CallList callList) {
        Call call = null;
        if (state == InCallPresenter.InCallState.INCOMING) {
            call = callList.getIncomingCall();
        } else if (state == InCallPresenter.InCallState.OUTGOING) {
            call = callList.getOutgoingCall();
        }

        if (call == null || getUi() == null) {
            return;
        }

        Log.d(this, "onStateChange "+call);

        final boolean callChanged = !Objects.equals(mCall, call);

        // If the call changed track it now.
        if (callChanged) {
            mCall = call;
        }

        checkForCallVideoProviderChange();
        checkForVideoStateChange();
    }

    /**
     * Handles changes to the details of the call.  The {@link VideoCallPresenter} is interested in
     * changes to the video state.
     *
     * @param call The call for which the details changed.
     * @param details The new call details.
     */
    @Override
    public void onDetailsChanged(Call call, android.telecomm.Call.Details details) {
        Log.d(this, "onDetailsChanged "+call);

        // If the details change is not for the currently active call no update is required.
        if (!call.equals(mCall)) {
            return;
        }

        checkForVideoStateChange();
    }

    /**
     * Checks for a change to the call video provider and changes it if required.
     */
    private void checkForCallVideoProviderChange() {
        RemoteCallVideoProvider callVideoProvider =
                mCall.getTelecommCall().getCallVideoProvider();
        if (!Objects.equals(callVideoProvider, mCallVideoProvider)) {
            changeCallVideoProvider(callVideoProvider);
        }
    }

    /**
     * Checks to see if the current video state has changed and updates the UI if required.
     */
    private void checkForVideoStateChange() {
        boolean newVideoState = mCall.isVideoCall();

        // Check if video state changed
        if (mIsVideoCall != newVideoState) {
            mIsVideoCall = newVideoState;

            if (mIsVideoCall) {
                enterVideoState();
            } else {
                exitVideoState();
            }
        }
    }

    /**
     * Handles a change to the call video provider.  Sets the surfaces on the previous provider
     * to null and sets the surfaces on the new provider accordingly.
     *
     * @param callVideoProvider The new call video provider.
     */
    private void changeCallVideoProvider(RemoteCallVideoProvider callVideoProvider) {
        Log.d(this, "changeCallVideoProvider");

        // Null out the surfaces on the previous provider
        if (mCallVideoProvider != null) {
            mCallVideoProvider.setDisplaySurface(null);
            mCallVideoProvider.setPreviewSurface(null);
        }

        mCallVideoProvider = callVideoProvider;
        setSurfaces();

    }

    /**
     * Enters video mode by showing the video surfaces.
     * TODO(vt): Need to adjust size and orientation of preview surface here.
     */
    private void enterVideoState() {
        Log.d(this, "enterVideoState");
        VideoCallUi ui = getUi();
        if (ui == null) {
            return;
        }

        ui.showVideoUi(true);
    }

    /**
     * Sets the surfaces on the specified {@link RemoteCallVideoProvider}.
     */
    private void setSurfaces() {
        Log.d(this, "setSurfaces");
        VideoCallUi ui = getUi();
        if (ui == null || mCallVideoProvider == null) {
            return;
        }

        if (getUi().isDisplayVideoSurfaceCreated()) {
            mCallVideoProvider.setDisplaySurface(ui.getDisplayVideoSurface());
        }

        if (getUi().isPreviewVideoSurfaceCreated()) {
            mCallVideoProvider.setPreviewSurface(ui.getPreviewVideoSurface());
        }
    }

    /**
     * Exits video mode by hiding the video surfaces.
     */
    private void exitVideoState() {
        Log.d(this, "exitVideoState");
        VideoCallUi ui = getUi();
        if (ui == null) {
            return;
        }

        ui.showVideoUi(false);
    }

    /**
     * Handles peer video pause state changes.
     *
     * @param call The call which paused or un-pausedvideo transmission.
     * @param paused {@code True} when the video transmission is paused, {@code false} when video
     *               transmission resumes.
     */
    @Override
    public void onPeerPauseStateChanged(Call call, boolean paused) {
        if (!call.equals(mCall)) {
            return;
        }

        // TODO(vt): Show/hide the peer contact photo.
    }

    /**
     * Handles peer video dimension changes.
     *
     * @param call The call which experienced a peer video dimension change.
     * @param width The new peer video width .
     * @param height The new peer video height.
     */
    @Override
    public void onUpdatePeerDimensions(Call call, int width, int height) {
        if (!call.equals(mCall)) {
            return;
        }

        // TODO(vt): Change display surface aspect ratio.
    }

    /**
     * Defines the VideoCallUI interactions.
     */
    public interface VideoCallUi extends Ui {
        void showVideoUi(boolean show);
        boolean isDisplayVideoSurfaceCreated();
        boolean isPreviewVideoSurfaceCreated();
        Surface getDisplayVideoSurface();
        Surface getPreviewVideoSurface();
    }
}