/*
 * Copyright (c) 2017 Kiall Mac Innes <kiall@macinnes.ie>
 *
 * 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 ie.macinnes.tvheadend.player.reader;

import android.content.Context;
import android.support.annotation.NonNull;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.MimeTypes;

import ie.macinnes.htsp.HtspMessage;
import ie.macinnes.tvheadend.TvhMappings;

public class Mpeg2AudioStreamReader extends PlainStreamReader {

    public Mpeg2AudioStreamReader(Context context) {
        super(context);
    }

    @NonNull
    @Override
    protected Format buildFormat(int streamIndex, @NonNull HtspMessage stream) {
        int rate = Format.NO_VALUE;
        if (stream.containsKey("rate")) {
            rate = TvhMappings.sriToRate(stream.getInteger("rate"));
        }

        return Format.createAudioSampleFormat(
                Integer.toString(streamIndex),
                MimeTypes.AUDIO_MPEG_L2,
                null,
                Format.NO_VALUE,
                Format.NO_VALUE,
                stream.getInteger("channels", Format.NO_VALUE),
                rate,
                C.ENCODING_PCM_16BIT,
                null,
                null,
                C.SELECTION_FLAG_AUTOSELECT,
                stream.getString("language", "und")
        );
    }

    @Override
    protected int getTrackType() {
        return C.TRACK_TYPE_AUDIO;
    }
}

@@ -1369,24 +1369,995 @@
 %0A        %7D%0A%0A
+        // TVHeadend calls all MPEG Audio MPEG2AUDIO - e.g. it could be either mp2 or mp3 audio. We%0A        // need to use the new audio_version field (4.1.2498+ only). Default to mp2 as that's most%0A        // common for DVB.%0A        int audioVersion = 2;%0A%0A        if (stream.containsKey(%22audio_version%22)) %7B%0A            audioVersion = stream.getInteger(%22audio_version%22);%0A        %7D%0A%0A        String mimeType;%0A%0A        switch (audioVersion) %7B%0A            case 1: // MP1 Audio - V.Unlikely these days%0A                mimeType = MimeTypes.AUDIO_MPEG_L1;%0A                break;%0A            case 2: // MP2 Audio - Pretty common in DVB streams%0A                mimeType = MimeTypes.AUDIO_MPEG_L2;%0A                break;%0A            case 3: // MP3 Audio - Pretty common in IPTV streams%0A                mimeType = MimeTypes.AUDIO_MPEG;%0A                break;%0A            default:%0A                throw new RuntimeException(%22Unknown MPEG Audio Version: %22 + audioVersion);%0A        %7D%0A%0A
         retu
@@ -2391,16 +2391,16 @@
 Format(%0A
-
         
@@ -2454,39 +2454,24 @@
         
-M
+m
 imeType
-s.AUDIO_MPEG_L2
 ,%0A      

Handle new audio_version field