sbTranscodeManager.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #include "sbTranscodeManager.h"
28 
29 #include <nsAutoLock.h>
30 #include <nsCOMPtr.h>
31 #include <nsAutoPtr.h>
32 
33 #include <nsServiceManagerUtils.h>
34 #include <nsComponentManagerUtils.h>
35 #include <nsIComponentRegistrar.h>
36 #include <nsISupportsPrimitives.h>
37 
38 #include <nsStringGlue.h>
39 #include <nsISimpleEnumerator.h>
40 #include <nsArrayUtils.h>
41 
42 #include <sbITranscodingConfigurator.h>
43 #include <sbITranscodeVideoJob.h>
44 
45 /* Global transcode manager (singleton) */
47 
48 static const char TranscodeContractIDPrefix[] =
49  "@songbirdnest.com/Songbird/Mediacore/Transcode/";
50 
52 
54 {
55  m_pContractListLock = PR_NewLock();
56  NS_ASSERTION(m_pContractListLock,
57  "Failed to create sbTranscodeManager::m_pContractListLock! "
58  "Object *not* threadsafe!");
59 
60  // Find the list of handlers for this object.
61  nsresult rv;
62  nsCOMPtr<nsIComponentRegistrar> registrar;
63  rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
64  NS_ENSURE_SUCCESS (rv, /* void */);
65 
66  nsCOMPtr<nsISimpleEnumerator> simpleEnumerator;
67  rv = registrar->EnumerateContractIDs(getter_AddRefs(simpleEnumerator));
68  NS_ENSURE_SUCCESS (rv, /* void */);
69 
70  PRBool moreAvailable = PR_FALSE;
71  while(simpleEnumerator->HasMoreElements(&moreAvailable) == NS_OK &&
72  moreAvailable)
73  {
74  nsCOMPtr<nsISupportsCString> contractString;
75  rv = simpleEnumerator->GetNext(getter_AddRefs(contractString));
76  if (NS_SUCCEEDED (rv))
77  {
78  nsCString contractID;
79  contractString->GetData(contractID);
80  if (contractID.Find(TranscodeContractIDPrefix) != -1)
81  {
82  m_ContractList.push_back(contractID);
83  }
84  }
85  }
86 }
87 
88 //-----------------------------------------------------------------------------
89 sbTranscodeManager::~sbTranscodeManager()
90 {
91  if(m_pContractListLock)
92  {
93  PR_DestroyLock(m_pContractListLock);
94  m_pContractListLock = nsnull;
95  }
96 }
97 
98 //-----------------------------------------------------------------------------
99 sbTranscodeManager *sbTranscodeManager::GetSingleton()
100 {
101  if (gTranscodeManager) {
102  NS_ADDREF(gTranscodeManager);
103  return gTranscodeManager;
104  }
105 
106  NS_NEWXPCOM(gTranscodeManager, sbTranscodeManager);
107  if (!gTranscodeManager)
108  return nsnull;
109 
110  NS_ADDREF(gTranscodeManager);
111  NS_ADDREF(gTranscodeManager);
112 
113  return gTranscodeManager;
114 }
115 
116 //-----------------------------------------------------------------------------
117 void sbTranscodeManager::DestroySingleton()
118 {
119  nsRefPtr<sbTranscodeManager> dummy;
120  dummy.swap(gTranscodeManager);
121 }
122 
123 NS_IMETHODIMP
124 sbTranscodeManager::GetTranscoderForMediaItem(sbIMediaItem *aMediaItem,
125  nsISupports **_retval)
126 {
127  NS_ENSURE_ARG_POINTER(aMediaItem);
128  NS_ENSURE_ARG_POINTER(_retval);
129  nsresult rv;
130 
131  int bestVote = -1;
132  nsCOMPtr<nsISupports> bestHandler;
133 
134  for (contractlist_t::iterator contractid = m_ContractList.begin();
135  contractid != m_ContractList.end();
136  ++contractid )
137  {
138  nsCOMPtr<nsISupports> supports =
139  do_CreateInstance((*contractid).get(), &rv);
140  if (NS_FAILED(rv) || !supports)
141  {
142  continue;
143  }
144 
145  nsCOMPtr<sbITranscodeVideoJob> videoJob = do_QueryInterface(supports, &rv);
146  if (NS_SUCCEEDED(rv) && videoJob)
147  {
148  // new, video-style transcoder
149  PRInt32 vote;
150  rv = videoJob->Vote(aMediaItem, &vote);
151  if (NS_FAILED(rv)) {
152  continue;
153  }
154 
155  /* Negative values are not ever permissible */
156  if (vote >= bestVote) {
157  bestVote = vote;
158  bestHandler = supports;
159  }
160  }
161  }
162 
163  if (bestHandler) {
164  NS_ADDREF(*_retval = bestHandler);
165  return NS_OK;
166  }
167  else
168  return NS_ERROR_FAILURE;
169 }
170 
171 NS_IMETHODIMP
172 sbTranscodeManager::GetTranscodeProfiles(PRUint32 aType, nsIArray **_retval)
173 {
174  nsresult rv;
175  nsCOMPtr<nsIMutableArray> array =
176  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
177  NS_ENSURE_SUCCESS(rv, rv);
178 
179  for (contractlist_t::iterator contractid = m_ContractList.begin();
180  contractid != m_ContractList.end();
181  ++contractid )
182  {
183  nsCOMPtr<sbITranscodingConfigurator> handler = do_CreateInstance(
184  (*contractid).get(), &rv);
185 
186  if(NS_SUCCEEDED(rv) && handler)
187  {
188  nsCOMPtr<nsIArray> profiles;
189  PRUint32 length;
190 
191  rv = handler->GetAvailableProfiles(getter_AddRefs(profiles));
192  NS_ENSURE_SUCCESS (rv, rv);
193 
194  rv = profiles->GetLength(&length);
195  NS_ENSURE_SUCCESS (rv, rv);
196 
197  for (PRUint32 i = 0; i < length; i++) {
198  nsCOMPtr<sbITranscodeProfile> profile =
199  do_QueryElementAt(profiles, i, &rv);
200  NS_ENSURE_SUCCESS (rv, rv);
201 
202  PRUint32 profileType;
203  rv = profile->GetType(&profileType);
204  NS_ENSURE_SUCCESS (rv, rv);
205 
206  if (profileType == aType) {
207  rv = array->AppendElement(profile, PR_FALSE);
208  NS_ENSURE_SUCCESS (rv, rv);
209  }
210  }
211  }
212  }
213 
214  nsCOMPtr<nsIArray> arr = do_QueryInterface(array, &rv);
215  NS_ENSURE_SUCCESS (rv, rv);
216 
217  arr.forget(_retval);
218 
219  return NS_OK;
220 }
221 
static const char TranscodeContractIDPrefix[]
return NS_OK
var registrar
inArray array
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
Interface that defines a single item of media in the system.
sbTranscodeManager * gTranscodeManager
_getSelectedPageStyle s i
GstMessage gpointer data sbGStreamerMessageHandler * handler
The manager from which to request a transcoding job, transcoding profiles, etc.