/*
 * vdr-podcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2006 Tobias Grimm <vdr@e-tobi.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: pesaudiopacket_test.cc 3211 2006-04-14 21:01:37Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "pesaudiopacket.h"

#define PES_HEADER_LENGTH 9
#define LPCM_HEADER_LENGTH 7

#define FIXED_PES_PREFIX             "\x00\x00\x01"
#define FIXED_PES_STREAM_ID          "\xBD"
#define FIXED_PES_EXTENSION          "\x87\x00"
#define FIXED_PES_HEADER_DATA_LENGTH "\x00"

#define FRAME_SIZE_FIELD_NO_SAMPLES "\x00\x0A"
#define FRAME_SIZE_FIELD_ONE_SAMPLE "\x00\x0E"

#define LPCM_ID "\xA0"
#define LPCM_UNKNOWN_1 "\xFF\x00\x00\x00"
#define LPCM_STEREO_48KHZ "\x01"
#define LPCM_UNKNOWN_2 "\x80"

#define OFFSET_FIRST_SAMPLE PES_HEADER_LENGTH + LPCM_HEADER_LENGTH

class PesAudioPacketTest: public CxxTest::TestSuite
{
    public:
        void TestEmptyPesAudioPacket()
        {
            PesAudioPacket pesPacket(2048);

            TS_ASSERT_EQUALS(16, pesPacket.GetBufferSize());

            TS_ASSERT_SAME_DATA(FIXED_PES_PREFIX,
              pesPacket.GetBufferPtr(), 3);

            TS_ASSERT_SAME_DATA(FIXED_PES_STREAM_ID,
              pesPacket.GetBufferPtr() + 3, 1);

            TS_ASSERT_SAME_DATA(FRAME_SIZE_FIELD_NO_SAMPLES,
              pesPacket.GetBufferPtr() + 4, 2);

            TS_ASSERT_SAME_DATA(FIXED_PES_EXTENSION,
              pesPacket.GetBufferPtr() + 6, 2);

            TS_ASSERT_SAME_DATA(FIXED_PES_HEADER_DATA_LENGTH,
              pesPacket.GetBufferPtr() + 8, 1);

            TS_ASSERT_SAME_DATA(LPCM_ID,
              pesPacket.GetBufferPtr() + 9, 1);

            TS_ASSERT_SAME_DATA(LPCM_UNKNOWN_1,
              pesPacket.GetBufferPtr() + 10, 4);

            TS_ASSERT_SAME_DATA(LPCM_STEREO_48KHZ,
              pesPacket.GetBufferPtr() + 14, 1);

            TS_ASSERT_SAME_DATA(LPCM_UNKNOWN_2,
              pesPacket.GetBufferPtr() + 15, 1);
        }

        void TestAddSamplesButNoSpaceLeftInPacket()
        {
            PesAudioPacket pesPacket(PES_HEADER_LENGTH + LPCM_HEADER_LENGTH);
            TS_ASSERT(!pesPacket.AddSample(0, 0));
        }

        void TestAddSamples()
        {
            PesAudioPacket pesPacket(24);

            TS_ASSERT(pesPacket.AddSample(0, 1));
            TS_ASSERT_EQUALS(20, pesPacket.GetBufferSize());
            TS_ASSERT_SAME_DATA(FRAME_SIZE_FIELD_ONE_SAMPLE,
              pesPacket.GetBufferPtr() + 4, 2);
            TS_ASSERT_SAME_DATA("\x00\x00\x00\x01",
              pesPacket.GetBufferPtr() + OFFSET_FIRST_SAMPLE, 4);
        }

        void TestTimeIndex()
        {
            PesAudioPacket pesPacket(24);
            TS_ASSERT_EQUALS(0, pesPacket.GetTimeIndex());
            pesPacket.SetTimeIndex(10);
            TS_ASSERT_EQUALS(10, pesPacket.GetTimeIndex());
        }

        void TestTotalTime()
        {
            PesAudioPacket pesPacket(24);
            TS_ASSERT_EQUALS(-1, pesPacket.GetTotalTime());
            pesPacket.SetTotalTime(60);
            TS_ASSERT_EQUALS(60, pesPacket.GetTotalTime());
        }
};
