OLD | NEW |
---|---|
(Empty) | |
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 DidChangeView(const pp::Rect& position, | |
yzshen1
2012/10/10 18:17:39
No need to define it if it is empty.
raymes
2012/10/11 18:39:26
Done.
| |
47 const pp::Rect& clip_ignored) {} | |
48 virtual void HandleMessage(const pp::Var& message_data); | |
49 | |
50 // pp::VideoCaptureClient_Dev implementation. | |
51 virtual void OnDeviceInfo(PP_Resource resource, | |
52 const PP_VideoCaptureDeviceInfo_Dev& info, | |
53 const std::vector<pp::Buffer_Dev>& buffers) {} | |
54 virtual void OnStatus(PP_Resource resource, uint32_t status) {} | |
55 virtual void OnError(PP_Resource resource, uint32_t error) {} | |
56 virtual void OnBufferReady(PP_Resource resource, uint32_t buffer) {} | |
57 | |
58 private: | |
59 void EnumerateDevicesFinished(int32_t result, | |
60 std::vector<pp::DeviceRef_Dev>& devices); | |
61 | |
62 pp::VideoCapture_Dev video_capture_; | |
63 pp::CompletionCallbackFactory<EnumerateDevicesDemoInstance> callback_factory_; | |
64 | |
65 std::vector<pp::DeviceRef_Dev> devices_; | |
66 }; | |
67 | |
68 EnumerateDevicesDemoInstance::EnumerateDevicesDemoInstance(PP_Instance instance, | |
69 pp::Module* module) | |
70 : pp::Instance(instance), | |
71 pp::VideoCaptureClient_Dev(this), | |
72 video_capture_(this), | |
73 callback_factory_(this) { | |
74 } | |
75 | |
76 EnumerateDevicesDemoInstance::~EnumerateDevicesDemoInstance() { | |
77 } | |
78 | |
79 void EnumerateDevicesDemoInstance::HandleMessage(const pp::Var& message_data) { | |
80 if (message_data.is_string()) { | |
81 std::string event = message_data.AsString(); | |
82 if (event == "EnumerateDevicesAsync") { | |
83 pp::CompletionCallbackWithOutput<std::vector<pp::DeviceRef_Dev> > | |
84 callback = callback_factory_.NewCallbackWithOutput( | |
85 &EnumerateDevicesDemoInstance::EnumerateDevicesFinished); | |
86 video_capture_.EnumerateDevices(callback); | |
87 } else if (event == "EnumerateDevicesSync") { | |
88 std::vector<pp::DeviceRef_Dev> devices; | |
89 int32_t result = pp::flash::Flash::EnumerateVideoDevicesSync( | |
90 this, video_capture_, &devices); | |
91 EnumerateDevicesFinished(result, devices); | |
92 } | |
93 } | |
94 } | |
95 | |
96 void EnumerateDevicesDemoInstance::EnumerateDevicesFinished( | |
97 int32_t result, | |
98 std::vector<pp::DeviceRef_Dev>& devices) { | |
99 static const char* const kDelimiter = "#__#"; | |
100 | |
101 if (result == PP_OK) { | |
102 devices_.swap(devices); | |
103 std::string device_names; | |
104 for (size_t index = 0; index < devices_.size(); ++index) { | |
105 pp::Var name = devices_[index].GetName(); | |
106 assert(name.is_string()); | |
107 | |
108 if (index != 0) | |
109 device_names += kDelimiter; | |
110 device_names += name.AsString(); | |
111 } | |
112 PostMessage(pp::Var("EnumerationSuccess" + device_names)); | |
113 } else { | |
114 PostMessage(pp::Var("EnumerationFailed")); | |
115 } | |
116 } | |
117 | |
118 pp::Instance* EnumerateDevicesDemoModule::CreateInstance(PP_Instance instance) { | |
119 return new EnumerateDevicesDemoInstance(instance, this); | |
120 } | |
121 | |
122 } // anonymous namespace | |
123 | |
124 namespace pp { | |
125 // Factory function for your specialization of the Module object. | |
126 Module* CreateModule() { | |
127 return new EnumerateDevicesDemoModule(); | |
128 } | |
129 } // namespace pp | |
OLD | NEW |