~ubuntu-branches/ubuntu/hoary/liboggz/hoary

« back to all changes in this revision

Viewing changes to src/examples/read-io.c

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Wilkinson
  • Date: 2004-04-13 21:48:13 UTC
  • Revision ID: james.westby@ubuntu.com-20040413214813-oq1cjx1n49vvqrcf
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2003 Commonwealth Scientific and Industrial Research
 
3
   Organisation (CSIRO) Australia
 
4
 
 
5
   Redistribution and use in source and binary forms, with or without
 
6
   modification, are permitted provided that the following conditions
 
7
   are met:
 
8
 
 
9
   - Redistributions of source code must retain the above copyright
 
10
   notice, this list of conditions and the following disclaimer.
 
11
 
 
12
   - Redistributions in binary form must reproduce the above copyright
 
13
   notice, this list of conditions and the following disclaimer in the
 
14
   documentation and/or other materials provided with the distribution.
 
15
 
 
16
   - Neither the name of CSIRO Australia nor the names of its
 
17
   contributors may be used to endorse or promote products derived from
 
18
   this software without specific prior written permission.
 
19
 
 
20
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
23
   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
 
24
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
25
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
26
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
27
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
28
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
29
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
30
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
31
*/
 
32
 
 
33
#include <stdio.h>
 
34
#include <stdlib.h>
 
35
#include <oggz/oggz.h>
 
36
 
 
37
static int got_an_eos = 0;
 
38
 
 
39
static int
 
40
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
 
41
{
 
42
#if 0
 
43
  if (got_an_eos) {
 
44
    printf ("[%010ld]\t%ld bytes\tgranulepos %ld\n", serialno, op->bytes,
 
45
            (long)op->granulepos);
 
46
  }
 
47
#endif
 
48
 
 
49
  if (op->b_o_s) {
 
50
    printf ("%010ld: [%lld] BOS %8s\n", serialno, op->granulepos, op->packet);
 
51
  }
 
52
 
 
53
  if (op->e_o_s) {
 
54
    got_an_eos = 1;
 
55
    printf ("%010ld: [%lld] EOS\n", serialno, op->granulepos);
 
56
  }
 
57
 
 
58
  return 0;
 
59
}
 
60
 
 
61
static size_t
 
62
my_io_read (void * user_handle, void * buf, size_t n)
 
63
{
 
64
  FILE * f = (FILE *)user_handle;
 
65
 
 
66
  return fread (buf, 1, n, f);
 
67
}
 
68
 
 
69
static int
 
70
my_io_seek (void * user_handle, long offset, int whence)
 
71
{
 
72
  FILE * f = (FILE *)user_handle;
 
73
 
 
74
  return (fseek (f, offset, whence));
 
75
}
 
76
 
 
77
static long
 
78
my_io_tell (void * user_handle)
 
79
{
 
80
  FILE * f = (FILE *)user_handle;
 
81
 
 
82
  return ftell (f);
 
83
}
 
84
 
 
85
int
 
86
main (int argc, char ** argv)
 
87
{
 
88
  FILE * f;
 
89
  OGGZ * oggz;
 
90
  long n;
 
91
  ogg_int64_t units;
 
92
 
 
93
  if (argc < 2) {
 
94
    printf ("usage: %s filename\n", argv[0]);
 
95
  }
 
96
 
 
97
  if ((f = fopen ((char *)argv[1], "rb")) == NULL) {
 
98
    printf ("unable to open file %s\n", argv[1]);
 
99
    exit (1);
 
100
  }
 
101
 
 
102
  if ((oggz = oggz_new (OGGZ_READ | OGGZ_AUTO)) == NULL) {
 
103
    printf ("unable to create oggz\n");
 
104
    exit (1);
 
105
  }
 
106
 
 
107
  oggz_io_set_read (oggz, my_io_read, f);
 
108
  oggz_io_set_seek (oggz, my_io_seek, f);
 
109
  oggz_io_set_tell (oggz, my_io_tell, f);
 
110
 
 
111
  oggz_set_read_callback (oggz, -1, read_packet, NULL);
 
112
 
 
113
  while ((n = oggz_read (oggz, 1024)) > 0);
 
114
 
 
115
  units = oggz_tell_units (oggz);
 
116
  printf ("Total length: %lld ms\n", units);
 
117
 
 
118
  oggz_seek_units (oggz, units/2, SEEK_SET);
 
119
 
 
120
  printf ("seeked to byte offset %lld\n", oggz_tell (oggz));
 
121
 
 
122
  while ((n = oggz_read (oggz, 1024)) > 0);
 
123
 
 
124
  oggz_close (oggz);
 
125
 
 
126
  exit (0);
 
127
}