| 1 | package Net::DAAP::Server::MythTV; | ||
| 2 | |||
| 3 | # vi: ts=4 sw=4 | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | use POE; | ||
| 10 | |||
| 11 | use base 'Net::DMAP::Server'; | ||
| 12 | |||
| 13 | our $VERSION = 0.01; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my $self = {@_}; | ||
| 18 | bless $self, $class; | ||
| 19 | |||
| 20 | # Start DAAP server | ||
| 21 | # TODO: Find available port | ||
| 22 | $self->{port} = $self->{port} || 3689; | ||
| 23 | $self->httpd(POE::Component::Server::HTTP->new( | ||
| 24 | Port => $self->{port}, | ||
| 25 | ContentHandler => {'/' => sub { | ||
| 26 | $self->_handler(@_); | ||
| 27 | }}, | ||
| 28 | StreamHandler => sub { | ||
| 29 | $self->stream_handler(@_); | ||
| 30 | })); | ||
| 31 | |||
| 32 | POE::Session->create( | ||
| 33 | inline_states => { | ||
| 34 | _start => sub { | ||
| 35 | # TODO: Make poll_interval configurable? | ||
| 36 | $_[KERNEL]->alarm(poll_changed => time + 20); | ||
| 37 | }, | ||
| 38 | poll_changed => sub { | ||
| 39 | $self->poll_changed; | ||
| 40 | $_[KERNEL]->yield('_start'); | ||
| 41 | }}); | ||
| 42 | |||
| 43 | # Publish DAAP server | ||
| 44 | $self->name || $self->name($class); | ||
| 45 | $self->{protocol} = $self->{protocol} || 'daap'; | ||
| 46 | |||
| 47 | # TODO: Calculate UUIDs? | ||
| 48 | $self->db_uuid || $self->db_uuid('13950142391337751523'); | ||
| 49 | $self->publisher(Net::Rendezvous::Publish->new); | ||
| 50 | $self->service($self->publisher->publish( | ||
| 51 | name => $self->name, | ||
| 52 | type => '_' . $self->{protocol} . '._tcp', | ||
| 53 | port => $self->{port}, | ||
| 54 | txt => 'Database ID=' . $self->db_uuid | ||
| 55 | . "\x01Machine name=" . $self->name)); | ||
| 56 | |||
| 57 | # Extra Net::DMAP::Server stuff | ||
| 58 | $self->revision(42); | ||
| 59 | $self->waiting_clients([]); | ||
| 60 | |||
| 61 | # TODO: Make this a command line option | ||
| 62 | $self->debug(1); | ||
| 63 | |||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub connect { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | # Connect to MythTV | ||
| 71 | $self->{db_name} = $self->{db_name} || 'mythconverg'; | ||
| 72 | $self->{db_host} = $self->{db_host} || 'localhost'; | ||
| 73 | $self->{db_username} = $self->{db_username} || 'mythtv'; | ||
| 74 | $self->{db_password} = $self->{db_password} || 'mythtv'; | ||
| 75 | |||
| 76 | return DBI->connect_cached( | ||
| 77 | 'dbi:mysql:database=' . $self->{db_name} . ':host=' . $self->{db_host}, | ||
| 78 | $self->{db_username}, | ||
| 79 | $self->{db_password}); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub stream_handler { | ||
| 83 | my $self = shift; | ||
| 84 | my ($request, $response) = @_; | ||
| 85 | |||
| 86 | read $response->{handle}, my $buffer, 4096; | ||
| 87 | $response->send($buffer); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub server_info { | ||
| 91 | my $self = shift; | ||
| 92 | my ($request, $response) = @_; | ||
| 93 | |||
| 94 | $response->content($self->_dmap_pack([ | ||
| 95 | ['dmap.serverinforesponse' => [ | ||
| 96 | ['dmap.status' => 200], | ||
| 97 | ['dmap.protocolversion' => 2], | ||
| 98 | ['daap.protocolversion' => $request->header('Client-DAAP-Version')], | ||
| 99 | ['dmap.itemname' => $self->name], | ||
| 100 | ['dmap.loginrequired' => 0], | ||
| 101 | ['dmap.timeoutinterval' => 1800], | ||
| 102 | ['dmap.supportsautologout' => 0], | ||
| 103 | ['dmap.supportsupdate' => 0], | ||
| 104 | ['dmap.supportspersistentids' => 0], | ||
| 105 | ['dmap.supportsextensions' => 0], | ||
| 106 | ['dmap.supportsbrowse' => 0], | ||
| 107 | ['dmap.supportsquery' => 0], | ||
| 108 | ['dmap.supportsindex' => 0], | ||
| 109 | ['dmap.supportsresolve' => 0], | ||
| 110 | ['dmap.databasescount' => 1]]]])); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub databases { | ||
| 114 | my $self = shift; | ||
| 115 | my ($request, $response) = @_; | ||
| 116 | |||
| 117 | my $dbh = $self->connect; | ||
| 118 | my $sth = $dbh->prepare( | ||
| 119 | 'SELECT COUNT(*) FROM videometadata'); | ||
| 120 | $sth->execute; | ||
| 121 | |||
| 122 | my ($itemcount) = $sth->fetchrow_array; | ||
| 123 | |||
| 124 | $response->content($self->_dmap_pack([ | ||
| 125 | ['daap.serverdatabases' => [ | ||
| 126 | ['dmap.status' => 200], | ||
| 127 | ['dmap.updatetype' => 0], | ||
| 128 | ['dmap.specifiedtotalcount' => 1], | ||
| 129 | ['dmap.returnedcount' => 1], | ||
| 130 | ['dmap.listing' => [ | ||
| 131 | ['dmap.listingitem' => [ | ||
| 132 | ['dmap.itemid' => 35], | ||
| 133 | ['dmap.persistentid' => $self->db_uuid], | ||
| 134 | ['dmap.itemname' => $self->name], | ||
| 135 | ['dmap.itemcount' => $itemcount], | ||
| 136 | ['dmap.containercount' => 1]]]]]]]])); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub database_items { | ||
| 140 | my $self = shift; | ||
| 141 | my ($request, $response) = @_; | ||
| 142 | |||
| 143 | my $dbh = $self->connect; | ||
| 144 | my $sth = $dbh->prepare( | ||
| 145 | 'SELECT filename, plot, intid, title, year FROM videometadata'); | ||
| 146 | $sth->execute; | ||
| 147 | |||
| 148 | my $listing = []; | ||
| 149 | while (my ($filename, $plot, $intid, $title, $year) = | ||
| 150 | $sth->fetchrow_array) { | ||
| 151 | # TODO: It would be faster to cache this data in the database | ||
| 152 | my @stat = stat $filename; | ||
| 153 | push @$listing, ['dmap.listingitem' => [ | ||
| 154 | ['dmap.itemkind' => 2], | ||
| 155 | ['daap.songcontentdescription' => $plot], | ||
| 156 | ['dmap.itemid' => $intid], | ||
| 157 | ['daap.songdescription' => $plot], | ||
| 158 | ['dmap.itemname' => $title], | ||
| 159 | ['daap.songsize' => $stat[7]], | ||
| 160 | ['daap.songyear' => $year], | ||
| 161 | ['com.apple.itunes.has-video' => 1]]]; | ||
| 162 | } | ||
| 163 | |||
| 164 | $response->content($self->_dmap_pack([ | ||
| 165 | ['daap.databasesongs' => [ | ||
| 166 | ['dmap.status' => 200], | ||
| 167 | ['dmap.updatetype' => 0], | ||
| 168 | ['dmap.specifiedtotalcount' => scalar @$listing], | ||
| 169 | ['dmap.returnedcount' => scalar @$listing], | ||
| 170 | ['dmap.listing' => $listing]]]])); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub database_playlists { | ||
| 174 | my $self = shift; | ||
| 175 | my ($request, $response) = @_; | ||
| 176 | |||
| 177 | my $dbh = $self->connect; | ||
| 178 | my $sth = $dbh->prepare( | ||
| 179 | 'SELECT COUNT(*) FROM videometadata'); | ||
| 180 | $sth->execute; | ||
| 181 | |||
| 182 | my ($itemcount) = $sth->fetchrow_array; | ||
| 183 | |||
| 184 | $response->content($self->_dmap_pack([ | ||
| 185 | ['daap.databaseplaylists' => [ | ||
| 186 | ['dmap.status' => 200], | ||
| 187 | ['dmap.updatetype' => 0], | ||
| 188 | ['dmap.specifiedtotalcount' => 1], | ||
| 189 | ['dmap.returnedcount' => 1], | ||
| 190 | ['dmap.listing' => [ | ||
| 191 | ['dmap.listingitem' => [ | ||
| 192 | ['dmap.itemid' => 39], | ||
| 193 | ['dmap.persistentid' => 13950142391337751524], | ||
| 194 | ['dmap.itemname' => $self->name], | ||
| 195 | ['com.apple.itunes.smart-playlist' => 0], | ||
| 196 | ['dmap.itemcount' => $itemcount]]]]]]]])); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub playlist_items { | ||
| 200 | my $self = shift; | ||
| 201 | my ($request, $response) = @_; | ||
| 202 | |||
| 203 | my $dbh = $self->connect; | ||
| 204 | my $sth = $dbh->prepare( | ||
| 205 | 'SELECT filename, plot, intid, title, year FROM videometadata'); | ||
| 206 | $sth->execute; | ||
| 207 | |||
| 208 | my $listing = []; | ||
| 209 | while (my ($filename, $plot, $intid, $title, $year) = | ||
| 210 | $sth->fetchrow_array) { | ||
| 211 | # TODO: It would be faster to cache this data in the database | ||
| 212 | my @stat = stat $filename; | ||
| 213 | push @$listing, ['dmap.listingitem' => [ | ||
| 214 | ['dmap.itemkind' => 2], | ||
| 215 | ['daap.songcontentdescription' => $plot], | ||
| 216 | ['dmap.itemid' => $intid], | ||
| 217 | ['daap.songdescription' => $plot], | ||
| 218 | ['dmap.itemname' => $title], | ||
| 219 | ['daap.songsize' => $stat[7]], | ||
| 220 | ['daap.songyear' => $year], | ||
| 221 | ['com.apple.itunes.has-video' => 1]]]; | ||
| 222 | } | ||
| 223 | |||
| 224 | $response->content($self->_dmap_pack([ | ||
| 225 | ['daap.playlistsongs' => [ | ||
| 226 | ['dmap.status' => 200], | ||
| 227 | ['dmap.updatetype' => 0], | ||
| 228 | ['dmap.specifiedtotalcount' => scalar @$listing], | ||
| 229 | ['dmap.returnedcount' => scalar @$listing], | ||
| 230 | ['dmap.listing' => $listing]]]])); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub database_item { | ||
| 234 | my $self = shift; | ||
| 235 | my ($request, $response) = @_; | ||
| 236 | |||
| 237 | my ($intid) = ($request->uri->path =~ /^\/databases\/\d+\/items\/(\d+)/); | ||
| 238 | my $dbh = $self->connect; | ||
| 239 | my $sth = $dbh->prepare( | ||
| 240 | 'SELECT filename FROM videometadata WHERE intid = ?'); | ||
| 241 | $sth->execute($intid); | ||
| 242 | |||
| 243 | my ($filename) = $sth->fetchrow_array; | ||
| 244 | open $response->{handle}, '<', $filename; | ||
| 245 | |||
| 246 | $response->streaming(1); | ||
| 247 | } | ||
| 248 | |||
| 249 | __END__ |