Home | History | Annotate | Download | only in audio
      1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/audio/audio_api.h"
      6 
      7 #include "base/lazy_instance.h"
      8 #include "base/values.h"
      9 #include "chrome/browser/extensions/event_names.h"
     10 #include "chrome/browser/extensions/event_router.h"
     11 #include "chrome/browser/extensions/extension_system.h"
     12 
     13 namespace extensions {
     14 
     15 static base::LazyInstance<ProfileKeyedAPIFactory<AudioAPI> >
     16 g_factory = LAZY_INSTANCE_INITIALIZER;
     17 
     18 // static
     19 ProfileKeyedAPIFactory<AudioAPI>* AudioAPI::GetFactoryInstance() {
     20   return &g_factory.Get();
     21 }
     22 
     23 AudioAPI::AudioAPI(Profile* profile)
     24     : profile_(profile),
     25       service_(AudioService::CreateInstance()) {
     26   service_->AddObserver(this);
     27 }
     28 
     29 AudioAPI::~AudioAPI() {
     30   service_->RemoveObserver(this);
     31   delete service_;
     32   service_ = NULL;
     33 }
     34 
     35 AudioService* AudioAPI::GetService() const {
     36   return service_;
     37 }
     38 
     39 void AudioAPI::OnDeviceChanged() {
     40   if (profile_ && ExtensionSystem::Get(profile_)->event_router()) {
     41     scoped_ptr<Event> event(new Event(
     42         event_names::kOnAudioDeviceChanged,
     43         scoped_ptr<base::ListValue>(new base::ListValue())));
     44     ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(
     45         event.Pass());
     46   }
     47 }
     48 
     49 bool AudioGetInfoFunction::RunImpl() {
     50   AudioService* service =
     51       AudioAPI::GetFactoryInstance()->GetForProfile(profile())->GetService();
     52   DCHECK(service);
     53   service->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted,
     54                                    this));
     55   return true;
     56 }
     57 
     58 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo& output_info,
     59                                               const InputInfo& input_info,
     60                                               bool success) {
     61   if (success)
     62     results_ = api::audio::GetInfo::Results::Create(output_info, input_info);
     63   else
     64     SetError("Error occurred when querying audio device information.");
     65   SendResponse(success);
     66 }
     67 
     68 bool AudioSetActiveDevicesFunction::RunImpl() {
     69   scoped_ptr<api::audio::SetActiveDevices::Params> params(
     70       api::audio::SetActiveDevices::Params::Create(*args_));
     71   EXTENSION_FUNCTION_VALIDATE(params.get());
     72 
     73   AudioService* service =
     74       AudioAPI::GetFactoryInstance()->GetForProfile(profile())->GetService();
     75   DCHECK(service);
     76 
     77   service->SetActiveDevices(params->ids);
     78   return true;
     79 }
     80 
     81 bool AudioSetPropertiesFunction::RunImpl() {
     82   scoped_ptr<api::audio::SetProperties::Params> params(
     83       api::audio::SetProperties::Params::Create(*args_));
     84   EXTENSION_FUNCTION_VALIDATE(params.get());
     85 
     86   AudioService* service =
     87       AudioAPI::GetFactoryInstance()->GetForProfile(profile())->GetService();
     88   DCHECK(service);
     89 
     90   int volume_value = params->properties.volume.get() ?
     91       *params->properties.volume : -1;
     92 
     93   int gain_value = params->properties.gain.get() ?
     94       *params->properties.gain : -1;
     95 
     96   if (!service->SetDeviceProperties(params->id,
     97                                     params->properties.is_muted,
     98                                     volume_value,
     99                                     gain_value))
    100     return false;
    101   else
    102     return true;
    103 }
    104 
    105 }  // namespace extensions
    106