summaryrefslogtreecommitdiff
path: root/src/outputstreamer.cc
blob: 0d7b557117ac3c7212e96e9ef1b41a2a41a5e302 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            streamer.cc
 *
 *  Fri Sep 19 21:05:08 CEST 2014
 *  Copyright 2014 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of SimpleRTP.
 *
 *  SimpleRTP 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.
 *
 *  SimpleRTP 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 SimpleRTP; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "outputstreamer.h"

#define CSRC_V 42
#define CSRC_A 43

OutputStreamer::OutputStreamer(QString key, unsigned int ssrc)
  : socket(this)
{
  total = 0;

  lrtp_status_t status;

  char ckey[61];
  strcpy(ckey, key.toStdString().c_str());

  lrtp = lrtp_init(&status, ckey, ssrc);
  if(status != LRTP_OK) printf("O:lrtp_init err: %d\n", status);

  int res;
  res = lrtp_create_profile(lrtp, PROFILE_JPEG, CSRC_V, OPTION_END);
  if(res != 0) printf("O:lrtp_create_profile (v) err: %d\n", res);

  res = lrtp_create_profile(lrtp, PROFILE_OPUS, CSRC_A, OPTION_END);
  if(res != 0) printf("O:lrtp_create_profile (a) err: %d\n", res);
}

OutputStreamer::~OutputStreamer()
{
  lrtp_status_t status;

  status = lrtp_destroy_profile(lrtp, CSRC_V);
  if(status != LRTP_OK) printf("O:lrtp_destroy_profile (v) err: %d\n", status);

  status = lrtp_destroy_profile(lrtp, CSRC_A);
  if(status != LRTP_OK) printf("O:lrtp_destroy_profile (a) err: %d\n", status);
 
  status = lrtp_close(lrtp);
  if(status != LRTP_OK) printf("O:lrtp_close err: %d\n", status);
}

void OutputStreamer::addPeer(QHostAddress addr, quint16 port)
{
  Peer peer;
  peer.addr = addr;
  peer.port = port;
  peers.push_back(peer);
}

void OutputStreamer::newImage(Frame frame)
{
  int ret = 0;
  ret = lrtp_enqueue_frame(lrtp, CSRC_V,
                           frame.data, frame.size, frame.ts,
                           LRTP_COPY);
  if(ret != 0) printf("O:lrtp_enqueue_frame (v) err: %d\n", ret);

  char packet[16*1024];
  while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) > 0) {
    //printf("v"); fflush(stdout);
    sendPackage(packet, ret);
  }
  //CPPUNIT_ASSERT_EQUAL(ret, 0);

    free(frame.data);
    //    delete frame;
  //free(frame);
}

void OutputStreamer::newAudio(framelist_t frames)
{
  framelist_t::iterator fi = frames.begin();
  while(fi != frames.end()) {
    Frame *frame = *fi;

    int ret = 0;
    ret = lrtp_enqueue_frame(lrtp, CSRC_A,
                             frame->data, frame->size, frame->ts,
                             LRTP_COPY);
    if(ret != 0) printf("O:lrtp_enqueue_frame (a) err: %d\n", ret);
    
    char packet[16*1024];
    while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) > 0) {
      //printf("a"); fflush(stdout);
      sendPackage(packet, ret);
    }
    //CPPUNIT_ASSERT_EQUAL(ret, 0);
    
    free(frame->data);
    delete frame;
    //free(frame);

    fi++;
  }
}

void OutputStreamer::sendPackage(const char *data, size_t size)
{
  QList<Peer>::iterator i = peers.begin();
  while(i != peers.end()) {
    Peer &peer = *i;
    socket.writeDatagram(data, (quint64)size, peer.addr, peer.port);
    total += size;
    i++;
  }
}

size_t OutputStreamer::getTotal()
{
  size_t t = total;
  total = 0;
  return t;
}