File: gstafcsrc.c

package info (click to toggle)
clementine 1.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 20,148 kB
  • sloc: cpp: 95,674; xml: 4,405; ansic: 1,429; python: 621; sql: 429; sh: 66; makefile: 23
file content (368 lines) | stat: -rw-r--r-- 10,857 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* This file is part of Clementine.
   Copyright 2010, David Sansome <me@davidsansome.com>

   Clementine is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Clementine 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "gstafcsrc.h"

#include <stdlib.h>
#include <string.h>
#include <gst/gst.h>


// Signals
enum {
  LAST_SIGNAL
};

// Properties
enum {
  PROP_0,
  PROP_LOCATION,
};

GST_DEBUG_CATEGORY_STATIC(gst_afc_src_debug);
#define GST_CAT_DEFAULT gst_afc_src_debug

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("ANY")
);

static void gst_afc_src_interface_init(GType type);
static void gst_afc_src_uri_handler_init(gpointer iface, gpointer data);
static void gst_afc_src_set_property(GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_afc_src_get_property(GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_afc_src_finalize(GObject* object);
static gboolean gst_afc_src_start(GstBaseSrc* src);
static gboolean gst_afc_src_stop(GstBaseSrc* src);
static GstFlowReturn gst_afc_src_create(GstBaseSrc* src, guint64 offset, guint length, GstBuffer** buffer);
static gboolean gst_afc_src_is_seekable(GstBaseSrc* src);
static gboolean gst_afc_src_get_size(GstBaseSrc* src, guint64* size);

GST_BOILERPLATE_FULL(GstAfcSrc, gst_afc_src, GstBaseSrc, GST_TYPE_BASE_SRC, gst_afc_src_interface_init);


static void gst_afc_src_interface_init(GType type) {
  static const GInterfaceInfo urihandler_info = {
    gst_afc_src_uri_handler_init,
    NULL,
    NULL
  };

  GST_DEBUG_CATEGORY_INIT (gst_afc_src_debug, "afcsrc", 0, "iPod/iPhone Source");

  g_type_add_interface_static(type, GST_TYPE_URI_HANDLER, &urihandler_info);
}

static void gst_afc_src_base_init(gpointer gclass) {
  GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);

  gst_element_class_set_details_simple(element_class,
    "iPod/iPhone Source",
    "Source/Afc",
    "Read using libimobiledevice",
    "David Sansome <me@davidsansome.com>");

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_factory));
}

static void gst_afc_src_class_init (GstAfcSrcClass* klass) {
  GObjectClass* gobject_class = (GObjectClass*) klass;
  GstBaseSrcClass* gstbasesrc_class = (GstBaseSrcClass*) klass;

  gobject_class->set_property = gst_afc_src_set_property;
  gobject_class->get_property = gst_afc_src_get_property;
  gobject_class->finalize = gst_afc_src_finalize;

  g_object_class_install_property(gobject_class, PROP_LOCATION,
    g_param_spec_string(
        "location", "URI",
        "The URI of the file to read, must be of the form afc://uuid/filename", NULL,
        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY));

  gstbasesrc_class->start = gst_afc_src_start;
  gstbasesrc_class->stop = gst_afc_src_stop;
  gstbasesrc_class->create = gst_afc_src_create;
  gstbasesrc_class->is_seekable = gst_afc_src_is_seekable;
  gstbasesrc_class->get_size = gst_afc_src_get_size;
}

static GstURIType gst_afc_src_uri_get_type() {
  return GST_URI_SRC;
}

static gchar** gst_afc_src_uri_get_protocols() {
  static const gchar* protocols[] = { "afc", NULL };
  return (char**) protocols;
}

static const gchar* gst_afc_src_uri_get_uri(GstURIHandler* handler) {
  GstAfcSrc* self = GST_AFCSRC(handler);
  return self->location_;
}

