// Path: googlemediaframework/src/main/java/com/google/android/libraries/mediaframework/exoplayerextensions/ExoplayerWrapper.java
// public interface RendererBuilder {
//   /**
//    * Constructs the necessary components for playback.
//    *
//    * @param player The parent player.
//    */
//   void buildRenderers(ExoplayerWrapper player);
// 
//   /**
//    * Cancels the current build operation, if there is one. Else does nothing.
//    */
//   void cancel();
// }
// 
// Path: googlemediaframework/src/main/java/com/google/android/libraries/mediaframework/layeredvideo/Util.java
// public class Util {
// 
//   /**
//    * Create a LayoutParams object for the given view which enforces a given width and height.
//    *
//    * <p>This method is a bit complicated because the TYPE of the LayoutParams that a view must
//    * receive (ex. LinearLayout.LayoutParams, RelativeLayout.LayoutParams) depends on the type of its
//    * PARENT view.
//    *
//    * <p>Thus, in this method, we look at the parent view of the given view, determine its type,
//    * and create the appropriate LayoutParams for that type.
//    *
//    * <p>This method only supports views which are nested inside a FrameLayout, LinearLayout, or
//    * GridLayout.
//    */
//   public static ViewGroup.LayoutParams getLayoutParamsBasedOnParent(View view, int width, int height)
//       throws IllegalArgumentException {
// 
//     // Get the parent of the given view.
//     ViewParent parent = view.getParent();
// 
//     // Determine what is the parent's type and return the appropriate type of LayoutParams.
//     if (parent instanceof FrameLayout) {
//       return new FrameLayout.LayoutParams(width, height);
//     }
//     if (parent instanceof RelativeLayout) {
//       return new RelativeLayout.LayoutParams(width, height);
//     }
//     if (parent instanceof LinearLayout) {
//       return new LinearLayout.LayoutParams(width, height);
//     }
// 
//     // Throw this exception if the parent is not the correct type.
//     IllegalArgumentException exception = new IllegalArgumentException("The PARENT of a " +
//         "FrameLayout container used by the GoogleMediaFramework must be a LinearLayout, " +
//         "FrameLayout, or RelativeLayout. Please ensure that the container is inside one of these " +
//         "three supported view groups.");
// 
//     // If the parent is not one of the supported types, throw our exception.
//     throw exception;
//   }
// 
//   /** Returns the consumer friendly device name */
//   public static String getDeviceInfo() {
//     return Build.MANUFACTURER + " " + Build.MODEL + " " + android.os.Build.VERSION.RELEASE;
//   }
// }


import android.content.Context;
import android.media.AudioManager;
import android.media.MediaCodec;
import android.net.Uri;
import android.util.Log;
import com.google.android.exoplayer.DecoderInfo;
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer;
import com.google.android.exoplayer.MediaCodecSelector;
import com.google.android.exoplayer.MediaCodecUtil;
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer;
import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.audio.AudioCapabilities;
import com.google.android.exoplayer.extractor.Extractor;
import com.google.android.exoplayer.extractor.ExtractorSampleSource;
import com.google.android.exoplayer.text.TextTrackRenderer;
import com.google.android.exoplayer.upstream.Allocator;
import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DefaultAllocator;
import com.google.android.exoplayer.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer.upstream.DefaultUriDataSource;
import com.google.android.libraries.mediaframework.exoplayerextensions.ExoplayerWrapper.RendererBuilder;
import com.google.android.libraries.mediaframework.layeredvideo.Util;
/*
 * Copyright (C) 2014 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 com.google.android.libraries.mediaframework.exoplayerextensions;



/**
 * A {@link RendererBuilder} for streams that can be read using an {@link Extractor}.
 */
public class ExtractorRendererBuilder implements RendererBuilder {
    private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
    private static final int BUFFER_SEGMENT_COUNT = 256;

    private final Context context;
    private final String userAgent;
    private final Uri uri;
    private final boolean preferSoftwareDecoder;

    public ExtractorRendererBuilder(Context context, String userAgent, Uri uri, boolean preferSoftwareDecoder) {
        this.context = context;
        this.userAgent = userAgent;
        this.uri = uri;
        this.preferSoftwareDecoder = preferSoftwareDecoder;
    }

