1 |
douglas |
542 |
// Player |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
560 |
#include <cxx/standard.hh> |
8 |
douglas |
542 |
|
9 |
douglas |
560 |
#include <api/pcre/regex.hpp> |
10 |
|
|
#include <api/posix/signals.hpp> |
11 |
|
|
#include <ext/stack.hpp> |
12 |
|
|
#include <hop/bind.hpp> |
13 |
douglas |
542 |
|
14 |
douglas |
547 |
#include <signal.h> |
15 |
|
|
|
16 |
douglas |
542 |
#include "Player.hpp" |
17 |
|
|
|
18 |
|
|
inline void Sleep() |
19 |
|
|
{ |
20 |
|
|
::timespec sleep = { 1, 0 }; |
21 |
|
|
|
22 |
|
|
::nanosleep(&sleep, NULL); |
23 |
|
|
} |
24 |
|
|
|
25 |
douglas |
555 |
Player::Player(const _R<Connector>& connector, int player) : state(STOPPED), 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)) |
26 |
douglas |
542 |
{ |
27 |
douglas |
546 |
api::Posix::SetSignalHandler(SIGUSR2, hop::BindAll(&Player::Change, this)); |
28 |
|
|
|
29 |
douglas |
542 |
if (!player_.IsEmpty()) |
30 |
|
|
{ |
31 |
|
|
player_->ClearReader(); |
32 |
|
|
player_->ClearWriter(); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
_forall (int, position, 0, remote.GetPlaylistLength()) |
36 |
douglas |
555 |
playlist.InsertLast(MediaFile(connector, remote.GetPlaylistFile(position))); |
37 |
douglas |
542 |
|
38 |
|
|
if (remote.IsPlaying()) |
39 |
|
|
state = PLAYING; |
40 |
|
|
else if (remote.IsPaused()) |
41 |
|
|
state = PAUSED; |
42 |
|
|
|
43 |
|
|
if (!Share::shares.Exists()) |
44 |
|
|
Share::shares.CreateDirectory(); |
45 |
|
|
|
46 |
|
|
Share::shares = Share::shares.GetRealPath(); |
47 |
|
|
running = true; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
Player::~Player() |
51 |
|
|
{ |
52 |
|
|
running = false; |
53 |
|
|
|
54 |
|
|
adder.Join(); |
55 |
|
|
copier.Join(); |
56 |
|
|
|
57 |
|
|
if (!player_.IsEmpty()) |
58 |
|
|
{ |
59 |
|
|
remote.Quit(); |
60 |
|
|
|
61 |
|
|
player_->Join(); |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
void Player::Add(const _R<Share>& share) |
66 |
|
|
{ |
67 |
|
|
ext::ScopeWriteLock<mta::ReaderWriterLock> writeLock(sharesLock); |
68 |
|
|
|
69 |
|
|
shares[share->host][share->share] = share; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
int Player::Adder() |
73 |
|
|
{ |
74 |
|
|
while (!running) |
75 |
|
|
Sleep(); |
76 |
|
|
|
77 |
|
|
while (running) |
78 |
|
|
{ |
79 |
|
|
playlistLock.ReadLock(); |
80 |
|
|
|
81 |
|
|
/*if (remote.GetPlaylistLength() != playlist.GetSize()) |
82 |
|
|
{ |
83 |
|
|
// |
84 |
|
|
|
85 |
|
|
playlistLock.ReadUnlock(); |
86 |
|
|
} |
87 |
|
|
else*/ |
88 |
|
|
{ |
89 |
|
|
playlistLock.ReadUnlock(); |
90 |
|
|
|
91 |
|
|
Sleep(); |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
return 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
douglas |
544 |
int Player::Copier() const |
99 |
douglas |
542 |
{ |
100 |
|
|
while (!running) |
101 |
|
|
Sleep(); |
102 |
|
|
|
103 |
|
|
api::Pcre::RegEx share_("^(.+)/([0-9a-f]+)/(.+)$"); |
104 |
|
|
|
105 |
|
|
while (running) |
106 |
|
|
_synchronized (queueLock) if (!queue.empty()) |
107 |
|
|
{ |
108 |
|
|
api::Path path(queue.front()); |
109 |
|
|
|
110 |
|
|
queue.pop(); |
111 |
|
|
|
112 |
|
|
_desynchronized (queueLock) |
113 |
|
|
{ |
114 |
|
|
_assert(path.GetPath().StartsWithAll(Share::shares.GetPath())); |
115 |
|
|
|
116 |
|
|
api::Pcre::RegEx::Match match(share_(path.GetPath().Substring(Share::shares.GetSize()))); |
117 |
|
|
|
118 |
|
|
_assert(match); |
119 |
|
|
|
120 |
douglas |
546 |
{ |
121 |
|
|
api::Path parent(path); |
122 |
|
|
ext::Stack<api::Path> stack; |
123 |
|
|
|
124 |
|
|
while ((parent = parent.GetParent()) != Share::shares) |
125 |
|
|
stack.Push(parent); |
126 |
|
|
|
127 |
|
|
while (stack.Pop(parent)) |
128 |
|
|
if (!parent.Exists()) |
129 |
|
|
parent.CreateDirectory(); |
130 |
|
|
} |
131 |
|
|
|
132 |
douglas |
542 |
ext::ScopeReadLock<mta::ReaderWriterLock> readLock(sharesLock); |
133 |
|
|
|
134 |
douglas |
546 |
const _R<Share>& share(shares[api::Address::Resolve(match[1], _B("6996")).First()][match[2]]); |
135 |
douglas |
542 |
} |
136 |
|
|
} |
137 |
|
|
else _desynchronized (queueLock) |
138 |
|
|
Sleep(); |
139 |
|
|
|
140 |
|
|
return 0; |
141 |
|
|
} |
142 |
douglas |
546 |
|
143 |
|
|
void Player::Change() |
144 |
|
|
{ |
145 |
|
|
position = remote.GetPlaylistPosition(); |
146 |
|
|
} |