Skip to content

Commit

Permalink
[modules] viewvideo: use UdpSocket instead of udp_periph
Browse files Browse the repository at this point in the history
  • Loading branch information
flixr committed Apr 14, 2015
1 parent 49ab80a commit fd77a7c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
12 changes: 3 additions & 9 deletions conf/modules/cv_opticflow.xml
Expand Up @@ -102,21 +102,15 @@
<file name="lucas_kanade.c" dir="modules/computer_vision/lib/vision"/>

<raw>
VIEWVIDEO_DEV ?= UDP1
VIEWVIDEO_HOST ?= $(MODEM_HOST)
VIEWVIDEO_PORT_OUT ?= 5000
VIEWVIDEO_PORT_IN ?= 4999
VIEWVIDEO_BROADCAST ?= $(MODEM_BROADCAST)
VIEWVIDEO_DEV_LOWER = $(shell echo $(VIEWVIDEO_DEV) | tr A-Z a-z)

VIEWVID_G_CFLAGS = -DVIEWVIDEO_HOST=\"$(VIEWVIDEO_HOST)\" -DVIEWVIDEO_PORT_OUT=$(VIEWVIDEO_PORT_OUT)
VIEWVID_CFLAGS = -DUSE_$(VIEWVIDEO_DEV) -DVIEWVIDEO_DEV=$(VIEWVIDEO_DEV_LOWER)
VIEWVID_CFLAGS += -D$(VIEWVIDEO_DEV)_PORT_OUT=$(VIEWVIDEO_PORT_OUT) -D$(VIEWVIDEO_DEV)_PORT_IN=$(VIEWVIDEO_PORT_IN)
VIEWVID_CFLAGS += -D$(VIEWVIDEO_DEV)_BROADCAST=$(VIEWVIDEO_BROADCAST) -D$(VIEWVIDEO_DEV)_HOST=\"$(VIEWVIDEO_HOST)\"
VIEWVID_CFLAGS = -DVIEWVIDEO_HOST=$(VIEWVIDEO_HOST) -DVIEWVIDEO_PORT_OUT=$(VIEWVIDEO_PORT_OUT)
ifeq ($(VIEWVIDEO_USE_NC),)
ap.CFLAGS += $(VIEWVID_G_CFLAGS) $(VIEWVID_CFLAGS)
ap.CFLAGS += $(VIEWVID_CFLAGS) -DVIEWVIDEO_BROADCAST=$(VIEWVIDEO_BROADCAST)
else
ap.CFLAGS += $(VIEWVID_G_CFLAGS) -DVIEWVIDEO_USE_NC
ap.CFLAGS += $(VIEWVID_CFLAGS) -DVIEWVIDEO_USE_NC
endif

ap.CFLAGS += -DGUIDANCE_V_MODE_MODULE_SETTING=GUIDANCE_V_MODE_HOVER
Expand Down
12 changes: 3 additions & 9 deletions conf/modules/video_rtp_stream.xml
Expand Up @@ -44,21 +44,15 @@

<!-- Define the network connection to send images over -->
<raw>
VIEWVIDEO_DEV ?= UDP1
VIEWVIDEO_HOST ?= $(MODEM_HOST)
VIEWVIDEO_PORT_OUT ?= 5000
VIEWVIDEO_PORT_IN ?= 4999
VIEWVIDEO_BROADCAST ?= $(MODEM_BROADCAST)
VIEWVIDEO_DEV_LOWER = $(shell echo $(VIEWVIDEO_DEV) | tr A-Z a-z)

