new plugin: FS#10559 - lrcplayer: a plugin to view .lrc file.
[kugel-rb.git] / apps / plugins / mpegplayer / stream_thread.h
blob1d3a445735c27c641acc4d26da0b4de8b841a2b3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Declarations for stream-specific threading
12 * Copyright (c) 2007 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #ifndef STREAM_THREAD_H
24 #define STREAM_THREAD_H
26 #define PKT_HAS_TS 0x1
28 /* Stream header which is the minimum to receive asynchronous buffering
29 * notifications.
30 * Layed-out to allow streaming access after random-access parsing */
31 struct stream_hdr
33 struct event_queue *q; /* Communication queue - separate to allow it
34 to be placed in another section */
35 off_t win_left; /* Left position within data stream */
36 union
38 off_t win_right; /* Right position within data stream */
39 off_t pos; /* Start/current position for random-access read */
41 off_t limit; /* Limit for random-access read */
44 struct stream
46 struct stream_hdr hdr; /* Base stream data */
47 unsigned int thread; /* Stream's thread */
48 uint8_t* curr_packet; /* Current stream packet beginning */
49 uint8_t* curr_packet_end; /* Current stream packet end */
50 int state; /* State machine parsing mode */
51 uint32_t start_pts; /* First timestamp for stream */
52 uint32_t end_pts; /* Last timestamp for stream */
53 uint32_t pts; /* Last presentation timestamp */
54 uint32_t pkt_flags; /* PKT_* flags */
55 unsigned id; /* Stream identifier */
58 #define STR_FROM_HDR(sh) ((struct stream *)(sh))
60 /* Make sure there there is always enough data buffered ahead for
61 * the worst possible case - regardless of whether a valid stream
62 * would actually produce that */
63 #define MIN_BUFAHEAD (21+65535+6+65535+6) /* 131103 */
65 /* States that a stream's thread assumes internally */
66 enum thread_states
68 /* Stream thread... */
69 TSTATE_INIT = 0, /* is initialized and primed */
70 TSTATE_DATA, /* is awaiting data to be available */
71 TSTATE_BUFFERING, /* is buffering data */
72 TSTATE_EOS, /* has hit the end of data */
73 TSTATE_DECODE, /* is in a decoding state */
74 TSTATE_RENDER, /* is in a rendering state */
75 TSTATE_RENDER_WAIT, /* is waiting to render */
76 TSTATE_RENDER_WAIT_END, /* is waiting on remaining data */
79 /* Commands that streams respond to */
80 enum stream_message
82 STREAM_NULL = 0, /* A NULL message for whatever reason -
83 usually ignored */
84 STREAM_PLAY, /* Start playback at current position */
85 STREAM_PAUSE, /* Stop playing and await further commands */
86 STREAM_RESET, /* Reset the stream for a discontinuity */
87 STREAM_STOP, /* Stop stream - requires a reset later */
88 STREAM_SEEK, /* Seek the current stream to a new location */
89 STREAM_OPEN, /* Open a new file */
90 STREAM_CLOSE, /* Close the current file */
91 STREAM_QUIT, /* Exit the stream and thread */
92 STREAM_NEEDS_SYNC, /* Need to sync before stream decoding? */
93 STREAM_SYNC, /* Sync to the specified time from some key point */
94 STREAM_FIND_END_TIME, /* Get the exact end time of an elementary
95 * stream - ie. time just after last frame is finished */
96 /* Disk buffer */
97 STREAM_DISK_BUF_FIRST,
98 DISK_BUF_DATA_NOTIFY = STREAM_DISK_BUF_FIRST,
99 DISK_BUF_CLEAR_DATA_NOTIFY, /* Cancel pending data notification */
100 DISK_BUF_CACHE_RANGE, /* Cache a range of the file in the buffer */
101 /* Audio stream */
102 STREAM_AUDIO_FIRST,
103 /* Video stream */
104 STREAM_VIDEO_FIRST,
105 VIDEO_DISPLAY_SHOW = STREAM_VIDEO_FIRST, /* Show/hide video output */
106 VIDEO_DISPLAY_IS_VISIBLE, /* Is the video output visible? */
107 VIDEO_GET_SIZE, /* Get the video dimensions */
108 VIDEO_PRINT_FRAME, /* Print the frame at the current position */
109 VIDEO_PRINT_THUMBNAIL, /* Print a thumbnail of the current position */
110 VIDEO_SET_CLIP_RECT, /* Set the visible video area */
111 STREAM_MESSAGE_LAST,
114 /* Data parameter for STREAM_SEEK */
115 struct stream_seek_data
117 uint32_t time; /* Time to seek to/by */
118 int whence; /* Specification of relationship to current position/file */
121 /* Data parameter for STREAM_SYNC */
122 struct str_sync_data
124 uint32_t time; /* Time to sync to */
125 struct stream_scan sk; /* Specification of start/limits/direction */
128 /* Stream status codes - not eqivalent to thread states */
129 enum stream_status
131 /* Stream status is... */
132 STREAM_DATA_END = -4, /* Stream has ended */
133 STREAM_DATA_NOT_READY = -3, /* Data was not available yet */
134 STREAM_UNSUPPORTED = -2, /* Format is unsupported */
135 STREAM_ERROR = -1, /* some kind of error - quit it or reset it */
136 STREAM_OK = 0, /* General inequality for success >= is OK, < error */
137 STREAM_STOPPED = 0, /* stopped and awaiting commands - send STREAM_INIT */
138 STREAM_PLAYING, /* playing and rendering its data */
139 STREAM_PAUSED, /* paused and awaiting commands */
140 /* Other status codes (> STREAM_OK) */
141 STREAM_MATCH, /* A good match was found */
142 STREAM_PERFECT_MATCH, /* Exactly what was wanted was found or
143 no better match is possible */
144 STREAM_NOT_FOUND, /* Match not found */
147 /* Clip time to range for a particular stream */
148 static inline uint32_t clip_time(struct stream *str, uint32_t time)
150 if (time < str->start_pts)
151 time = str->start_pts;
152 else if (time >= str->end_pts)
153 time = str->end_pts;
155 return time;
158 extern struct stream video_str IBSS_ATTR;
159 extern struct stream audio_str IBSS_ATTR;
161 bool video_thread_init(void);
162 void video_thread_exit(void);
163 bool audio_thread_init(void);
164 void audio_thread_exit(void);
166 /* Some queue function wrappers to keep things clean-ish */
168 /* For stream use only */
169 static inline bool str_have_msg(struct stream *str)
170 { return !rb->queue_empty(str->hdr.q); }
172 static inline void str_get_msg(struct stream *str, struct queue_event *ev)
173 { rb->queue_wait(str->hdr.q, ev); }
175 static inline void str_get_msg_w_tmo(struct stream *str, struct queue_event *ev,
176 int timeout)
177 { rb->queue_wait_w_tmo(str->hdr.q, ev, timeout); }
179 static inline void str_reply_msg(struct stream *str, intptr_t reply)
180 { rb->queue_reply(str->hdr.q, reply); }
182 /* Public use */
183 static inline intptr_t str_send_msg(struct stream *str, long id, intptr_t data)
184 { return rb->queue_send(str->hdr.q, id, data); }
186 static inline void str_post_msg(struct stream *str, long id, intptr_t data)
187 { rb->queue_post(str->hdr.q, id, data); }
189 #endif /* STREAM_THREAD_H */