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