VIEWVID_G_CFLAGS = -DVIEWVIDEO_HOST=$(VIEWVIDEO_HOST) -DVIEWVIDEO_PORT_OUT=$(VIEWVIDEO_PORT_OUT)
VIEWVID_CFLAGS = -DUSE_$(VIEWVIDEO_DEV) -DVIEWVIDEO_DEV=$(VIEWVIDEO_DEV_LOWER)
VIEWVID_CFLAGS += -D$(VIEWVIDEO_DEV)_PORT_OUT=$(VIEWVIDEO_PORT_OUT) -D$(VIEWVIDEO_DEV)_PORT_IN=$(VIEWVIDEO_PORT_IN)
VIEWVID_CFLAGS += -D$(VIEWVIDEO_DEV)_BROADCAST=$(VIEWVIDEO_BROADCAST) -D$(VIEWVIDEO_DEV)_HOST=\"$(VIEWVIDEO_HOST)\"
VIEWVID_CFLAGS = -DVIEWVIDEO_HOST=$(VIEWVIDEO_HOST) -DVIEWVIDEO_PORT_OUT=$(VIEWVIDEO_PORT_OUT)
ifeq ($(VIEWVIDEO_USE_NC),)
ap.CFLAGS += $(VIEWVID_G_CFLAGS) $(VIEWVID_CFLAGS)
ap.CFLAGS += $(VIEWVID_CFLAGS) -DVIEWVIDEO_BROADCAST=$(VIEWVIDEO_BROADCAST)
else
ap.CFLAGS += $(VIEWVID_G_CFLAGS) -DVIEWVIDEO_USE_NC
ap.CFLAGS += $(VIEWVID_CFLAGS) -DVIEWVIDEO_USE_NC
endif
</raw>

Expand Down
18 changes: 18 additions & 0 deletions sw/airborne/arch/linux/udp_socket.c
Expand Up @@ -126,6 +126,24 @@ int udp_socket_send(struct UdpSocket *sock, uint8_t *buffer, uint16_t len)
return bytes_sent;
}

/**
* Send a packet from buffer, non-blocking.
* @param[in] sock pointer to UdpSocket struct
* @param[in] buffer buffer to send
* @param[in] len buffer length in bytes
* @return number of bytes sent (-1 on error)
*/
int udp_socket_send_dontwait(struct UdpSocket *sock, uint8_t *buffer, uint16_t len)
{
if (sock == NULL) {
return -1;
}

ssize_t bytes_sent = sendto(sock->sockfd, buffer, len, MSG_DONTWAIT,
(struct sockaddr *)&sock->addr_out, sizeof(sock->addr_out));
return bytes_sent;
}

/**
* Receive a UDP packet, dont wait.
* Sets the MSG_DONTWAIT flag, returns 0 if no data is available.
Expand Down
9 changes: 9 additions & 0 deletions sw/airborne/arch/linux/udp_socket.h
Expand Up @@ -57,6 +57,15 @@ extern int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, i
*/
extern int udp_socket_send(struct UdpSocket *sock, uint8_t *buffer, uint16_t len);

/**
* Send a packet from buffer, non-blocking.
* @param[in] sock pointer to UdpSocket struct
* @param[in] buffer buffer to send
* @param[in] len buffer length in bytes
* @return number of bytes sent (-1 on error)
*/
extern int udp_socket_send_dontwait(struct UdpSocket *sock, uint8_t *buffer, uint16_t len);

