# VLC Overview

## Code modules

The VLC code uses modules and plugins. A module is a group of compiled-in C source files. They are linked against the main application at build time. At present, these modules are :

- Interface : this is the entry point of the program. It manages all user interactions and thread spawning.

- Input : it opens the input socket, reads packets, parses them and passes reconstituted elementary streams to the decoder(s).

- Video output : it initializes the video display. Then it gets all pictures and subpictures (ie. subtitles) from the decoder(s), optionally converts them to RGB format (from YUV), and displays them.

- Audio output : it initializes the audio mixer, ie. finds the right playing frequency, and then resamples audio frames received from the decoder(s).

- Misc : miscellaneous utilities used in other modules. This is the only module that will never launch a thread.

- ac3_decoder, audio_decoder, generic_decoder, lpcm_decoder, spu_decoder, video_decoder, video_parser : decoders used by VLC to decode different kinds of elementary stream data. [these are subject to move to `plugins/` in a forthcoming version]

Data flow between modules

## Plug-ins

Plugins are located in the `plugins/` subdirectory and are loaded at runtime. Every plug-in may offer different features that will best suit a particular file or a particular environment. Besides, most portability works result in the writing of an audio_output/video_output/interface plug-in to support a new platform (eg. BeOS or MacOS X).

Plug-ins are loaded and unloaded dynamically by functions in `src/misc/modules.c` and `include/modules*.h` . The API for writing plugins will be discussed in a following chapter.

Plugins can also be built into the VLC main application by changing the `BUILTINS` line in `Makefile.opts`.

## Threads

### Thread management

VLC is heavily multi-threaded. We chose against a single-thread approach because decoder preemptibility and scheduling would be a mastermind (for instance decoders and outputs have to be separated, otherwise it cannot be warrantied that a frame will be played at the exact presentation time), and we currently have no plan to support a single-threaded client. Multi-process decoders usually imply more overhead (problems of shared memory) and communication between processes is harder.

Our threading structure is modeled on pthreads. However, for portability reasons, we don't call `pthread_*` functions directly, but use a similar wrapper, made of `vlc_thread_create, vlc_thread_exit, vlc_thread_join, vlc_mutex_init, vlc_mutex_lock, vlc_mutex_unlock, vlc_mutex_destroy, vlc_cond_init, vlc_cond_signal, vlc_cond_broadcast, vlc_cond_wait, vlc_cond_destroy`, and structures vlc_thread_t, vlc_mutex_t, and vlc_cond_t.

### Synchronization

Another key feature of VLC is that decoding and playing are asynchronous : decoding is done by a *_decoder thread, playing is done by audio_output or video_output thread. The design goal is to ensure that an audio or video frame is played exactly at the right time, without blocking any of the decoder threads. This leads to a complex communication structure between the interface, the input, the decoders and the outputs.

Having several input and video_output threads reading multiple files at the same time is permitted, despite the fact that the current interface doesn't allow any way to do it [this is subject to change in the near future]. Anyway the client has been written from the ground up with this in mind. This also implies that a non-reentrant library (including in particular LiViD's libac3) cannot be used.

Presentation Time Stamps located in the system layer of the stream are passed to the decoders, and all resulting samples are dated accordingly. The output layers are supposed to play them at the right time. Dates are converted to microseconds ; an absolute date is the number of microseconds since Epoch (Jan 1st 1970). The mtime_t type is a signed 64-bit integer.

The current date can be retrieved with `mdate()`. Te execution of a thread can be suspended until a certain `date` via `mwait` `( mtime_t date )`. You can sleep for a fixed number of microseconds with `msleep` `( mtime_t delay )`.

Please remember to wake up a little while before the presentation date, if some particular treatment needs to be done (e.g. a YUV transform). For instance in `src/video_parser/vpar_synchro.c`, track of the average decoding times is kept to ensure pictures are not decoded too late.

## Code conventions

### Function naming

All functions are named accordingly : module name (in lower case) + _ + function name (in mixed case, without underscores). For instance : `intf_FooFunction`. Static functions don't need usage of the module name.

### Variable naming

Hungarian notations are used, that means we have the following prefixes :

- i_ for integers (sometimes l_ for long integers) ;

- b_ for booleans ;

- d_ for doubles (sometimes f_ for floats) ;

- pf_ for function pointers ;

- psz_ for a Pointer to a String terminated by a Zero (C-string) ;

- More generally, we add a p when the variable is a pointer to a type.

If one variable has no basic type (for instance a complex structure), don't put any prefix (except p_* if it's a pointer). After one prefix, put an explicit variable name in lower case. If several words are required, join them with an underscore (no mixed case). Examples :

- data_packet_t * `p_buffer;`

- char `psz_msg_date[42];`

- int `pi_es_refcount[MAX_ES];`

- void `(* pf_next_data_packet)( int * );`

### A few words about white spaces

First, never use tabs in the source (you're entitled to use them in the Makefile :-). Use `set expandtab` under vim or the equivalent under emacs. Indents are 4 spaces long.

Second, put spaces before and after operators, and inside brackets. For instance :

     for( i = 0; i < 12; i++, j += 42 );

Third, leave braces alone on their lines (GNU style). For instance :

    if( i_es == 42 )
    {
        p_buffer[0] = 0x12;
    }

We write C, so use C-style comments /* ... */.