1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Camera.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Corporation
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Brad Lassey <blassey@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "plstr.h"
39 : #include "nsComponentManagerUtils.h"
40 : #include "nsDeviceChannel.h"
41 : #include "nsDeviceCaptureProvider.h"
42 : #include "mozilla/Preferences.h"
43 :
44 : #ifdef MOZ_WIDGET_ANDROID
45 : #include "AndroidCaptureProvider.h"
46 : #endif
47 :
48 : #ifdef MOZ_WIDGET_GONK
49 : #include "GonkCaptureProvider.h"
50 : #endif
51 :
52 : using namespace mozilla;
53 :
54 : // Copied from image/decoders/icon/nsIconURI.cpp
55 : // takes a string like ?size=32&contentType=text/html and returns a new string
56 : // containing just the attribute values. i.e you could pass in this string with
57 : // an attribute name of "size=", this will return 32
58 : // Assumption: attribute pairs are separated by &
59 0 : void extractAttributeValue(const char* searchString, const char* attributeName, nsCString& result)
60 : {
61 0 : result.Truncate();
62 :
63 0 : if (!searchString || !attributeName)
64 0 : return;
65 :
66 0 : PRUint32 attributeNameSize = strlen(attributeName);
67 0 : const char *startOfAttribute = PL_strcasestr(searchString, attributeName);
68 0 : if (!startOfAttribute ||
69 0 : !( *(startOfAttribute-1) == '?' || *(startOfAttribute-1) == '&') )
70 0 : return;
71 :
72 0 : startOfAttribute += attributeNameSize; // Skip the attributeName
73 0 : if (!*startOfAttribute)
74 0 : return;
75 :
76 0 : const char *endOfAttribute = strchr(startOfAttribute, '&');
77 0 : if (endOfAttribute) {
78 0 : result.Assign(Substring(startOfAttribute, endOfAttribute));
79 : } else {
80 0 : result.Assign(startOfAttribute);
81 : }
82 : }
83 :
84 0 : NS_IMPL_ISUPPORTS_INHERITED1(nsDeviceChannel,
85 : nsBaseChannel,
86 : nsIChannel)
87 :
88 : // nsDeviceChannel methods
89 0 : nsDeviceChannel::nsDeviceChannel()
90 : {
91 0 : SetContentType(NS_LITERAL_CSTRING("image/png"));
92 0 : }
93 :
94 0 : nsDeviceChannel::~nsDeviceChannel()
95 : {
96 0 : }
97 :
98 : nsresult
99 0 : nsDeviceChannel::Init(nsIURI* aUri)
100 : {
101 0 : nsBaseChannel::Init();
102 0 : nsBaseChannel::SetURI(aUri);
103 0 : return NS_OK;
104 : }
105 :
106 : nsresult
107 0 : nsDeviceChannel::OpenContentStream(bool aAsync,
108 : nsIInputStream** aStream,
109 : nsIChannel** aChannel)
110 : {
111 0 : if (!aAsync)
112 0 : return NS_ERROR_NOT_IMPLEMENTED;
113 :
114 0 : nsCOMPtr<nsIURI> uri = nsBaseChannel::URI();
115 0 : *aStream = nsnull;
116 0 : *aChannel = nsnull;
117 0 : NS_NAMED_LITERAL_CSTRING(width, "width=");
118 0 : NS_NAMED_LITERAL_CSTRING(height, "height=");
119 :
120 0 : nsCAutoString spec;
121 0 : uri->GetSpec(spec);
122 :
123 0 : nsCAutoString type;
124 :
125 0 : nsRefPtr<nsDeviceCaptureProvider> capture;
126 : nsCaptureParams captureParams;
127 0 : captureParams.camera = 0;
128 0 : if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=image/png"),
129 : true,
130 : 0,
131 0 : -1)) {
132 0 : type.AssignLiteral("image/png");
133 0 : SetContentType(type);
134 0 : captureParams.captureAudio = false;
135 0 : captureParams.captureVideo = true;
136 0 : captureParams.timeLimit = 0;
137 0 : captureParams.frameLimit = 1;
138 0 : nsCAutoString buffer;
139 0 : extractAttributeValue(spec.get(), "width=", buffer);
140 : nsresult err;
141 0 : captureParams.width = buffer.ToInteger(&err);
142 0 : if (!captureParams.width)
143 0 : captureParams.width = 640;
144 0 : extractAttributeValue(spec.get(), "height=", buffer);
145 0 : captureParams.height = buffer.ToInteger(&err);
146 0 : if (!captureParams.height)
147 0 : captureParams.height = 480;
148 0 : extractAttributeValue(spec.get(), "camera=", buffer);
149 0 : captureParams.camera = buffer.ToInteger(&err);
150 0 : captureParams.bpp = 32;
151 : #ifdef MOZ_WIDGET_ANDROID
152 : capture = GetAndroidCaptureProvider();
153 : #endif
154 : #ifdef MOZ_WIDGET_GONK
155 : capture = GetGonkCaptureProvider();
156 : #endif
157 0 : } else if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=video/x-raw-yuv"),
158 : true,
159 : 0,
160 0 : -1)) {
161 0 : type.AssignLiteral("video/x-raw-yuv");
162 0 : SetContentType(type);
163 0 : captureParams.captureAudio = false;
164 0 : captureParams.captureVideo = true;
165 0 : nsCAutoString buffer;
166 0 : extractAttributeValue(spec.get(), "width=", buffer);
167 : nsresult err;
168 0 : captureParams.width = buffer.ToInteger(&err);
169 0 : if (!captureParams.width)
170 0 : captureParams.width = 640;
171 0 : extractAttributeValue(spec.get(), "height=", buffer);
172 0 : captureParams.height = buffer.ToInteger(&err);
173 0 : if (!captureParams.height)
174 0 : captureParams.height = 480;
175 0 : extractAttributeValue(spec.get(), "camera=", buffer);
176 0 : captureParams.camera = buffer.ToInteger(&err);
177 0 : captureParams.bpp = 32;
178 0 : captureParams.timeLimit = 0;
179 0 : captureParams.frameLimit = 60000;
180 : #ifdef MOZ_WIDGET_ANDROID
181 : // only enable if "device.camera.enabled" is true.
182 : if (Preferences::GetBool("device.camera.enabled", false) == true)
183 : capture = GetAndroidCaptureProvider();
184 : #endif
185 : #ifdef MOZ_WIDGET_GONK
186 : if (Preferences::GetBool("device.camera.enabled", false) == true)
187 : capture = GetGonkCaptureProvider();
188 : #endif
189 : } else {
190 0 : return NS_ERROR_NOT_IMPLEMENTED;
191 : }
192 :
193 0 : if (!capture)
194 0 : return NS_ERROR_FAILURE;
195 :
196 0 : return capture->Init(type, &captureParams, aStream);
197 : }
|