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.testimsmediahal;
18 
19 import android.hardware.radio.ims.media.IImsMedia;
20 import android.hardware.radio.ims.media.IImsMediaListener;
21 import android.hardware.radio.ims.media.LocalEndPoint;
22 import android.hardware.radio.ims.media.RtpConfig;
23 import android.os.Parcel;
24 import android.telephony.imsmedia.AudioConfig;
25 import android.util.Log;
26 
27 import com.android.telephony.imsmedia.AudioSession;
28 import com.android.telephony.imsmedia.Utils;
29 
30 /**
31  * Instantiates the {@link AudioListenerProxy} and {@link JNIConnector}
32  * {@link JNIConnector} used to connect with {@link libimsmediahaljni}
33  * to send session modification request to {@link libimsmedia}
34  * {@link AudioListenerProxy} used to get respose from {@link libimsmedia}
35  */
36 
37 public class IImsMediaImpl extends IImsMedia.Stub {
38 
39     private static final String TAG = "IImsMediaImpl";
40     private AudioListenerProxy mMediaResponse;
41     private static JNIConnector connector;
42 
IImsMediaImpl()43     public IImsMediaImpl() {
44         Log.d(TAG, "Instantiated");
45         mMediaResponse = AudioListenerProxy.getInstance();
46         connector = JNIConnector.getInstance();
47     }
48 
49     @Override
getInterfaceHash()50     public String getInterfaceHash() {
51         return IImsMedia.HASH;
52     }
53 
54     @Override
getInterfaceVersion()55     public int getInterfaceVersion() {
56         return IImsMedia.VERSION;
57     }
58 
59     @Override
setListener(IImsMediaListener mediaListener)60     public void setListener(IImsMediaListener mediaListener) {
61         Log.d(TAG, "setListener");
62         mMediaResponse.setImsMediaListener(mediaListener);
63     }
64 
65     /**
66      * Opening Media session.
67      *
68      * @param sessionId     unique identifier of session.
69      * @param localEndPoint used to get rtp/rtcp socket file descriptors.
70      * @param config        HAl RtpConfig used configure session related params.
71      */
72 
73     @Override
openSession(int sessionId, LocalEndPoint localEndPoint, RtpConfig config)74     public void openSession(int sessionId, LocalEndPoint localEndPoint, RtpConfig config) {
75         Log.d(TAG, "openSession");
76         connector.connectJni(sessionId);
77         mMediaResponse.setSessionId(sessionId);
78         Parcel parcel = Parcel.obtain();
79 
80         parcel.writeInt(AudioSession.CMD_OPEN_SESSION);
81 
82         final int socketFdRtp = (localEndPoint.rtpFd != null) ? localEndPoint.rtpFd.getFd() : -1;
83         final int socketFdRtcp = (localEndPoint.rtcpFd != null) ? localEndPoint.rtcpFd.getFd() : -1;
84 
85         parcel.writeInt(socketFdRtp);
86         parcel.writeInt(socketFdRtcp);
87 
88         if (config != null) {
89             AudioConfig audioConfig = (AudioConfig) Utils.convertToAudioConfig(config);
90             audioConfig.writeToParcel(parcel, 0);
91         }
92         connector.sendRequest(sessionId, parcel);
93     }
94 
95     /**
96      * Closing Media session on libimsmedia side.
97      *
98      * @param sessionId unique sessionId used to close respective session.
99      */
100 
101     @Override
closeSession(int sessionId)102     public void closeSession(int sessionId) {
103         Log.d(TAG, "closeSession");
104         Parcel parcel = Parcel.obtain();
105         parcel.writeInt(AudioSession.CMD_CLOSE_SESSION);
106         connector.sendRequest(sessionId, parcel);
107     }
108 }
109