Skip to content

Commit

Permalink
#5 Added change_file function
Browse files Browse the repository at this point in the history
  • Loading branch information
pashamesh authored and mkrufky committed Dec 19, 2015
1 parent 6a36b53 commit e648666
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ patches/
# dirs
libdvbpsi/
usr/
nbproject/

# compiled binary
dvbtee/dvbtee
Expand Down
46 changes: 40 additions & 6 deletions libdvbtee/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <sstream>
#include <netdb.h>

#include "output.h"
Expand Down Expand Up @@ -301,7 +302,7 @@ void* output_stream::output_stream_thread()
uint8_t *data = NULL;
int buf_size, ret = 0;

dPrintf("(%d)", sock);
dPrintf("sock: %d, stream_method: %d", sock, stream_method);
#if 1
switch (stream_method) {
case OUTPUT_STREAM_HTTP:
Expand Down Expand Up @@ -553,6 +554,38 @@ void output_stream::close_file()
}
}

/**
Open or reopen file for output.
@param char* output filename.
@return int 0 if file opened and -1 if open fails.
**/
int output_stream::change_file(char* target_file)
{
std::stringstream tmp_stream;
std::string new_name;

tmp_stream << target_file << "_" << name_index;
tmp_stream >> new_name;

dprintf("sock: %d, old: %s, new: %s", sock, target_file, new_name.c_str());

if (sock >= 0) {
close(sock);
//sock = -1;
}

if (
(sock = open(new_name.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU)) < 0
) {
perror("file open failed");
return -1;
}

name_index++;
return 0;
}

int output_stream::add(void* priv, stream_callback callback, map_pidtype &pids)
{
stream_cb = callback;
Expand Down Expand Up @@ -647,13 +680,14 @@ int output_stream::add(char* target, map_pidtype &pids)

if (b_file) {
dPrintf("opening %s...", ip);
if ((sock = open(ip, O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU)) < 0) {
perror("file failed");
if (change_file(ip) < 0) {
return -1;
} else {
ringbuffer.reset();
stream_method = OUTPUT_STREAM_FILE;
return set_pids(pids);
name_index = 0;

ringbuffer.reset();
stream_method = OUTPUT_STREAM_FILE;
return set_pids(pids);
}
}

Expand Down
2 changes: 2 additions & 0 deletions libdvbtee/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class output_stream
bool drain();
void stop();
inline void stop_after_drain() { if (drain()) stop(); }
int change_file(char*);
void close_file();

bool push(uint8_t*, int);
Expand Down Expand Up @@ -116,6 +117,7 @@ class output_stream
enum output_mimetype mimetype;

char name[21];
unsigned int name_index;

rbuf ringbuffer;

Expand Down

0 comments on commit e648666

Please sign in to comment.