sbDeviceStatistics.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2009 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27 */
28 
34 //------------------------------------------------------------------------------
35 //
36 // Device statistics imported services.
37 //
38 //------------------------------------------------------------------------------
39 
40 // Self imports.
41 #include "sbDeviceStatistics.h"
42 
43 // Local imports.
44 #include "sbBaseDevice.h"
45 
46 // Songbird imports.
47 #include <sbIDeviceCapabilities.h>
48 #include <sbStandardProperties.h>
49 
50 // Mozilla imports.
51 #include <prprf.h>
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // Device statistics nsISupports services.
57 //
58 //------------------------------------------------------------------------------
59 
62 
63 
64 //------------------------------------------------------------------------------
65 //
66 // Device statistics sbIMediaListEnumerationListener services.
67 //
68 //------------------------------------------------------------------------------
69 
70 
80 NS_IMETHODIMP
81 sbDeviceStatistics::OnEnumerationBegin(sbIMediaList* aMediaList,
82  PRUint16* _retval)
83 {
84  // Continue enumerating.
85  NS_ENSURE_ARG_POINTER(_retval);
87  return NS_OK;
88 }
89 
90 
102 NS_IMETHODIMP
103 sbDeviceStatistics::OnEnumeratedItem(sbIMediaList* aMediaList,
104  sbIMediaItem* aMediaItem,
105  PRUint16* _retval)
106 {
107  // Validate arguments.
108  NS_ENSURE_ARG_POINTER(aMediaList);
109  NS_ENSURE_ARG_POINTER(aMediaItem);
110  NS_ENSURE_ARG_POINTER(_retval);
111 
112  // Add the media item to the device statistics.
113  AddItem(aMediaItem);
114 
115  // Continue enumerating.
117  return NS_OK;
118 }
119 
120 
128 NS_IMETHODIMP
129 sbDeviceStatistics::OnEnumerationEnd(sbIMediaList* aMediaList,
130  nsresult aStatusCode)
131 {
132  return NS_OK;
133 }
134 
135 
136 //------------------------------------------------------------------------------
137 //
138 // Public device statistics services.
139 //
140 //------------------------------------------------------------------------------
141 
151 /* static */ nsresult
153  sbDeviceStatistics** aDeviceStatistics)
154 {
155  // Validate arguments.
156  NS_ENSURE_ARG_POINTER(aDevice);
157  NS_ENSURE_ARG_POINTER(aDeviceStatistics);
158 
159  // Function variables.
160  nsresult rv;
161 
162  // Create and initialize a new device statistics instance.
163  nsRefPtr<sbDeviceStatistics>
164  deviceStatistics = new sbDeviceStatistics();
165  NS_ENSURE_TRUE(deviceStatistics, NS_ERROR_OUT_OF_MEMORY);
166  rv = deviceStatistics->Initialize(aDevice);
167  NS_ENSURE_SUCCESS(rv, rv);
168 
169  // Return results.
170  deviceStatistics.forget(aDeviceStatistics);
171 
172  return NS_OK;
173 }
174 
175 
182 nsresult
184 {
185  // Validate arguments.
186  NS_ENSURE_ARG_POINTER(aLibrary);
187 
188  // Function variables.
189  nsresult rv;
190 
191  // Clear library statistics.
192  rv = ClearLibraryStatistics(aLibrary);
193  NS_ENSURE_SUCCESS(rv, rv);
194 
195  // Collect the media statistics from the library.
196  rv = aLibrary->EnumerateAllItems(this,
198  NS_ENSURE_SUCCESS(rv, rv);
199 
200  return NS_OK;
201 }
202 
203 
210 nsresult
212 {
213  // Validate arguments.
214  NS_ENSURE_ARG_POINTER(aLibrary);
215 
216  // Function variables.
217  nsresult rv;
218 
219  // Clear library statistics.
220  rv = ClearLibraryStatistics(aLibrary);
221  NS_ENSURE_SUCCESS(rv, rv);
222 
223  return NS_OK;
224 }
225 
226 
233 nsresult
235 {
236  // Validate arguments.
237  NS_ENSURE_ARG_POINTER(aMediaItem);
238 
239  // Function variables.
240  nsresult rv;
241 
242  // Update the statistics for the added item.
243  rv = UpdateForItem(aMediaItem, PR_TRUE);
244  NS_ENSURE_SUCCESS(rv, rv);
245 
246  return NS_OK;
247 }
248 
249 
256 nsresult
258 {
259  // Validate arguments.
260  NS_ENSURE_ARG_POINTER(aMediaItem);
261 
262  // Function variables.
263  nsresult rv;
264 
265  // Update the statistics for the removed item.
266  rv = UpdateForItem(aMediaItem, PR_FALSE);
267  NS_ENSURE_SUCCESS(rv, rv);
268 
269  return NS_OK;
270 }
271 
272 
280 nsresult
282 {
283  // Validate arguments.
284  NS_ENSURE_ARG_POINTER(aLibrary);
285 
286  // Function variables.
287  nsresult rv;
288 
289  // Clear library statistics.
290  rv = ClearLibraryStatistics(aLibrary);
291  NS_ENSURE_SUCCESS(rv, rv);
292 
293  return NS_OK;
294 }
295 
296 
297 //------------------------------------------------------------------------------
298 //
299 // Device statistics setter/getter services.
300 //
301 //------------------------------------------------------------------------------
302 
303 //
304 // Audio count setter/getters.
305 //
306 
308 {
309  nsAutoLock autoStatLock(mStatLock);
310  return mAudioCount;
311 }
312 
313 void sbDeviceStatistics::SetAudioCount(PRUint32 aAudioCount)
314 {
315  nsAutoLock autoStatLock(mStatLock);
316  mAudioCount = aAudioCount;
317 }
318 
319 void sbDeviceStatistics::AddAudioCount(PRInt32 aAddAudioCount)
320 {
321  nsAutoLock autoStatLock(mStatLock);
322  PRInt32 audioCount =
323  PR_MAX(static_cast<PRInt32>(mAudioCount) + aAddAudioCount, 0);
324  mAudioCount = audioCount;
325 }
326 
327 
328 //
329 // Audio used setter/getters.
330 //
331 
333 {
334  nsAutoLock autoStatLock(mStatLock);
335  return mAudioUsed;
336 }
337 
338 void sbDeviceStatistics::SetAudioUsed(PRUint64 aAudioUsed)
339 {
340  nsAutoLock autoStatLock(mStatLock);
341  mAudioUsed = aAudioUsed;
342 }
343 
344 void sbDeviceStatistics::AddAudioUsed(PRInt64 aAddAudioUsed)
345 {
346  nsAutoLock autoStatLock(mStatLock);
347  PRInt64 audioUsed =
348  PR_MAX(static_cast<PRInt64>(mAudioUsed) + aAddAudioUsed, 0);
349  mAudioUsed = audioUsed;
350 }
351 
352 
353 //
354 // Audio play time setter/getters.
355 //
356 
358 {
359  nsAutoLock autoStatLock(mStatLock);
360  return mAudioPlayTime;
361 }
362 
363 void sbDeviceStatistics::SetAudioPlayTime(PRUint64 aAudioPlayTime)
364 {
365  nsAutoLock autoStatLock(mStatLock);
366  mAudioPlayTime = aAudioPlayTime;
367 }
368 
369 void sbDeviceStatistics::AddAudioPlayTime(PRInt64 aAddAudioPlayTime)
370 {
371  nsAutoLock autoStatLock(mStatLock);
372  PRInt64 audioPlayTime =
373  PR_MAX(static_cast<PRInt64>(mAudioPlayTime) + aAddAudioPlayTime, 0);
374  mAudioPlayTime = audioPlayTime;
375 }
376 
377 
378 //
379 // Video count setter/getters.
380 //
381 
383 {
384  nsAutoLock autoStatLock(mStatLock);
385  return mVideoCount;
386 }
387 
388 void sbDeviceStatistics::SetVideoCount(PRUint32 aVideoCount)
389 {
390  nsAutoLock autoStatLock(mStatLock);
391  mVideoCount = aVideoCount;
392 }
393 
394 void sbDeviceStatistics::AddVideoCount(PRInt32 aAddVideoCount)
395 {
396  nsAutoLock autoStatLock(mStatLock);
397  PRInt32 videoCount =
398  PR_MAX(static_cast<PRInt32>(mVideoCount) + aAddVideoCount, 0);
399  mVideoCount = videoCount;
400 }
401 
402 
403 //
404 // Video used setter/getters.
405 //
406 
408 {
409  nsAutoLock autoStatLock(mStatLock);
410  return mVideoUsed;
411 }
412 
413 void sbDeviceStatistics::SetVideoUsed(PRUint64 aVideoUsed)
414 {
415  nsAutoLock autoStatLock(mStatLock);
416  mVideoUsed = aVideoUsed;
417 }
418 
419 void sbDeviceStatistics::AddVideoUsed(PRInt64 aAddVideoUsed)
420 {
421  nsAutoLock autoStatLock(mStatLock);
422  PRInt64 videoUsed =
423  PR_MAX(static_cast<PRInt64>(mVideoUsed) + aAddVideoUsed, 0);
424  mVideoUsed = videoUsed;
425 }
426 
427 
428 //
429 // Video play time setter/getters.
430 //
431 
433 {
434  nsAutoLock autoStatLock(mStatLock);
435  return mVideoPlayTime;
436 }
437 
438 void sbDeviceStatistics::SetVideoPlayTime(PRUint64 aVideoPlayTime)
439 {
440  nsAutoLock autoStatLock(mStatLock);
441  mVideoPlayTime = aVideoPlayTime;
442 }
443 
444 void sbDeviceStatistics::AddVideoPlayTime(PRInt64 aAddVideoPlayTime)
445 {
446  nsAutoLock autoStatLock(mStatLock);
447  PRInt64 videoPlayTime =
448  PR_MAX(static_cast<PRInt64>(mVideoPlayTime) + aAddVideoPlayTime, 0);
449  mVideoPlayTime = videoPlayTime;
450 }
451 
452 
453 //
454 // Image count setter/getters.
455 //
456 
458 {
459  nsAutoLock autoStatLock(mStatLock);
460  return mImageCount;
461 }
462 
463 void sbDeviceStatistics::SetImageCount(PRUint32 aImageCount)
464 {
465  nsAutoLock autoStatLock(mStatLock);
466  mImageCount = aImageCount;
467 }
468 
469 void sbDeviceStatistics::AddImageCount(PRInt32 aAddImageCount)
470 {
471  nsAutoLock autoStatLock(mStatLock);
472  PRInt32 ImageCount =
473  PR_MAX(static_cast<PRInt32>(mImageCount) + aAddImageCount, 0);
474  mImageCount = ImageCount;
475 }
476 
477 
478 //
479 // Image used setter/getters.
480 //
481 
483 {
484  nsAutoLock autoStatLock(mStatLock);
485  return mImageUsed;
486 }
487 
488 void sbDeviceStatistics::SetImageUsed(PRUint64 aImageUsed)
489 {
490  nsAutoLock autoStatLock(mStatLock);
491  mImageUsed = aImageUsed;
492 }
493 
494 void sbDeviceStatistics::AddImageUsed(PRInt64 aAddImageUsed)
495 {
496  nsAutoLock autoStatLock(mStatLock);
497  PRInt64 ImageUsed =
498  PR_MAX(static_cast<PRInt64>(mImageUsed) + aAddImageUsed, 0);
499  mImageUsed = ImageUsed;
500 }
501 
502 
503 //------------------------------------------------------------------------------
504 //
505 // Private device statistics services.
506 //
507 //------------------------------------------------------------------------------
508 
513 sbDeviceStatistics::sbDeviceStatistics() :
514  mBaseDevice(nsnull),
515  mStatLock(nsnull),
516  mAudioCount(0),
517  mAudioUsed(0),
518  mAudioPlayTime(0),
519  mVideoCount(0),
520  mVideoUsed(0),
521  mVideoPlayTime(0),
522  mImageCount(0),
523  mImageUsed(0)
524 {
525 }
526 
527 
532 sbDeviceStatistics::~sbDeviceStatistics()
533 {
534  // Dispose of the statistics lock.
535  if (mStatLock)
536  nsAutoLock::DestroyLock(mStatLock);
537  mStatLock = nsnull;
538 }
539 
540 
548 nsresult
549 sbDeviceStatistics::Initialize(class sbBaseDevice* aDevice)
550 {
551  // Validate arguments.
552  NS_ENSURE_ARG_POINTER(aDevice);
553 
554  // Get the device.
555  mBaseDevice = aDevice;
556 
557  // Create the statistics lock.
558  mStatLock = nsAutoLock::NewLock(__FILE__"::mStatLock");
559  NS_ENSURE_TRUE(mStatLock, NS_ERROR_OUT_OF_MEMORY);
560 
561  return NS_OK;
562 }
563 
564 
571 nsresult
572 sbDeviceStatistics::ClearLibraryStatistics(sbIDeviceLibrary* aLibrary)
573 {
574  // Validate arguments.
575  NS_ENSURE_ARG_POINTER(aLibrary);
576 
577  // Clear library statistics.
578  mAudioCount = 0;
579  mAudioUsed = 0;
580  mAudioPlayTime = 0;
581  mVideoCount = 0;
582  mVideoUsed = 0;
583  mVideoPlayTime = 0;
584  mImageCount = 0;
585  mImageUsed = 0;
586 
587  return NS_OK;
588 }
589 
590 
601 nsresult
602 sbDeviceStatistics::UpdateForItem(sbIMediaItem* aMediaItem,
603  PRBool aItemAdded)
604 {
605  // Validate arguments.
606  NS_ENSURE_ARG_POINTER(aMediaItem);
607 
608  // Function variables.
609  nsresult rv;
610 
611  // Ignore media lists.
612  nsCOMPtr<sbIMediaList> mediaList = do_QueryInterface(aMediaItem, &rv);
613  if (NS_SUCCEEDED(rv))
614  return NS_OK;
615 
616  // Get the item content type.
617  PRUint32 contentType;
618  rv = mBaseDevice->GetItemContentType(aMediaItem, &contentType);
619  if (NS_FAILED(rv)) {
621  }
622 
623  // Get the item count update value.
624  PRInt32 itemCountUpdate;
625  if (aItemAdded)
626  itemCountUpdate = 1;
627  else
628  itemCountUpdate = -1;
629 
630  // Get the item used update value.
631  PRInt64 itemUsedUpdate;
632  rv = aMediaItem->GetContentLength(&itemUsedUpdate);
633  NS_ENSURE_SUCCESS(rv, rv);
634  if (!aItemAdded)
635  itemUsedUpdate = -itemUsedUpdate;
636 
637  // Get the item play time update value.
638  PRInt64 itemPlayTimeUpdate = 0;
639  nsAutoString duration;
640  rv = aMediaItem->GetProperty(NS_LITERAL_STRING(SB_PROPERTY_DURATION),
641  duration);
642  if (NS_SUCCEEDED(rv)) {
643  if (PR_sscanf(NS_ConvertUTF16toUTF8(duration).get(),
644  "%lld",
645  &itemPlayTimeUpdate) == 0) {
646  itemPlayTimeUpdate = 0;
647  }
648  }
649  if (!aItemAdded)
650  itemPlayTimeUpdate = -itemPlayTimeUpdate;
651 
652  // Update appropriate statistics depending upon the content type.
653  // Statistics for other content types will be collected together elsewhere
654  // in the total other statistics.
655  if (contentType == sbIDeviceCapabilities::CONTENT_AUDIO) {
656  AddAudioCount(itemCountUpdate);
657  AddAudioUsed(itemUsedUpdate);
658  AddAudioPlayTime(itemPlayTimeUpdate);
659  } else if (contentType == sbIDeviceCapabilities::CONTENT_VIDEO) {
660  AddVideoCount(itemCountUpdate);
661  AddVideoUsed(itemUsedUpdate);
662  AddVideoPlayTime(itemPlayTimeUpdate);
663  }
664 
665  return NS_OK;
666 }
667 
668 
nsresult RemoveLibrary(sbIDeviceLibrary *aLibrary)
nsresult RemoveAllItems(sbIDeviceLibrary *aLibrary)
void SetVideoCount(PRUint32 aVideoCount)
return NS_OK
void AddImageUsed(PRInt64 aAddImageUsed)
Interface used to enumerate the items in a media list.
void AddVideoCount(PRInt32 aAddVideoCount)
NS_DECL_ISUPPORTS static NS_DECL_SBIMEDIALISTENUMERATIONLISTENER nsresult New(class sbBaseDevice *aDevice, sbDeviceStatistics **aDeviceStatistics)
void AddAudioUsed(PRInt64 aAddAudioUsed)
nsresult AddLibrary(sbIDeviceLibrary *aLibrary)
void AddImageCount(PRInt32 aAddImageCount)
A brief description of the contents of this interface.
void SetVideoPlayTime(PRUint64 aVideoPlayTime)
void AddAudioPlayTime(PRInt64 aAddAudioPlayTime)
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceStatistics, sbIMediaListEnumerationListener) NS_IMETHODIMP sbDeviceStatistics
Called when enumeration is about to begin.
void SetAudioCount(PRUint32 aAudioCount)
void AddAudioCount(PRInt32 aAddAudioCount)
_hideDatepicker duration
const unsigned short ENUMERATIONTYPE_SNAPSHOT
This flag means that the list being enumerated is a copy that may become out of date.
virtual nsresult GetItemContentType(sbIMediaItem *aMediaItem, PRUint32 *aContentType)
#define SB_PROPERTY_DURATION
nsresult AddItem(sbIMediaItem *aMediaItem)
void AddVideoUsed(PRInt64 aAddVideoUsed)
nsresult RemoveItem(sbIMediaItem *aMediaItem)
void SetImageUsed(PRUint64 aImageUsed)
void SetVideoUsed(PRUint64 aVideoUsed)
Songbird Device Statistics Definitions.
void AddVideoPlayTime(PRInt64 aAddVideoPlayTime)
Interface that defines a single item of media in the system.
void SetImageCount(PRUint32 aImageCount)
void SetAudioUsed(PRUint64 aAudioUsed)
void SetAudioPlayTime(PRUint64 aAudioPlayTime)