1 /* 2 * Copyright (C) 2017 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 package com.google.android.exoplayer2.ext.cast; 17 18 import androidx.annotation.Nullable; 19 import com.google.android.exoplayer2.C; 20 import com.google.android.exoplayer2.Format; 21 import com.google.android.gms.cast.CastStatusCodes; 22 import com.google.android.gms.cast.MediaInfo; 23 import com.google.android.gms.cast.MediaTrack; 24 25 /** 26 * Utility methods for ExoPlayer/Cast integration. 27 */ 28 /* package */ final class CastUtils { 29 30 /** 31 * Returns the duration in microseconds advertised by a media info, or {@link C#TIME_UNSET} if 32 * unknown or not applicable. 33 * 34 * @param mediaInfo The media info to get the duration from. 35 * @return The duration in microseconds, or {@link C#TIME_UNSET} if unknown or not applicable. 36 */ getStreamDurationUs(@ullable MediaInfo mediaInfo)37 public static long getStreamDurationUs(@Nullable MediaInfo mediaInfo) { 38 if (mediaInfo == null) { 39 return C.TIME_UNSET; 40 } 41 long durationMs = mediaInfo.getStreamDuration(); 42 return durationMs != MediaInfo.UNKNOWN_DURATION ? C.msToUs(durationMs) : C.TIME_UNSET; 43 } 44 45 /** 46 * Returns a descriptive log string for the given {@code statusCode}, or "Unknown." if not one of 47 * {@link CastStatusCodes}. 48 * 49 * @param statusCode A Cast API status code. 50 * @return A descriptive log string for the given {@code statusCode}, or "Unknown." if not one of 51 * {@link CastStatusCodes}. 52 */ getLogString(int statusCode)53 public static String getLogString(int statusCode) { 54 switch (statusCode) { 55 case CastStatusCodes.APPLICATION_NOT_FOUND: 56 return "A requested application could not be found."; 57 case CastStatusCodes.APPLICATION_NOT_RUNNING: 58 return "A requested application is not currently running."; 59 case CastStatusCodes.AUTHENTICATION_FAILED: 60 return "Authentication failure."; 61 case CastStatusCodes.CANCELED: 62 return "An in-progress request has been canceled, most likely because another action has " 63 + "preempted it."; 64 case CastStatusCodes.ERROR_SERVICE_CREATION_FAILED: 65 return "The Cast Remote Display service could not be created."; 66 case CastStatusCodes.ERROR_SERVICE_DISCONNECTED: 67 return "The Cast Remote Display service was disconnected."; 68 case CastStatusCodes.FAILED: 69 return "The in-progress request failed."; 70 case CastStatusCodes.INTERNAL_ERROR: 71 return "An internal error has occurred."; 72 case CastStatusCodes.INTERRUPTED: 73 return "A blocking call was interrupted while waiting and did not run to completion."; 74 case CastStatusCodes.INVALID_REQUEST: 75 return "An invalid request was made."; 76 case CastStatusCodes.MESSAGE_SEND_BUFFER_TOO_FULL: 77 return "A message could not be sent because there is not enough room in the send buffer at " 78 + "this time."; 79 case CastStatusCodes.MESSAGE_TOO_LARGE: 80 return "A message could not be sent because it is too large."; 81 case CastStatusCodes.NETWORK_ERROR: 82 return "Network I/O error."; 83 case CastStatusCodes.NOT_ALLOWED: 84 return "The request was disallowed and could not be completed."; 85 case CastStatusCodes.REPLACED: 86 return "The request's progress is no longer being tracked because another request of the " 87 + "same type has been made before the first request completed."; 88 case CastStatusCodes.SUCCESS: 89 return "Success."; 90 case CastStatusCodes.TIMEOUT: 91 return "An operation has timed out."; 92 case CastStatusCodes.UNKNOWN_ERROR: 93 return "An unknown, unexpected error has occurred."; 94 default: 95 return CastStatusCodes.getStatusCodeString(statusCode); 96 } 97 } 98 99 /** 100 * Creates a {@link Format} instance containing all information contained in the given 101 * {@link MediaTrack} object. 102 * 103 * @param mediaTrack The {@link MediaTrack}. 104 * @return The equivalent {@link Format}. 105 */ mediaTrackToFormat(MediaTrack mediaTrack)106 public static Format mediaTrackToFormat(MediaTrack mediaTrack) { 107 return new Format.Builder() 108 .setId(mediaTrack.getContentId()) 109 .setContainerMimeType(mediaTrack.getContentType()) 110 .setLanguage(mediaTrack.getLanguage()) 111 .build(); 112 } 113 CastUtils()114 private CastUtils() {} 115 116 } 117