static gboolean gst_afc_src_uri_set_uri(GstURIHandler* handler, const gchar* uri) {
  GstAfcSrc* self = GST_AFCSRC(handler);
  self->location_ = g_strdup(uri);
  return TRUE;
}

static void gst_afc_src_uri_handler_init(gpointer g_iface, gpointer data) {
  GstURIHandlerInterface* iface = (GstURIHandlerInterface*) g_iface;

  iface->get_type = gst_afc_src_uri_get_type;
  iface->get_protocols = gst_afc_src_uri_get_protocols;
  iface->set_uri = gst_afc_src_uri_set_uri;
  iface->get_uri = gst_afc_src_uri_get_uri;
}


static void gst_afc_src_init(GstAfcSrc* element, GstAfcSrcClass* gclass) {
  element->location_ = NULL;
  element->uuid_ = NULL;
  element->path_ = NULL;
  element->connected_ = FALSE;
  element->afc_ = NULL;
  element->afc_port_ = 0;
  element->device_ = NULL;
  element->file_handle_ = 0;
  element->buffer_ = NULL;
  element->buffer_is_valid_ = FALSE;
  element->buffer_length_ = 0;
  element->buffer_offset_ = 0;
}

static void gst_afc_src_finalize(GObject* object) {
  GstAfcSrc* self = GST_AFCSRC(object);
  free(self->location_);
  free(self->uuid_);
  free(self->path_);
  free(self->buffer_);

  if (self->file_handle_)
    afc_file_close(self->afc_, self->file_handle_);

  if (self->afc_)
    afc_client_free(self->afc_);

  if (self->device_)
    idevice_free(self->device_);


  G_OBJECT_CLASS(parent_class)->finalize(object);
}

static void gst_afc_src_set_property(
    GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec) {
  GstAfcSrc* self = GST_AFCSRC(object);

  switch (prop_id) {
    case PROP_LOCATION:
      self->location_ = g_strdup(g_value_get_string(value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void gst_afc_src_get_property(
    GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) {
  GstAfcSrc* self = GST_AFCSRC(object);

  switch (prop_id) {
    case PROP_LOCATION:
      g_value_set_string(value, self->location_);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean gst_afc_src_start(GstBaseSrc* src) {
  GstAfcSrc* self = GST_AFCSRC(src);

  // Don't connect again
  if (self->connected_)
    return TRUE;

  // Check that a URI has been passed
  if (!self->location_ || self->location_[0] == '\0') {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("No URI specified"), (NULL));
    return FALSE;
  }

  // Parse the URI
  // HERE BE DRAGONS
  gchar* location = gst_uri_get_location(self->location_);
  char* path_pos = strstr(location, "/");

  self->uuid_ = (char*) malloc(path_pos - location + 1);
  memcpy(self->uuid_, location, path_pos - location);
  self->uuid_[path_pos - location] = '\0';
  self->path_ = g_strdup(path_pos);

  g_free(location);

  // Open the device
  idevice_error_t err = idevice_new(&self->device_, self->uuid_);
  if (err != IDEVICE_E_SUCCESS) {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("idevice error: %d", err), (NULL));
    return FALSE;
  }

  lockdownd_client_t lockdown;

  lockdownd_error_t lockdown_err =
      lockdownd_client_new_with_handshake(self->device_, &lockdown, "GstAfcSrc");
  if (lockdown_err != LOCKDOWN_E_SUCCESS) {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("lockdown error: %d", lockdown_err), (NULL));
    return FALSE;
  }

  lockdown_err = lockdownd_start_service(lockdown, "com.apple.afc", &self->afc_port_);
  if (lockdown_err != LOCKDOWN_E_SUCCESS) {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("lockdown error: %d", lockdown_err), (NULL));
    lockdownd_client_free(lockdown);
    return FALSE;
  }

  afc_error_t afc_err = afc_client_new(self->device_, self->afc_port_, &self->afc_);
  if (afc_err != 0) {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("afc error: %d", afc_err), (NULL));
    lockdownd_client_free(lockdown);
    return FALSE;
  }

  lockdownd_client_free(lockdown);

  // Try opening the file
  afc_err = afc_file_open(self->afc_, self->path_, AFC_FOPEN_RDONLY, &self->file_handle_);
  if (afc_err != 0) {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("afc error: %d", afc_err), (NULL));
    return FALSE;
  }

  self->connected_ = TRUE;

  return TRUE;
}

