ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/Player.cpp
Revision: 546
Committed: 2005-07-03T22:13:07-07:00 (19 years, 11 months ago) by douglas
File size: 2751 byte(s)
Log Message:
Stuff!

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id