camogm_jpeg.c 4.48 KB
Newer Older
1
/*!***************************************************************************
Mikhail Karpenko's avatar
Mikhail Karpenko committed
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
   *! FILE NAME  : camogm_jpeg.c
   *! DESCRIPTION: Provides writing to series of individual JPEG files for camogm
   *! Copyright (C) 2007 Elphel, Inc.
   *! -----------------------------------------------------------------------------**
   *!  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 3 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, see <http://www.gnu.org/licenses/>.
   *! -----------------------------------------------------------------------------**
   *!
   *!  $Log: camogm_jpeg.c,v $
   *!  Revision 1.2  2009/02/25 17:50:51  spectr_rain
   *!  removed deprecated dependency
   *!
   *!  Revision 1.1.1.1  2008/11/27 20:04:01  elphel
   *!
   *!
   *!  Revision 1.3  2008/04/11 23:09:33  elphel
   *!  modified to handle kml generation
   *!
   *!  Revision 1.2  2007/11/19 03:23:21  elphel
   *!  7.1.5.5 Added support for *.mov files in camogm.
   *!
   *!  Revision 1.1  2007/11/16 08:49:57  elphel
   *!  Initial release of camogm - program to record video/image to the camera hard drive (or other storage)
   *!
 */
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
//!Not all are needed, just copied from the camogm.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
//#include <ctype.h>
//#include <getopt.h>
#include <time.h>
#include <string.h>

#include <netinet/in.h> /*little <-> big endian ?*/
Mikhail Karpenko's avatar
Mikhail Karpenko committed
54
#include <sys/mman.h>   /* mmap */
55 56
#include <sys/ioctl.h>

Mikhail Karpenko's avatar
Mikhail Karpenko committed
57
#include <c313a.h>
58 59 60
#include <asm/byteorder.h>


Mikhail Karpenko's avatar
Mikhail Karpenko committed
61
#include <ogg/ogg.h>    // has to be before ogmstreams.h
62 63 64
#include "ogmstreams.h" // move it to <>?

#include "camogm_jpeg.h"
65

66
//! may add something - called first time format is changed to this one (only once) recording is stopped
Mikhail Karpenko's avatar
Mikhail Karpenko committed
67 68 69
int camogm_init_jpeg(void)
{
	return 0;
70
}
Mikhail Karpenko's avatar
Mikhail Karpenko committed
71 72
void camogm_free_jpeg(void)
{
73 74
}

75
int camogm_start_jpeg(camogm_state *state)
Mikhail Karpenko's avatar
Mikhail Karpenko committed
76
{
77
//!TODO: make directory if it does not exist (find the last "/" in the state->path
Mikhail Karpenko's avatar
Mikhail Karpenko committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	char * slash;
	int rslt;

	strcpy(state->path, state->path_prefix); //!make state->path a directory name (will be replaced when the frames will be written)
	slash = strrchr(state->path, '/');
	D2(fprintf(debug_file, "camogm_start_jpeg\n"));
	if (slash) {
		D3(fprintf(debug_file, "Full path %s\n", state->path));
		slash[0] = '\0'; //! truncate path to the directory name
		D3(fprintf(debug_file, "directory path %s\n", state->path));
		rslt = mkdir(state->path, 0777);
		D3(fprintf(debug_file, "mkdir (%s, 0777) returned %d, errno=%d\n", state->path, rslt, errno));
		if ((rslt < 0) && (errno != EEXIST)) { // already exists is OK
			D0(fprintf(debug_file, "Error creating directory %s, errno=%d\n", state->path, errno));
			return -CAMOGM_FRAME_FILE_ERR;
		}
	}
	return 0;
96
}
97 98

int camogm_frame_jpeg(camogm_state *state)
Mikhail Karpenko's avatar
Mikhail Karpenko committed
99 100
{
	int i, j;
101
//   int fd;
Mikhail Karpenko's avatar
Mikhail Karpenko committed
102 103 104 105 106 107 108 109 110
	ssize_t iovlen, l;
	struct iovec chunks_iovec[7];

	l = 0;
	for (i = 0; i < (state->chunk_index) - 1; i++) {
		chunks_iovec[i].iov_base = state->packetchunks[i + 1].chunk;
		chunks_iovec[i].iov_len = state->packetchunks[i + 1].bytes;
		l += chunks_iovec[i].iov_len;
	}
111

Mikhail Karpenko's avatar
Mikhail Karpenko committed
112
	sprintf(state->path, "%s%010ld_%06ld.jpeg", state->path_prefix, state->this_frame_params.timestamp_sec, state->this_frame_params.timestamp_usec);
113 114 115
// if ((devfd = open("/dev/fpgaio", O_RDWR))<0)  {printf("error opening /dev/fpgaio\r\n"); return -1;}
//_1__12_Error opening /tmp/z/video1195147018_273452.jpeg for writing

Mikhail Karpenko's avatar
Mikhail Karpenko committed
116 117 118 119
	if (((state->ivf = open(state->path, O_RDWR | O_CREAT, 0777))) < 0) {
		D0(fprintf(debug_file, "Error opening %s for writing, returned %d, errno=%d\n", state->path, state->ivf, errno));
		return -CAMOGM_FRAME_FILE_ERR;
	}
120

Mikhail Karpenko's avatar
Mikhail Karpenko committed
121 122 123 124 125 126 127 128 129
	iovlen = writev(state->ivf, chunks_iovec, (state->chunk_index) - 1);
	if (iovlen < l) {
		j = errno;
		D0(fprintf(debug_file, "writev error %d (returned %d, expected %d)\n", j, iovlen, l));
		close(state->ivf);
		return -CAMOGM_FRAME_FILE_ERR;
	}
	close(state->ivf);
	return 0;
130 131
}

Mikhail Karpenko's avatar
Mikhail Karpenko committed
132 133 134
int camogm_end_jpeg(void)
{
	return 0;
135
}