static gboolean gst_afc_src_stop(GstBaseSrc* src) {
  return TRUE;
}

static GstFlowReturn gst_afc_src_create(GstBaseSrc* src, guint64 offset, guint length, GstBuffer** buffer) {
  GstAfcSrc* self = GST_AFCSRC(src);

  GstBuffer* buf = gst_buffer_try_new_and_alloc(length);
  if (buf == NULL && length > 0) {
    GST_ERROR_OBJECT(src, "Failed to allocate %u bytes", length);
    return GST_FLOW_ERROR;
  }

  // Is this section within our cache?
  if (!self->buffer_is_valid_ || offset < self->buffer_offset_ ||
      offset + length > self->buffer_offset_ + self->buffer_length_) {
    // No - read from the device to fill our internal cache.
    // Always read twice the requested size so the next read(s) hit the cache too.
    if (self->buffer_length_ != length * 2) {
      self->buffer_ = (char*)realloc(self->buffer_, length * 2);
      self->buffer_is_valid_ = TRUE;
      self->buffer_length_ = length * 2;
    }
    self->buffer_offset_ = offset;

    uint32_t bytes_read = 0;
    afc_error_t err;
    err = afc_file_seek(self->afc_, self->file_handle_, offset, SEEK_SET);
    if (err == AFC_E_SUCCESS) {
      err = afc_file_read(self->afc_, self->file_handle_, self->buffer_, length * 2, &bytes_read);
    }

    if (err != AFC_E_SUCCESS) {
      gst_buffer_unref(buf);
      return GST_FLOW_ERROR;
    }
  }

  // Copy from our internal buffer to the output buffer
  memcpy(GST_BUFFER_DATA(buf), self->buffer_ + offset - self->buffer_offset_, length);

  *buffer = buf;
  return GST_FLOW_OK;
}

static gboolean gst_afc_src_is_seekable(GstBaseSrc* src) {
  return TRUE;
}

static gboolean gst_afc_src_get_size(GstBaseSrc* src, guint64* size) {
  GstAfcSrc* self = GST_AFCSRC(src);

  char** infolist = NULL;
  afc_error_t err = afc_get_file_info(self->afc_, self->path_, &infolist);
  if (err != AFC_E_SUCCESS || !infolist) {
    GST_ELEMENT_ERROR(src, RESOURCE, NOT_FOUND, ("afc error: %d", err), (NULL));
    return FALSE;
  }

  gboolean found_size = FALSE;

  char** p = infolist;
  while (*p != NULL) {
    if (g_strcmp0(*p, "st_size") == 0) {
      *size = strtoll(*(p+1), NULL, 0);
      found_size = TRUE;
    }
    free(*p); ++p;
    free(*p); ++p;
  }
  free(infolist);

  if (!found_size) {
    *size = 0;
    return FALSE;
  }

  return TRUE;
}

#define PACKAGE "Clementine"

static gboolean afcsrc_init(GstPlugin* afcsrc) {
  return gst_element_register(afcsrc, "afcsrc", GST_RANK_PRIMARY, GST_TYPE_AFCSRC);
}

void afcsrc_register_static() {
  gst_plugin_register_static(
    GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "afcsrc",
    "iPod/iPhone Source",
    afcsrc_init,
    "0.1",
    "GPL",
    "Clementine",
    "Clementine",
    "http://www.clementine-player.org/");
}