summaryrefslogtreecommitdiff
path: root/src/encoder.cc
blob: 52a7f93861296ba462810470b240a474d4fea72b (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * RTVideoRec Realtime video recoder and encoder for Linux
 *
 * Copyright (C) 2004  Bent Bisballe
 * Copyright (C) 2004  B. Stultiens
 * Copyright (C) 2004  Koen Otter and Glenn van der Meyden
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <config.h>
#ifdef USE_GUI

#include "encoder.h"

Encoder::Encoder(Error* err,
                 const char *gip,
                 const int gport,
                 sem_t	*gsem,
                 Queue<Frame> *gqueue,
                 pthread_mutex_t *gmutex,
                 volatile int *grunning)
{
  errobj = err;

  strcpy(ip, gip);
  port = gport;
  memset(cpr, 0, sizeof(cpr));

  sem = gsem;
  queue = gqueue;
  mutex = gmutex;
  running = grunning;

  record = 0;

 	sem_init(&record_sem, 0, 0);

  s = NULL;
  n = NULL;

  shoot_request = 0;
  shoot_value = 0;
  freeze_request = 0;
  freeze_value = 0;
}


Encoder::~Encoder()
{
  if(n) delete n;
  if(s) delete s;
}


void Encoder::encode()
{ 
  Frame *frame;

  while(*running) {
    sem_wait(sem);

    frame = queue->pop();
    
    if(frame) {
      if(record || 
         (freeze_request != freeze_value) || 
         (shoot_request != shoot_value)) {
        n_header h;
      
        h.header_type = DATA_HEADER;
        sprintf(h.header.h_data.cpr, cpr);
        h.header.h_data.freeze = (freeze_request != freeze_value);
        h.header.h_data.snapshot = (shoot_request != shoot_value);
        h.header.h_data.record = record;
        h.header.h_data.savestate = NO_CHANGE;
        
        if(freeze_request != freeze_value) freeze_value = freeze_request;
        if(shoot_request != shoot_value) shoot_value = shoot_request;
        
        n->sendPackage(&h, frame->data, frame->size);
      }
      if(frame) delete frame;
    }
  }
}


void Encoder::setCpr(char *newcpr)
{
  strcpy(cpr, newcpr);
}


void Encoder::freeze()
{  
  if(!s) {
    s = new Socket(port, errobj);
    s->sconnect(ip);
    n = new Network(s, errobj);
  }
  if(!errobj->hasError()) freeze_request = 1 - freeze_request;
}


void Encoder::shoot()
{
  if(!s) {
    s = new Socket(port, errobj);
    s->sconnect(ip);
    n = new Network(s, errobj);
  }
  if(!errobj->hasError()) shoot_request = 1 - shoot_request;
}


void Encoder::run() {
  encode();
  fprintf(stderr, "Encoder thread stopped.\n"); fflush(stderr);
}


void Encoder::start() {
  if(!s) {
    s = new Socket(port, errobj);
    s->sconnect(ip);
    n = new Network(s, errobj);
  }
  if(!errobj->hasError()) record = 1;
}


void Encoder::stop(n_savestate save) {
  // TODO: set save state in package header.
  // TODO: Flush not yet sent video packages.
  record = 0;
  if(s) {
    if(n) delete n;
    delete s;
    s = NULL;
    n = NULL;
  }
}

#endif /*USE_GUI*/