File: RTMP.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (112 lines) | stat: -rw-r--r-- 2,831 bytes parent folder | download | duplicates (3)
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
/*
 *  Copyright 1994-2022 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou 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 lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "context.h"

uint32_t options = BO_NONE;
uint32_t version = 0;
char desc[] = "RTMP streaming";


#define FFMPEG_CHECK     "ffmpeg -h >/dev/null 2>&1"
#define FFMPEG           "ffmpeg"
#define RTMP_FFMPEG_ARGS "-loglevel quiet -re -vcodec ppm -f image2pipe -i pipe: -c:v libx264 -b:v 5M -pix_fmt yuv420p -c:a:0 libfdk_aac -b:a:0 480k -f flv"
#define RTMP_URL         "rtmp://localhost/live/stream"

static FILE *rtmp = NULL;


static int8_t
open_rtmp(void)
{
  char cmd[MAXLEN+1];
  char *args = NULL, *url = NULL;

  memset(&cmd, '\0', MAXLEN+1);
  if (NULL == (args = getenv("LEBINIOU_RTMP_FFMPEG_ARGS"))) {
    args = RTMP_FFMPEG_ARGS;
  }
  if (NULL == (url = getenv("LEBINIOU_RTMP_URL"))) {
    url = RTMP_URL;
  }

  g_snprintf(cmd, MAXLEN, "%s %s %s", FFMPEG, args, url);
  if (NULL == (rtmp = popen(cmd, "w"))) {
    xperror("popen");
  } else {
    VERBOSE(printf("[i] %s: opened stream to %s\n", __FILE__, url));
    VERBOSE(printf("[i] %s: ffmpeg args: '%s'\n", __FILE__, args));
  }

  return 1;
}


int8_t
create(Context_t *ctx)
{
  if (check_command(FFMPEG_CHECK) == -1) {
    printf("[!] %s: ffmpeg binary not found, plugin disabled\n", __FILE__);
    return 0;
  } else {
    return open_rtmp();
  }
}


void
destroy(Context_t *ctx)
{
  if (NULL != rtmp)
    if (-1 == pclose(rtmp)) {
      xperror("pclose");
    }
}


void
run(Context_t *ctx)
{
  uint8_t *data;
  char buff[MAXLEN+1];
  size_t res;

  /* get picture */
  data = export_RGB_active_buffer(ctx, 1);

  memset(&buff, '\0', MAXLEN+1);
  g_snprintf(buff, MAXLEN, "P6  %d %d 255\n", WIDTH, HEIGHT);

  /* PPM header */
  res = fwrite((const void *)&buff, sizeof(char), strlen(buff), rtmp);
  if (res != strlen(buff)) {
    fprintf(stderr, "[!] %s:write_header: short write (%zu of %d)\n", __FILE__, res, (int)strlen(buff));
    exit(1);
  }

  /* PPM data */
  res = fwrite((const void *)data, sizeof(Pixel_t), RGB_BUFFSIZE, rtmp);
  xfree(data);
  if (res != RGB_BUFFSIZE) {
    fprintf(stderr, "[!] %s:write_image: short write (%zu of %li)\n", __FILE__, res, RGB_BUFFSIZE);
    exit(1);
  }

  fflush(rtmp);
}