// Player // // Douglas Thrift // // $Id$ #include #include #include #include "Player.hpp" inline void Sleep() { ::timespec sleep = { 1, 0 }; ::nanosleep(&sleep, NULL); } Player::Player(const _R& connection, int player) : remote(player), player_(remote.IsRunning() ? NULL : new _H(_B("/usr/X11R6/bin/beep-media-player"))), adder(hop::BindAll(&Player::Adder, this)), copier(hop::BindAll(&Player::Copier, this, shares)) { if (!player_.IsEmpty()) { player_->ClearReader(); player_->ClearWriter(); } _forall (int, position, 0, remote.GetPlaylistLength()) playlist.InsertLast(MediaFile(connection, remote.GetPlaylistFile(position))); if (remote.IsPlaying()) state = PLAYING; else if (remote.IsPaused()) state = PAUSED; if (!Share::shares.Exists()) Share::shares.CreateDirectory(); Share::shares = Share::shares.GetRealPath(); running = true; } Player::~Player() { running = false; adder.Join(); copier.Join(); if (!player_.IsEmpty()) { remote.Quit(); player_->Join(); } } void Player::Add(const _R& share) { ext::ScopeWriteLock writeLock(sharesLock); shares[share->host][share->share] = share; } int Player::Adder() { while (!running) Sleep(); while (running) { playlistLock.ReadLock(); /*if (remote.GetPlaylistLength() != playlist.GetSize()) { // playlistLock.ReadUnlock(); } else*/ { playlistLock.ReadUnlock(); Sleep(); } } return 0; } int Player::Copier(const ext::RedBlackMap > >& shares) { while (!running) Sleep(); api::Pcre::RegEx share_("^(.+)/([0-9a-f]+)/(.+)$"); while (running) _synchronized (queueLock) if (!queue.empty()) { api::Path path(queue.front()); queue.pop(); _desynchronized (queueLock) { _assert(path.GetPath().StartsWithAll(Share::shares.GetPath())); api::Pcre::RegEx::Match match(share_(path.GetPath().Substring(Share::shares.GetSize()))); _assert(match); ext::ScopeReadLock readLock(sharesLock); const _R& share(shares[api::InternetAddress(match[1], 6996)][match[2]]); } } else _desynchronized (queueLock) Sleep(); return 0; }