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 17 package com.android.incallui.call; 18 19 import android.support.annotation.NonNull; 20 import android.support.annotation.Nullable; 21 import java.util.Collections; 22 import java.util.Objects; 23 import java.util.Set; 24 import java.util.concurrent.ConcurrentHashMap; 25 26 /** Class used to notify interested parties of incoming video related events. */ 27 public class InCallVideoCallCallbackNotifier { 28 29 /** Singleton instance of this class. */ 30 private static InCallVideoCallCallbackNotifier instance = new InCallVideoCallCallbackNotifier(); 31 32 /** 33 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before 34 * resizing, 1 means we only expect a single thread to access the map so make only a single shard 35 */ 36 private final Set<SurfaceChangeListener> surfaceChangeListeners = 37 Collections.newSetFromMap(new ConcurrentHashMap<SurfaceChangeListener, Boolean>(8, 0.9f, 1)); 38 39 /** Private constructor. Instance should only be acquired through getRunningInstance(). */ InCallVideoCallCallbackNotifier()40 private InCallVideoCallCallbackNotifier() {} 41 42 /** Static singleton accessor method. */ getInstance()43 public static InCallVideoCallCallbackNotifier getInstance() { 44 return instance; 45 } 46 47 /** 48 * Adds a new {@link SurfaceChangeListener}. 49 * 50 * @param listener The listener. 51 */ addSurfaceChangeListener(@onNull SurfaceChangeListener listener)52 public void addSurfaceChangeListener(@NonNull SurfaceChangeListener listener) { 53 Objects.requireNonNull(listener); 54 surfaceChangeListeners.add(listener); 55 } 56 57 /** 58 * Remove a {@link SurfaceChangeListener}. 59 * 60 * @param listener The listener. 61 */ removeSurfaceChangeListener(@ullable SurfaceChangeListener listener)62 public void removeSurfaceChangeListener(@Nullable SurfaceChangeListener listener) { 63 if (listener != null) { 64 surfaceChangeListeners.remove(listener); 65 } 66 } 67 68 /** 69 * Inform listeners of a change to peer dimensions. 70 * 71 * @param call The call. 72 * @param width New peer width. 73 * @param height New peer height. 74 */ peerDimensionsChanged(DialerCall call, int width, int height)75 public void peerDimensionsChanged(DialerCall call, int width, int height) { 76 for (SurfaceChangeListener listener : surfaceChangeListeners) { 77 listener.onUpdatePeerDimensions(call, width, height); 78 } 79 } 80 81 /** 82 * Inform listeners of a change to camera dimensions. 83 * 84 * @param call The call. 85 * @param width The new camera video width. 86 * @param height The new camera video height. 87 */ cameraDimensionsChanged(DialerCall call, int width, int height)88 public void cameraDimensionsChanged(DialerCall call, int width, int height) { 89 for (SurfaceChangeListener listener : surfaceChangeListeners) { 90 listener.onCameraDimensionsChange(call, width, height); 91 } 92 } 93 94 /** 95 * Listener interface for any class that wants to be notified of changes to the video surfaces. 96 */ 97 public interface SurfaceChangeListener { 98 99 /** 100 * Called when the peer video feed changes dimensions. This can occur when the peer rotates 101 * their device, changing the aspect ratio of the video signal. 102 * 103 * @param call The call which experienced a peer video 104 */ onUpdatePeerDimensions(DialerCall call, int width, int height)105 void onUpdatePeerDimensions(DialerCall call, int width, int height); 106 107 /** 108 * Called when the local camera changes dimensions. This occurs when a change in camera occurs. 109 * 110 * @param call The call which experienced the camera dimension change. 111 * @param width The new camera video width. 112 * @param height The new camera video height. 113 */ onCameraDimensionsChange(DialerCall call, int width, int height)114 void onCameraDimensionsChange(DialerCall call, int width, int height); 115 } 116 } 117