Skip to content

03. API Description [API 설명]

Lee Jae Hyun edited this page May 31, 2019 · 4 revisions

Run video call SDK PlayRTC

  • PlayRTC SDK download(https://www.playrtc.com)
  • Use the bundle to run the playRTC class with the helper's token and name
new MaterialDialog.Builder(getActivity())
  .title(R.string.app_name)
  .titleColor(getResources().getColor(R.color.colorPrimary))
  .content("도움을 요청하시겠습니까?")
  .positiveText("확인")
  .negativeText("취소")
  .onPositive(new MaterialDialog.SingleButtonCallback() {
  @Override
  public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
  //Use the bundle to run the playRTC class with the helper's token and name
       Bundle bundle = new Bundle();
       Intent intent = new Intent(getActivity(),PlayRTCMain.class);
       bundle.putString("token",pushToken);
       bundle.putString("name",nameText.getText().toString());
       intent.putExtras(bundle);
       startActivity(intent);
       }
       })
        .onNegative(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
                }
         })
         .show();

Send FCM(Firebase Cloud Message)

  • Implementation okHttp library in app level
  • Setting the content of the notification to send
  • Set body and header of request
  • Create OkHttpClient class object to send a request
void sendFCM() {
        Gson gson = new Gson();
        NotificationModel notificationModel = new NotificationModel();
        notificationModel.to = destinationUserModel.pushToken;
        notificationModel.data.title = InitApp.sUser.getDisplayName() + " requested video call to you";
        notificationModel.data.text = playrtc.getChannelId();
        notificationModel.data.ip = getLocalIpAddress();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf8"),
                                  gson.toJson(notificationModel));
        Request request = new Request.Builder()
                .header("Content-Type", "application/json")
                .addHeader("Authorization", "key=AIzaSyA3Bv1IEgeHVEGLajuQ0c7uaPe9ERPMMaI")
                .url("https://fcm.googleapis.com/fcm/send")
                .post(requestBody)
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newCall(request).enqueue((new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        }));
    }

Receive text & drawing data

  • Create channel to connect two client (helper & helpee) and receive data
  • run thread for receiving data (text, drawing, color option)
  • Depending on the protocol, if it is illustrated, it receives coordinates and a string value

        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    //create server socket
                    serversocket = new ServerSocket(PORT);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
                try {

                    //get client socket that are connected to the server (return client sockets when clients connect)
                    socket = serversocket.accept(); //The server waits here for the client to connect...

                    //Build a path to send and receive data with clients
                    is = new DataInputStream(socket.getInputStream()); //path for receiving messages from clients
                    os = new DataOutputStream(socket.getOutputStream());//path for sending messages to clients

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }

                //receive client messages indefinitely until client disconnects
                while (helpeeIsConnected) {
                    try {
                        helpee_temp = is.readUTF();

                        if (helpee_temp.startsWith("<text> ")) {
                            helpeeMsg = helpee_temp.substring(7);
                        } else if (helpee_temp.startsWith("<draw>")) {
                            x = new Vector<Float>();
                            y = new Vector<Float>();
                            String strX = is.readUTF();
                            String strY = is.readUTF();
                            String[] tempX = strX.split(" ");
                            String[] tempY = strY.split(" ");

                            for (int i = 0; i < tempX.length; i++) {
                                x.add(Float.parseFloat(tempX[i]));
                                y.add(Float.parseFloat(tempY[i]));
                            }

                        } else if (helpee_temp.startsWith("<color>")) {
                            changedColor = Integer.parseInt(is.readUTF());
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        //       e.printStackTrace();
                    }

                    //display message read from client into TextView
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //display text message
                            if (helpee_temp.startsWith("<text>")) {
                                printMs.append("상대방 : " + helpeeMsg + "\n");
                                //display drawing point
                            } else if (helpee_temp.startsWith("<draw>")) {
                                mDraw.draw2(x, y);
                                //change color option
                            } else if (helpee_temp.startsWith("<color>")) {
                                switch (changedColor) {
                                    case R.id.btn_red:
                                        mDraw = new DrawOnTop(getApplicationContext(), Color.parseColor("#B71C1C"));
                                        myVideoViewGroup.addView(mDraw);
                                        break;
                                    case R.id.btn_yellow:
                                        mDraw = new DrawOnTop(getApplicationContext(), Color.parseColor("#FFEB3B"));
                                        myVideoViewGroup.addView(mDraw);
                                        break;
                                    case R.id.btn_green:
                                        mDraw = new DrawOnTop(getApplicationContext(), Color.parseColor("#388E3C"));
                                        myVideoViewGroup.addView(mDraw);
                                        break;
                                    case R.id.btn_blue:
                                        mDraw = new DrawOnTop(getApplicationContext(), Color.parseColor("#1E88E5"));
                                        myVideoViewGroup.addView(mDraw);
                                        break;
                                    case R.id.btn_black:
                                        mDraw = new DrawOnTop(getApplicationContext(), Color.parseColor("#000000"));
                                        myVideoViewGroup.addView(mDraw);
                                        break;
                                }
                            }

                        }
                    });
                }//while..
            }//run method...
        }).start();


Send text & drawing data

  • run thread for sending data (text, drawing, color option)
  • send multimedia data by encoding UTF format
        //send client drawing messages indefinitely until client disconnects
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (helpeeIsConnected) {
                    try {
                        if (mDraw.drawing == false) {
                            os.writeUTF("<draw>");
                            os.flush();
                            os.writeUTF(mDraw.getCoordX());
                            os.flush();
                            os.writeUTF(mDraw.getCoordY());
                            os.flush();
                            mDraw.drawing = true;
                        }
                        if (colorChanged == true) {
                            os.writeUTF("<color>");
                            os.writeUTF(String.valueOf(color));
                            colorChanged = false;
                        }
                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }