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