    private MediaCodecSelector preferSoftwareMediaCodecSelector = new MediaCodecSelector() {
        @Override
        public DecoderInfo getDecoderInfo(String mimeType, boolean requiresSecureDecoder) throws MediaCodecUtil.DecoderQueryException {
// Path: googlemediaframework/src/main/java/com/google/android/libraries/mediaframework/exoplayerextensions/ExoplayerWrapper.java
// public interface RendererBuilder {
//   /**
//    * Constructs the necessary components for playback.
//    *
//    * @param player The parent player.
//    */
//   void buildRenderers(ExoplayerWrapper player);
// 
//   /**
//    * Cancels the current build operation, if there is one. Else does nothing.
//    */
//   void cancel();
// }
// 
// Path: googlemediaframework/src/main/java/com/google/android/libraries/mediaframework/layeredvideo/Util.java
// public class Util {
// 
//   /**
//    * Create a LayoutParams object for the given view which enforces a given width and height.
//    *
//    * <p>This method is a bit complicated because the TYPE of the LayoutParams that a view must
//    * receive (ex. LinearLayout.LayoutParams, RelativeLayout.LayoutParams) depends on the type of its
//    * PARENT view.
//    *
//    * <p>Thus, in this method, we look at the parent view of the given view, determine its type,
//    * and create the appropriate LayoutParams for that type.
//    *
//    * <p>This method only supports views which are nested inside a FrameLayout, LinearLayout, or
//    * GridLayout.
//    */
//   public static ViewGroup.LayoutParams getLayoutParamsBasedOnParent(View view, int width, int height)
//       throws IllegalArgumentException {
// 
//     // Get the parent of the given view.
//     ViewParent parent = view.getParent();
// 
//     // Determine what is the parent's type and return the appropriate type of LayoutParams.
//     if (parent instanceof FrameLayout) {
//       return new FrameLayout.LayoutParams(width, height);
//     }
//     if (parent instanceof RelativeLayout) {
//       return new RelativeLayout.LayoutParams(width, height);
//     }
//     if (parent instanceof LinearLayout) {
//       return new LinearLayout.LayoutParams(width, height);
//     }
// 
//     // Throw this exception if the parent is not the correct type.
//     IllegalArgumentException exception = new IllegalArgumentException("The PARENT of a " +
//         "FrameLayout container used by the GoogleMediaFramework must be a LinearLayout, " +
//         "FrameLayout, or RelativeLayout. Please ensure that the container is inside one of these " +
//         "three supported view groups.");
// 
//     // If the parent is not one of the supported types, throw our exception.
//     throw exception;
//   }
// 
//   /** Returns the consumer friendly device name */
//   public static String getDeviceInfo() {
//     return Build.MANUFACTURER + " " + Build.MODEL + " " + android.os.Build.VERSION.RELEASE;
//   }
// }


// Path: googlemediaframework/src/main/java/com/google/android/libraries/mediaframework/exoplayerextensions/ExtractorRendererBuilder.java
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaCodec;
import android.net.Uri;
import android.util.Log;
import com.google.android.exoplayer.DecoderInfo;
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer;
import com.google.android.exoplayer.MediaCodecSelector;
import com.google.android.exoplayer.MediaCodecUtil;
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer;
import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.audio.AudioCapabilities;
import com.google.android.exoplayer.extractor.Extractor;
import com.google.android.exoplayer.extractor.ExtractorSampleSource;
import com.google.android.exoplayer.text.TextTrackRenderer;
import com.google.android.exoplayer.upstream.Allocator;
import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DefaultAllocator;
import com.google.android.exoplayer.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer.upstream.DefaultUriDataSource;
import com.google.android.libraries.mediaframework.exoplayerextensions.ExoplayerWrapper.RendererBuilder;
import com.google.android.libraries.mediaframework.layeredvideo.Util;

/*
 * Copyright (C) 2014 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 com.google.android.libraries.mediaframework.exoplayerextensions;



/**
 * A {@link RendererBuilder} for streams that can be read using an {@link Extractor}.
 */
public class ExtractorRendererBuilder implements RendererBuilder {
    private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
    private static final int BUFFER_SEGMENT_COUNT = 256;

    private final Context context;
    private final String userAgent;
    private final Uri uri;
    private final boolean preferSoftwareDecoder;

    public ExtractorRendererBuilder(Context context, String userAgent, Uri uri, boolean preferSoftwareDecoder) {
        this.context = context;
        this.userAgent = userAgent;
        this.uri = uri;
        this.preferSoftwareDecoder = preferSoftwareDecoder;
    }

    private MediaCodecSelector preferSoftwareMediaCodecSelector = new MediaCodecSelector() {
        @Override
        public DecoderInfo getDecoderInfo(String mimeType, boolean requiresSecureDecoder) throws MediaCodecUtil.DecoderQueryException {
            Log.d("Kaltura", "DeviceInfo: " + Util.getDeviceInfo() + ", mimeType:" + mimeType + ", requiresSecureDecoder" + requiresSecureDecoder);