ParticipantDisplayItem.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.nextcloud.talk.adapters;
  2. import android.os.Handler;
  3. import android.os.Looper;
  4. import android.text.TextUtils;
  5. import com.nextcloud.talk.call.CallParticipantModel;
  6. import com.nextcloud.talk.utils.ApiUtils;
  7. import org.webrtc.EglBase;
  8. import org.webrtc.MediaStream;
  9. import org.webrtc.PeerConnection;
  10. public class ParticipantDisplayItem {
  11. public interface Observer {
  12. void onChange();
  13. }
  14. /**
  15. * Shared handler to receive change notifications from the model on the main thread.
  16. */
  17. private static final Handler handler = new Handler(Looper.getMainLooper());
  18. private final ParticipantDisplayItemNotifier participantDisplayItemNotifier = new ParticipantDisplayItemNotifier();
  19. private final String baseUrl;
  20. private final String defaultGuestNick;
  21. private final EglBase rootEglBase;
  22. private final String session;
  23. private final String streamType;
  24. private final CallParticipantModel callParticipantModel;
  25. private final CallParticipantModel.Observer callParticipantModelObserver = this::updateFromModel;
  26. private String userId;
  27. private PeerConnection.IceConnectionState iceConnectionState;
  28. private String nick;
  29. private String urlForAvatar;
  30. private MediaStream mediaStream;
  31. private boolean streamEnabled;
  32. private boolean isAudioEnabled;
  33. public ParticipantDisplayItem(String baseUrl, String defaultGuestNick, EglBase rootEglBase, String streamType,
  34. CallParticipantModel callParticipantModel) {
  35. this.baseUrl = baseUrl;
  36. this.defaultGuestNick = defaultGuestNick;
  37. this.rootEglBase = rootEglBase;
  38. this.session = callParticipantModel.getSessionId();
  39. this.streamType = streamType;
  40. this.callParticipantModel = callParticipantModel;
  41. this.callParticipantModel.addObserver(callParticipantModelObserver, handler);
  42. updateFromModel();
  43. }
  44. public void destroy() {
  45. this.callParticipantModel.removeObserver(callParticipantModelObserver);
  46. }
  47. private void updateFromModel() {
  48. userId = callParticipantModel.getUserId();
  49. nick = callParticipantModel.getNick();
  50. this.updateUrlForAvatar();
  51. if ("screen".equals(streamType)) {
  52. iceConnectionState = callParticipantModel.getScreenIceConnectionState();
  53. mediaStream = callParticipantModel.getScreenMediaStream();
  54. isAudioEnabled = true;
  55. streamEnabled = true;
  56. } else {
  57. iceConnectionState = callParticipantModel.getIceConnectionState();
  58. mediaStream = callParticipantModel.getMediaStream();
  59. isAudioEnabled = callParticipantModel.isAudioAvailable() != null ?
  60. callParticipantModel.isAudioAvailable() : false;
  61. streamEnabled = callParticipantModel.isVideoAvailable() != null ?
  62. callParticipantModel.isVideoAvailable() : false;
  63. }
  64. participantDisplayItemNotifier.notifyChange();
  65. }
  66. private void updateUrlForAvatar() {
  67. if (!TextUtils.isEmpty(userId)) {
  68. urlForAvatar = ApiUtils.getUrlForAvatar(baseUrl, userId, true);
  69. } else {
  70. urlForAvatar = ApiUtils.getUrlForGuestAvatar(baseUrl, getNick(), true);
  71. }
  72. }
  73. public boolean isConnected() {
  74. return iceConnectionState == PeerConnection.IceConnectionState.CONNECTED ||
  75. iceConnectionState == PeerConnection.IceConnectionState.COMPLETED ||
  76. // If there is no connection state that means that no connection is needed, so it is a special case that is
  77. // also seen as "connected".
  78. iceConnectionState == null;
  79. }
  80. public String getNick() {
  81. if (TextUtils.isEmpty(userId) && TextUtils.isEmpty(nick)) {
  82. return defaultGuestNick;
  83. }
  84. return nick;
  85. }
  86. public String getUrlForAvatar() {
  87. return urlForAvatar;
  88. }
  89. public MediaStream getMediaStream() {
  90. return mediaStream;
  91. }
  92. public boolean isStreamEnabled() {
  93. return streamEnabled;
  94. }
  95. public EglBase getRootEglBase() {
  96. return rootEglBase;
  97. }
  98. public boolean isAudioEnabled() {
  99. return isAudioEnabled;
  100. }
  101. public void addObserver(Observer observer) {
  102. participantDisplayItemNotifier.addObserver(observer);
  103. }
  104. public void removeObserver(Observer observer) {
  105. participantDisplayItemNotifier.removeObserver(observer);
  106. }
  107. @Override
  108. public String toString() {
  109. return "ParticipantSession{" +
  110. "userId='" + userId + '\'' +
  111. ", session='" + session + '\'' +
  112. ", nick='" + nick + '\'' +
  113. ", urlForAvatar='" + urlForAvatar + '\'' +
  114. ", mediaStream=" + mediaStream +
  115. ", streamType='" + streamType + '\'' +
  116. ", streamEnabled=" + streamEnabled +
  117. ", rootEglBase=" + rootEglBase +
  118. '}';
  119. }
  120. }