Home | History | Annotate | Download | only in mbms
      1 /*
      2  * Copyright (C) 2016 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 android.telephony.mbms;
     18 
     19 import android.annotation.IntDef;
     20 import android.annotation.Nullable;
     21 import android.net.Uri;
     22 import android.os.RemoteException;
     23 import android.telephony.MbmsStreamingSession;
     24 import android.telephony.mbms.vendor.IMbmsStreamingService;
     25 import android.util.Log;
     26 
     27 import java.lang.annotation.Retention;
     28 import java.lang.annotation.RetentionPolicy;
     29 
     30 /**
     31  * Class used to represent a single MBMS stream. After a stream has been started with
     32  * {@link MbmsStreamingSession#startStreaming(StreamingServiceInfo, java.util.concurrent.Executor,
     33  * StreamingServiceCallback)},
     34  * this class is used to hold information about the stream and control it.
     35  */
     36 public class StreamingService implements AutoCloseable {
     37     private static final String LOG_TAG = "MbmsStreamingService";
     38 
     39     /**
     40      * The state of a stream, reported via {@link StreamingServiceCallback#onStreamStateUpdated}
     41      * @hide
     42      */
     43     @Retention(RetentionPolicy.SOURCE)
     44     @IntDef(prefix = { "STATE_" }, value = {STATE_STOPPED, STATE_STARTED, STATE_STALLED})
     45     public @interface StreamingState {}
     46     public final static int STATE_STOPPED = 1;
     47     public final static int STATE_STARTED = 2;
     48     public final static int STATE_STALLED = 3;
     49 
     50     /**
     51      * The reason for a stream state change, reported via
     52      * {@link StreamingServiceCallback#onStreamStateUpdated}
     53      * @hide
     54      */
     55     @Retention(RetentionPolicy.SOURCE)
     56     @IntDef(prefix = { "REASON_" },
     57             value = {REASON_BY_USER_REQUEST, REASON_END_OF_SESSION, REASON_FREQUENCY_CONFLICT,
     58             REASON_OUT_OF_MEMORY, REASON_NOT_CONNECTED_TO_HOMECARRIER_LTE,
     59             REASON_LEFT_MBMS_BROADCAST_AREA, REASON_NONE})
     60     public @interface StreamingStateChangeReason {}
     61 
     62     /**
     63      * Indicates that the middleware does not have a reason to provide for the state change.
     64      */
     65     public static final int REASON_NONE = 0;
     66 
     67     /**
     68      * State changed due to a call to {@link #close()} or
     69      * {@link MbmsStreamingSession#startStreaming(StreamingServiceInfo,
     70      * java.util.concurrent.Executor, StreamingServiceCallback)}
     71      */
     72     public static final int REASON_BY_USER_REQUEST = 1;
     73 
     74     /**
     75      * State changed due to the streaming session ending at the carrier.
     76      */
     77     public static final int REASON_END_OF_SESSION = 2;
     78 
     79     /**
     80      * State changed due to a frequency conflict with another requested stream.
     81      */
     82     public static final int REASON_FREQUENCY_CONFLICT = 3;
     83 
     84     /**
     85      * State changed due to the middleware running out of memory
     86      */
     87     public static final int REASON_OUT_OF_MEMORY = 4;
     88 
     89     /**
     90      * State changed due to the device leaving the home carrier's LTE network.
     91      */
     92     public static final int REASON_NOT_CONNECTED_TO_HOMECARRIER_LTE = 5;
     93 
     94     /**
     95      * State changed due to the device leaving the where this stream is being broadcast.
     96      */
     97     public static final int REASON_LEFT_MBMS_BROADCAST_AREA = 6;
     98 
     99     /**
    100      * The method of transmission currently used for a stream,
    101      * reported via {@link StreamingServiceCallback#onStreamMethodUpdated}
    102      */
    103     public final static int BROADCAST_METHOD = 1;
    104     public final static int UNICAST_METHOD   = 2;
    105 
    106     private final int mSubscriptionId;
    107     private final MbmsStreamingSession mParentSession;
    108     private final StreamingServiceInfo mServiceInfo;
    109     private final InternalStreamingServiceCallback mCallback;
    110 
    111     private IMbmsStreamingService mService;
    112 
    113     /**
    114      * @hide
    115      */
    116     public StreamingService(int subscriptionId,
    117             IMbmsStreamingService service,
    118             MbmsStreamingSession session,
    119             StreamingServiceInfo streamingServiceInfo,
    120             InternalStreamingServiceCallback callback) {
    121         mSubscriptionId = subscriptionId;
    122         mParentSession = session;
    123         mService = service;
    124         mServiceInfo = streamingServiceInfo;
    125         mCallback = callback;
    126     }
    127 
    128     /**
    129      * Retrieve the Uri used to play this stream.
    130      *
    131      * May throw an {@link IllegalArgumentException} or an {@link IllegalStateException}.
    132      *
    133      * @return The {@link Uri} to pass to the streaming client, or {@code null} if an error
    134      *         occurred.
    135      */
    136     public @Nullable Uri getPlaybackUri() {
    137         if (mService == null) {
    138             throw new IllegalStateException("No streaming service attached");
    139         }
    140 
    141         try {
    142             return mService.getPlaybackUri(mSubscriptionId, mServiceInfo.getServiceId());
    143         } catch (RemoteException e) {
    144             Log.w(LOG_TAG, "Remote process died");
    145             mService = null;
    146             mParentSession.onStreamingServiceStopped(this);
    147             sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, null);
    148             return null;
    149         }
    150     }
    151 
    152     /**
    153      * Retrieve the {@link StreamingServiceInfo} corresponding to this stream.
    154      */
    155     public StreamingServiceInfo getInfo() {
    156         return mServiceInfo;
    157     }
    158 
    159     /**
    160      * Stop streaming this service. Further operations on this object will fail with an
    161      * {@link IllegalStateException}.
    162      *
    163      * May throw an {@link IllegalArgumentException} or an {@link IllegalStateException}
    164      */
    165     @Override
    166     public void close() {
    167         if (mService == null) {
    168             throw new IllegalStateException("No streaming service attached");
    169         }
    170 
    171         try {
    172             mService.stopStreaming(mSubscriptionId, mServiceInfo.getServiceId());
    173         } catch (RemoteException e) {
    174             Log.w(LOG_TAG, "Remote process died");
    175             mService = null;
    176             sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, null);
    177         } finally {
    178             mParentSession.onStreamingServiceStopped(this);
    179         }
    180     }
    181 
    182     /** @hide */
    183     public InternalStreamingServiceCallback getCallback() {
    184         return mCallback;
    185     }
    186 
    187     private void sendErrorToApp(int errorCode, String message) {
    188         try {
    189             mCallback.onError(errorCode, message);
    190         } catch (RemoteException e) {
    191             // Ignore, should not happen locally.
    192         }
    193     }
    194 }
    195 
    196