AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
|
AiC audio player, reads an audio stream from the vm and transfers it to ffmpeg. More...
#include <errno.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avassert.h>
#include <libavutil/avutil.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/dict.h>
#include <libavutil/error.h>
#include <libavutil/mem.h>
#include <libavutil/rational.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/audio_fifo.h>
#include <libavutil/frame.h>
#include <libavutil/opt.h>
#include "socket.h"
#include "logger.h"
#include "config_env.h"
Go to the source code of this file.
Data Structures | |
struct | OutputStream |
A wrapper around a single output AVStream. More... | |
Macros | |
#define | LOG_TAG "audio" |
#define | ANDROIDINCLOUD_PCM_CLIENT_PORT 24296 |
Typedefs | |
typedef struct OutputStream | OutputStream |
Functions | |
const char * | get_error_text (const int error) |
void | init_packet (AVPacket *packet) |
int | init_input_frame (AVFrame **frame) |
int | init_resampler (AVCodecContext *input_codec_context, AVCodecContext *output_codec_context, SwrContext **resample_context) |
int | init_fifo (AVAudioFifo **fifo, AVCodecContext *output_codec_context) |
int | decode_audio_frame (AVFrame *frame, AVFormatContext *input_format_context, AVCodecContext *input_codec_context, int *data_present, int *finished) |
int | init_converted_samples (uint8_t ***converted_input_samples, AVCodecContext *output_codec_context, int frame_size) |
int | convert_samples (const uint8_t **input_data, uint8_t **converted_data, const int frame_size, SwrContext *resample_context) |
int | add_samples_to_fifo (AVAudioFifo *fifo, uint8_t **converted_input_samples, const int frame_size) |
int | init_output_frame (AVFrame **frame, AVCodecContext *output_codec_context, int frame_size) |
int | encode_audio_frame (AVFrame *frame, AVFormatContext *output_format_context, AVCodecContext *output_codec_context, int *data_present) |
int | load_encode_and_write (AVAudioFifo *fifo, AVFormatContext *output_format_context, AVCodecContext *output_codec_context) |
int | write_output_file_trailer (AVFormatContext *output_format_context) |
void | audiolive_decode_encode (AVFormatContext *oc, OutputStream *ost, unsigned char *inbuf, int num_read, int size) |
void * | aic_audioplayer (char *vmip) |
int | main () |
AiC audio player, reads an audio stream from the vm and transfers it to ffmpeg.
Based on ffmpeg API examples..
Definition in file player_audio.c.
#define ANDROIDINCLOUD_PCM_CLIENT_PORT 24296 |
Port open on the VM
Definition at line 39 of file player_audio.c.
#define LOG_TAG "audio" |
Definition at line 36 of file player_audio.c.
typedef struct OutputStream OutputStream |
int add_samples_to_fifo | ( | AVAudioFifo * | fifo, |
uint8_t ** | converted_input_samples, | ||
const int | frame_size | ||
) |
Add converted input audio samples to the FIFO buffer for later processing.
Make the FIFO as large as it needs to be to hold both, the old and the new samples.
Store the new samples in the FIFO buffer.
Definition at line 231 of file player_audio.c.
void* aic_audioplayer | ( | char * | vmip | ) |
Definition at line 784 of file player_audio.c.
void audiolive_decode_encode | ( | AVFormatContext * | oc, |
OutputStream * | ost, | ||
unsigned char * | inbuf, | ||
int | num_read, | ||
int | size | ||
) |
Use the encoder's desired frame size for processing.
Make sure that there is one frame worth of samples in the FIFO buffer so that the encoder can do its work. Since the decoder's and the encoder's frame size may differ, we need to FIFO buffer to store as many frames worth of input samples that they make up at least one frame worth of output samples.
Decode one frame worth of audio samples, convert it to the output sample format and put it into the FIFO buffer.
Convert the input samples to the desired output sample format. This requires a temporary storage provided by converted_input_samples.
Add the converted input samples to the FIFO buffer for later processing.
If we are at the end of the input file, we continue encoding the remaining audio samples to the output file.
If we have enough samples for the encoder, we encode them. At the end of the file, we pass the remaining samples to the encoder.
Take one frame worth of audio samples from the FIFO buffer, encode it and write it to the output file.
Definition at line 576 of file player_audio.c.
int convert_samples | ( | const uint8_t ** | input_data, |
uint8_t ** | converted_data, | ||
const int | frame_size, | ||
SwrContext * | resample_context | ||
) |
Convert the input audio samples into the output sample format. The conversion happens on a per-frame basis, the size of which is specified by frame_size.
Convert the samples using the resampler.
Definition at line 214 of file player_audio.c.
int decode_audio_frame | ( | AVFrame * | frame, |
AVFormatContext * | input_format_context, | ||
AVCodecContext * | input_codec_context, | ||
int * | data_present, | ||
int * | finished | ||
) |
Decode one audio frame from the input file.
Packet used for temporary storage.
Read one audio frame from the input file into a temporary packet.
If we are at the end of the file, flush the decoder below.
Decode the audio frame stored in the temporary packet. The input audio stream decoder is used to do this. If we are at the end of the file, pass an empty packet to the decoder to flush it.
If the decoder has not been flushed completely, we are not finished, so that this function has to be called again.
Definition at line 126 of file player_audio.c.
int encode_audio_frame | ( | AVFrame * | frame, |
AVFormatContext * | output_format_context, | ||
AVCodecContext * | output_codec_context, | ||
int * | data_present | ||
) |
Encode one frame worth of audio to the output file.
Packet used for temporary storage.
Set a timestamp based on the sample rate for the container.
Encode the audio frame and store it in the temporary packet. The output audio stream encoder is used to do this.
Write one audio frame from the temporary packet to the output file.
Definition at line 299 of file player_audio.c.
const char* get_error_text | ( | const int | error | ) |
int init_converted_samples | ( | uint8_t *** | converted_input_samples, |
AVCodecContext * | output_codec_context, | ||
int | frame_size | ||
) |
Initialize a temporary storage for the specified number of audio samples. The conversion requires temporary storage due to the different format. The number of audio samples to be allocated is specified in frame_size.
Allocate as many pointers as there are audio channels. Each pointer will later point to the audio samples of the corresponding channels (although it may be NULL for interleaved formats).
Allocate memory for the samples of all channels in one consecutive block for convenience.
Definition at line 176 of file player_audio.c.
int init_fifo | ( | AVAudioFifo ** | fifo, |
AVCodecContext * | output_codec_context | ||
) |
Initialize a FIFO buffer for the audio samples to be encoded.
Create the FIFO buffer based on the specified output sample format.
Definition at line 113 of file player_audio.c.
int init_input_frame | ( | AVFrame ** | frame | ) |
Initialize one audio frame for reading from the input file
Definition at line 58 of file player_audio.c.
int init_output_frame | ( | AVFrame ** | frame, |
AVCodecContext * | output_codec_context, | ||
int | frame_size | ||
) |
Initialize one input frame for writing to the output file. The frame will be exactly frame_size samples large.
Create a new frame to store the audio samples.
Set the frame's parameters, especially its size and format. av_frame_get_buffer needs this to allocate memory for the audio samples of the frame. Default channel layouts based on the number of channels are assumed for simplicity.
Allocate the samples of the created frame. This call will make sure that the audio frame can hold as many samples as specified.
Definition at line 258 of file player_audio.c.
void init_packet | ( | AVPacket * | packet | ) |
Initialize one data packet for reading or writing.
Set the packet data and size so that it is recognized as being empty.
Definition at line 49 of file player_audio.c.
int init_resampler | ( | AVCodecContext * | input_codec_context, |
AVCodecContext * | output_codec_context, | ||
SwrContext ** | resample_context | ||
) |
Initialize the audio resampler based on the input and output codec settings. If the input and output sample formats differ, a conversion is required libswresample takes care of this, but requires initialization.
Create a resampler context for the conversion. Set the conversion parameters. Default channel layouts based on the number of channels are assumed for simplicity (they are sometimes not detected properly by the demuxer and/or decoder).
Perform a sanity check so that the number of converted samples is not greater than the number of samples to be converted. If the sample rates differ, this case has to be handled differently
Open the resampler with the specified parameters.
Definition at line 73 of file player_audio.c.
int load_encode_and_write | ( | AVAudioFifo * | fifo, |
AVFormatContext * | output_format_context, | ||
AVCodecContext * | output_codec_context | ||
) |
Load one audio frame from the FIFO buffer, encode and write it to the output file.
Temporary storage of the output samples of the frame written to the file.
Use the maximum number of possible samples per frame. If there is less than the maximum possible frame size in the FIFO buffer use this number. Otherwise, use the maximum possible frame size
Initialize temporary storage for one output frame.
Read as many samples from the FIFO buffer as required to fill the frame. The samples are stored in the frame temporarily.
Encode one frame worth of audio samples.
Definition at line 346 of file player_audio.c.
int main | ( | ) |
int write_output_file_trailer | ( | AVFormatContext * | output_format_context | ) |
Write the trailer of the output file container.
Definition at line 386 of file player_audio.c.