30 #include <nsIDOM3Node.h>
31 #include <nsIDOMDocument.h>
32 #include <nsIDOMElement.h>
33 #include <nsIDOMNode.h>
34 #include <nsIDOMParser.h>
36 #include <nsIFileStreams.h>
37 #include <nsIMutableArray.h>
39 #include <nsAutoPtr.h>
41 #include <nsComponentManagerUtils.h>
42 #include <nsStringGlue.h>
43 #include <nsThreadUtils.h>
60 sbTranscodeProfileLoader::~sbTranscodeProfileLoader()
66 sbTranscodeProfileLoader::LoadProfile(nsIFile *aFile,
69 NS_ENSURE_ARG_POINTER(_retval);
74 if (NS_IsMainThread()) {
77 NS_ENSURE_SUCCESS(rv, rv);
79 rv = CallQueryInterface(
mProfile.get(), _retval);
80 NS_ENSURE_SUCCESS(rv, rv);
85 nsCOMPtr<nsIRunnable> runnable =
86 do_QueryInterface(NS_ISUPPORTS_CAST(nsIRunnable*,
this), &rv);
87 NS_ASSERTION(runnable,
"sbTranscodeProfileLoader should implement nsIRunnable");
88 rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
89 NS_ENSURE_SUCCESS(rv, rv);
91 rv = CallQueryInterface(
mProfile.get(), _retval);
92 NS_ENSURE_SUCCESS(rv, rv);
106 sbTranscodeProfileLoader::Run()
115 const PRInt32 IOFLAGS_DEFAULT = -1;
116 const PRInt32 PERMISSIONS_DEFAULT = -1;
121 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_UNEXPECTED);
122 NS_ENSURE_ARG_POINTER(
mFile);
124 nsCOMPtr<nsIDOMParser> domParser =
125 do_CreateInstance(
"@mozilla.org/xmlextras/domparser;1", &rv);
126 NS_ENSURE_SUCCESS(rv, rv);
128 nsCOMPtr<nsIFileInputStream> fileStream =
129 do_CreateInstance(
"@mozilla.org/network/file-input-stream;1", &rv);
130 NS_ENSURE_SUCCESS(rv, rv);
132 rv = fileStream->Init(
mFile,
138 rv = fileStream->Available(&fileSize);
139 NS_ENSURE_SUCCESS(rv, rv);
141 nsCOMPtr<nsIDOMDocument>
doc;
142 rv = domParser->ParseFromStream(fileStream,
146 getter_AddRefs(doc));
147 NS_ENSURE_SUCCESS(rv, rv);
149 rv = fileStream->Close();
150 NS_ENSURE_SUCCESS(rv, rv);
153 NS_ENSURE_SUCCESS(rv, rv);
155 nsCOMPtr<nsIDOMElement>
element;
156 rv = doc->GetDocumentElement(getter_AddRefs(element));
157 NS_ENSURE_SUCCESS(rv, rv);
159 nsCOMPtr<nsIDOMNode> childNode;
160 rv = element->GetFirstChild(getter_AddRefs(childNode));
161 NS_ENSURE_SUCCESS(rv, rv);
164 nsCOMPtr<nsIDOMElement> childElement = do_QueryInterface(childNode, &rv);
166 if (NS_SUCCEEDED(rv)) {
168 rv = childElement->GetLocalName(localName);
169 NS_ENSURE_SUCCESS(rv, rv);
171 if (localName.EqualsLiteral(
"type")) {
173 rv =
GetType(childNode, &type);
174 NS_ENSURE_SUCCESS(rv, rv);
176 NS_ENSURE_SUCCESS(rv, rv);
177 }
else if (localName.EqualsLiteral(
"description")) {
178 nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(childNode);
180 nsString textContent;
181 rv = dom3Node->GetTextContent(textContent);
182 NS_ENSURE_SUCCESS(rv, rv);
183 rv =
mProfile->SetDescription(textContent);
184 NS_ENSURE_SUCCESS(rv, rv);
186 }
else if (localName.EqualsLiteral(
"priority")) {
187 nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(childNode);
189 nsString textContent;
190 rv = dom3Node->GetTextContent(textContent);
191 NS_ENSURE_SUCCESS(rv, rv);
192 PRInt32 priority = textContent.ToInteger(&rv);
193 NS_ENSURE_SUCCESS(rv, rv);
195 PRBool hasQuality = PR_FALSE;
196 rv = childElement->HasAttribute(NS_LITERAL_STRING(
"quality"),
198 NS_ENSURE_SUCCESS(rv, rv);
200 nsString qualityString;
201 rv = childElement->GetAttribute(NS_LITERAL_STRING(
"quality"),
203 NS_ENSURE_SUCCESS(rv, rv);
204 NS_LossyConvertUTF16toASCII qualityCString(qualityString);
205 PRFloat64 quality = PR_strtod(qualityCString.get(), nsnull);
206 rv =
mProfile->AddPriority((
double)quality, priority);
207 NS_ENSURE_SUCCESS(rv, rv);
209 rv =
mProfile->SetPriority(priority);
210 NS_ENSURE_SUCCESS(rv, rv);
213 }
else if (localName.EqualsLiteral(
"id")) {
214 nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(childNode);
216 nsString textContent;
217 rv = dom3Node->GetTextContent(textContent);
218 NS_ENSURE_SUCCESS(rv, rv);
220 NS_ENSURE_SUCCESS(rv, rv);
222 }
else if (localName.EqualsLiteral(
"extension")) {
223 nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(childNode);
225 nsString textContent;
226 rv = dom3Node->GetTextContent(textContent);
227 NS_ENSURE_SUCCESS(rv, rv);
228 rv =
mProfile->SetFileExtension(NS_ConvertUTF16toUTF8(textContent));
229 NS_ENSURE_SUCCESS(rv, rv);
231 }
else if (localName.EqualsLiteral(
"container")) {
233 NS_ENSURE_SUCCESS(rv, rv);
234 }
else if (localName.EqualsLiteral(
"audio")) {
236 NS_ENSURE_SUCCESS(rv, rv);
237 }
else if (localName.EqualsLiteral(
"video")) {
239 NS_ENSURE_SUCCESS(rv, rv);
243 rv = childNode->GetNextSibling(getter_AddRefs(childNode));
244 NS_ENSURE_SUCCESS(rv, rv);
253 NS_ENSURE_ARG_POINTER(aTypeNode);
254 NS_ENSURE_ARG_POINTER(_retval);
258 nsCOMPtr<nsIDOM3Node> dom3node = do_QueryInterface(aTypeNode, &rv);
259 NS_ENSURE_SUCCESS(rv, rv);
262 rv = dom3node->GetTextContent(type);
263 NS_ENSURE_SUCCESS(rv, rv);
265 if (type.EqualsLiteral(
"audio")) {
267 }
else if (type.EqualsLiteral(
"video")) {
269 }
else if (type.EqualsLiteral(
"image")) {
272 return NS_ERROR_INVALID_ARG;
281 NS_ENSURE_ARG_POINTER(aAttributeElement);
282 NS_ENSURE_ARG_POINTER(_retval);
286 nsRefPtr<sbTranscodeProfileAttribute> attr =
288 NS_ENSURE_TRUE(attr, NS_ERROR_OUT_OF_MEMORY);
291 rv = aAttributeElement->GetAttribute(NS_LITERAL_STRING(
"name"),
293 NS_ENSURE_SUCCESS(rv, rv);
294 rv = attr->SetName(name);
295 NS_ENSURE_SUCCESS(rv, rv);
298 rv = aAttributeElement->GetAttribute(NS_LITERAL_STRING(
"type"),
300 NS_ENSURE_SUCCESS(rv, rv);
302 nsString valueString;
303 rv = aAttributeElement->GetAttribute(NS_LITERAL_STRING(
"value"),
305 NS_ENSURE_SUCCESS(rv, rv);
307 if (type.EqualsLiteral(
"int")) {
310 value = valueString.ToInteger(&rv);
311 NS_ENSURE_SUCCESS(rv, rv);
314 NS_ENSURE_SUCCESS(rv, rv);
316 else if (type.EqualsLiteral(
"string")) {
318 NS_ENSURE_SUCCESS(rv, rv);
321 return NS_ERROR_NOT_IMPLEMENTED;
324 rv = CallQueryInterface(attr.get(), _retval);
325 NS_ENSURE_SUCCESS(rv, rv);
334 NS_ENSURE_ARG_POINTER(aPropertyElement);
335 NS_ENSURE_ARG_POINTER(_retval);
339 nsRefPtr<sbTranscodeProfileProperty>
property =
341 NS_ENSURE_TRUE(property, NS_ERROR_OUT_OF_MEMORY);
343 nsString attributeValue;
344 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"name"),
346 NS_ENSURE_SUCCESS(rv, rv);
347 rv =
property->SetPropertyName(attributeValue);
348 NS_ENSURE_SUCCESS(rv, rv);
350 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"hidden"),
352 NS_ENSURE_SUCCESS(rv, rv);
353 if (attributeValue.IsEmpty() ||
354 attributeValue.EqualsLiteral(
"0") ||
355 attributeValue.LowerCaseEqualsLiteral(
"false"))
357 rv =
property->SetHidden(PR_FALSE);
358 NS_ENSURE_SUCCESS(rv, rv);
361 rv =
property->SetHidden(PR_TRUE);
362 NS_ENSURE_SUCCESS(rv, rv);
365 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"mapping"),
367 NS_ENSURE_SUCCESS(rv, rv);
368 if (!attributeValue.IsEmpty()) {
369 rv =
property->SetMapping(NS_ConvertUTF16toUTF8(attributeValue));
370 NS_ENSURE_SUCCESS(rv, rv);
373 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"scale"),
375 NS_ENSURE_SUCCESS(rv, rv);
376 if (!attributeValue.IsEmpty()) {
377 rv =
property->SetScale(NS_ConvertUTF16toUTF8(attributeValue));
378 NS_ENSURE_SUCCESS(rv, rv);
381 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"type"),
383 NS_ENSURE_SUCCESS(rv, rv);
385 if (attributeValue.EqualsLiteral(
"int")) {
388 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"min"),
390 NS_ENSURE_SUCCESS(rv, rv);
392 value = attributeValue.ToInteger(&rv);
393 NS_ENSURE_SUCCESS(rv, rv);
396 NS_ENSURE_SUCCESS(rv, rv);
398 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"max"),
400 NS_ENSURE_SUCCESS(rv, rv);
402 value = attributeValue.ToInteger(&rv);
403 NS_ENSURE_SUCCESS(rv, rv);
406 NS_ENSURE_SUCCESS(rv, rv);
408 rv = aPropertyElement->GetAttribute(NS_LITERAL_STRING(
"default"),
410 NS_ENSURE_SUCCESS(rv, rv);
412 value = attributeValue.ToInteger(&rv);
413 NS_ENSURE_SUCCESS(rv, rv);
416 NS_ENSURE_SUCCESS(rv, rv);
418 return NS_ERROR_NOT_IMPLEMENTED;
421 rv = CallQueryInterface(property.get(), _retval);
422 NS_ENSURE_SUCCESS(rv, rv);
430 nsIDOMElement* aContainer)
432 NS_ENSURE_ARG_POINTER(aProfile);
433 NS_ENSURE_ARG_POINTER(aContainer);
437 nsCOMPtr<nsIMutableArray> properties =
438 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
439 NS_ENSURE_SUCCESS(rv, rv);
442 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
443 NS_ENSURE_SUCCESS(rv, rv);
445 nsCOMPtr<nsIDOMNode> childNode;
446 rv = aContainer->GetFirstChild(getter_AddRefs(childNode));
447 NS_ENSURE_SUCCESS(rv, rv);
450 nsCOMPtr<nsIDOMElement> childElement = do_QueryInterface(childNode);
453 rv = childElement->GetLocalName(localName);
454 NS_ENSURE_SUCCESS(rv, rv);
456 if (localName.EqualsLiteral(
"type")) {
457 nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(childNode, &rv);
458 NS_ENSURE_SUCCESS(rv, rv);
460 nsString textContent;
461 rv = dom3Node->GetTextContent(textContent);
462 NS_ENSURE_SUCCESS(rv, rv);
464 switch (aContainerType) {
466 rv = aProfile->SetContainerFormat(textContent);
469 rv = aProfile->SetAudioCodec(textContent);
472 rv = aProfile->SetVideoCodec(textContent);
475 rv = NS_ERROR_UNEXPECTED;
477 NS_ENSURE_SUCCESS(rv, rv);
479 else if (localName.EqualsLiteral(
"attribute")) {
480 nsCOMPtr<sbITranscodeProfileAttribute> attr;
482 NS_ENSURE_SUCCESS(rv, rv);
484 rv = attributes->AppendElement(attr, PR_FALSE);
485 NS_ENSURE_SUCCESS(rv, rv);
487 else if (localName.EqualsLiteral(
"property")) {
488 nsCOMPtr<sbITranscodeProfileProperty> property;
490 NS_ENSURE_SUCCESS(rv, rv);
492 rv = properties->AppendElement(property, PR_FALSE);
493 NS_ENSURE_SUCCESS(rv, rv);
495 else if (localName.EqualsLiteral(
"quality-property")) {
497 rv = childElement->GetAttribute(NS_LITERAL_STRING(
"quality"), attrVal);
498 NS_ENSURE_SUCCESS(rv, rv);
499 nsCString attrCVal = NS_LossyConvertUTF16toASCII(attrVal);
500 PRFloat64 quality = PR_strtod(attrCVal.get(), nsnull);
502 rv = childElement->GetAttribute(NS_LITERAL_STRING(
"value"), attrVal);
503 NS_ENSURE_SUCCESS(rv, rv);
504 attrCVal = NS_LossyConvertUTF16toASCII(attrVal);
505 PRFloat64
value = PR_strtod(attrCVal.get(), nsnull);
507 rv = childElement->GetAttribute(NS_LITERAL_STRING(
"name"), attrVal);
508 NS_ENSURE_SUCCESS(rv, rv);
510 if (attrVal.EqualsLiteral(
"bitrate")) {
513 NS_ENSURE_SUCCESS(rv, rv);
515 else if (attrVal.EqualsLiteral(
"bpp")) {
517 rv = aProfile->
AddVideoBPP((
double)quality, (
double)value);
518 NS_ENSURE_SUCCESS(rv, rv);
521 NS_ENSURE_TRUE(
false, NS_ERROR_UNEXPECTED);
525 rv = childNode->GetNextSibling(getter_AddRefs(childNode));
526 NS_ENSURE_SUCCESS(rv, rv);
529 switch (aContainerType) {
531 rv = aProfile->SetContainerProperties(properties);
532 rv = aProfile->SetContainerAttributes(attributes);
535 rv = aProfile->SetAudioProperties(properties);
536 rv = aProfile->SetAudioAttributes(attributes);
539 rv = aProfile->SetVideoProperties(properties);
540 rv = aProfile->SetVideoAttributes(attributes);
543 rv = NS_ERROR_UNEXPECTED;
545 NS_ENSURE_SUCCESS(rv, rv);
nsRefPtr< sbTranscodeProfile > mProfile
const unsigned long TRANSCODE_TYPE_IMAGE
nsresult GetType(nsIDOMNode *aTypeNode, PRUint32 *_retval)
nsresult ProcessProperty(nsIDOMElement *aPropertyElement, sbITranscodeProfileProperty **_retval)
function doc() browser.contentDocument
An object that can create an sbITranscodeProfile from an XML description file.
nsCOMPtr< nsIFile > mFile
nsresult LoadProfileInternal()
const unsigned long TRANSCODE_TYPE_AUDIO
An object defining a transcoding profile.
Songbird Variant Utility Definitions.
const unsigned long TRANSCODE_TYPE_AUDIO_VIDEO
NS_DECL_ISUPPORTS NS_DECL_SBITRANSCODEPROFILELOADER NS_DECL_NSIRUNNABLE sbTranscodeProfileLoader()
nsresult AddVideoBPP(double aQuality, double aBPP)
nsresult AddAudioBitrate(double aQuality, double aBitrate)
NS_IMPL_THREADSAFE_ISUPPORTS2(sbTranscodeProfileLoader, sbITranscodeProfileLoader, nsIRunnable)
nsresult ProcessAttribute(nsIDOMElement *aAttributeElement, sbITranscodeProfileAttribute **_retval)
nsresult ProcessContainer(sbTranscodeProfile *aProfile, ContainerType_t aContainerType, nsIDOMElement *aContainer)