nsDeviceChannel.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "plstr.h"
  6. #include "nsDeviceChannel.h"
  7. #include "nsDeviceCaptureProvider.h"
  8. using namespace mozilla;
  9. // Copied from image/decoders/icon/nsIconURI.cpp
  10. // takes a string like ?size=32&contentType=text/html and returns a new string
  11. // containing just the attribute values. i.e you could pass in this string with
  12. // an attribute name of "size=", this will return 32
  13. // Assumption: attribute pairs are separated by &
  14. void extractAttributeValue(const char* searchString, const char* attributeName, nsCString& result)
  15. {
  16. result.Truncate();
  17. if (!searchString || !attributeName)
  18. return;
  19. uint32_t attributeNameSize = strlen(attributeName);
  20. const char *startOfAttribute = PL_strcasestr(searchString, attributeName);
  21. if (!startOfAttribute ||
  22. !( *(startOfAttribute-1) == '?' || *(startOfAttribute-1) == '&') )
  23. return;
  24. startOfAttribute += attributeNameSize; // Skip the attributeName
  25. if (!*startOfAttribute)
  26. return;
  27. const char *endOfAttribute = strchr(startOfAttribute, '&');
  28. if (endOfAttribute) {
  29. result.Assign(Substring(startOfAttribute, endOfAttribute));
  30. } else {
  31. result.Assign(startOfAttribute);
  32. }
  33. }
  34. NS_IMPL_ISUPPORTS_INHERITED(nsDeviceChannel,
  35. nsBaseChannel,
  36. nsIChannel)
  37. // nsDeviceChannel methods
  38. nsDeviceChannel::nsDeviceChannel()
  39. {
  40. SetContentType(NS_LITERAL_CSTRING("image/png"));
  41. }
  42. nsDeviceChannel::~nsDeviceChannel()
  43. {
  44. }
  45. nsresult
  46. nsDeviceChannel::Init(nsIURI* aUri)
  47. {
  48. nsBaseChannel::Init();
  49. nsBaseChannel::SetURI(aUri);
  50. return NS_OK;
  51. }
  52. nsresult
  53. nsDeviceChannel::OpenContentStream(bool aAsync,
  54. nsIInputStream** aStream,
  55. nsIChannel** aChannel)
  56. {
  57. if (!aAsync)
  58. return NS_ERROR_NOT_IMPLEMENTED;
  59. nsCOMPtr<nsIURI> uri = nsBaseChannel::URI();
  60. *aStream = nullptr;
  61. *aChannel = nullptr;
  62. NS_NAMED_LITERAL_CSTRING(width, "width=");
  63. NS_NAMED_LITERAL_CSTRING(height, "height=");
  64. nsAutoCString spec;
  65. nsresult rv = uri->GetSpec(spec);
  66. NS_ENSURE_SUCCESS(rv, rv);
  67. nsAutoCString type;
  68. RefPtr<nsDeviceCaptureProvider> capture;
  69. nsCaptureParams captureParams;
  70. captureParams.camera = 0;
  71. if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=image/png"),
  72. true,
  73. 0,
  74. -1)) {
  75. type.AssignLiteral("image/png");
  76. SetContentType(type);
  77. captureParams.captureAudio = false;
  78. captureParams.captureVideo = true;
  79. captureParams.timeLimit = 0;
  80. captureParams.frameLimit = 1;
  81. nsAutoCString buffer;
  82. extractAttributeValue(spec.get(), "width=", buffer);
  83. nsresult err;
  84. captureParams.width = buffer.ToInteger(&err);
  85. if (!captureParams.width)
  86. captureParams.width = 640;
  87. extractAttributeValue(spec.get(), "height=", buffer);
  88. captureParams.height = buffer.ToInteger(&err);
  89. if (!captureParams.height)
  90. captureParams.height = 480;
  91. extractAttributeValue(spec.get(), "camera=", buffer);
  92. captureParams.camera = buffer.ToInteger(&err);
  93. captureParams.bpp = 32;
  94. } else if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=video/x-raw-yuv"),
  95. true,
  96. 0,
  97. -1)) {
  98. type.AssignLiteral("video/x-raw-yuv");
  99. SetContentType(type);
  100. captureParams.captureAudio = false;
  101. captureParams.captureVideo = true;
  102. nsAutoCString buffer;
  103. extractAttributeValue(spec.get(), "width=", buffer);
  104. nsresult err;
  105. captureParams.width = buffer.ToInteger(&err);
  106. if (!captureParams.width)
  107. captureParams.width = 640;
  108. extractAttributeValue(spec.get(), "height=", buffer);
  109. captureParams.height = buffer.ToInteger(&err);
  110. if (!captureParams.height)
  111. captureParams.height = 480;
  112. extractAttributeValue(spec.get(), "camera=", buffer);
  113. captureParams.camera = buffer.ToInteger(&err);
  114. captureParams.bpp = 32;
  115. captureParams.timeLimit = 0;
  116. captureParams.frameLimit = 60000;
  117. } else {
  118. return NS_ERROR_NOT_IMPLEMENTED;
  119. }
  120. if (!capture)
  121. return NS_ERROR_FAILURE;
  122. return capture->Init(type, &captureParams, aStream);
  123. }