summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTuomas Virtanen <katajakasa@gmail.com>2016-01-10 20:09:06 +0200
committerTuomas Virtanen <katajakasa@gmail.com>2016-01-10 20:09:06 +0200
commit8ee9b3e560a39d65c62a87fc797ddcc1279b3a78 (patch)
tree95fcffd406041b6ee09721e3d5d708588ab7de47 /examples
parentc29fb5865954310042aa833baf85064364883b28 (diff)
Cleanups + allow audio only playback
Diffstat (limited to 'examples')
-rw-r--r--examples/example_audio.c140
-rw-r--r--examples/example_video.c (renamed from examples/example_play.c)7
2 files changed, 147 insertions, 0 deletions
diff --git a/examples/example_audio.c b/examples/example_audio.c
new file mode 100644
index 0000000..d7fdd2a
--- /dev/null
+++ b/examples/example_audio.c
@@ -0,0 +1,140 @@
+#include <kitchensink/kitchensink.h>
+#include <SDL2/SDL.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+/*
+* Requires SDL2 2.0.4 !
+*
+* Note! This example does not do proper error handling etc.
+* It is for example use only!
+*/
+
+#define AUDIOBUFFER_SIZE 8192
+
+const char *stream_types[] = {
+ "KIT_STREAMTYPE_UNKNOWN",
+ "KIT_STREAMTYPE_VIDEO",
+ "KIT_STREAMTYPE_AUDIO",
+ "KIT_STREAMTYPE_DATA",
+ "KIT_STREAMTYPE_SUBTITLE",
+ "KIT_STREAMTYPE_ATTACHMENT"
+};
+
+int main(int argc, char *argv[]) {
+ int err = 0, ret = 0;
+ const char* filename = NULL;
+
+ // Events
+ bool run = true;
+
+ // Kitchensink
+ Kit_Source *src = NULL;
+ Kit_Player *player = NULL;
+
+ // Audio playback
+ SDL_AudioSpec wanted_spec, audio_spec;
+ SDL_AudioDeviceID audio_dev;
+ char audiobuf[AUDIOBUFFER_SIZE];
+
+ // Get filename to open
+ if(argc != 2) {
+ fprintf(stderr, "Usage: exampleplay <filename>\n");
+ return 0;
+ }
+ filename = argv[1];
+
+ // Init SDL
+ err = SDL_Init(SDL_INIT_AUDIO);
+ if(err != 0) {
+ fprintf(stderr, "Unable to initialize SDL!\n");
+ return 1;
+ }
+
+ Kit_Init(KIT_INIT_FORMATS|KIT_INIT_NETWORK);
+
+ // Open up the sourcefile.
+ src = Kit_CreateSourceFromUrl(filename);
+ if(src == NULL) {
+ fprintf(stderr, "Unable to load file '%s': %s\n", filename, Kit_GetError());
+ return 1;
+ }
+
+ // Print stream information
+ Kit_StreamInfo sinfo;
+ fprintf(stderr, "Source streams:\n");
+ for(int i = 0; i < Kit_GetSourceStreamCount(src); i++) {
+ err = Kit_GetSourceStreamInfo(src, &sinfo, i);
+ if(err) {
+ fprintf(stderr, "Unable to fetch stream #%d information: %s.\n", i, Kit_GetError());
+ return 1;
+ }
+ fprintf(stderr, " * Stream #%d: %s\n", i, stream_types[sinfo.type]);
+ }
+
+ // Create the player
+ player = Kit_CreatePlayer(src);
+ if(player == NULL) {
+ fprintf(stderr, "Unable to create player: %s\n", Kit_GetError());
+ return 1;
+ }
+
+ // Print some information
+ Kit_PlayerInfo pinfo;
+ Kit_GetPlayerInfo(player, &pinfo);
+
+ if(!pinfo.audio.is_enabled) {
+ fprintf(stderr, "File contains no audio!\n");
+ return 1;
+ }
+
+ fprintf(stderr, "Media information:\n");
+ fprintf(stderr, " * Audio: %s (%s), %dHz, %dch, %db, %s\n",
+ pinfo.acodec,
+ pinfo.acodec_name,
+ pinfo.audio.samplerate,
+ pinfo.audio.channels,
+ pinfo.audio.bytes,
+ pinfo.audio.is_signed ? "signed" : "unsigned");
+
+ // Init audio
+ SDL_memset(&wanted_spec, 0, sizeof(wanted_spec));
+ wanted_spec.freq = pinfo.audio.samplerate;
+ wanted_spec.format = AUDIO_S16SYS;
+ wanted_spec.channels = pinfo.audio.channels;
+ audio_dev = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &audio_spec, 0);
+ SDL_PauseAudioDevice(audio_dev, 0);
+
+ // Start playback
+ Kit_PlayerPlay(player);
+
+ while(run) {
+ if(Kit_GetPlayerState(player) == KIT_STOPPED) {
+ run = false;
+ continue;
+ }
+
+ // Refresh audio
+ ret = SDL_GetQueuedAudioSize(audio_dev);
+ if(ret < AUDIOBUFFER_SIZE) {
+ ret = Kit_GetAudioData(player, (unsigned char*)audiobuf, AUDIOBUFFER_SIZE);
+ if(ret > 0) {
+ SDL_LockAudio();
+ SDL_QueueAudio(audio_dev, audiobuf, ret);
+ SDL_UnlockAudio();
+ SDL_PauseAudioDevice(audio_dev, 0);
+ }
+ }
+
+ SDL_Delay(1);
+ }
+
+ SDL_CloseAudioDevice(audio_dev);
+
+ Kit_ClosePlayer(player);
+ Kit_CloseSource(src);
+
+ Kit_Quit();
+ SDL_Quit();
+ return 0;
+}
diff --git a/examples/example_play.c b/examples/example_video.c
index 1f31a6a..1afe957 100644
--- a/examples/example_play.c
+++ b/examples/example_video.c
@@ -132,6 +132,12 @@ int main(int argc, char *argv[]) {
// Print some information
Kit_PlayerInfo pinfo;
Kit_GetPlayerInfo(player, &pinfo);
+
+ if(!pinfo.video.is_enabled) {
+ fprintf(stderr, "File contains no video!\n");
+ return 1;
+ }
+
fprintf(stderr, "Media information:\n");
fprintf(stderr, " * Audio: %s (%s), %dHz, %dch, %db, %s\n",
pinfo.acodec,
@@ -170,6 +176,7 @@ int main(int argc, char *argv[]) {
// Set logical size for the renderer. This way when we scale, we keep aspect ratio.
SDL_RenderSetLogicalSize(renderer, pinfo.video.width, pinfo.video.height);
+ // Start playback
Kit_PlayerPlay(player);
while(run) {