CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
mythfileplayback.cpp
1/*
2 * Copyright (C) 2014 Jean-Luc Barriere
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#include "mythfileplayback.h"
23#include "mythlivetvplayback.h"
24#include "private/debug.h"
25#include "private/os/threads/latch.h"
26#include "private/builtin.h"
27
28#include <limits>
29#include <cstdio>
30
31using namespace Myth;
32
37
38FilePlayback::FilePlayback(const std::string& server, unsigned port)
39: ProtoPlayback(server, port)
40, m_transfer(nullptr)
41{
42 ProtoPlayback::Open();
43}
44
45FilePlayback::~FilePlayback()
46{
47 Close();
48}
49
50bool FilePlayback::Open()
51{
52 // Begin critical section
53 OS::WriteLock lock(*m_latch);
54 if (ProtoPlayback::IsOpen())
55 return true;
56 return ProtoPlayback::Open();
57}
58
59void FilePlayback::Close()
60{
61 // Begin critical section
62 OS::WriteLock lock(*m_latch);
63 CloseTransfer();
64 ProtoPlayback::Close();
65}
66
67bool FilePlayback::OpenTransfer(const std::string& pathname, const std::string& sgname)
68{
69 // Begin critical section
70 OS::WriteLock lock(*m_latch);
71 if (!ProtoPlayback::IsOpen())
72 return false;
73 CloseTransfer();
74 m_transfer.reset(new ProtoTransfer(m_server, m_port, pathname, sgname));
75 if (m_transfer->Open())
76 return true;
77 return false;
78}
79
80void FilePlayback::CloseTransfer()
81{
82 // Begin critical section
83 OS::WriteLock lock(*m_latch);
84 if (m_transfer)
85 {
86 TransferDone(*m_transfer);
87 m_transfer->Close();
88 m_transfer.reset();
89 }
90}
91
92bool FilePlayback::TransferIsOpen()
93{
94 m_latch->lock_shared();
95 ProtoTransferPtr transfer(m_transfer);
96 m_latch->unlock_shared();
97 if (transfer)
98 return ProtoPlayback::TransferIsOpen(*transfer);
99 return false;
100}
101
102int64_t FilePlayback::GetSize() const
103{
104 m_latch->lock_shared();
105 ProtoTransferPtr transfer(m_transfer);
106 m_latch->unlock_shared();
107 if (transfer)
108 return transfer->GetSize();
109 return 0;
110}
111
112int FilePlayback::Read(void *buffer, unsigned n)
113{
114 m_latch->lock_shared();
115 ProtoTransferPtr transfer(m_transfer);
116 m_latch->unlock_shared();
117 if (transfer)
118 {
119 int r = 0;
120 int64_t s = transfer->GetRemaining(); // Acceptable block size
121 if (s > 0)
122 {
123 if (s < (int64_t)n)
124 n = (unsigned)s ;
125 // Request block data from transfer socket
126 r = TransferRequestBlock(*transfer, buffer, n);
127 }
128 return r;
129 }
130 return -1;
131}
132
133int64_t FilePlayback::Seek(int64_t offset, WHENCE_t whence)
134{
135 m_latch->lock_shared();
136 ProtoTransferPtr transfer(m_transfer);
137 m_latch->unlock_shared();
138 if (transfer)
139 return TransferSeek(*transfer, offset, whence);
140 return -1;
141}
142
143int64_t FilePlayback::GetPosition() const
144{
145 m_latch->lock_shared();
146 ProtoTransferPtr transfer(m_transfer);
147 m_latch->unlock_shared();
148 if (transfer)
149 return transfer->GetPosition();
150 return 0;
151}
This is the main namespace that encloses all public classes.
Definition mythcontrol.h:30