/*
 * Copyright 2014 Mario Guggenberger <mg@protyposis.net>
 *
 * 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 net.protyposis.android.mediaplayer.dash;

import android.content.Context;
import android.net.Uri;

import java.io.IOException;
import java.util.Map;

import net.protyposis.android.mediaplayer.MediaExtractor;
import net.protyposis.android.mediaplayer.UriSource;

public class DashSource extends UriSource {

    private AdaptationLogic mAdaptationLogic;
    private MPD mMPD;

    public DashSource(Context context, Uri uri, Map<String, String> headers, AdaptationLogic adaptationLogic) {
        super(context, uri, headers);
        mAdaptationLogic = adaptationLogic;
        init();
    }

    public DashSource(Context context, Uri uri, AdaptationLogic adaptationLogic) {
        super(context, uri);
        mAdaptationLogic = adaptationLogic;
        init();
    }

    public DashSource(Context context, MPD mpd, AdaptationLogic adaptationLogic) {
        super(context, null);
        mMPD = mpd;
        mAdaptationLogic = adaptationLogic;
    }

    private void init() {
        if(mAdaptationLogic == null) {
            throw new RuntimeException("AdaptationLogic missing!");
        }
        if(getUri() != null) {
            try {
                mMPD = new DashParser().parse(this);
            } catch (DashParserException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public MediaExtractor getVideoExtractor() throws IOException {
        DashMediaExtractor mediaExtractor = new DashMediaExtractor();
        mediaExtractor.setDataSource(getContext(), mMPD, getHeaders(), mMPD.getFirstPeriod().getFirstVideoSet(), mAdaptationLogic);
        return mediaExtractor;
    }

    @Override
    public MediaExtractor getAudioExtractor() throws IOException {
        AdaptationSet audioSet = mMPD.getFirstPeriod().getFirstAudioSet();
        if(audioSet != null){
            DashMediaExtractor mediaExtractor = new DashMediaExtractor();
            mediaExtractor.setDataSource(getContext(), mMPD, getHeaders(), audioSet, mAdaptationLogic);
            return mediaExtractor;
        } else {
            return null;
        }
    }
}
@@ -723,16 +723,58 @@
 t.Uri;%0A%0A
+import com.squareup.okhttp.OkHttpClient;%0A%0A
 import j
@@ -973,16 +973,54 @@
 urce %7B%0A%0A
+    private OkHttpClient mHttpClient;%0A
     priv
@@ -1127,16 +1127,41 @@
 Uri uri,
+ OkHttpClient httpClient,
  Map%3CStr
@@ -1251,24 +1251,58 @@
 , headers);%0A
+        mHttpClient = httpClient;%0A
         mAda
@@ -1403,16 +1403,220 @@
 Uri uri,
+ Map%3CString, String%3E headers, AdaptationLogic adaptationLogic) %7B%0A        this(context, uri, null, headers, adaptationLogic);%0A    %7D%0A%0A    public DashSource(Context context, Uri uri, OkHttpClient httpClient,
  Adaptat
@@ -1672,16 +1672,50 @@
 , uri);%0A
+        mHttpClient = httpClient;%0A
         
@@ -1816,15 +1816,195 @@
 xt, 
-MPD mpd
+Uri uri, AdaptationLogic adaptationLogic) %7B%0A        this(context, uri, (OkHttpClient)null, adaptationLogic);%0A    %7D%0A%0A    public DashSource(Context context, MPD mpd, OkHttpClient httpClient
 , Ad
@@ -2086,16 +2086,50 @@
  = mpd;%0A
+        mHttpClient = httpClient;%0A
         
@@ -2180,28 +2180,390 @@
    p
-rivate void init() %7B
+ublic DashSource(Context context, MPD mpd, AdaptationLogic adaptationLogic) %7B%0A        this(context, mpd, null, adaptationLogic);%0A    %7D%0A%0A    private void initHttpClient() %7B%0A        // Create a http client instance if there is none yet%0A        if(mHttpClient == null) %7B%0A            mHttpClient = new OkHttpClient();%0A        %7D%0A    %7D%0A%0A    private void init() %7B%0A        initHttpClient();
 %0A   
@@ -2775,16 +2775,29 @@
 rse(this
+, mHttpClient
 );%0A     
@@ -2988,32 +2988,96 @@
 s IOException %7B%0A
+        initHttpClient(); // in case init() has not been called%0A
         DashMedi
@@ -3119,32 +3119,43 @@
 hMediaExtractor(
+mHttpClient
 );%0A        media
@@ -3384,32 +3384,96 @@
 s IOException %7B%0A
+        initHttpClient(); // in case init() has not been called%0A
         Adaptati
@@ -3632,16 +3632,27 @@
 tractor(
+mHttpClient
 );%0A     

Allow external OkHttpClient and use same client for MPD, audio and video segment download