CPPMyth
Library to interoperate with MythTV server
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/mutex.h"
26 #include "private/builtin.h"
27 
28 #include <limits>
29 #include <cstdio>
30 
31 using namespace Myth;
32 
37 
38 FilePlayback::FilePlayback(const std::string& server, unsigned port)
39 : ProtoPlayback(server, port)
40 , m_transfer(NULL)
41 {
42  ProtoPlayback::Open();
43 }
44 
45 FilePlayback::~FilePlayback()
46 {
47  Close();
48 }
49 
50 bool FilePlayback::Open()
51 {
52  // Begin critical section
53  OS::CLockGuard lock(*m_mutex);
54  if (ProtoPlayback::IsOpen())
55  return true;
56  return ProtoPlayback::Open();
57 }
58 
59 void FilePlayback::Close()
60 {
61  // Begin critical section
62  OS::CLockGuard lock(*m_mutex);
63  CloseTransfer();
64  ProtoPlayback::Close();
65 }
66 
67 bool FilePlayback::OpenTransfer(const std::string& pathname, const std::string& sgname)
68 {
69  // Begin critical section
70  OS::CLockGuard lock(*m_mutex);
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 
80 void FilePlayback::CloseTransfer()
81 {
82  // Begin critical section
83  OS::CLockGuard lock(*m_mutex);
84  if (m_transfer)
85  {
86  TransferDone(*m_transfer);
87  m_transfer->Close();
88  m_transfer.reset();
89  }
90 }
91 
92 bool FilePlayback::TransferIsOpen()
93 {
94  ProtoTransferPtr transfer(m_transfer);
95  if (transfer)
96  return ProtoPlayback::TransferIsOpen(*transfer);
97  return false;
98 }
99 
100 int64_t FilePlayback::GetSize() const
101 {
102  ProtoTransferPtr transfer(m_transfer);
103  if (transfer)
104  return transfer->GetSize();
105  return 0;
106 }
107 
108 int FilePlayback::Read(void *buffer, unsigned n)
109 {
110  ProtoTransferPtr transfer(m_transfer);
111  if (transfer)
112  {
113  int r = 0;
114  int64_t s = transfer->GetRemaining(); // Acceptable block size
115  if (s > 0)
116  {
117  if (s < (int64_t)n)
118  n = (unsigned)s ;
119  // Request block data from transfer socket
120  r = TransferRequestBlock(*transfer, buffer, n);
121  }
122  return r;
123  }
124  return -1;
125 }
126 
127 int64_t FilePlayback::Seek(int64_t offset, WHENCE_t whence)
128 {
129  ProtoTransferPtr transfer(m_transfer);
130  if (transfer)
131  return TransferSeek(*transfer, offset, whence);
132  return -1;
133 }
134 
135 int64_t FilePlayback::GetPosition() const
136 {
137  ProtoTransferPtr transfer(m_transfer);
138  if (transfer)
139  return transfer->GetPosition();
140  return 0;
141 }
This is the main namespace that encloses all public classes.
Definition: mythcontrol.h:29