1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <assert.h> 6 #include <string.h> 7 8 #include <vector> 9 10 #include "ppapi/c/dev/ppb_video_capture_dev.h" 11 #include "ppapi/c/pp_errors.h" 12 #include "ppapi/cpp/dev/device_ref_dev.h" 13 #include "ppapi/cpp/dev/video_capture_dev.h" 14 #include "ppapi/cpp/dev/video_capture_client_dev.h" 15 #include "ppapi/cpp/completion_callback.h" 16 #include "ppapi/cpp/instance.h" 17 #include "ppapi/cpp/module.h" 18 #include "ppapi/cpp/private/flash.h" 19 #include "ppapi/cpp/var.h" 20 #include "ppapi/utility/completion_callback_factory.h" 21 22 // When compiling natively on Windows, PostMessage can be #define-d to 23 // something else. 24 #ifdef PostMessage 25 #undef PostMessage 26 #endif 27 28 namespace { 29 30 // This object is the global object representing this plugin library as long 31 // as it is loaded. 32 class EnumerateDevicesDemoModule : public pp::Module { 33 public: 34 EnumerateDevicesDemoModule() : pp::Module() {} 35 virtual ~EnumerateDevicesDemoModule() {} 36 virtual pp::Instance* CreateInstance(PP_Instance instance); 37 }; 38 39 class EnumerateDevicesDemoInstance : public pp::Instance, 40 public pp::VideoCaptureClient_Dev { 41 public: 42 EnumerateDevicesDemoInstance(PP_Instance instance, pp::Module* module); 43 virtual ~EnumerateDevicesDemoInstance(); 44 45 // pp::Instance implementation (see PPP_Instance). 46 virtual void HandleMessage(const pp::Var& message_data); 47 48 // pp::VideoCaptureClient_Dev implementation. 49 virtual void OnDeviceInfo(PP_Resource resource, 50 const PP_VideoCaptureDeviceInfo_Dev& info, 51 const std::vector<pp::Buffer_Dev>& buffers) {} 52 virtual void OnStatus(PP_Resource resource, uint32_t status) {} 53 virtual void OnError(PP_Resource resource, uint32_t error) {} 54 virtual void OnBufferReady(PP_Resource resource, uint32_t buffer) {} 55 56 private: 57 void EnumerateDevicesFinished(int32_t result, 58 std::vector<pp::DeviceRef_Dev>& devices); 59 60 pp::VideoCapture_Dev video_capture_; 61 pp::CompletionCallbackFactory<EnumerateDevicesDemoInstance> callback_factory_; 62 63 std::vector<pp::DeviceRef_Dev> devices_; 64 }; 65 66 EnumerateDevicesDemoInstance::EnumerateDevicesDemoInstance(PP_Instance instance, 67 pp::Module* module) 68 : pp::Instance(instance), 69 pp::VideoCaptureClient_Dev(this), 70 video_capture_(this), 71 callback_factory_(this) { 72 } 73 74 EnumerateDevicesDemoInstance::~EnumerateDevicesDemoInstance() { 75 } 76 77 void EnumerateDevicesDemoInstance::HandleMessage(const pp::Var& message_data) { 78 if (message_data.is_string()) { 79 std::string event = message_data.AsString(); 80 if (event == "EnumerateDevicesAsync") { 81 pp::CompletionCallbackWithOutput<std::vector<pp::DeviceRef_Dev> > 82 callback = callback_factory_.NewCallbackWithOutput( 83 &EnumerateDevicesDemoInstance::EnumerateDevicesFinished); 84 video_capture_.EnumerateDevices(callback); 85 } else if (event == "EnumerateDevicesSync") { 86 std::vector<pp::DeviceRef_Dev> devices; 87 int32_t result = pp::flash::Flash::EnumerateVideoCaptureDevices( 88 this, video_capture_, &devices); 89 EnumerateDevicesFinished(result, devices); 90 } 91 } 92 } 93 94 void EnumerateDevicesDemoInstance::EnumerateDevicesFinished( 95 int32_t result, 96 std::vector<pp::DeviceRef_Dev>& devices) { 97 static const char* const kDelimiter = "#__#"; 98 99 if (result == PP_OK) { 100 devices_.swap(devices); 101 std::string device_names; 102 for (size_t index = 0; index < devices_.size(); ++index) { 103 pp::Var name = devices_[index].GetName(); 104 assert(name.is_string()); 105 106 if (index != 0) 107 device_names += kDelimiter; 108 device_names += name.AsString(); 109 } 110 PostMessage(pp::Var("EnumerationSuccess" + device_names)); 111 } else { 112 PostMessage(pp::Var("EnumerationFailed")); 113 } 114 } 115 116 pp::Instance* EnumerateDevicesDemoModule::CreateInstance(PP_Instance instance) { 117 return new EnumerateDevicesDemoInstance(instance, this); 118 } 119 120 } // anonymous namespace 121 122 namespace pp { 123 // Factory function for your specialization of the Module object. 124 Module* CreateModule() { 125 return new EnumerateDevicesDemoModule(); 126 } 127 } // namespace pp 128