summaryrefslogtreecommitdiffstats
path: root/stream/stream_vstream.c
blob: ff173855f5551a6f32da3b8b688afcec912615d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) Joey Parrish
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 *   If you have a tivo with the vstream server installed, (and most tivo
 *   hackers do,) then you can connect to it and stream ty files using
 *   this module.  The url syntax is tivo://host/fsid or tivo://host/list
 *   to list the available recordings and their fsid's.
 *   This module depends on libvstream-client, which is available from
 *   http://armory.nicewarrior.org/projects/vstream-client .
 *
 */


#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <errno.h>

#include "mp_msg.h"
#include "stream.h"
#include "help_mp.h"
#include "m_option.h"
#include "m_struct.h"
#include "tcp.h"

#include <vstream-client.h>

void vstream_error(const char *format, ...) {
    char buf[1024];
    va_list va;
    va_start(va, format);
    vsnprintf(buf, 1024, format, va);
    va_end(va);
    mp_msg(MSGT_STREAM, MSGL_ERR, buf);
}

static struct stream_priv_s {
  char* host;
  char* fsid;
} stream_priv_dflts = {
  NULL,
  NULL
};

#define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
/// URL definition
static const m_option_t stream_opts_fields[] = {
  {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
  {"filename", ST_OFF(fsid), CONF_TYPE_STRING, 0, 0 ,0, NULL},
  { NULL, NULL, 0, 0, 0, 0,  NULL }
};

static const struct m_struct_st stream_opts = {
  "vstream",
  sizeof(struct stream_priv_s),
  &stream_priv_dflts,
  stream_opts_fields
};

static int fill_buffer(stream_t *s, char* buffer, int max_len){
  struct stream_priv_s* p = (struct stream_priv_s*)s->priv;
  int len = vstream_load_chunk(p->fsid, buffer, max_len, s->pos);
  if (len <= 0) return 0;
  return len;
}

static int seek(stream_t *s,off_t newpos) {
  s->pos = newpos;
  return 1;
}

static int control(struct stream_st *s,int cmd,void* arg) {
  return STREAM_UNSUPPORTED;
}

static void close_s(struct stream_st *s) {
}

static int open_s(stream_t *stream, int mode, void* opts, int* file_format) {
  int f;
  struct stream_priv_s* p = (struct stream_priv_s*)opts;

  if(mode != STREAM_READ)
    return STREAM_UNSUPPORTED;

  if(!p->host) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "We need a host name (ex: tivo://hostname/fsid)\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  if(!p->fsid || strlen(p->fsid) == 0) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "We need an fsid (ex: tivo://hostname/fsid)\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  f = connect2Server(p->host, VSERVER_PORT, 1);

  if(f < 0) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "Connection to %s failed\n", p->host);
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }
  stream->fd = f;

  vstream_set_socket_fd(f);

  if (!strcmp(p->fsid, "list")) {
    vstream_list_streams(0);
    return STREAM_ERROR;
  } else if (!strcmp(p->fsid, "llist")) {
    vstream_list_streams(1);
    return STREAM_ERROR;
  }

  if (vstream_start()) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "Cryptic internal error #1\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }
  if (vstream_startstream(p->fsid)) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "Cryptic internal error #2\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  stream->start_pos = 0;
  stream->end_pos = vstream_streamsize();
  mp_msg(MSGT_OPEN, MSGL_DBG2, "Tivo stream size is %d\n", stream->end_pos);

  stream->priv = p;
  stream->fill_buffer = fill_buffer;
  stream->control = control;
  stream->seek = seek;
  stream->close = close_s;
  stream->type = STREAMTYPE_VSTREAM;

  return STREAM_OK;
}

const stream_info_t stream_info_vstream = {
  "vstream client",
  "vstream",
  "Joey",
  "",
  open_s,
  { "tivo", NULL },
  &stream_opts,
  1 // Url is an option string
};