1 /** 2 * Copyright (C) 2022 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.telephony.imsmedia; 18 19 import android.os.Parcel; 20 import android.os.ParcelFileDescriptor; 21 import android.telephony.imsmedia.ImsMediaSession; 22 import android.util.Log; 23 24 import com.android.telephony.imsmedia.Utils.OpenSessionParams; 25 26 /** 27 * Text service for internal AP based RTP stack. This interacts with native library to open/close 28 * {@link TextLocalSession} 29 */ 30 public class TextService { 31 private static final String LOG_TAG = "TextService"; 32 private long mNativeObject = 0; 33 private TextListener mListener = null; 34 TextService()35 TextService() { 36 mNativeObject = JNIImsMediaService.getInterface(ImsMediaSession.SESSION_TYPE_RTT); 37 } 38 39 /** Returns the native instance identifier of TextManager in libimsmedia */ getNativeObject()40 public long getNativeObject() { 41 return mNativeObject; 42 } 43 44 /** Sets JNI listener to get JNI callback from libimsmediajni library */ setListener(final TextListener listener)45 public void setListener(final TextListener listener) { 46 mListener = listener; 47 } 48 49 /** 50 * Sends request message with the corresponding arguments to libimsmediajni 51 * library to operate 52 * 53 * @param sessionId : session identifier 54 * @param parcel : parcel argument to send to JNI 55 */ sendRequest(final int sessionId, Parcel parcel)56 public void sendRequest(final int sessionId, Parcel parcel) { 57 if (mNativeObject != 0) { 58 byte[] data = parcel.marshall(); 59 parcel.recycle(); 60 parcel = null; 61 JNIImsMediaService.sendMessage(mNativeObject, sessionId, data); 62 } 63 } 64 65 /** 66 * Opens a RTP session based on local the local sockets with the associated initial remote 67 * configuration if there is a valid RtpConfig passed. It starts the media flow if the media 68 * direction in the RtpConfig is set to any value other than MEDIA_DIRECTION_NO_FLOW. If the 69 * open session is successful then a new TextLocalSession object will be created using the 70 * JNIImsMediaListener#onMessage() API. If the open session is failed then a error code will be 71 * returned using JNIImsMediaListener#onMessage(int) API. 72 * 73 * @param sessionId A unique RTP session identifier 74 * @param sessionParams Paratmers including rtp, rtcp socket to send and receive incoming RTP 75 * packets and RtpConfig to create session. 76 * 77 * @return RESULT_INVALID_PARAM - input params are not valid and 78 * RESULT_SUCCESS - open session request is accepted. 79 */ openSession(final int sessionId, final OpenSessionParams sessionParams)80 public int openSession(final int sessionId, final OpenSessionParams sessionParams) { 81 if (mNativeObject == 0 || sessionParams == null) { 82 return ImsMediaSession.RESULT_INVALID_PARAM; 83 } 84 85 ParcelFileDescriptor rtpSockFd = sessionParams.getRtpFd(); 86 ParcelFileDescriptor rtcpSockFd = sessionParams.getRtcpFd(); 87 if (rtpSockFd == null || rtcpSockFd == null) { 88 Log.e(LOG_TAG, "Rtp/Rtcp socket fds are null"); 89 return ImsMediaSession.RESULT_INVALID_PARAM; 90 } 91 92 Log.d(LOG_TAG, "openSession: sessionId = " + sessionId 93 + "," + sessionParams.getRtpConfig()); 94 95 JNIImsMediaService.setListener(sessionId, mListener); 96 97 final int socketFdRtp = rtpSockFd.detachFd(); 98 final int socketFdRtcp = rtcpSockFd.detachFd(); 99 100 Parcel parcel = Parcel.obtain(); 101 parcel.writeInt(TextSession.CMD_OPEN_SESSION); 102 parcel.writeInt(socketFdRtp); 103 parcel.writeInt(socketFdRtcp); 104 105 if (sessionParams.getRtpConfig() != null) { 106 sessionParams.getRtpConfig().writeToParcel(parcel, ImsMediaSession.SESSION_TYPE_RTT); 107 } 108 sendRequest(sessionId, parcel); 109 return ImsMediaSession.RESULT_SUCCESS; 110 } 111 112 /** 113 * Closes the RTP session including cleanup of all the resources associated with the session. 114 * This will also close the session object and associated callback. 115 * 116 * @param sessionId The session id to be closed. 117 */ closeSession(final int sessionId)118 public void closeSession(final int sessionId) { 119 Log.d(LOG_TAG, "closeSession"); 120 Parcel parcel = Parcel.obtain(); 121 parcel.writeInt(TextSession.CMD_CLOSE_SESSION); 122 sendRequest(sessionId, parcel); 123 } 124 } 125