public boolean turnVideoOn(String sParticipantId, VideoChannelPtr in) {
    if (!_users.containsKey(sParticipantId)) {
      LogSdk.e(TAG, "turnVideoOn: Participant " + sParticipantId + " not found");
      return false;
    }

    RenderViewData d = null;
    Integer view_id = _users.get(sParticipantId).getViewId();
    if (view_id == -1) {
      LogSdk.d(TAG, "Participant " + sParticipantId + " is not connected to any GLView");
      d = getFreeRender();
      if (d != null) _users.get(sParticipantId).setViewId(d._view_id);

    } else {
      d = _renders.get(view_id);
    }

    if (d != null) {
      d._render.connect(in, sParticipantId);
      d._video_on = true;
      d._user = sParticipantId;
      LogSdk.d(TAG, "video is ON for " + sParticipantId + " GLView " + d._view_id);
      return true;
    } else {
      LogSdk.w(TAG, "No available GVView found for Participant " + sParticipantId);
    }
    return false;
  }
 public int prepareParticipantAsActiveRender(String sParticipantId) {
   int numActiveRenders = getActiveRenders();
   LogSdk.i(Utils.getOoVooTag(), "Active renders = " + numActiveRenders);
   if ((!_on_full_mode
       && numActiveRenders < ParticipantsManager.MAX_ACTIVE_PARTICIPANTS_IN_CALL)) {
     LogSdk.i(Utils.getOoVooTag(), "Add joined user to active renders " + sParticipantId);
     RenderViewData d = getFreeRender();
     if (d != null) {
       _users.get(sParticipantId).setViewId(d._view_id);
       LogSdk.i(
           Utils.getOoVooTag(), "Add free render for " + sParticipantId + " as " + d._view_id);
       d._video_on = false;
       d._user = sParticipantId;
       return d._view_id;
     } else {
       LogSdk.i(
           Utils.getOoVooTag(), "No available GLView found for Participant " + sParticipantId);
     }
   } else {
     LogSdk.i(
         Utils.getOoVooTag(),
         "ConferenceManager.OnParticipantJoinedSession - can't add user to active surfaces");
   }
   return -1;
 }
  public void Resume() {
    LogSdk.d(TAG, "Resume: on_pause=" + _on_pause);
    _on_pause = false;

    for (int i = 0; i < _renders.size(); i++) {
      RenderViewData d = _renders.valueAt(i);
      if ((d._video_on && d._user != null) && (!_on_full_mode || (_on_full_mode && d.isFullMode))) {
        ConferenceCore.instance().receiveParticipantVideoOn(d._user);
        LogSdk.d(TAG, "resume GLview " + d._view_id + " for user " + d._user);
      }
    }
  }
 public void setVideoStateOn(String participant, Boolean on, short state) {
   Integer view_id = _users.get(participant).getViewId();
   if (view_id != null && view_id != -1) {
     _renders.get(view_id).setVideoStateOn(on, state);
     LogSdk.d(TAG, "Set video state to " + state + " for participant " + participant);
   }
 }
 public void moveToFullMode(int viewIdForFullMode) {
   _on_full_mode = true;
   for (int i = 0; i < _renders.size(); i++) {
     RenderViewData d = _renders.valueAt(i);
     if (d._view_id != viewIdForFullMode) {
       String user = getParticipantByViewId(d._view_id);
       LogSdk.d(TAG, "pausing GLview " + d._view_id + " for user " + user);
       if (d._video_on) {
         ConferenceCore.instance().receiveParticipantVideoOff(user);
         LogSdk.d(TAG, "send turn video Off for user " + user);
       }
       d.isFullMode = false;
     } else {
       d.isFullMode = true;
     }
   }
 }
  public void addGLView(int view_id) {
    boolean isPreview =
        (view_id == R.id.myVideoSurface || view_id == R.id.preview_layout_id ? true : false);
    _renders.put(view_id, new RenderViewData(view_id, isPreview));
    LogSdk.d(TAG, "adding GLview " + view_id + " total = " + _renders.size());

    if (isPreview) {
      _users.put("", new VideoParticipant("", "Me", view_id));
    }
  }
  public void Pause() {
    LogSdk.d(TAG, "Pause: on_pause=" + _on_pause);
    _on_pause = true;

    for (int i = 0; i < _renders.size(); i++) {
      RenderViewData d = _renders.valueAt(i);
      String user = getParticipantByViewId(d._view_id);

      LogSdk.d(TAG, "pausing GLview " + d._view_id + " for user " + user);
      if (d._video_on) {
        ConferenceCore.instance().receiveParticipantVideoOff(user);
        LogSdk.d(TAG, "send turn video Off for user " + user);
        d.updateVideoState(VIDEO_PAUSED_DUE_QOS);

        // d._render.channel().Disconnect();
        // d._render = null;

      }
    }
  }
  public void updateGLViews(SessionUIPresenter presenter) {
    //		_on_pause = false;
    for (int i = 0; i < _renders.size(); i++) {
      RenderViewData d = _renders.valueAt(i);
      LogSdk.d(TAG, "updating GLview " + d._view_id + " for user " + d._user);

      GLSurfaceView glview = (GLSurfaceView) presenter.findViewById(d._view_id);
      if (glview == null) {
        LogSdk.e(TAG, "NULL GL view!!! " + d._view_id);
        continue;
      }
      try {
        glview.setEGLContextClientVersion(2);
        d._render = new VideoRenderer(glview);
        glview.setRenderer(d._render);
        glview.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // .RENDERMODE_CONTINUOUSLY);
      } catch (IllegalStateException e) {
      }
    }
  }
 public Boolean isVideoOn(String participant) {
   VideoParticipant vp = _users.get(participant);
   if (vp == null) {
     LogSdk.w(TAG, "Participant " + participant + " not found!");
     return false;
   }
   Integer view_id = vp.getViewId();
   if (view_id != null && view_id != -1) {
     return _renders.get(view_id).isVideoOn();
   }
   return false;
 }
  public void turnVideoOff(String sParticipantId) {
    if (!_users.containsKey(sParticipantId)) {
      LogSdk.e(TAG, "Participant " + sParticipantId + " not found");
      return;
    }

    Integer view_id = _users.get(sParticipantId).getViewId();
    if (view_id == -1) {
      LogSdk.w(TAG, "No GLview found for participant " + sParticipantId);
      return;
    }

    RenderViewData d = _renders.get(view_id);
    if (d != null) {
      if (d._render != null) d._render.disconnect();
      if (!_on_pause && !_on_full_mode) {
        d._video_on = false;
        d._user = null;
      }
      LogSdk.d(TAG, "video is OFF for " + sParticipantId);
    } else LogSdk.w(TAG, "turnVideoOff: No GVView found for Participant " + sParticipantId);
  }
 private RenderViewData getFreeRender() {
   RenderViewData rd = null;
   LogSdk.i(Utils.getOoVooTag(), "RENDERS Size = " + _renders.size());
   for (int i = 0; i < _renders.size(); i++) {
     RenderViewData d = _renders.valueAt(i);
     //			if (!d._video_on && d._user == null) {
     if (TextUtils.isEmpty(d._user)) {
       rd = d;
       break;
     }
   }
   return rd;
 }
  public boolean removeParticipant(String sParticipantId) {
    boolean updateFullMode = false;
    if (!_users.containsKey(sParticipantId)) {
      LogSdk.e(TAG, "remove Participant failed! Participant " + sParticipantId + " not found");
      return updateFullMode;
    }
    Integer view_id = _users.get(sParticipantId).getViewId();
    if (view_id == -1) {
      LogSdk.d(TAG, "Participant " + sParticipantId + " is not connected to any GLView");
    } else {
      RenderViewData d = _renders.get(view_id);
      updateFullMode = d.isFullMode;
      d._render.disconnect();
      d._video_on = false;
      d._user = null;
    }
    _users.remove(sParticipantId);

    if (updateFullMode) {
      _on_full_mode = true;
      for (int i = 0; i < _renders.size(); i++) {
        RenderViewData d = _renders.valueAt(i);
        if (d._view_id != view_id) {
          String user = getParticipantByViewId(d._view_id);
          LogSdk.d(TAG, "pausing GLview " + d._view_id + " for user " + user);
          if (d._video_on) {
            ConferenceCore.instance().receiveParticipantVideoOff(user);
            LogSdk.d(TAG, "send turn video Off for user " + user);
          }
        } else {
          d.isFullMode = false;
        }
      }
    }
    LogSdk.d(TAG, "remove Participant " + sParticipantId + " GLView " + view_id + " is free.");
    return updateFullMode;
  }
  public boolean removeParticipant(String sParticipantId) {
    boolean updateFullMode = false;
    if (!_users.containsKey(sParticipantId)) {
      LogSdk.e(TAG, "remove Participant failed! Participant " + sParticipantId + " not found");
      return updateFullMode;
    }
    Integer view_id = _users.get(sParticipantId).getViewId();
    if (view_id == -1) {
      LogSdk.d(TAG, "Participant " + sParticipantId + " is not connected to any GLView");
    } else {
      RenderViewData d = _renders.get(view_id);
      updateFullMode = d.isFullMode;
      d.isFullMode = false;
      d._render.disconnect();
      d._video_on = false;
      d._user = null;
    }
    _users.remove(sParticipantId);

    if (updateFullMode) moveToMultiMode(-1);

    LogSdk.d(TAG, "remove Participant " + sParticipantId + " GLView " + view_id + " is free.");
    return updateFullMode;
  }
 private void fillEmptyRenders() {
   for (Map.Entry<String, VideoParticipant> e : _users.entrySet()) {
     VideoParticipant participant = e.getValue();
     if (participant.getViewId() == -1) {
       RenderViewData d = getFreeRender();
       if (d != null) {
         participant.setViewId(d._view_id);
         LogSdk.i(
             Utils.getOoVooTag(),
             "Add free render for " + participant._participantId + " as " + d._view_id);
         d._video_on = true;
         d._user = participant.getParticipantId();
         ConferenceCore.instance().receiveParticipantVideoOn(participant.getParticipantId());
       } else {
         break;
       }
     }
   }
 }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    try {
      int count = getChildCount();

      int maxHeight = 0;
      int maxWidth = 0;

      // Find out how big everyone wants to be
      measureChildren(widthMeasureSpec, heightMeasureSpec);

      // Find rightmost and bottom-most child
      for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
          int childRight;
          int childBottom;

          DynamicAbsoluteLayout.LayoutParams lp =
              (DynamicAbsoluteLayout.LayoutParams) child.getLayoutParams();

          childRight = lp.x + child.getMeasuredWidth();
          childBottom = lp.y + child.getMeasuredHeight();

          maxWidth = Math.max(maxWidth, childRight);
          maxHeight = Math.max(maxHeight, childBottom);
        }
      }

      // Account for padding too
      maxWidth += getPaddingLeft() + getPaddingRight();
      maxHeight += getPaddingTop() + getPaddingBottom();

      // Check against minimum height and width
      maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
      maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
      measuredWidth = resolveSize(maxWidth, widthMeasureSpec);
      measuredHeight = resolveSize(maxHeight, heightMeasureSpec);
      setMeasuredDimension(measuredWidth, measuredHeight);
    } catch (Exception ex) {
      LogSdk.e("", "", ex);
    }
  }
  public void moveToMultiMode(int viewIdForFullMode) {
    _on_full_mode = false;
    int numActiveRenders = 0;
    for (int i = 0; i < _renders.size(); i++) {
      RenderViewData d = _renders.valueAt(i);
      if (d._view_id != viewIdForFullMode) {
        d.isFullMode = false;
        if (d._video_on && d._user != null) {
          numActiveRenders++;
          ConferenceCore.instance().receiveParticipantVideoOn(d._user);
          LogSdk.d(TAG, "resume GLview " + d._view_id + " for user " + d._user);
        }
      } else {
        numActiveRenders++;
      }
    }

    if (numActiveRenders < ParticipantsManager.MAX_ACTIVE_PARTICIPANTS_IN_CALL
        && _users.size() > numActiveRenders) {
      fillEmptyRenders();
    }
  }
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    try {
      int count = getChildCount();
      for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {

          DynamicAbsoluteLayout.LayoutParams lp =
              (DynamicAbsoluteLayout.LayoutParams) child.getLayoutParams();

          int childLeft = getPaddingLeft() + lp.x;
          int childTop = getPaddingTop() + lp.y;
          child.layout(
              childLeft,
              childTop,
              childLeft + child.getMeasuredWidth(),
              childTop + child.getMeasuredHeight());
        }
      }
    } catch (Exception ex) {
      LogSdk.e("", "", ex);
    }
  }
 private void switchParticipantVideoOff(Participant participant) {
   LogSdk.d(Utils.getOoVooTag(), "turning video off for " + participant.getId());
   mConferenceManager.turnParticipantVideoOff(participant.getId(), participant.getDisplayName());
 }
 public void addGLView(int view_id) {
   _renders.put(view_id, new RenderViewData(view_id));
   LogSdk.d(TAG, "adding GLview " + view_id + " total = " + _renders.size());
 }
 public void addParticipant(String sParticipantId, String sOpaqueString) {
   if (_users.containsKey(sParticipantId)) {
     LogSdk.w(TAG, "add Participant failed! Participant " + sParticipantId + " already exist");
   }
   _users.put(sParticipantId, new VideoParticipant(sParticipantId, sOpaqueString, -1));
 }