Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / media / audio / pulse / pulse_util.cc
index 96831cf..1cfb038 100644 (file)
@@ -2,6 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#if defined(OS_TIZEN)
+#include "media/audio/pulse/pulse_output.h"
+#endif
+
 #include "media/audio/pulse/pulse_util.h"
 
 #include "base/logging.h"
@@ -41,8 +45,6 @@ pa_channel_position ChromiumToPAChannelPosition(Channels channel) {
       return PA_CHANNEL_POSITION_SIDE_LEFT;
     case SIDE_RIGHT:
       return PA_CHANNEL_POSITION_SIDE_RIGHT;
-    case CHANNELS_MAX:
-      return PA_CHANNEL_POSITION_INVALID;
     default:
       NOTREACHED() << "Invalid channel: " << channel;
       return PA_CHANNEL_POSITION_INVALID;
@@ -86,7 +88,7 @@ pa_channel_map ChannelLayoutToPAChannelMap(ChannelLayout channel_layout) {
   pa_channel_map_init(&channel_map);
 
   channel_map.channels = ChannelLayoutToChannelCount(channel_layout);
-  for (Channels ch = LEFT; ch < CHANNELS_MAX;
+  for (Channels ch = LEFT; ch <= CHANNELS_MAX;
        ch = static_cast<Channels>(ch + 1)) {
     int channel_index = ChannelOrder(channel_layout, ch);
     if (channel_index < 0)
@@ -205,6 +207,7 @@ bool CreateOutputStream(pa_threaded_mainloop** mainloop,
                         pa_context** context,
                         pa_stream** stream,
                         const AudioParameters& params,
+                        const std::string& device_id,
                         pa_stream_notify_cb_t stream_callback,
                         pa_stream_request_cb_t write_callback,
                         void* user_data) {
@@ -260,7 +263,20 @@ bool CreateOutputStream(pa_threaded_mainloop** mainloop,
     // than the default channel map (NULL).
     map = &source_channel_map;
   }
+
+#if defined(OS_TIZEN)
+  PulseAudioOutputStream* data =
+      static_cast<PulseAudioOutputStream*>(user_data);
+  pa_proplist* proplist = pa_proplist_new();
+  pa_proplist_sets(proplist, "resource.set.name", data->app_id().c_str());
+  pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, data->app_class().c_str());
+  *stream = pa_stream_new_with_proplist(*context, "Playback",
+                                        &sample_specifications,
+                                        map, proplist);
+  pa_proplist_free(proplist);
+#else
   *stream = pa_stream_new(*context, "Playback", &sample_specifications, map);
+#endif
   RETURN_ON_FAILURE(*stream, "failed to create PA playback stream");
 
   pa_stream_set_state_callback(*stream, stream_callback, user_data);
@@ -275,9 +291,13 @@ bool CreateOutputStream(pa_threaded_mainloop** mainloop,
   // |minreq| bytes.  |tlength| should be a multiple of |minreq|; too low and
   // Pulse will issue callbacks way too fast, too high and we don't get
   // callbacks frequently enough.
+  //
+  // Setting |minreq| to the exact buffer size leads to more callbacks than
+  // necessary, so we've clipped it to half the buffer size.  Regardless of the
+  // requested amount, we'll always fill |params.GetBytesPerBuffer()| though.
   pa_buffer_attr pa_buffer_attributes;
   pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1);
-  pa_buffer_attributes.minreq = params.GetBytesPerBuffer();
+  pa_buffer_attributes.minreq = params.GetBytesPerBuffer() / 2;
   pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1);
   pa_buffer_attributes.tlength = params.GetBytesPerBuffer() * 3;
   pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1);
@@ -287,12 +307,16 @@ bool CreateOutputStream(pa_threaded_mainloop** mainloop,
   // and error.
   RETURN_ON_FAILURE(
       pa_stream_connect_playback(
-          *stream, NULL, &pa_buffer_attributes,
+          *stream,
+          device_id == AudioManagerBase::kDefaultDeviceId ?
+              NULL : device_id.c_str(),
+          &pa_buffer_attributes,
           static_cast<pa_stream_flags_t>(
               PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY |
               PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_NOT_MONOTONIC |
               PA_STREAM_START_CORKED),
-          NULL, NULL) == 0,
+          NULL,
+          NULL) == 0,
       "pa_stream_connect_playback FAILED ");
 
   // Wait for the stream to be ready.