/**
* Receive a UDP packet, dont wait.
* @param[in] network pointer to UdpSocket struct
Expand Down
10 changes: 5 additions & 5 deletions sw/airborne/modules/computer_vision/lib/encoding/rtp.c
Expand Up @@ -31,7 +31,7 @@

#include "rtp.h"

static void rtp_packet_send(struct udp_periph *udp, uint8_t *Jpeg, int JpegLen, uint32_t m_SequenceNumber,
static void rtp_packet_send(struct UdpSocket *udp, uint8_t *Jpeg, int JpegLen, uint32_t m_SequenceNumber,
uint32_t m_Timestamp, uint32_t m_offset, uint8_t marker_bit, int w, int h, uint8_t format_code, uint8_t quality_code,
uint8_t has_dri_header);

Expand Down Expand Up @@ -64,7 +64,7 @@ uint8_t JpegScanDataCh2B[KJpegCh2ScanDataLen] = {
* Send a test RTP frame
* @param[in] *udp The udp connection to send the test frame over
*/
void rtp_frame_test(struct udp_periph *udp)
void rtp_frame_test(struct UdpSocket *udp)
{
static uint32_t framecounter = 0;
static uint32_t timecounter = 0;
Expand Down Expand Up @@ -94,7 +94,7 @@ void rtp_frame_test(struct udp_periph *udp)
* @param[in] has_dri_header Whether we have an DRI header or not
* @param[in] delta_t Time between images (if set to 0 or less it is calculated)
*/
void rtp_frame_send(struct udp_periph *udp, struct image_t *img, uint8_t format_code,
void rtp_frame_send(struct UdpSocket *udp, struct image_t *img, uint8_t format_code,
uint8_t quality_code, uint8_t has_dri_header, uint32_t delta_t)
{
static uint32_t packetcounter = 0;
Expand Down Expand Up @@ -156,7 +156,7 @@ void rtp_frame_send(struct udp_periph *udp, struct image_t *img, uint8_t format_
* @param[in] has_dri_header Whether we have an DRI header or not
*/
static void rtp_packet_send(
struct udp_periph *udp,
struct UdpSocket *udp,
uint8_t *Jpeg, int JpegLen,
uint32_t m_SequenceNumber, uint32_t m_Timestamp,
uint32_t m_offset, uint8_t marker_bit,
Expand Down Expand Up @@ -234,5 +234,5 @@ static void rtp_packet_send(
// append the JPEG scan data to the RTP buffer
memcpy(&RtpBuf[20], Jpeg, JpegLen);

udp_send_raw(udp, RtpBuf, RtpPacketSize);
udp_socket_send_dontwait(udp, RtpBuf, RtpPacketSize);
};
6 changes: 3 additions & 3 deletions sw/airborne/modules/computer_vision/lib/encoding/rtp.h
Expand Up @@ -30,10 +30,10 @@

#include "std.h"
#include "lib/vision/image.h"
#include "mcu_periph/udp.h"
#include "udp_socket.h"

void rtp_frame_send(struct udp_periph *udp, struct image_t *img, uint8_t format_code, uint8_t quality_code,
void rtp_frame_send(struct UdpSocket *udp, struct image_t *img, uint8_t format_code, uint8_t quality_code,
uint8_t has_dri_header, uint32_t delta_t);
void rtp_frame_test(struct udp_periph *udp);
void rtp_frame_test(struct UdpSocket *udp);

#endif /* _CV_ENCODING_RTP_H */
8 changes: 5 additions & 3 deletions sw/airborne/modules/computer_vision/viewvideo.c
Expand Up @@ -38,13 +38,13 @@
#include <unistd.h>
#include <sys/time.h>
#include <math.h>
#include "mcu_periph/udp.h"

// Video
#include "lib/v4l/v4l2.h"
#include "lib/vision/image.h"
#include "lib/encoding/jpeg.h"
#include "lib/encoding/rtp.h"
#include "udp_socket.h"

// Threaded computer vision
#include <pthread.h>
Expand Down Expand Up @@ -107,7 +107,6 @@ PRINT_CONFIG_VAR(VIEWVIDEO_SHOT_PATH)
PRINT_CONFIG_MSG("[viewvideo] Using netcat.")
#else
PRINT_CONFIG_MSG("[viewvideo] Using RTP/UDP stream.")
PRINT_CONFIG_VAR(VIEWVIDEO_DEV)
#endif

/* These are defined with configure */
Expand Down Expand Up @@ -160,6 +159,9 @@ static void *viewvideo_thread(void *data __attribute__((unused)))
#if VIEWVIDEO_USE_NETCAT
char nc_cmd[64];
sprintf(nc_cmd, "nc %s %d 2>/dev/null", STRINGIFY(VIEWVIDEO_HOST), VIEWVIDEO_PORT_OUT);
#else
struct UdpSocket video_sock;
udp_socket_create(&video_sock, STRINGIFY(VIEWVIDEO_HOST), VIEWVIDEO_PORT_OUT, -1, VIEWVIDEO_BROADCAST);
#endif

// Start streaming
Expand Down Expand Up @@ -241,7 +243,7 @@ static void *viewvideo_thread(void *data __attribute__((unused)))
#else
// Send image with RTP
rtp_frame_send(
&VIEWVIDEO_DEV, // UDP device
&video_sock, // UDP socket
&img_jpeg,
0, // Format 422
VIEWVIDEO_QUALITY_FACTOR, // Jpeg-Quality
Expand Down

0 comments on commit fd77a7c

Please sign in to comment.