summaryrefslogtreecommitdiff
path: root/src/writer.c
blob: ead016846ad684cec7cc9b2fc40d79b517cd32ba (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
/*
 *  rharchive
 *
 *  rharchive is a simple tcp connection proxy which combines the 
 *  features of rinetd and 6tunnel. rharchive supports IPv4 and 
 *  IPv6 and also supports connections from IPv6 to IPv4 
 *  endpoints and vice versa.
 *  
 *
 *  Copyright (C) 2010-2011 Christian Pointner <equinox@helsinki.at>
 *                         
 *  This file is part of rharchive.
 *
 *  rharchive 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
 *  any later version.
 *
 *  rharchive 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 rharchive. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gst/gst.h>
#include <gst/app/gstappsink.h>

#include "writer.h"

#include "datatypes.h"
#include "log.h"

int writer_init(writer_t* writer, const char* name_format, const char* output_dir, int length, int offset)
{
  if(!writer)
    return -1;

  writer->sink_ = gst_element_factory_make("appsink", "writer");
  if(!writer->sink_) {
    log_printf(ERROR, "the writer object could not be created. Exiting.");
    return -1;
  }
  writer->name_format_ = name_format;
  writer->output_dir_ = output_dir;
  writer->length_ = length;
  writer->offset_ = offset;

  return 0;
}

static gpointer writer_thread_func(gpointer data)
{
  writer_t *writer = (writer_t*)data;
  log_printf(NOTICE, "writer thread started");

  GstBuffer* buf = NULL;
  for(;;) {
    buf = gst_app_sink_pull_buffer((GstAppSink*)writer->sink_);
    if(!buf) {
      break;
    }
    log_printf(DEBUG, "got buffer of size %d", buf->size);
  }

  log_printf(NOTICE, "writer thread stopped");
	return NULL;
}

int writer_start(writer_t* writer)
{
  if(!writer)
    return;

  writer->thread_ = g_thread_create(writer_thread_func, writer, TRUE, NULL);
  if(!writer->thread_) {
    log_printf(ERROR, "writer thread could not be started");
    return -1;
  } 
}

void writer_stop(writer_t* writer)
{
  if(!writer)
    return;
  
  if(writer->thread_) {
    log_printf(NOTICE, "waiting for writer thread to stop");
    g_thread_join(writer->thread_);
  }
}