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.IImsMediaSession;
20 import android.hardware.radio.ims.media.IImsMediaSessionListener;
21 import android.hardware.radio.ims.media.MediaQualityThreshold;
22 import android.hardware.radio.ims.media.RtpConfig;
23 import android.hardware.radio.ims.media.RtpHeaderExtension;
24 import android.os.Parcel;
25 import android.telephony.imsmedia.AudioConfig;
26 import android.util.Log;
27 
28 import com.android.telephony.imsmedia.AudioSession;
29 import com.android.telephony.imsmedia.Utils;
30 
31 import java.util.List;
32 
33 /**
34  * MediaSession implementation class for adding modifying media session.
35  * Class is creating JNI connection with {@link libimsmedia} via
36  * {@link libimsmediahaljni}.
37  */
38 
39 public class IImsMediaSessionImpl extends IImsMediaSession.Stub {
40 
41     private static final String TAG = "IImsMediaSessionImpl";
42 
43     private int mSessionId;
44     private AudioListenerProxy mNativeListener;
45     private static JNIConnector connector;
46 
47     /**
48      * @constructor of the IIMsMediaSession class.
49      *
50      * @param sessionId used to identify session.
51      */
52 
IImsMediaSessionImpl(int sessionId)53     public IImsMediaSessionImpl(int sessionId) {
54         Log.d(TAG, "Instantiated");
55         mSessionId = sessionId;
56         mNativeListener = AudioListenerProxy.getInstance();
57         connector = JNIConnector.getInstance();
58     }
59 
60     @Override
getInterfaceHash()61     public String getInterfaceHash() {
62         return IImsMediaSession.HASH;
63     }
64 
65     @Override
getInterfaceVersion()66     public int getInterfaceVersion() {
67         return IImsMediaSession.VERSION;
68     }
69 
70     @Override
setListener(IImsMediaSessionListener mediaSessionListener)71     public void setListener(IImsMediaSessionListener mediaSessionListener) {
72 
73         mNativeListener.setMediaSessionListener(mediaSessionListener);
74     }
75 
76     @Override
modifySession(RtpConfig config)77     public void modifySession(RtpConfig config) {
78         Log.d(TAG, "modifyConfig: " + config);
79         Parcel parcel = Parcel.obtain();
80         parcel.writeInt(AudioSession.CMD_MODIFY_SESSION);
81 
82         if (config != null) {
83             AudioConfig audioConfig = Utils.convertToAudioConfig(config);
84             audioConfig.writeToParcel(parcel, 0);
85         }
86         connector.sendRequest(mSessionId, parcel);
87     }
88 
89     @Override
sendDtmf(char dtmfDigit, int duration)90     public void sendDtmf(char dtmfDigit, int duration) {
91         Log.d(TAG, "sendDtmf: digit= " + dtmfDigit + ", duration=" + duration);
92         Parcel parcel = Parcel.obtain();
93         parcel.writeInt(AudioSession.CMD_SEND_DTMF);
94         parcel.writeByte((byte) dtmfDigit);
95         parcel.writeInt(duration);
96         connector.sendRequest(mSessionId, parcel);
97     }
98 
99     @Override
startDtmf(char dtmfDigit)100     public void startDtmf(char dtmfDigit) {
101         Log.d(TAG, "startDtmf: digit= " + dtmfDigit);
102         Parcel parcel = Parcel.obtain();
103         parcel.writeInt(AudioSession.CMD_START_DTMF);
104         parcel.writeByte((byte) dtmfDigit);
105         connector.sendRequest(mSessionId, parcel);
106     }
107 
108     @Override
stopDtmf()109     public void stopDtmf() {
110         Log.d(TAG, "stopDtmf");
111         Parcel parcel = Parcel.obtain();
112         parcel.writeInt(AudioSession.CMD_STOP_DTMF);
113         connector.sendRequest(mSessionId, parcel);
114     }
115 
116     @Override
sendHeaderExtension(List<RtpHeaderExtension> data)117     public void sendHeaderExtension(List<RtpHeaderExtension> data) {
118         Log.d(TAG, "sendHeaderExtension: " + data);
119         Parcel parcel = Parcel.obtain();
120         parcel.writeInt(AudioSession.CMD_SEND_RTP_HDR_EXTN);
121         parcel.writeInt(data.size());
122         for (RtpHeaderExtension item : data) {
123             item.writeToParcel(parcel, 0);
124         }
125         connector.sendRequest(mSessionId, parcel);
126     }
127 
128     @Override
setMediaQualityThreshold(MediaQualityThreshold threshold)129     public void setMediaQualityThreshold(MediaQualityThreshold threshold) {
130         Log.d(TAG, "setMediaQualityThreshold: " + threshold);
131         Parcel parcel = Parcel.obtain();
132         parcel.writeInt(AudioSession.CMD_SET_MEDIA_QUALITY_THRESHOLD);
133         if (threshold != null) {
134             threshold.writeToParcel(parcel, 0);
135         }
136         connector.sendRequest(mSessionId, parcel);
137     }
138 }
139