Merge branch 'refs/heads/t/mixer-fix'
[paraslash.git] / afh.h
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file afh.h Structures for audio format handling (para_server). */
4
5 /**
6 * The tags used by all audio format handlers.
7 *
8 * Paraslash only uses the more common tags. These are recognized
9 * for all supported audio formats.
10 */
11 struct taginfo {
12 /** TPE1 (id3v2) / ARTIST (vorbis) / ART (aac)/ author(spx) */
13 char *artist;
14 /** TIT2/TITLE/nam */
15 char *title;
16 /** TDRC/YEAR/day */
17 char *year;
18 /** TALB/ALBUM/alb */
19 char *album;
20 /** COMM/COMMENT/cmt */
21 char *comment;
22 };
23
24 /** Audio format dependent information. */
25 struct afh_info {
26 /** The number of chunks this audio file contains. */
27 uint32_t chunks_total;
28 /** The length of the audio file in seconds. */
29 uint32_t seconds_total;
30 /** Audio handler specific info about the file. */
31 char *techinfo;
32 /** Id3 tags, vorbis comments, aac tags. */
33 struct taginfo tags;
34 /**
35 * The table that specifies the offset of the individual pieces in
36 * the current audio file.
37 */
38 uint32_t *chunk_table;
39 /** Size of the largest chunk, introduced in v0.6.0. */
40 uint32_t max_chunk_size;
41 /** Period of time between sending data chunks. */
42 struct timeval chunk_tv;
43 /**
44 * The header is needed by senders in case a new client connects in the
45 * middle of the stream. The length of the header defaults to zero
46 * which means that this audio format does not need any special header
47 * treatment. The audio format handler does not need to set this to
48 * zero in this case.
49 */
50 uint32_t header_len;
51 /** The number of channels. */
52 uint8_t channels;
53 /** Frequency in Hz. */
54 uint16_t frequency;
55 /** Exact meaning depends on audio format. */
56 uint16_t bitrate;
57 };
58
59 /** Data about the current audio file, passed from afs to server. */
60 struct audio_file_data {
61 /** The open file descriptor to the current audio file. */
62 int fd;
63 /** Vss needs this for streaming. */
64 struct afh_info afhi;
65 /**
66 * Size of the largest chunk. Superseded by afhi->max_chunk_size. May
67 * be removed after v0.6.1.
68 */
69 uint32_t max_chunk_size;
70 /** Needed to get the audio file header. */
71 uint8_t audio_format_id;
72 };
73
74 /**
75 * Structure for audio format handling.
76 *
77 * There's one such struct for each supported audio format. Initially, only \a
78 * name and \a init are defined. During the startup process, para_server calls
79 * the \a init function of each audio format handler which is expected to fill
80 * in the other part of this struct.
81 */
82 struct audio_format_handler {
83 /** Name of the audio format. */
84 const char *name;
85 /**
86 * Pointer to the audio format handler's init function.
87 *
88 * Must initialize all function pointers and is assumed to succeed.
89 */
90 void (*init)(struct audio_format_handler*);
91 /** Typical file endings for files that can be handled by this afh. */
92 const char * const *suffixes;
93 /**
94 * Check if this audio format handler can handle the file.
95 *
96 * This is a pointer to a function returning whether a given file is
97 * valid for this audio format. A negative return value indicates that
98 * this audio format handler is unable to decode the given file. On
99 * success, the function must return a positive value and fill in the
100 * given struct afh_info.
101 *
102 * \sa struct \ref afh_info.
103 */
104 int (*get_file_info)(char *map, size_t numbytes, int fd,
105 struct afh_info *afhi);
106 /** Optional, used for header-rewriting. See \ref afh_get_header(). */
107 void (*get_header)(void *map, size_t mapsize, char **buf, size_t *len);
108 /**
109 * An audio format handler may signify support for dynamic chunks by
110 * defining ->get_chunk below. In this case the vss calls ->open() at
111 * BOS, ->get_chunk() for each chunk while streaming, and ->close() at
112 * EOS. The chunk table is not accessed at all.
113 *
114 * The function may return its (opaque) context through the last
115 * argument. The returned pointer is passed to subsequent calls to
116 * ->get_chunk() and ->close().
117 */
118 int (*open)(const void *map, size_t mapsize, void **afh_context);
119 /**
120 * Return a reference to one chunk. The returned pointer points to a
121 * portion of the memory mapped audio file. The caller must not call
122 * free() on it.
123 */
124 int (*get_chunk)(long unsigned chunk_num, void *afh_context,
125 const char **buf, size_t *len);
126 /** Deallocate the resources occupied by ->open(). */
127 void (*close)(void *afh_context);
128 /**
129 * Write audio file with altered tags, optional.
130 *
131 * The output file descriptor has been opened by the caller and must not
132 * be closed in this function.
133 */
134 int (*rewrite_tags)(const char *map, size_t mapsize, struct taginfo *tags,
135 int output_fd, const char *filename);
136 };
137
138 void afh_init(void);
139 int guess_audio_format(const char *name);
140 int compute_afhi(const char *path, char *data, size_t size,
141 int fd, struct afh_info *afhi);
142 const char *audio_format_name(int);
143 __must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
144 uint8_t audio_format_id, const void *map, size_t mapsize,
145 const char **buf, size_t *len, void **afh_context);
146 void afh_close(void *afh_context, uint8_t audio_format_id);
147 int32_t afh_get_start_chunk(int32_t approx_chunk_num,
148 const struct afh_info *afhi, uint8_t audio_format_id);
149 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
150 void *map, size_t mapsize, char **buf, size_t *len);
151 void afh_free_header(char *header_buf, uint8_t audio_format_id);
152 void clear_afhi(struct afh_info *afhi);
153 unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **result);
154 int afh_rewrite_tags(int audio_format_id, void *map, size_t mapsize,
155 struct taginfo *tags, int output_fd, const char *filename);
156 void set_max_chunk_size(struct afh_info *afhi);
157 bool afh_supports_dynamic_chunks(int audio_format_id);