summaryrefslogtreecommitdiffstats
path: root/ppapi/thunk/ppb_audio_input_thunk.cc
blob: 5fa512ee3b6784e9f4103fd739e1baf08f547326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/c/pp_errors.h"
#include "ppapi/shared_impl/ppb_device_ref_shared.h"
#include "ppapi/shared_impl/tracked_callback.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_device_ref_api.h"
#include "ppapi/thunk/ppb_audio_input_api.h"
#include "ppapi/thunk/resource_creation_api.h"
#include "ppapi/thunk/thunk.h"

namespace ppapi {
namespace thunk {

namespace {

typedef EnterResource<PPB_AudioInput_API> EnterAudioInput;

PP_Resource Create(PP_Instance instance) {
  EnterResourceCreation enter(instance);
  if (enter.failed())
    return 0;

  return enter.functions()->CreateAudioInput(instance);
}

PP_Bool IsAudioInput(PP_Resource resource) {
  EnterAudioInput enter(resource, false);
  return PP_FromBool(enter.succeeded());
}

int32_t EnumerateDevices0_2(PP_Resource audio_input,
                            PP_Resource* devices,
                            PP_CompletionCallback callback) {
  EnterAudioInput enter(audio_input, callback, true);
  if (enter.failed())
    return enter.retval();

  return enter.SetResult(enter.object()->EnumerateDevices0_2(devices,
                                                             enter.callback()));
}

int32_t EnumerateDevices(PP_Resource audio_input,
                         PP_ArrayOutput output,
                         PP_CompletionCallback callback) {
  EnterAudioInput enter(audio_input, callback, true);
  if (enter.failed())
    return enter.retval();

  return enter.SetResult(enter.object()->EnumerateDevices(output,
                                                          enter.callback()));
}

int32_t MonitorDeviceChange(PP_Resource audio_input,
                            PP_MonitorDeviceChangeCallback callback,
                            void* user_data) {
  EnterAudioInput enter(audio_input, true);
  if (enter.failed())
    return enter.retval();
  return enter.object()->MonitorDeviceChange(callback, user_data);
}

int32_t Open(PP_Resource audio_input,
             PP_Resource device_ref,
             PP_Resource config,
             PPB_AudioInput_Callback audio_input_callback,
             void* user_data,
             PP_CompletionCallback callback) {
  EnterAudioInput enter(audio_input, callback, true);
  if (enter.failed())
    return enter.retval();

  std::string device_id;
  // |device_id| remains empty if |device_ref| is 0, which means the default
  // device.
  if (device_ref != 0) {
    EnterResourceNoLock<PPB_DeviceRef_API> enter_device_ref(device_ref, true);
    if (enter_device_ref.failed())
      return enter.SetResult(PP_ERROR_BADRESOURCE);
    device_id = enter_device_ref.object()->GetDeviceRefData().id;
  }

  return enter.SetResult(enter.object()->Open(
      device_id, config, audio_input_callback, user_data, enter.callback()));
}

PP_Resource GetCurrentConfig(PP_Resource audio_input) {
  EnterAudioInput enter(audio_input, true);
  if (enter.failed())
    return 0;
  return enter.object()->GetCurrentConfig();
}

PP_Bool StartCapture(PP_Resource audio_input) {
  EnterAudioInput enter(audio_input, true);
  if (enter.failed())
    return PP_FALSE;

  return enter.object()->StartCapture();
}

PP_Bool StopCapture(PP_Resource audio_input) {
  EnterAudioInput enter(audio_input, true);
  if (enter.failed())
    return PP_FALSE;

  return enter.object()->StopCapture();
}

void Close(PP_Resource audio_input) {
  EnterAudioInput enter(audio_input, true);
  if (enter.succeeded())
    enter.object()->Close();
}

const PPB_AudioInput_Dev_0_2 g_ppb_audioinput_0_2_thunk = {
  &Create,
  &IsAudioInput,
  &EnumerateDevices0_2,
  &Open,
  &GetCurrentConfig,
  &StartCapture,
  &StopCapture,
  &Close
};

const PPB_AudioInput_Dev_0_3 g_ppb_audioinput_0_3_thunk = {
  &Create,
  &IsAudioInput,
  &EnumerateDevices,
  &MonitorDeviceChange,
  &Open,
  &GetCurrentConfig,
  &StartCapture,
  &StopCapture,
  &Close
};

}  // namespace

const PPB_AudioInput_Dev_0_2* GetPPB_AudioInput_Dev_0_2_Thunk() {
  return &g_ppb_audioinput_0_2_thunk;
}

const PPB_AudioInput_Dev_0_3* GetPPB_AudioInput_Dev_0_3_Thunk() {
  return &g_ppb_audioinput_0_3_thunk;
}

}  // namespace thunk
}  // namespace ppapi