"Could not open audio device for playback" error if creating/destructing pipeline from multiple threads

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

"Could not open audio device for playback" error if creating/destructing pipeline from multiple threads

segr
Hi, All,

I was testing gstreamer for using it from multiple threads.
I make the following:
  1. gst_init;
  2. start main_loop;
  3. start while(1) calling thread-function that constructs pipeline, starts playing and stop it immediately.

In some cases (I suppose it is any type of race condition), I get the following error:

4: Could not open audio device for playback. Device is being used by another application.

Have anybody the same problem?
I tested both on lenny and squeeze (but haven`t tested sid yet) the problem appear from time to time on both versions.

PS: To reproduce the problem I always start testing application for a few times before getting an error.
Reply | Threaded
Open this post in threaded view
|

Re: "Could not open audio device for playback" error if creating/destructing pipeline from multiple threads

segr
The test program:

<code>
#include "gst/gst.h"
#include <iostream>
#include <string.h>
#include <sys/time.h>
#include <sstream>
#include <gst/interfaces/xoverlay.h>

#include "assert.h"
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data);

static void *create_pipe (void *el)
{
  GError *err = 0;
//  GstElement *pipe = (GstElement *)el;
  pipe = gst_parse_launch("filesrc location=\"/mnt/media/1/Sting - Collection/Sting - Collection - 10.wav\" ! decodebin ! volume volume=10 ! audioconvert ! alsasink", &err);
  gst_object_ref(pipe);
   
  GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipe));

  int wid = gst_bus_add_watch(bus, bus_call, 0);
  gst_object_unref(bus);

  gst_element_set_state(pipe, GST_STATE_PLAYING);
  gst_element_get_state(pipe, 0, 0, GST_CLOCK_TIME_NONE);


  static int iter = 0;
  std::cout << iter++ << std::endl;

  g_source_remove(wid);
  gst_element_set_state(pipe, GST_STATE_NULL);

  gst_object_unref(pipe);
  pipe = 0;
}

static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{

  switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_EOS:
    {
      std::cout << "EOS" << std::endl;
      break;
    }
    case GST_MESSAGE_ERROR:
    {
      gchar *debug;
      GError *err;

      gst_message_parse_error (msg, &err, &debug);
      g_free (debug);

      std::cout << "Error occured in gstreamer: " << err->code << ": " << err->message << std::endl;
      g_error_free (err);
      assert(0);
      return false;
    }
    case GST_MESSAGE_BUFFERING:
    {
      gint percent;

      gst_message_parse_buffering (msg, &percent);
      std::cout << "Buffering percent is: " << percent << std::endl;
    }
    case GST_MESSAGE_WARNING: {
      gchar *debug;
      GError *err;

      gst_message_parse_warning (msg, &err, &debug);
      g_free (debug);

      std::cout << "Warning occured in gstreamer: " << err->code << ": " << err->message << std::endl;
      g_error_free (err);
      break;
    }
    case GST_MESSAGE_TAG: {
      GstTagList *tag_list, *result;

      gst_message_parse_tag (msg, &tag_list);
    }
    case GST_MESSAGE_STATE_CHANGED:{
      break;
    }
    default:
      break;
  }
  return TRUE;
}

void *start_loop(void *data)
{
  GMainLoop *loop = g_main_loop_new(0, false);
  g_main_loop_run(loop);

}

int main(int argc, char *argv[])
{
 

 
  std::cout << "Hello World!" << std::endl;
  g_thread_init(0);
  gst_init(0, 0);
 
  GThread *loop = g_thread_create(start_loop, 0, false, 0);
  GstElement *pipe = 0;
  GError *err = 0;
  float sl_t = 0;
  while(1) {
    g_thread_create(create_pipe, pipe, false, 0);

    static int qw = 0;
    std::cout << qw++ << std::endl;

    if(sl_t > 1) sl_t = 0;
    sleep(sl_t);
    sl_t +=0.05;
  }
  sleep(10);
}

</code>
Reply | Threaded
Open this post in threaded view
|

Re: "Could not open audio device for playback" error if creating/destructing pipeline from multiple threads

segr
In reply to this post by segr
I suppose that some thing like the following occurs:

1. When starting multiple streams, multiple requests to device are sent;
2. When two requests (for example to close and to open device) are executed in the same time the first locks the device than the second is trying to lock and fails, because the device is locked.

Can it be error on ALSA layer???


segr wrote
Hi, All,

I was testing gstreamer for using it from multiple threads.
I make the following:
  1. gst_init;
  2. start main_loop;
  3. start while(1) calling thread-function that constructs pipeline, starts playing and stop it immediately.

In some cases (I suppose it is any type of race condition), I get the following error:

4: Could not open audio device for playback. Device is being used by another application.

Have anybody the same problem?
I tested both on lenny and squeeze (but haven`t tested sid yet) the problem appear from time to time on both versions.

PS: To reproduce the problem I always start testing application for a few times before getting an error.
Reply | Threaded
Open this post in threaded view
|

Re: "Could not open audio device for playback" error if creating/destructing pipeline from multiple threads

rmkart
In reply to this post by segr
Whether you try to open audio device in 2 threads simultaneously?
If you have any audio player application that is open also can create a problem
segr wrote
Hi, All,

I was testing gstreamer for using it from multiple threads.
I make the following:
  1. gst_init;
  2. start main_loop;
  3. start while(1) calling thread-function that constructs pipeline, starts playing and stop it immediately.

In some cases (I suppose it is any type of race condition), I get the following error:

4: Could not open audio device for playback. Device is being used by another application.

Have anybody the same problem?
I tested both on lenny and squeeze (but haven`t tested sid yet) the problem appear from time to time on both versions.

PS: To reproduce the problem I always start testing application for a few times before getting an error.
Reply | Threaded
Open this post in threaded view
|

Re: "Could not open audio device for playback" error if creating/destructing pipeline from multiple threads

segr
Thanks for your reply.

rmkart wrote
Whether you try to open audio device in 2 threads simultaneously?
I suppose, that yes... I`m creating pipeline in multiple threads, though it is possible that they use device concurrently.

rmkart wrote
If you have any audio player application that is open also can create a problem
I think that most of players have no need to work in multiple threads with gstreamer, because the usual scenario is to start one media after another.

I`m working on application that include different functionality: Video playback, audio playback and sip-based phone, though there are some scenarios(for example incoming call during video playback) that need creation of few pipelines from different threads. Because of that I tested these things and get the following error. The test program I posted earlier, is just to reproduce the problem.