1 |
// Player |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <menes/standard.hh> |
8 |
|
9 |
#include <menes-api/pcre/regex.hpp> |
10 |
#include <menes-hop/bind.hpp> |
11 |
|
12 |
#include "Player.hpp" |
13 |
|
14 |
inline void Sleep() |
15 |
{ |
16 |
::timespec sleep = { 1, 0 }; |
17 |
|
18 |
::nanosleep(&sleep, NULL); |
19 |
} |
20 |
|
21 |
Player::Player(const _R<dbi::Connection>& connection, int player) : remote(player), player_(remote.IsRunning() ? NULL : new _H<api::Process>(_B("/usr/X11R6/bin/beep-media-player"))), position(remote.GetPlaylistPosition()), adder(hop::BindAll(&Player::Adder, this)), copier(hop::BindAll(&Player::Copier, this)) |
22 |
{ |
23 |
if (!player_.IsEmpty()) |
24 |
{ |
25 |
player_->ClearReader(); |
26 |
player_->ClearWriter(); |
27 |
} |
28 |
|
29 |
_forall (int, position, 0, remote.GetPlaylistLength()) |
30 |
playlist.InsertLast(MediaFile(connection, remote.GetPlaylistFile(position))); |
31 |
|
32 |
if (remote.IsPlaying()) |
33 |
state = PLAYING; |
34 |
else if (remote.IsPaused()) |
35 |
state = PAUSED; |
36 |
|
37 |
if (!Share::shares.Exists()) |
38 |
Share::shares.CreateDirectory(); |
39 |
|
40 |
Share::shares = Share::shares.GetRealPath(); |
41 |
running = true; |
42 |
} |
43 |
|
44 |
Player::~Player() |
45 |
{ |
46 |
running = false; |
47 |
|
48 |
adder.Join(); |
49 |
copier.Join(); |
50 |
|
51 |
if (!player_.IsEmpty()) |
52 |
{ |
53 |
remote.Quit(); |
54 |
|
55 |
player_->Join(); |
56 |
} |
57 |
} |
58 |
|
59 |
void Player::Add(const _R<Share>& share) |
60 |
{ |
61 |
ext::ScopeWriteLock<mta::ReaderWriterLock> writeLock(sharesLock); |
62 |
|
63 |
shares[share->host][share->share] = share; |
64 |
} |
65 |
|
66 |
int Player::Adder() |
67 |
{ |
68 |
while (!running) |
69 |
Sleep(); |
70 |
|
71 |
while (running) |
72 |
{ |
73 |
playlistLock.ReadLock(); |
74 |
|
75 |
/*if (remote.GetPlaylistLength() != playlist.GetSize()) |
76 |
{ |
77 |
// |
78 |
|
79 |
playlistLock.ReadUnlock(); |
80 |
} |
81 |
else*/ |
82 |
{ |
83 |
playlistLock.ReadUnlock(); |
84 |
|
85 |
Sleep(); |
86 |
} |
87 |
} |
88 |
|
89 |
return 0; |
90 |
} |
91 |
|
92 |
int Player::Copier() const |
93 |
{ |
94 |
while (!running) |
95 |
Sleep(); |
96 |
|
97 |
api::Pcre::RegEx share_("^(.+)/([0-9a-f]+)/(.+)$"); |
98 |
|
99 |
while (running) |
100 |
_synchronized (queueLock) if (!queue.empty()) |
101 |
{ |
102 |
api::Path path(queue.front()); |
103 |
|
104 |
queue.pop(); |
105 |
|
106 |
_desynchronized (queueLock) |
107 |
{ |
108 |
_assert(path.GetPath().StartsWithAll(Share::shares.GetPath())); |
109 |
|
110 |
api::Pcre::RegEx::Match match(share_(path.GetPath().Substring(Share::shares.GetSize()))); |
111 |
|
112 |
_assert(match); |
113 |
|
114 |
ext::ScopeReadLock<mta::ReaderWriterLock> readLock(sharesLock); |
115 |
|
116 |
const _R<Share>& share(shares[api::InternetAddress(match[1], 6996)][match[2]]); |
117 |
} |
118 |
} |
119 |
else _desynchronized (queueLock) |
120 |
Sleep(); |
121 |
|
122 |
return 0; |
123 |
} |