MythTV  0.27pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
video.cpp
Go to the documentation of this file.
1 
2 // Program Name: video.cpp
3 // Created : Apr. 21, 2011
4 //
5 // Copyright (c) 2011 Robert McNamara <rmcnamara@mythtv.org>
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //
25 
26 #include <QList>
27 #include <QFile>
28 #include <QMutex>
29 
30 #include <math.h>
31 
32 #include "video.h"
33 
34 #include "videometadata.h"
35 #include "metadatafactory.h"
36 #include "bluraymetadata.h"
37 
38 #include "compat.h"
39 #include "mythversion.h"
40 #include "mythcorecontext.h"
41 #include "storagegroup.h"
42 #include "remotefile.h"
43 #include "globals.h"
44 #include "mythdate.h"
45 #include "serviceUtil.h"
46 #include "mythmiscutil.h"
47 
49 //
51 
53  int nStartIndex,
54  int nCount )
55 {
57  QString sql = "ORDER BY intid";
58  if (bDescending)
59  sql += " DESC";
61 
62  std::vector<VideoMetadataListManager::VideoMetadataPtr> videos(videolist.begin(), videolist.end());
63 
64  // ----------------------------------------------------------------------
65  // Build Response
66  // ----------------------------------------------------------------------
67 
68  DTC::VideoMetadataInfoList *pVideoMetadataInfos = new DTC::VideoMetadataInfoList();
69 
70  nStartIndex = min( nStartIndex, (int)videos.size() );
71  nCount = (nCount > 0) ? min( nCount, (int)videos.size() ) : videos.size();
72  int nEndIndex = min((nStartIndex + nCount), (int)videos.size() );
73 
74  for( int n = nStartIndex; n < nEndIndex; n++ )
75  {
76  DTC::VideoMetadataInfo *pVideoMetadataInfo = pVideoMetadataInfos->AddNewVideoMetadataInfo();
77 
79 
80  if (metadata)
81  FillVideoMetadataInfo ( pVideoMetadataInfo, metadata, true );
82  }
83 
84  int curPage = 0, totalPages = 0;
85  if (nCount == 0)
86  totalPages = 1;
87  else
88  totalPages = (int)ceil((float)videos.size() / nCount);
89 
90  if (totalPages == 1)
91  curPage = 1;
92  else
93  {
94  curPage = (int)ceil((float)nStartIndex / nCount) + 1;
95  }
96 
97  pVideoMetadataInfos->setStartIndex ( nStartIndex );
98  pVideoMetadataInfos->setCount ( nCount );
99  pVideoMetadataInfos->setCurrentPage ( curPage );
100  pVideoMetadataInfos->setTotalPages ( totalPages );
101  pVideoMetadataInfos->setTotalAvailable( videos.size() );
102  pVideoMetadataInfos->setAsOf ( MythDate::current() );
103  pVideoMetadataInfos->setVersion ( MYTH_BINARY_VERSION );
104  pVideoMetadataInfos->setProtoVer ( MYTH_PROTO_VERSION );
105 
106  return pVideoMetadataInfos;
107 }
108 
110 //
112 
114 {
117 
118  if ( !metadata )
119  throw( QString( "No metadata found for selected ID!." ));
120 
121  DTC::VideoMetadataInfo *pVideoMetadataInfo = new DTC::VideoMetadataInfo();
122 
123  FillVideoMetadataInfo ( pVideoMetadataInfo, metadata, true );
124 
125  return pVideoMetadataInfo;
126 }
127 
129 //
131 
133 {
137  mlm->setList(videolist);
138  VideoMetadataListManager::VideoMetadataPtr metadata = mlm->byFilename(FileName);
139 
140  if ( !metadata )
141  throw( QString( "No metadata found for selected filename!." ));
142 
143  DTC::VideoMetadataInfo *pVideoMetadataInfo = new DTC::VideoMetadataInfo();
144 
145  FillVideoMetadataInfo ( pVideoMetadataInfo, metadata, true );
146 
147  delete mlm;
148 
149  return pVideoMetadataInfo;
150 }
151 
153 //
155 
157  const QString &Subtitle,
158  const QString &Inetref,
159  int Season,
160  int Episode,
161  const QString &GrabberType,
162  bool AllowGeneric )
163 {
164  DTC::VideoLookupList *pVideoLookups = new DTC::VideoLookupList();
165 
166  MetadataLookupList list;
167 
168  MetadataFactory *factory = new MetadataFactory(NULL);
169 
170  if (factory)
171  list = factory->SynchronousLookup(Title, Subtitle,
172  Inetref, Season, Episode,
173  GrabberType, AllowGeneric);
174 
175  if ( !list.size() )
176  return pVideoLookups;
177 
178  for( int n = 0; n < list.size(); n++ )
179  {
180  DTC::VideoLookup *pVideoLookup = pVideoLookups->AddNewVideoLookup();
181 
182  MetadataLookup *lookup = list[n];
183 
184  if (lookup)
185  {
186  pVideoLookup->setTitle(lookup->GetTitle());
187  pVideoLookup->setSubTitle(lookup->GetSubtitle());
188  pVideoLookup->setSeason(lookup->GetSeason());
189  pVideoLookup->setEpisode(lookup->GetEpisode());
190  pVideoLookup->setYear(lookup->GetYear());
191  pVideoLookup->setTagline(lookup->GetTagline());
192  pVideoLookup->setDescription(lookup->GetDescription());
193  pVideoLookup->setCertification(lookup->GetCertification());
194  pVideoLookup->setInetref(lookup->GetInetref());
195  pVideoLookup->setCollectionref(lookup->GetCollectionref());
196  pVideoLookup->setHomePage(lookup->GetHomepage());
197  pVideoLookup->setReleaseDate(
198  QDateTime(lookup->GetReleaseDate(),
199  QTime(0,0),Qt::LocalTime).toUTC());
200  pVideoLookup->setUserRating(lookup->GetUserRating());
201  pVideoLookup->setLength(lookup->GetRuntime());
202  pVideoLookup->setLanguage(lookup->GetLanguage());
203  pVideoLookup->setCountries(lookup->GetCountries());
204  pVideoLookup->setPopularity(lookup->GetPopularity());
205  pVideoLookup->setBudget(lookup->GetBudget());
206  pVideoLookup->setRevenue(lookup->GetRevenue());
207  pVideoLookup->setIMDB(lookup->GetIMDB());
208  pVideoLookup->setTMSRef(lookup->GetTMSref());
209 
210  ArtworkList coverartlist = lookup->GetArtwork(kArtworkCoverart);
211  ArtworkList::iterator c;
212  for (c = coverartlist.begin(); c != coverartlist.end(); ++c)
213  {
214  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
215  art->setType("coverart");
216  art->setUrl((*c).url);
217  art->setThumbnail((*c).thumbnail);
218  art->setWidth((*c).width);
219  art->setHeight((*c).height);
220  }
221  ArtworkList fanartlist = lookup->GetArtwork(kArtworkFanart);
222  ArtworkList::iterator f;
223  for (f = fanartlist.begin(); f != fanartlist.end(); ++f)
224  {
225  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
226  art->setType("fanart");
227  art->setUrl((*f).url);
228  art->setThumbnail((*f).thumbnail);
229  art->setWidth((*f).width);
230  art->setHeight((*f).height);
231  }
232  ArtworkList bannerlist = lookup->GetArtwork(kArtworkBanner);
233  ArtworkList::iterator b;
234  for (b = bannerlist.begin(); b != bannerlist.end(); ++b)
235  {
236  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
237  art->setType("banner");
238  art->setUrl((*b).url);
239  art->setThumbnail((*b).thumbnail);
240  art->setWidth((*b).width);
241  art->setHeight((*b).height);
242  }
243  ArtworkList screenshotlist = lookup->GetArtwork(kArtworkScreenshot);
244  ArtworkList::iterator s;
245  for (s = screenshotlist.begin(); s != screenshotlist.end(); ++s)
246  {
247  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
248  art->setType("screenshot");
249  art->setUrl((*s).url);
250  art->setThumbnail((*s).thumbnail);
251  art->setWidth((*s).width);
252  art->setHeight((*s).height);
253  }
254 
255  delete lookup;
256  }
257  }
258 
259  pVideoLookups->setCount ( list.count() );
260  pVideoLookups->setAsOf ( MythDate::current() );
261  pVideoLookups->setVersion ( MYTH_BINARY_VERSION );
262  pVideoLookups->setProtoVer ( MYTH_PROTO_VERSION );
263 
264  delete factory;
265 
266  return pVideoLookups;
267 }
268 
270 //
272 
274 {
275  bool bResult = false;
276 
280  mlm->setList(videolist);
282 
283  if (metadata)
284  bResult = metadata->DeleteFromDatabase();
285 
286  delete mlm;
287 
288  return bResult;
289 }
290 
292 //
294 
295 bool Video::AddVideo( const QString &sFileName,
296  const QString &sHostName )
297 {
298  if ( sHostName.isEmpty() )
299  throw( QString( "Host not provided! Local storage is deprecated and "
300  "is not supported by the API." ));
301 
302  if ( sFileName.isEmpty() ||
303  (sFileName.contains("/../")) ||
304  (sFileName.startsWith("../")) )
305  {
306  throw( QString( "Filename not provided, or fails sanity checks!" ));
307  }
308 
309  StorageGroup sgroup("Videos", sHostName);
310 
311  QString fullname = sgroup.FindFile(sFileName);
312 
313  if ( !QFile::exists(fullname) )
314  throw( QString( "Provided filename does not exist!" ));
315 
316  QString hash = FileHash(fullname);
317 
318  if (hash == "NULL")
319  {
320  LOG(VB_GENERAL, LOG_ERR, "Video Hash Failed. Unless this is a DVD or "
321  "Blu-ray, something has probably gone wrong.");
322  hash = "";
323  }
324 
325  VideoMetadata newFile(sFileName, hash,
331  VideoMetadata::FilenameToMeta(sFileName, 1),
332  VideoMetadata::FilenameToMeta(sFileName, 4),
333  QString(), VIDEO_YEAR_DEFAULT,
334  QDate::fromString("0000-00-00","YYYY-MM-DD"),
335  VIDEO_INETREF_DEFAULT, 0, QString(),
337  0.0, VIDEO_RATING_DEFAULT, 0, 0,
338  VideoMetadata::FilenameToMeta(sFileName, 2).toInt(),
339  VideoMetadata::FilenameToMeta(sFileName, 3).toInt(),
340  MythDate::current().date(), 0,
342 
343  newFile.SetHost(sHostName);
344  newFile.SaveToDatabase();
345 
346  return true;
347 }
348 
350 //
352 
353 DTC::BlurayInfo* Video::GetBluray( const QString &sPath )
354 {
355  QString path = sPath;
356 
357  if (sPath.isEmpty())
358  path = gCoreContext->GetSetting( "BluRayMountpoint", "/media/cdrom");
359 
360  LOG(VB_GENERAL, LOG_NOTICE,
361  QString("Parsing Blu-ray at path: %1 ").arg(path));
362 
363  BlurayMetadata *bdmeta = new BlurayMetadata(path);
364 
365  if ( !bdmeta )
366  throw( QString( "Unable to open Blu-ray Metadata Parser!" ));
367 
368  if ( !bdmeta->OpenDisc() )
369  throw( QString( "Unable to open Blu-ray Disc/Path!" ));
370 
371  if ( !bdmeta->ParseDisc() )
372  throw( QString( "Unable to parse metadata from Blu-ray Disc/Path!" ));
373 
374  DTC::BlurayInfo *pBlurayInfo = new DTC::BlurayInfo();
375 
376  pBlurayInfo->setPath(path);
377  pBlurayInfo->setTitle(bdmeta->GetTitle());
378  pBlurayInfo->setAltTitle(bdmeta->GetAlternateTitle());
379  pBlurayInfo->setDiscLang(bdmeta->GetDiscLanguage());
380  pBlurayInfo->setDiscNum(bdmeta->GetCurrentDiscNumber());
381  pBlurayInfo->setTotalDiscNum(bdmeta->GetTotalDiscNumber());
382  pBlurayInfo->setTitleCount(bdmeta->GetTitleCount());
383  pBlurayInfo->setThumbCount(bdmeta->GetThumbnailCount());
384  pBlurayInfo->setTopMenuSupported(bdmeta->GetTopMenuSupported());
385  pBlurayInfo->setFirstPlaySupported(bdmeta->GetFirstPlaySupported());
386  pBlurayInfo->setNumHDMVTitles((uint)bdmeta->GetNumHDMVTitles());
387  pBlurayInfo->setNumBDJTitles((uint)bdmeta->GetNumBDJTitles());
388  pBlurayInfo->setNumUnsupportedTitles((uint)bdmeta->GetNumUnsupportedTitles());
389  pBlurayInfo->setAACSDetected(bdmeta->GetAACSDetected());
390  pBlurayInfo->setLibAACSDetected(bdmeta->GetLibAACSDetected());
391  pBlurayInfo->setAACSHandled(bdmeta->GetAACSHandled());
392  pBlurayInfo->setBDPlusDetected(bdmeta->GetBDPlusDetected());
393  pBlurayInfo->setLibBDPlusDetected(bdmeta->GetLibBDPlusDetected());
394  pBlurayInfo->setBDPlusHandled(bdmeta->GetBDPlusHandled());
395 
396  QStringList thumbs = bdmeta->GetThumbnails();
397  if (thumbs.size())
398  pBlurayInfo->setThumbPath(thumbs.at(0));
399 
400  delete bdmeta;
401 
402  return pBlurayInfo;
403 }
404 
406 //