Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / ppapi / thunk / ppb_audio_input_thunk.cc
blob94d978df98ee2e476fbb77bd9ec5b5924791977e
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.
5 #include "ppapi/c/pp_errors.h"
6 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
7 #include "ppapi/shared_impl/tracked_callback.h"
8 #include "ppapi/thunk/enter.h"
9 #include "ppapi/thunk/ppb_device_ref_api.h"
10 #include "ppapi/thunk/ppb_audio_input_api.h"
11 #include "ppapi/thunk/resource_creation_api.h"
12 #include "ppapi/thunk/thunk.h"
14 namespace ppapi {
15 namespace thunk {
17 namespace {
19 typedef EnterResource<PPB_AudioInput_API> EnterAudioInput;
21 PP_Resource Create(PP_Instance instance) {
22 EnterResourceCreation enter(instance);
23 if (enter.failed())
24 return 0;
26 return enter.functions()->CreateAudioInput(instance);
29 PP_Bool IsAudioInput(PP_Resource resource) {
30 EnterAudioInput enter(resource, false);
31 return PP_FromBool(enter.succeeded());
34 int32_t EnumerateDevices(PP_Resource audio_input,
35 PP_Resource* devices,
36 PP_CompletionCallback callback) {
37 EnterAudioInput enter(audio_input, callback, true);
38 if (enter.failed())
39 return enter.retval();
41 return enter.SetResult(enter.object()->EnumerateDevices(devices,
42 enter.callback()));
45 int32_t Open(PP_Resource audio_input,
46 PP_Resource device_ref,
47 PP_Resource config,
48 PPB_AudioInput_Callback audio_input_callback,
49 void* user_data,
50 PP_CompletionCallback callback) {
51 EnterAudioInput enter(audio_input, callback, true);
52 if (enter.failed())
53 return enter.retval();
55 std::string device_id;
56 // |device_id| remains empty if |device_ref| is 0, which means the default
57 // device.
58 if (device_ref != 0) {
59 EnterResourceNoLock<PPB_DeviceRef_API> enter_device_ref(device_ref, true);
60 if (enter_device_ref.failed())
61 return enter.SetResult(PP_ERROR_BADRESOURCE);
62 device_id = enter_device_ref.object()->GetDeviceRefData().id;
65 return enter.SetResult(enter.object()->Open(
66 device_id, config, audio_input_callback, user_data, enter.callback()));
69 PP_Resource GetCurrentConfig(PP_Resource audio_input) {
70 EnterAudioInput enter(audio_input, true);
71 if (enter.failed())
72 return 0;
73 return enter.object()->GetCurrentConfig();
76 PP_Bool StartCapture(PP_Resource audio_input) {
77 EnterAudioInput enter(audio_input, true);
78 if (enter.failed())
79 return PP_FALSE;
81 return enter.object()->StartCapture();
84 PP_Bool StopCapture(PP_Resource audio_input) {
85 EnterAudioInput enter(audio_input, true);
86 if (enter.failed())
87 return PP_FALSE;
89 return enter.object()->StopCapture();
92 void Close(PP_Resource audio_input) {
93 EnterAudioInput enter(audio_input, true);
94 if (enter.succeeded())
95 enter.object()->Close();
98 const PPB_AudioInput_Dev_0_2 g_ppb_audioinput_0_2_thunk = {
99 &Create,
100 &IsAudioInput,
101 &EnumerateDevices,
102 &Open,
103 &GetCurrentConfig,
104 &StartCapture,
105 &StopCapture,
106 &Close
109 } // namespace
111 const PPB_AudioInput_Dev_0_2* GetPPB_AudioInput_Dev_0_2_Thunk() {
112 return &g_ppb_audioinput_0_2_thunk;
115 } // namespace thunk
116 } // namespace ppapi