summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_audio_config_proxy.cc
blob: 0c8eaabb9f1df308d354d5f0eba14e6c52842249 (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
153
154
155
156
157
// Copyright (c) 2011 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/proxy/ppb_audio_config_proxy.h"

#include "ppapi/c/ppb_audio_config.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace pp {
namespace proxy {

class AudioConfig : public PluginResource {
 public:
  AudioConfig(const HostResource& resource,
              PP_AudioSampleRate sample_rate,
              uint32_t sample_frame_count)
      : PluginResource(resource),
        sample_rate_(sample_rate),
        sample_frame_count_(sample_frame_count) {
  }
  virtual ~AudioConfig() {}

  // Resource overrides.
  virtual AudioConfig* AsAudioConfig() { return this; }

  PP_AudioSampleRate sample_rate() const { return sample_rate_; }
  uint32_t sample_frame_count() const { return sample_frame_count_; }

 private:
  PP_AudioSampleRate sample_rate_;
  uint32_t sample_frame_count_;

  DISALLOW_COPY_AND_ASSIGN(AudioConfig);
};

namespace {

PP_Resource CreateStereo16bit(PP_Instance instance,
                              PP_AudioSampleRate sample_rate,
                              uint32_t sample_frame_count) {
  HostResource resource;
  PluginDispatcher::GetForInstance(instance)->Send(
      new PpapiHostMsg_PPBAudioConfig_Create(
          INTERFACE_ID_PPB_AUDIO_CONFIG, instance,
          static_cast<int32_t>(sample_rate), sample_frame_count,
          &resource));
  if (resource.is_null())
    return 0;

  linked_ptr<AudioConfig> object(
      new AudioConfig(resource, sample_rate, sample_frame_count));
  return PluginResourceTracker::GetInstance()->AddResource(object);
}

uint32_t RecommendSampleFrameCount(PP_AudioSampleRate sample_rate,
                                   uint32_t requested_sample_frame_count) {
  // TODO(brettw) Currently we don't actually query to get a value from the
  // hardware, so we always return the input for in-range values.
  //
  // Danger: this code is duplicated in the audio config implementation.
  if (requested_sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
    return PP_AUDIOMINSAMPLEFRAMECOUNT;
  if (requested_sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT)
    return PP_AUDIOMAXSAMPLEFRAMECOUNT;
  return requested_sample_frame_count;
}

PP_Bool IsAudioConfig(PP_Resource resource) {
  AudioConfig* object = PluginResource::GetAs<AudioConfig>(resource);
  return BoolToPPBool(!!object);
}

PP_AudioSampleRate GetSampleRate(PP_Resource config_id) {
  AudioConfig* object = PluginResource::GetAs<AudioConfig>(config_id);
  if (!object)
    return PP_AUDIOSAMPLERATE_NONE;
  return object->sample_rate();
}

uint32_t GetSampleFrameCount(PP_Resource config_id) {
  AudioConfig* object = PluginResource::GetAs<AudioConfig>(config_id);
  if (!object)
    return 0;
  return object->sample_frame_count();
}

const PPB_AudioConfig audio_config_interface = {
  &CreateStereo16bit,
  &RecommendSampleFrameCount,
  &IsAudioConfig,
  &GetSampleRate,
  &GetSampleFrameCount
};

InterfaceProxy* CreateAudioConfigProxy(Dispatcher* dispatcher,
                                       const void* target_interface) {
  return new PPB_AudioConfig_Proxy(dispatcher, target_interface);
}

}  // namespace

PPB_AudioConfig_Proxy::PPB_AudioConfig_Proxy(Dispatcher* dispatcher,
                                             const void* target_interface)
    : InterfaceProxy(dispatcher, target_interface) {
}

PPB_AudioConfig_Proxy::~PPB_AudioConfig_Proxy() {
}

// static
const InterfaceProxy::Info* PPB_AudioConfig_Proxy::GetInfo() {
  static const Info info = {
    &audio_config_interface,
    PPB_AUDIO_CONFIG_INTERFACE,
    INTERFACE_ID_PPB_AUDIO_CONFIG,
    false,
    &CreateAudioConfigProxy,
  };
  return &info;
}

bool PPB_AudioConfig_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_AudioConfig_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioConfig_Create,
                        OnMsgCreateStereo16Bit)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudioConfig_RecommendSampleFrameCount,
                        OnMsgRecommendSampleFrameCount)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void PPB_AudioConfig_Proxy::OnMsgCreateStereo16Bit(PP_Instance instance,
                                                   int32_t sample_rate,
                                                   uint32_t sample_frame_count,
                                                   HostResource* result) {
  result->SetHostResource(instance,
      ppb_audio_config_target()->CreateStereo16Bit(
          instance, static_cast<PP_AudioSampleRate>(sample_rate),
          sample_frame_count));
}

void PPB_AudioConfig_Proxy::OnMsgRecommendSampleFrameCount(
    int32_t sample_rate,
    uint32_t requested_sample_frame_count,
    uint32_t* result) {
  *result = ppb_audio_config_target()->RecommendSampleFrameCount(
      static_cast<PP_AudioSampleRate>(sample_rate),
      requested_sample_frame_count);
}

}  // namespace proxy
}  // namespace pp