Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "plstr.h" |
michael@0 | 7 | #include "nsDeviceChannel.h" |
michael@0 | 8 | #include "nsDeviceCaptureProvider.h" |
michael@0 | 9 | |
michael@0 | 10 | #ifdef MOZ_WIDGET_ANDROID |
michael@0 | 11 | #include "mozilla/Preferences.h" |
michael@0 | 12 | #include "AndroidCaptureProvider.h" |
michael@0 | 13 | #endif |
michael@0 | 14 | |
michael@0 | 15 | using namespace mozilla; |
michael@0 | 16 | |
michael@0 | 17 | // Copied from image/decoders/icon/nsIconURI.cpp |
michael@0 | 18 | // takes a string like ?size=32&contentType=text/html and returns a new string |
michael@0 | 19 | // containing just the attribute values. i.e you could pass in this string with |
michael@0 | 20 | // an attribute name of "size=", this will return 32 |
michael@0 | 21 | // Assumption: attribute pairs are separated by & |
michael@0 | 22 | void extractAttributeValue(const char* searchString, const char* attributeName, nsCString& result) |
michael@0 | 23 | { |
michael@0 | 24 | result.Truncate(); |
michael@0 | 25 | |
michael@0 | 26 | if (!searchString || !attributeName) |
michael@0 | 27 | return; |
michael@0 | 28 | |
michael@0 | 29 | uint32_t attributeNameSize = strlen(attributeName); |
michael@0 | 30 | const char *startOfAttribute = PL_strcasestr(searchString, attributeName); |
michael@0 | 31 | if (!startOfAttribute || |
michael@0 | 32 | !( *(startOfAttribute-1) == '?' || *(startOfAttribute-1) == '&') ) |
michael@0 | 33 | return; |
michael@0 | 34 | |
michael@0 | 35 | startOfAttribute += attributeNameSize; // Skip the attributeName |
michael@0 | 36 | if (!*startOfAttribute) |
michael@0 | 37 | return; |
michael@0 | 38 | |
michael@0 | 39 | const char *endOfAttribute = strchr(startOfAttribute, '&'); |
michael@0 | 40 | if (endOfAttribute) { |
michael@0 | 41 | result.Assign(Substring(startOfAttribute, endOfAttribute)); |
michael@0 | 42 | } else { |
michael@0 | 43 | result.Assign(startOfAttribute); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | NS_IMPL_ISUPPORTS_INHERITED(nsDeviceChannel, |
michael@0 | 48 | nsBaseChannel, |
michael@0 | 49 | nsIChannel) |
michael@0 | 50 | |
michael@0 | 51 | // nsDeviceChannel methods |
michael@0 | 52 | nsDeviceChannel::nsDeviceChannel() |
michael@0 | 53 | { |
michael@0 | 54 | SetContentType(NS_LITERAL_CSTRING("image/png")); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | nsDeviceChannel::~nsDeviceChannel() |
michael@0 | 58 | { |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | nsresult |
michael@0 | 62 | nsDeviceChannel::Init(nsIURI* aUri) |
michael@0 | 63 | { |
michael@0 | 64 | nsBaseChannel::Init(); |
michael@0 | 65 | nsBaseChannel::SetURI(aUri); |
michael@0 | 66 | return NS_OK; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | nsresult |
michael@0 | 70 | nsDeviceChannel::OpenContentStream(bool aAsync, |
michael@0 | 71 | nsIInputStream** aStream, |
michael@0 | 72 | nsIChannel** aChannel) |
michael@0 | 73 | { |
michael@0 | 74 | if (!aAsync) |
michael@0 | 75 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 76 | |
michael@0 | 77 | nsCOMPtr<nsIURI> uri = nsBaseChannel::URI(); |
michael@0 | 78 | *aStream = nullptr; |
michael@0 | 79 | *aChannel = nullptr; |
michael@0 | 80 | NS_NAMED_LITERAL_CSTRING(width, "width="); |
michael@0 | 81 | NS_NAMED_LITERAL_CSTRING(height, "height="); |
michael@0 | 82 | |
michael@0 | 83 | nsAutoCString spec; |
michael@0 | 84 | uri->GetSpec(spec); |
michael@0 | 85 | |
michael@0 | 86 | nsAutoCString type; |
michael@0 | 87 | |
michael@0 | 88 | nsRefPtr<nsDeviceCaptureProvider> capture; |
michael@0 | 89 | nsCaptureParams captureParams; |
michael@0 | 90 | captureParams.camera = 0; |
michael@0 | 91 | if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=image/png"), |
michael@0 | 92 | true, |
michael@0 | 93 | 0, |
michael@0 | 94 | -1)) { |
michael@0 | 95 | type.AssignLiteral("image/png"); |
michael@0 | 96 | SetContentType(type); |
michael@0 | 97 | captureParams.captureAudio = false; |
michael@0 | 98 | captureParams.captureVideo = true; |
michael@0 | 99 | captureParams.timeLimit = 0; |
michael@0 | 100 | captureParams.frameLimit = 1; |
michael@0 | 101 | nsAutoCString buffer; |
michael@0 | 102 | extractAttributeValue(spec.get(), "width=", buffer); |
michael@0 | 103 | nsresult err; |
michael@0 | 104 | captureParams.width = buffer.ToInteger(&err); |
michael@0 | 105 | if (!captureParams.width) |
michael@0 | 106 | captureParams.width = 640; |
michael@0 | 107 | extractAttributeValue(spec.get(), "height=", buffer); |
michael@0 | 108 | captureParams.height = buffer.ToInteger(&err); |
michael@0 | 109 | if (!captureParams.height) |
michael@0 | 110 | captureParams.height = 480; |
michael@0 | 111 | extractAttributeValue(spec.get(), "camera=", buffer); |
michael@0 | 112 | captureParams.camera = buffer.ToInteger(&err); |
michael@0 | 113 | captureParams.bpp = 32; |
michael@0 | 114 | #ifdef MOZ_WIDGET_ANDROID |
michael@0 | 115 | capture = GetAndroidCaptureProvider(); |
michael@0 | 116 | #endif |
michael@0 | 117 | } else if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=video/x-raw-yuv"), |
michael@0 | 118 | true, |
michael@0 | 119 | 0, |
michael@0 | 120 | -1)) { |
michael@0 | 121 | type.AssignLiteral("video/x-raw-yuv"); |
michael@0 | 122 | SetContentType(type); |
michael@0 | 123 | captureParams.captureAudio = false; |
michael@0 | 124 | captureParams.captureVideo = true; |
michael@0 | 125 | nsAutoCString buffer; |
michael@0 | 126 | extractAttributeValue(spec.get(), "width=", buffer); |
michael@0 | 127 | nsresult err; |
michael@0 | 128 | captureParams.width = buffer.ToInteger(&err); |
michael@0 | 129 | if (!captureParams.width) |
michael@0 | 130 | captureParams.width = 640; |
michael@0 | 131 | extractAttributeValue(spec.get(), "height=", buffer); |
michael@0 | 132 | captureParams.height = buffer.ToInteger(&err); |
michael@0 | 133 | if (!captureParams.height) |
michael@0 | 134 | captureParams.height = 480; |
michael@0 | 135 | extractAttributeValue(spec.get(), "camera=", buffer); |
michael@0 | 136 | captureParams.camera = buffer.ToInteger(&err); |
michael@0 | 137 | captureParams.bpp = 32; |
michael@0 | 138 | captureParams.timeLimit = 0; |
michael@0 | 139 | captureParams.frameLimit = 60000; |
michael@0 | 140 | #ifdef MOZ_WIDGET_ANDROID |
michael@0 | 141 | // only enable if "device.camera.enabled" is true. |
michael@0 | 142 | if (Preferences::GetBool("device.camera.enabled", false) == true) |
michael@0 | 143 | capture = GetAndroidCaptureProvider(); |
michael@0 | 144 | #endif |
michael@0 | 145 | } else { |
michael@0 | 146 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | if (!capture) |
michael@0 | 150 | return NS_ERROR_FAILURE; |
michael@0 | 151 | |
michael@0 | 152 | return capture->Init(type, &captureParams, aStream); |
michael@0 | 153 | } |