summaryrefslogtreecommitdiff
path: root/framework/src/android/telephony/imsmedia/ImsMediaManager.java
blob: d6e6530a166d60c3cbb6394e5f396accb834e4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.telephony.imsmedia;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import java.net.DatagramSocket;
import java.util.Objects;
import java.util.concurrent.Executor;


/**
 * IMS media manager APIs to manage the IMS media sessions
 *
 * @hide
 */
public class ImsMediaManager {
    private static final String TAG = "ImsMediaManager";
    @VisibleForTesting
    protected static final String MEDIA_SERVICE_PACKAGE = "com.android.telephony.imsmedia";
    @VisibleForTesting
    protected static final String MEDIA_SERVICE_CLASS =
            MEDIA_SERVICE_PACKAGE + ".ImsMediaController";
    private final Context mContext;
    private final OnConnectedCallback mOnConnectedCallback;
    private final Executor mExecutor;
    private final ServiceConnection mConnection;
    private volatile IImsMedia mImsMedia;

    /**
     * Opens a RTP session based on the local sockets with the associated initial
     * remote configuration if there is a valid {@link RtpConfig} passed. It starts
     * the media flow if the media direction in the {@link RtpConfig} is set to any
     * value other than {@link RtpConfig#NO_MEDIA_FLOW}. If the open session is
     * successful then a new {@link ImsMediaSession} object will be returned using
     * the {@link SessionCallback#onOpenSessionSuccess(ImsMediaSession)} API. If the
     * open session is failed then an error code {@link SessionOperationResult} will
     * be returned using {@link SessionCallback#onOpenSessionFailure(int)} API.
     *
     * @param rtpSocket  local UDP socket to send and receive incoming RTP packets
     * @param rtcpSocket local UDP socket to send and receive incoming RTCP packets
     * @param rtpConfig  provides remote endpoint info and codec details.
     *                   This could be null initially and the application may update
     *                   this later using {@link ImsMediaSession#modifySession()}
     *                   API.
     * @param callback   callbacks to receive session specific notifications.
     */
    public void openSession(@NonNull final DatagramSocket rtpSocket,
            @NonNull final DatagramSocket rtcpSocket,
            @NonNull final @ImsMediaSession.SessionType int sessionType,
            @Nullable final RtpConfig rtpConfig,
            @NonNull final Executor executor,
            @NonNull final SessionCallback callback) {
        if (isConnected()) {
            try {
                callback.setExecutor(executor);
                mImsMedia.openSession(ParcelFileDescriptor.fromDatagramSocket(rtpSocket),
                        ParcelFileDescriptor.fromDatagramSocket(rtcpSocket), sessionType,
                        rtpConfig, callback.getBinder());
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to openSession: " + e);
                // TODO throw exception or callback.onOpenSessionFailure()
            }
        }
    }

    /**
     * Closes the RTP session including cleanup of all the resources
     * associated with the session. This will also close the session object
     * and associated callback.
     *
     * @param session RTP session to be closed.
     */
    public void closeSession(@NonNull ImsMediaSession session) {
        if (isConnected()) {
            try {
                mImsMedia.closeSession(session.getBinder());
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to closeSession: " + e);
            }
        }
    }

    /**
     * Generates the array of SPROP strings for the given array of video
     * configurations and returns via IImsMediaCallback.
     *
     * @param videoConfigList array of video configuration for which sprop should be generated.
     * @param callback Binder interface implemented by caller and called with array of generated
     * sprop values.
     **/
    public void generateVideoSprop(@NonNull VideoConfig[] videoConfigList, IBinder callback) {
        if (isConnected()) {
            try {
                mImsMedia.generateVideoSprop(videoConfigList, callback);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to closeSession: " + e);
            }
        }
    }

    /**
     * Unbinds the service connection with ImsMediaService
     */
    public void release() {
        // TODO: close all the open sessions
        if (isConnected()) {
            try {
                mContext.unbindService(mConnection);
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "IllegalArgumentException: " + e.toString());
            }
            mImsMedia = null;
        }
    }

    /**
     * Interface to send call-backs to the application when the service is
     * connected.
     */
    public interface OnConnectedCallback {
        /**
         * Called by the ImsMedia framework when the service is connected.
         */
        void onConnected();

        /**
         * Called by the ImsMedia framework when the service is disconnected.
         */
        void onDisconnected();
    }

    public ImsMediaManager(@NonNull Context context, @NonNull Executor executor,
            @NonNull OnConnectedCallback callback) {

        mContext = Objects.requireNonNull(context, "context cannot be null");
        mExecutor = Objects.requireNonNull(executor, "executor cannot be null");
        mOnConnectedCallback = Objects.requireNonNull(callback, "callback cannot be null");

        mConnection = new ServiceConnection() {

            public synchronized void onServiceConnected(
                    ComponentName className, IBinder service) {

                mImsMedia = IImsMedia.Stub.asInterface(service);
                Log.d(TAG, "onServiceConnected");
                mExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        mOnConnectedCallback.onConnected();
                    }
                });
            }

            public void onServiceDisconnected(ComponentName className) {
                Log.d(TAG, "onServiceDisconnected");
                mImsMedia = null;
                mExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        mOnConnectedCallback.onDisconnected();
                    }
                });
            }
        };

        Intent intent = new Intent(IImsMedia.class.getName());
        intent.setClassName(MEDIA_SERVICE_PACKAGE, MEDIA_SERVICE_CLASS);
        boolean bindingSuccessful = mContext.bindService(intent, mConnection,
                Context.BIND_AUTO_CREATE);

        Log.d(TAG, "binding: " + bindingSuccessful);
        if (bindingSuccessful) {
            Log.d(TAG, "bindService successful");
        }
    }

    private boolean isConnected() {
        return mImsMedia != null;
    }

    public static abstract class SessionCallback {
        /** @hide */
        public IBinder getBinder() {
            // Base Implementation
            return null;
        }

        /** @hide */
        public void setExecutor(Executor executor) {
            // Base Implementation
        }

        /**
         * Called when the session is opened successfully
         *
         * @param session session object
         */
        public void onOpenSessionSuccess(ImsMediaSession session) {
            // Base Implementation
        }

        /**
         * Called when the open session fails
         *
         * @param error Error code
         */
        public void onOpenSessionFailure(int error) {
            // Base Implementation
        }

        /**
         * Called when the session is closed.
         */
        public void onSessionClosed() {
            // Base Implementation
        }

    }
}