Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / stream_vstream.c
blob8dd0d2cd5a90f39ad5e581b86f6db6b921fbcda6
1 /*
2 * stream_vstream.c
4 * Copyright (C) Joey Parrish
6 * This file is part of MPlayer, a free movie player.
7 *
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with MPlayer; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * If you have a tivo with the vstream server installed, (and most tivo
25 * hackers do,) then you can connect to it and stream ty files using
26 * this module. The url syntax is tivo://host/fsid or tivo://host/list
27 * to list the available recordings and their fsid's.
28 * This module depends on libvstream-client, which is available from
29 * http://armory.nicewarrior.org/projects/vstream-client .
34 #include "config.h"
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <inttypes.h>
45 #include <errno.h>
47 #include "mp_msg.h"
48 #include "stream.h"
49 #include "help_mp.h"
50 #include "m_option.h"
51 #include "m_struct.h"
52 #include "tcp.h"
54 #include <vstream-client.h>
56 void vstream_error(const char *format, ...) {
57 char buf[1024];
58 va_list va;
59 va_start(va, format);
60 vsnprintf(buf, 1024, format, va);
61 va_end(va);
62 mp_msg(MSGT_STREAM, MSGL_ERR, buf);
65 static struct stream_priv_s {
66 char* host;
67 char* fsid;
68 } stream_priv_dflts = {
69 NULL,
70 NULL
73 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
74 /// URL definition
75 static const m_option_t stream_opts_fields[] = {
76 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
77 {"filename", ST_OFF(fsid), CONF_TYPE_STRING, 0, 0 ,0, NULL},
78 { NULL, NULL, 0, 0, 0, 0, NULL }
81 static struct m_struct_st stream_opts = {
82 "vstream",
83 sizeof(struct stream_priv_s),
84 &stream_priv_dflts,
85 stream_opts_fields
88 static int fill_buffer(stream_t *s, char* buffer, int max_len){
89 struct stream_priv_s* p = (struct stream_priv_s*)s->priv;
90 int len = vstream_load_chunk(p->fsid, buffer, max_len, s->pos);
91 if (len <= 0) return 0;
92 return len;
95 static int seek(stream_t *s,off_t newpos) {
96 s->pos = newpos;
97 return 1;
100 static int control(struct stream_st *s,int cmd,void* arg) {
101 return STREAM_UNSUPPORTED;
104 static void close_s(struct stream_st *s) {
107 static int open_s(stream_t *stream, int mode, void* opts, int* file_format) {
108 int f;
109 struct stream_priv_s* p = (struct stream_priv_s*)opts;
111 if(mode != STREAM_READ)
112 return STREAM_UNSUPPORTED;
114 if(!p->host) {
115 mp_msg(MSGT_OPEN, MSGL_ERR, "We need a host name (ex: tivo://hostname/fsid)\n");
116 m_struct_free(&stream_opts, opts);
117 return STREAM_ERROR;
120 if(!p->fsid || strlen(p->fsid) == 0) {
121 mp_msg(MSGT_OPEN, MSGL_ERR, "We need an fsid (ex: tivo://hostname/fsid)\n");
122 m_struct_free(&stream_opts, opts);
123 return STREAM_ERROR;
126 f = connect2Server(p->host, VSERVER_PORT, 1);
128 if(f < 0) {
129 mp_msg(MSGT_OPEN, MSGL_ERR, "Connection to %s failed\n", p->host);
130 m_struct_free(&stream_opts, opts);
131 return STREAM_ERROR;
133 stream->fd = f;
135 vstream_set_socket_fd(f);
137 if (!strcmp(p->fsid, "list")) {
138 vstream_list_streams(0);
139 return STREAM_ERROR;
140 } else if (!strcmp(p->fsid, "llist")) {
141 vstream_list_streams(1);
142 return STREAM_ERROR;
145 if (vstream_start()) {
146 mp_msg(MSGT_OPEN, MSGL_ERR, "Cryptic internal error #1\n");
147 m_struct_free(&stream_opts, opts);
148 return STREAM_ERROR;
150 if (vstream_startstream(p->fsid)) {
151 mp_msg(MSGT_OPEN, MSGL_ERR, "Cryptic internal error #2\n");
152 m_struct_free(&stream_opts, opts);
153 return STREAM_ERROR;
156 stream->start_pos = 0;
157 stream->end_pos = vstream_streamsize();
158 mp_msg(MSGT_OPEN, MSGL_DBG2, "Tivo stream size is %d\n", stream->end_pos);
160 stream->priv = p;
161 stream->fill_buffer = fill_buffer;
162 stream->control = control;
163 stream->seek = seek;
164 stream->close = close_s;
165 stream->type = STREAMTYPE_VSTREAM;
167 return STREAM_OK;
170 const stream_info_t stream_info_vstream = {
171 "vstream client",
172 "vstream",
173 "Joey",
175 open_s,
176 { "tivo", NULL },
177 &stream_opts,
178 1 // Url is an option string