/**************************************************************************************************************** * Function: ReceiveNetworkMessage * Desc: Reads the next NetworkMessage from the connection. New message is allocated and must be release by the * caller with FreeNetworkMessage() * Params: * h - Pointer to URLContext struct used to store all connection info associated with this connection * message - Address of pointer to network message. This will be allocated by this function * Return: * 0 on success, non 0 on failure ****************************************************************************************************************/ static int ReceiveNetworkMessage( URLContext *h, NetworkMessage **message ) { int retVal = 0; /* Allocate a new NetworkMessage struct */ *message = av_mallocz( sizeof(NetworkMessage) ); if( *message == NULL ) return AVERROR(ENOMEM); if( (retVal = ReadNetworkMessageHeader( h, &(*message)->header )) != 0 ) { FreeNetworkMessage( message ); return retVal; } if( (retVal = ReadNetworkMessageBody( h, *message )) != 0 ) { FreeNetworkMessage( message ); return retVal; } return 0; }
static int dspicReadPacket( AVFormatContext *s, AVPacket *pkt ) { AVIOContext * ioContext = s->pb; int retVal = 0; MessageHeader header; int dataSize = 0; struct DMImageData * videoFrameData = NULL; AVStream * stream = NULL; struct ADFrameData * frameData = NULL; enum ADFrameType frameType = FrameTypeUnknown; /* Attempt to read in a network message header */ if( (retVal = ReadNetworkMessageHeader( ioContext, &header )) != 0 ) return retVal; /* Validate the header */ if( header.magicNumber == DSPacketHeaderMagicNumber ) { if( header.messageType == TCP_SRV_NUDGE ) { frameType = DMNudge; /* Read any extra bytes then try again */ dataSize = header.length - (SIZEOF_MESSAGE_HEADER_IO - sizeof(unsigned long)); if( (retVal = ad_new_packet( pkt, dataSize )) < 0 ) return retVal; if( avio_read( ioContext, pkt->data, dataSize ) != dataSize ) return AVERROR(EIO); } else if( header.messageType == TCP_SRV_IMG_DATA ) { frameType = DMVideo; /* This should be followed by a jfif image */ dataSize = header.length - (SIZEOF_MESSAGE_HEADER_IO - sizeof(unsigned long)); /* Allocate packet data large enough for what we have */ if( (retVal = ad_new_packet( pkt, dataSize )) < 0 ) return retVal; /* Read the jfif data out of the buffer */ if( avio_read( ioContext, pkt->data, dataSize ) != dataSize ) return AVERROR(EIO); /* Now extract the frame info that's in there */ if( (videoFrameData = parseDSJFIFHeader( pkt->data, dataSize )) == NULL ) return AVERROR(EIO); /* if( audioFrameData != NULL ) frameType |= DS1_PACKET_TYPE_AUDIO; */ if ( (stream = ad_get_vstream(s, 0, 0, 1, PIC_MODE_JPEG_422, NULL)) == NULL ) return AVERROR(EIO); } } else return AVERROR(EIO); /* Now create a wrapper to hold this frame's data which we'll store in the packet's private member field */ if( (frameData = av_malloc( sizeof(*frameData) )) != NULL ) { frameData->frameType = frameType; frameData->frameData = videoFrameData; frameData->additionalData = NULL; pkt->priv = frameData; } else goto fail_mem; pkt->stream_index = ( stream != NULL ) ? stream->index : 0; pkt->duration = ((int)(AV_TIME_BASE * 1.0)); return retVal; fail_mem: /* Make sure everything that might have been allocated is released before we return... */ av_free( frameData ); av_free( videoFrameData ); return AVERROR(ENOMEM); }