summaryrefslogtreecommitdiff
path: root/src/debug/local-play.c
blob: 8dbb90ddedbc44dae76f635c9a02e5e431f74a7e (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
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
/*
 * vim: set sts=2 sw=2 et :
 *
 * License: LGPL-2.1+
 * Copyright (c) 2014 Nirbheek Chauhan <nirbheek@centricular.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.
 */

#include "local-play.h"

static void
pad_has_video_caps (TranscodeServerCtx *ctx,
                    GstPad             *decodebin_pad)
{
  GstPad *sink_pad;
  GstElement *bin;
  GstElement *videoconv, *videoscale, *videosink;

  bin = gst_bin_new (NULL);
  videoconv = gst_element_factory_make ("videoconvert", "videoconv");
  videoscale = gst_element_factory_make ("videoscale", "videoscale");
  videosink = gst_element_factory_make ("autovideosink", "videosink");

  gst_bin_add_many (GST_BIN (bin), videoconv, videoscale, videosink, NULL);
  gst_element_link_many (videoconv, videoscale, videosink, NULL);

  sink_pad = gst_element_get_static_pad (videoconv, "sink");
  gst_element_add_pad (bin, gst_ghost_pad_new ("sink", sink_pad));
  gst_object_unref (sink_pad);

  gst_bin_add (GST_BIN (ctx->pipeline), bin);

  sink_pad = gst_element_get_static_pad (bin, "sink");
  if (gst_pad_link (decodebin_pad, sink_pad) != GST_PAD_LINK_OK) {
    g_critical ("ERROR: Unable to link video pads");
    soup_message_set_status (ctx->msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    goto out;
  }

  /* Since these elements have been added to the pipeline after the state was
   * changed from GST_STATE_NULL, they have to be moved to PAUSED manually */
  gst_element_set_state (bin, GST_STATE_PAUSED);
out:
  gst_object_unref (sink_pad);
  return;
}

static void
pad_has_audio_caps (TranscodeServerCtx *ctx,
                    GstPad             *decodebin_pad)
{
  GstPad *sink_pad;
  GstElement *bin;
  GstElement *audioconv, *audioresample, *audiosink;

  bin = gst_bin_new (NULL);
  audioconv = gst_element_factory_make ("audioconvert", "audioconv");
  audioresample = gst_element_factory_make ("audioresample", "audioresample");
  audiosink = gst_element_factory_make ("autoaudiosink", "audiosink");

  gst_bin_add_many (GST_BIN (bin), audioconv, audioresample, audiosink, NULL);
  gst_element_link_many (audioconv, audioresample, audiosink, NULL);

  sink_pad = gst_element_get_static_pad (audioconv, "sink");
  gst_element_add_pad (bin, gst_ghost_pad_new ("sink", sink_pad));
  gst_object_unref (sink_pad);

  gst_bin_add (GST_BIN (ctx->pipeline), bin);

  sink_pad = gst_element_get_static_pad (bin, "sink");
  if (gst_pad_link (decodebin_pad, sink_pad) != GST_PAD_LINK_OK) {
    g_critical ("ERROR: Unable to link video pads");
    soup_message_set_status (ctx->msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    goto out;
  }

  /* Since these elements have been added to the pipeline after the state was
   * changed from GST_STATE_NULL, they have to be moved to PAUSED manually */
  gst_element_set_state (bin, GST_STATE_PAUSED);
out:
  gst_object_unref (sink_pad);
  return;
}

static gboolean
structure_printf (GQuark field_id,
                  const GValue *value,
                  gpointer user_data)
{
  char *char_value;

  char_value = g_strdup_value_contents (value);
  g_print ("%s = %s\n", g_quark_to_string (field_id), char_value);
  g_free (char_value);
  return TRUE;
}

static void
decodebin_pad_added (GstElement *decodebin,
                     GstPad     *src_pad,
                     gpointer    user_data)
{
  GstCaps *pad_caps;
  GstStructure *structure;
  TranscodeServerCtx *ctx = user_data;

  if (!gst_pad_has_current_caps (src_pad)) {
    g_critical ("Decodebin pad doesn't have current caps");
    soup_message_set_status (ctx->msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }

  pad_caps = gst_pad_get_current_caps (src_pad);
  structure = gst_caps_get_structure (pad_caps, 0);
  gst_structure_foreach (structure, structure_printf, NULL);

  if (gst_structure_has_name (structure, "audio/x-raw"))
    pad_has_audio_caps (ctx, src_pad);
  else if (gst_structure_has_name (structure, "video/x-raw"))
    pad_has_video_caps (ctx, src_pad);
  else
    g_critical ("Found unsupported caps: %s",
                gst_structure_get_name (structure));

  gst_caps_unref (pad_caps);
}

void
stp_play_from_msg (TranscodeServerCtx *ctx)
{
  GstBus *bus;
  GstElement *src, *decodebin;

  g_debug ("Constructing pipeline\n");
  src = gst_element_factory_make ("appsrc", "src");
  g_object_set (src, "is-live", TRUE,
                "stream-type", 0, NULL);
  ctx->appsrc = src;

  decodebin = gst_element_factory_make ("decodebin", "decodebin");
  gst_bin_add_many (GST_BIN (ctx->pipeline), src, decodebin, NULL);
  gst_element_link (src, decodebin);

  g_signal_connect (decodebin, "pad-added",
                    G_CALLBACK (decodebin_pad_added), ctx);

  bus = gst_pipeline_get_bus (GST_PIPELINE (ctx->pipeline));
  gst_bus_add_signal_watch (bus);
  g_signal_connect (bus, "message", G_CALLBACK (stp_on_gst_bus_message), ctx);
  g_object_unref (bus);
}