Changeset 1155

Show
Ignore:
Timestamp:
12/04/07 12:07:26 (1 year ago)
Author:
alban
Message:

Try to find the next Ogg page in OggStreamSource?. Fixes #162

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/source/org/kolaka/freecast/ogg/OggStreamSource.java

    r725 r1155  
    22 * FreeCast - streaming over Internet 
    33 * 
    4  * This code was developped by Alban Peignier (http://people.tryphon.org/~alban/)  
     4 * This code was developped by Alban Peignier (http://people.tryphon.org/~alban/) 
    55 * and contributors (their names can be found in the CONTRIBUTORS file). 
    66 * 
     
    3939import org.apache.commons.lang.builder.ToStringBuilder; 
    4040import org.apache.commons.logging.LogFactory; 
     41import org.kolaka.freecast.lang.ArrayUtils; 
    4142import org.kolaka.freecast.io.ReminderInputStream; 
    4243 
    4344/** 
    44  *  
    45  *  
     45 * 
     46 * 
    4647 * @author <a href="mailto:alban.peignier@free.fr">Alban Peignier </a> 
    4748 */ 
     
    6566                                reminderInput)); 
    6667        } 
    67          
     68 
    6869        public String getDescription() { 
    6970                return description; 
     
    7778 
    7879        private final byte[] capturePatternBuffer = new byte[4]; 
    79          
     80 
    8081        private boolean beginOfStream = true; 
    81          
     82 
    8283        public OggPage next() throws IOException { 
    8384                dataInput.readFully(capturePatternBuffer, 0, 4); 
    8485 
    8586                if (!Arrays.equals(OggPage.CAPTURE_PATTERN, capturePatternBuffer)) { 
    86                         LogFactory.getLog(getClass()).trace(createHexDump(capturePatternBuffer)); 
    87                         throw createIOException("Missing capture pattern"); 
     87                        LogFactory.getLog(getClass()).warn("invalid ogg stream"); 
     88                        LogFactory.getLog(getClass()).trace(OggPages.createHexDump(capturePatternBuffer)); 
     89 
     90                        findNextCapturePattern(); 
    8891                } 
    8992 
     
    99102                try { 
    100103                        int headerTypeFlag = dataInput.readUnsignedByte(); 
    101                          
     104 
    102105                        page.setFirstPage((headerTypeFlag & 0x02) != 0); 
    103106                        page.setLastPage((headerTypeFlag & 0x04) != 0); 
     
    114117                                pageSize += dataInput.readUnsignedByte(); 
    115118                        } 
    116                          
     119 
    117120      LogFactory.getLog(getClass()).trace("payload size: " + pageSize); 
    118        
     121 
    119122      byte payload[] = new byte[pageSize]; 
    120123                        dataInput.readFully(payload); 
     
    126129                        throw e; 
    127130                } catch (IOException e) { 
    128                  
     131 
    129132                        LogFactory.getLog(getClass()).error("io error while reading " + page); 
    130133                        lastPages.log(); 
     
    132135                        if (readData.length > 0) { 
    133136                                LogFactory.getLog(getClass()).trace("last bytes read"); 
    134                                 LogFactory.getLog(getClass()).trace(createHexDump(readData)); 
     137                                LogFactory.getLog(getClass()).trace(OggPages.createHexDump(readData)); 
    135138                        } 
    136139                        throw e; 
    137140                } 
    138                  
     141 
    139142                if (beginOfStream) { 
    140143                        if (!page.isFirstPage()) { 
     
    143146                        beginOfStream = false; 
    144147                } 
    145                  
     148 
    146149                if (page.isLastPage()) { 
    147150                        beginOfStream = true; 
    148151                } 
    149152 
    150                 LogFactory.getLog(getClass()).trace("returns " + page); 
    151                 // LogFactory.getLog(getClass()).trace(createHexDump(page.getRawBytes())); 
    152                  
     153    if (LogFactory.getLog(getClass()).isTraceEnabled()) { 
     154                LogFactory.getLog(getClass()).trace("returns " + page); 
     155 
     156      int capturePatternInPayload = 
     157        ArrayUtils.indexOf(page.getPayload(), OggPage.CAPTURE_PATTERN); 
     158      if (capturePatternInPayload > -1) { 
     159                LogFactory.getLog(getClass()).warn("capture pattern found into payload : 0x" + Integer.toHexString( capturePatternInPayload)); 
     160                    LogFactory.getLog(getClass()).trace(OggPages.createHexDump(page.getPayload())); 
     161      } 
     162          } 
     163 
    153164                lastPages.add(page); 
    154                  
     165 
    155166                return page; 
    156167        } 
     168 
     169  private void findNextCapturePattern() throws IOException { 
     170                LogFactory.getLog(getClass()).info("try to find next valid Ogg page"); 
     171 
     172                final int searchLimit = 10 * 1024; 
     173                int searchLength = 0; 
     174 
     175    int patternIndex = 0; 
     176    byte[] pattern = OggPage.CAPTURE_PATTERN; 
     177 
     178    while (searchLength < searchLimit && patternIndex < pattern.length) { 
     179                int nextByte = dataInput.readUnsignedByte(); 
     180 
     181                if (nextByte == pattern[patternIndex]) { 
     182                patternIndex++; 
     183                } else { 
     184                patternIndex = 0; 
     185                } 
     186                searchLength++; 
     187        } 
     188 
     189    if (patternIndex < pattern.length) { 
     190      throw createIOException("Invalid Ogg stream"); 
     191    } 
     192 
     193                LogFactory.getLog(getClass()).info("next valid Ogg page found after " + searchLength + " bytes"); 
     194  } 
    157195 
    158196        /** 
     
    165203        } 
    166204 
    167         /** 
    168          * @param buffer TODO 
    169          * @return 
    170          * @throws IOException 
    171          */ 
    172         private String createHexDump(byte[] buffer) throws IOException { 
    173                 ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    174                 HexDump.dump(buffer, 0, output, 0); 
    175                 String hexdump = output.toString(); 
    176                 output.close(); 
    177                 return hexdump; 
    178         } 
    179  
    180205        public void close() throws IOException { 
    181206                dataInput.close(); 
    182207        } 
    183          
     208 
    184209        private final LastPages lastPages = new LastPages(); 
    185          
     210 
    186211        class LastPages { 
    187                  
     212 
    188213                private final int remaindedPages = 5; 
    189214                private final List pages = new LinkedList(); 
     
    202227                                LogFactory.getLog(getClass()).trace(page); 
    203228                                try { 
    204                                         LogFactory.getLog(getClass()).trace(createHexDump(page.getRawBytes())); 
     229                                        LogFactory.getLog(getClass()).trace(OggPages.createHexDump(page)); 
    205230                                } catch (IOException e) { 
    206231                                        LogFactory.getLog(getClass()).error("can't dump " + page); 
     
    208233                        } 
    209234                } 
    210                  
    211                  
    212                  
     235 
     236 
     237 
    213238        } 
    214239 
  • trunk/source/org/kolaka/freecast/ogg/test/Dump.java

    r405 r1155  
    22 * FreeCast - streaming over Internet 
    33 * 
    4  * This code was developped by Alban Peignier (http://people.tryphon.org/~alban/)  
     4 * This code was developped by Alban Peignier (http://people.tryphon.org/~alban/) 
    55 * and contributors (their names can be found in the CONTRIBUTORS file). 
    66 * 
     
    3131 
    3232import org.kolaka.freecast.ogg.OggPage; 
     33import org.kolaka.freecast.ogg.OggPages; 
    3334import org.kolaka.freecast.ogg.OggSource; 
    3435import org.kolaka.freecast.ogg.OggStreamSource; 
    3536 
    3637/** 
    37  *  
    38  *  
     38 * 
     39 * 
    3940 * @author <a href="mailto:alban.peignier@free.fr">Alban Peignier</a> 
    4041 */ 
     
    4748                Map absolutePositions = new TreeMap(); 
    4849 
    49                 for (int i = 0; i < 20; i++) { 
     50    int pageCount = 0; 
     51 
     52                while (true) { 
    5053                        OggPage page = source.next(); 
    5154 
    52                         writer.println("page " + i); 
     55                        writer.println("page " + pageCount); 
    5356                        writer.println("\tfirstpage: " + page.isFirstPage() 
    5457                                        + "\tlastpage: " + page.isLastPage()); 
     
    6871                                                                                .longValue())); 
    6972                        } 
     73 
     74      writer.println(); 
     75                        writer.println(page); 
     76                        writer.println(OggPages.createHexDump(page)); 
     77 
    7078                        absolutePositions.put(streamSerialNumberString, new Long( 
    7179                                        absolutePosition)); 
     80                        pageCount++; 
     81                        writer.flush(); 
    7282                } 
    73                 writer.close(); 
     83                // writer.close(); 
    7484        } 
    7585 
  • trunk/source/org/kolaka/freecast/ogg/test/OggStreamSourceTest.java

    r884 r1155  
    22 * FreeCast - streaming over Internet 
    33 * 
    4  * This code was developped by Alban Peignier (http://people.tryphon.org/~alban/)  
     4 * This code was developped by Alban Peignier (http://people.tryphon.org/~alban/) 
    55 * and contributors (their names can be found in the CONTRIBUTORS file). 
    66 * 
     
    2626import java.io.IOException; 
    2727import java.io.InputStream; 
     28import java.io.EOFException; 
    2829 
    2930import junit.framework.TestCase; 
     
    3334 
    3435/** 
    35  *  
    36  *  
     36 * 
     37 * 
    3738 * @author <a href="mailto:alban.peignier@free.fr">Alban Peignier </a> 
    3839 */ 
    3940public class OggStreamSourceTest extends TestCase { 
    4041 
    41         public void testReadFile() throws IOException { 
    42     InputStream resource = OggTestResources.getResourceAsStream("sample.ogg"); 
    43      
    44                 OggStreamSource oggSource = new OggStreamSource(resource); 
     42        public void testValidFile() throws IOException { 
     43    testStream("sample.ogg", 14); 
     44        } 
     45 
     46        public void testInvalidStream() throws IOException { 
     47    testStream("invalid-stream.ogg", 60); 
     48        } 
     49 
     50 
     51        private void testStream(String resourceName, int expectedPageCount) 
     52          throws IOException 
     53        { 
     54                OggStreamSource oggSource = new OggStreamSource(OggTestResources.getResourceAsStream(resourceName)); 
    4555 
    4656                OggPage firstPage = oggSource.next(); 
     
    5060                OggPage page = firstPage; 
    5161                while (!page.isLastPage()) { 
    52                         page = oggSource.next(); 
     62                  try { 
     63                        page = oggSource.next(); 
     64                } catch (EOFException e) { 
     65        if (pageCount < expectedPageCount) { 
     66          throw e; 
     67        } 
     68 
     69        break; 
     70                } 
     71 
    5372                        assertFalse("page can't be first", page.isFirstPage()); 
    5473      pageCount++; 
     
    5675 
    5776                oggSource.close(); 
    58     assertEquals(14, pageCount); 
     77    assertEquals(expectedPageCount, pageCount); 
    5978        } 
    6079 
     80 
    6181}