summaryrefslogtreecommitdiff
path: root/src/streamer.c
blob: fd1223d2a400acf3f9b8cd3f18e5c17481123a45 (plain) (blame)
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
/*
 *  gstdvbbackend
 *
 *  gstdvbbackend is a small programm which captures a given set of dvb
 *  channels from one dvb device and provides the streams via minimal http.
 *
 *
 *  Copyright (C) 2011 Christian Pointner <equinox@spreadspace.org>
 *                         
 *  This file is part of gstdvbbackend.
 *
 *  gstdvbbackend 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.
 *
 *  gstdvbbackend 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 gstdvbbackend. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gst/gst.h>

#include <sys/types.h>

#include <errno.h>
#include <string.h>

#include "streamer.h"

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

static void added_cb(GstElement* sink, gint fd, gpointer data)
{
  gint num_fds;
  g_object_get(G_OBJECT(sink), "num_fds", &num_fds, NULL);
  log_printf(INFO, "fdsink: successfully added client %d (sink has now %d fds)", fd, num_fds);
}

static void removed_cb(GstElement* sink, gint fd, gpointer data)
{
  gint num_fds;
  g_object_get(G_OBJECT(sink), "num_fds", &num_fds, NULL);  
  log_printf(INFO, "fdsink: successfully removed client %d (sink has now %d fds)", fd, num_fds);
}

static void fdremoved_cb(GstElement* sink, gint fd, gpointer data)
{
  gint num_fds;
  g_object_get(G_OBJECT(sink), "num_fds", &num_fds, NULL);  
  log_printf(INFO, "fdsink: successfully removed fd %d (sink has now %d fds)", fd, num_fds);
}

int streamer_init(streamer_t* streamer, GMainLoop *loop, const char* host, const char* port)
{
  if(!streamer)
    return -1;

      // TODO: init server socket here

  streamer->loop_ = loop;
  streamer->sink_ = gst_element_factory_make("multifdsink", "streamer");
  if(!streamer->sink_) {
    log_printf(ERROR, "the streamer object could not be created. Exiting.");
    return -1;
  }

      // TODO: how the heck should we get the right value? 3 means keyframe...
  g_object_set(G_OBJECT(streamer->sink_), "recover-policy", 3, NULL);
  g_signal_connect(G_OBJECT(streamer->sink_), "client-added", G_CALLBACK(added_cb), streamer);
  g_signal_connect(G_OBJECT(streamer->sink_), "client-removed", G_CALLBACK(removed_cb), streamer);
  g_signal_connect(G_OBJECT(streamer->sink_), "client-fd-removed", G_CALLBACK(fdremoved_cb), streamer);

  streamer->thread_ = NULL;
  return 0;
}

static void add_fd(streamer_t* streamer, int fd)
{
  log_printf(INFO, "adding fd %d to fdsink", fd);
  g_signal_emit_by_name(G_OBJECT(streamer->sink_), "add", fd, NULL);
}

static void remove_fd(streamer_t* streamer, int fd)
{
  log_printf(INFO, "removing fd %d from fdsink", fd);
  g_signal_emit_by_name(G_OBJECT(streamer->sink_), "remove-flush", fd, NULL);
}
  
static gpointer streamer_thread_func(gpointer data)
{
  streamer_t *streamer = (streamer_t*)data;
  log_printf(NOTICE, "streamer thread started");
  
  GstBuffer* buf = NULL;
  for(;;) {
        // TODO: call accept here
    sleep(100);
    break;
  }
 
  log_printf(NOTICE, "streamer thread stopped");
  g_main_loop_quit(streamer->loop_);
  return NULL;
}

int streamer_start(streamer_t* streamer)
{
  if(!streamer)
    return -1;
  
  streamer->thread_ = g_thread_create(streamer_thread_func, streamer, TRUE, NULL);
  if(!streamer->thread_) {
    log_printf(ERROR, "streamer thread could not be started");
    return -1;
  }

  return 0;
}

void streamer_stop(streamer_t* streamer)
{
  if(!streamer)
    return;

      // TODO: close server socket here

  if(streamer->thread_) {
    log_printf(NOTICE, "waiting for streamer thread to stop");
    g_thread_join(streamer->thread_);
  }
}