1/*
2 * Copyright 2011, Gabriel Hartmann, gabriel.hartmann@gmail.com.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "UVCDeframer.h"
8
9#include "CamDebug.h"
10#include "CamDevice.h"
11
12#include <Autolock.h>
13
14
15#define MAX_TAG_LEN CAMDEFRAMER_MAX_TAG_LEN
16#define MAXFRAMEBUF CAMDEFRAMER_MAX_QUEUED_FRAMES
17
18
19UVCDeframer::UVCDeframer(CamDevice* device)
20	: CamDeframer(device),
21	fFrameCount(0),
22	fID(0)
23{
24}
25
26
27UVCDeframer::~UVCDeframer()
28{
29}
30
31
32ssize_t
33UVCDeframer::Write(const void* buffer, size_t size)
34{
35	const uint8* buf = (const uint8*)buffer;
36	int payloadSize = size - buf[0]; // total length - header length
37
38	// This packet is just a header
39	if (size == buf[0])
40		return 0;
41
42	// Allocate frame
43	if (!fCurrentFrame) {
44		BAutolock l(fLocker);
45		if (fFrames.CountItems() < MAXFRAMEBUF)
46			fCurrentFrame = AllocFrame();
47		else {
48			printf("Dropped %ld bytes. Too many queued frames.)\n", size);
49			return size;
50		}
51	}
52
53	// Write payload to buffer
54	fInputBuffer.Write(&buf[buf[0]], payloadSize);
55
56	// If end of frame add frame to list of frames
57	if ((buf[1] & 2) || (buf[1] & 1) != fID) {
58		fID = buf[1] & 1;
59		fFrameCount++;
60		buf = (uint8*)fInputBuffer.Buffer();
61		fCurrentFrame->Write(buf, fInputBuffer.BufferLength());
62		fFrames.AddItem(fCurrentFrame);
63		release_sem(fFrameSem);
64		fCurrentFrame = NULL;
65	}
66
67	return size;
68}
69
70
71void
72UVCDeframer::_PrintBuffer(const void* buffer, size_t size)
73{
74	uint8* b = (uint8*)buffer;
75	for (size_t i = 0; i < size; i++)
76		printf("0x%x\t", b[i]);
77	printf("\n");
78}
79
80