summaryrefslogtreecommitdiff
path: root/sys/applemedia-nonpublic/coremediactx.c
blob: 28902059d13bb4e0ab66e2bff9c8f029b24b56e6 (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
/*
 * Copyright (C) 2010 Ole André Vadla Ravnås <oleavr@soundrop.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "coremediactx.h"

#include <gst/gst.h>

typedef struct _GstApiProvider GstApiProvider;

typedef gpointer (*GstApiProviderObtainFunc) (GError ** error);

struct _GstApiProvider
{
  GstCoreMediaApi api;
  GstApiProviderObtainFunc obtain;
  guint offset;
};

#define API_PROVIDER(AN, a_n) \
  { GST_API_##AN, (GstApiProviderObtainFunc) gst_##a_n##_api_obtain, \
    G_STRUCT_OFFSET (GstCoreMediaCtx, a_n) }

static const GstApiProvider api_provider[] = {
  API_PROVIDER (CORE_VIDEO, cv),
  API_PROVIDER (CORE_MEDIA, cm),
#ifdef HAVE_IOS
#if 0
  API_PROVIDER (MEDIA_TOOLBOX, mt),
  API_PROVIDER (CELESTIAL, cel)
#endif
#else
  API_PROVIDER (MIO, mio),
#endif
};

G_DEFINE_TYPE (GstCoreMediaCtx, gst_core_media_ctx, G_TYPE_OBJECT);

static void
gst_core_media_ctx_init (GstCoreMediaCtx * self)
{
}

static void
gst_core_media_ctx_dispose (GObject * object)
{
  GstCoreMediaCtx *self = GST_CORE_MEDIA_CTX_CAST (object);
  guint i;

  for (i = 0; i != G_N_ELEMENTS (api_provider); i++) {
    const GstApiProvider *ap = &api_provider[i];
    gpointer *api_ptr = (gpointer *) ((guint8 *) self + ap->offset);

    if (*api_ptr != NULL) {
      g_object_unref (*api_ptr);
      *api_ptr = NULL;
    }
  }

  G_OBJECT_CLASS (gst_core_media_ctx_parent_class)->dispose (object);
}

static void
gst_core_media_ctx_class_init (GstCoreMediaCtxClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = gst_core_media_ctx_dispose;
}

GstCoreMediaCtx *
gst_core_media_ctx_new (GstCoreMediaApi required_apis, GError ** error)
{
  GstCoreMediaCtx *ctx;
  GArray *error_messages;
  guint i;

  ctx = g_object_new (GST_TYPE_CORE_MEDIA_CTX, NULL);

  error_messages = g_array_new (TRUE, FALSE, sizeof (gchar *));

  for (i = 0; i != G_N_ELEMENTS (api_provider); i++) {
    const GstApiProvider *ap = &api_provider[i];

    if ((required_apis & ap->api) != 0) {
      gpointer *api_ptr = (gpointer *) ((guint8 *) ctx + ap->offset);
      GError *tmp_error = NULL;

      *api_ptr = ap->obtain (&tmp_error);
      if (tmp_error != NULL) {
        gchar *message_copy = g_strdup (tmp_error->message);
        g_array_append_val (error_messages, message_copy);
        g_clear_error (&tmp_error);
      }
    }
  }

  if (error_messages->len != 0) {
    gchar *errors_joined;

    errors_joined = g_strjoinv ("\n\t* ", (gchar **) error_messages->data);
    g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
        "Could not obtain required API%s:%s%s",
        (error_messages->len == 1) ? "" : "s",
        (error_messages->len == 1) ? " " : "\n\t* ", errors_joined);
    g_free (errors_joined);

    g_object_unref (ctx);
    ctx = NULL;
  }

  for (i = 0; i != error_messages->len; i++)
    g_free (g_array_index (error_messages, gchar *, i));
  g_array_free (error_messages, TRUE);

  return ctx;
}