MediaClient: Add play and test options
This commit is contained in:
@@ -6,5 +6,7 @@ UsePrivateHeaders [ FDirName media experimental ] ;
|
|||||||
|
|
||||||
SimpleTest media_client :
|
SimpleTest media_client :
|
||||||
media_client.cpp
|
media_client.cpp
|
||||||
|
MediaPlay.cpp
|
||||||
|
MediaTest.cpp
|
||||||
: libmedia.so be root [ TargetLibsupc++ ]
|
: libmedia.so be root [ TargetLibsupc++ ]
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017, Dario Casalinuovo. All rights reserved.
|
||||||
|
* Copyright 2005, Marcus Overhagen, [email protected]. All rights reserved.
|
||||||
|
* Copyright 2005, Jérôme Duval. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MediaPlay.h"
|
||||||
|
|
||||||
|
#include <Entry.h>
|
||||||
|
#include <MediaFile.h>
|
||||||
|
#include <MediaTrack.h>
|
||||||
|
#include <OS.h>
|
||||||
|
#include <SoundPlayer.h>
|
||||||
|
#include <Url.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
thread_id reader = -1;
|
||||||
|
sem_id finished = -1;
|
||||||
|
BMediaTrack* playTrack;
|
||||||
|
media_format playFormat;
|
||||||
|
BSoundPlayer* player = 0;
|
||||||
|
volatile bool interrupt = false;
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
play_buffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format)
|
||||||
|
{
|
||||||
|
int64 frames = 0;
|
||||||
|
|
||||||
|
// Use your feeling, Obi-Wan, and find him you will.
|
||||||
|
playTrack->ReadFrames(buffer, &frames);
|
||||||
|
|
||||||
|
if (frames <=0) {
|
||||||
|
player->SetHasData(false);
|
||||||
|
release_sem(finished);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
keyb_int(int)
|
||||||
|
{
|
||||||
|
// Are you threatening me, Master Jedi?
|
||||||
|
interrupt = true;
|
||||||
|
release_sem(finished);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int media_play(const char* uri)
|
||||||
|
{
|
||||||
|
BUrl url;
|
||||||
|
entry_ref ref;
|
||||||
|
BMediaFile* playFile;
|
||||||
|
|
||||||
|
if (get_ref_for_path(uri, &ref) != B_OK) {
|
||||||
|
url.SetUrlString(uri);
|
||||||
|
if (url.IsValid()) {
|
||||||
|
playFile = new BMediaFile(url);
|
||||||
|
} else
|
||||||
|
return 2;
|
||||||
|
} else
|
||||||
|
playFile = new BMediaFile(&ref);
|
||||||
|
|
||||||
|
if (playFile->InitCheck() != B_OK) {
|
||||||
|
delete playFile;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < playFile->CountTracks(); i++) {
|
||||||
|
BMediaTrack* track = playFile->TrackAt(i);
|
||||||
|
playFormat.type = B_MEDIA_RAW_AUDIO;
|
||||||
|
if ((track->DecodedFormat(&playFormat) == B_OK)
|
||||||
|
&& (playFormat.type == B_MEDIA_RAW_AUDIO)) {
|
||||||
|
playTrack = track;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (track)
|
||||||
|
playFile->ReleaseTrack(track);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Good relations with the Wookiees, I have.
|
||||||
|
signal(SIGINT, keyb_int);
|
||||||
|
|
||||||
|
finished = create_sem(0, "finish wait");
|
||||||
|
|
||||||
|
printf("Playing file...\n");
|
||||||
|
|
||||||
|
// Execute Plan 66!
|
||||||
|
player = new BSoundPlayer(&playFormat.u.raw_audio, "playfile", play_buffer);
|
||||||
|
player->SetVolume(1.0f);
|
||||||
|
|
||||||
|
// Join me, Padmé and together we can rule this galaxy.
|
||||||
|
player->SetHasData(true);
|
||||||
|
player->Start();
|
||||||
|
|
||||||
|
acquire_sem(finished);
|
||||||
|
|
||||||
|
if (interrupt == true) {
|
||||||
|
// Once more, the Sith will rule the galaxy.
|
||||||
|
printf("Interrupted\n");
|
||||||
|
player->Stop();
|
||||||
|
kill_thread(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Playback finished.\n");
|
||||||
|
|
||||||
|
delete player;
|
||||||
|
delete playFile;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017, Dario Casalinuovo. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MEDIA_CLIENT_PLAY
|
||||||
|
#define _MEDIA_CLIENT_PLAY
|
||||||
|
|
||||||
|
int media_play(const char* uri);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017, Dario Casalinuovo. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MediaTest.h"
|
||||||
|
|
||||||
|
#include <SimpleMediaClient.h>
|
||||||
|
#include <MediaConnection.h>
|
||||||
|
#include <SupportDefs.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <debug.h>
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define DELAYED_MODE 1
|
||||||
|
#define SNOOZE_FOR 10000000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static BSimpleMediaClient* sProducer = NULL;
|
||||||
|
static BSimpleMediaClient* sConsumer = NULL;
|
||||||
|
static BSimpleMediaClient* sFilter = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
_InitClients(bool hasFilter)
|
||||||
|
{
|
||||||
|
sProducer = new BSimpleMediaClient("MediaClientProducer");
|
||||||
|
sConsumer = new BSimpleMediaClient("MediaClientConsumer");
|
||||||
|
|
||||||
|
if (hasFilter)
|
||||||
|
sFilter = new BSimpleMediaClient("MediaClientFilter");
|
||||||
|
else
|
||||||
|
sFilter = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
_DeleteClients()
|
||||||
|
{
|
||||||
|
delete sProducer;
|
||||||
|
delete sConsumer;
|
||||||
|
delete sFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
media_format
|
||||||
|
_BuildRawAudioFormat()
|
||||||
|
{
|
||||||
|
media_format format;
|
||||||
|
format.type = B_MEDIA_RAW_AUDIO;
|
||||||
|
format.u.raw_audio = media_multi_audio_format::wildcard;
|
||||||
|
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
_ConsumerProducerTest()
|
||||||
|
{
|
||||||
|
_InitClients(false);
|
||||||
|
|
||||||
|
BSimpleMediaOutput* output = sProducer->BeginOutput();
|
||||||
|
BSimpleMediaInput* input = sConsumer->BeginInput();
|
||||||
|
|
||||||
|
output->SetAcceptedFormat(_BuildRawAudioFormat());
|
||||||
|
input->SetAcceptedFormat(_BuildRawAudioFormat());
|
||||||
|
|
||||||
|
assert(sConsumer->Connect(input, output) == B_OK);
|
||||||
|
|
||||||
|
#ifdef DELAYED_MODE
|
||||||
|
snooze(SNOOZE_FOR);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(sConsumer->Disconnect() == B_OK);
|
||||||
|
|
||||||
|
_DeleteClients();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
_ProducerConsumerTest()
|
||||||
|
{
|
||||||
|
_InitClients(false);
|
||||||
|
|
||||||
|
BMediaOutput* output = sProducer->BeginOutput();
|
||||||
|
BMediaInput* input = sConsumer->BeginInput();
|
||||||
|
|
||||||
|
assert(sProducer->Connect(output, input) == B_OK);
|
||||||
|
|
||||||
|
#ifdef DELAYED_MODE
|
||||||
|
snooze(SNOOZE_FOR);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(sProducer->Disconnect() == B_OK);
|
||||||
|
|
||||||
|
_DeleteClients();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
_ProducerFilterConsumerTest()
|
||||||
|
{
|
||||||
|
_InitClients(true);
|
||||||
|
|
||||||
|
BMediaOutput* output = sProducer->BeginOutput();
|
||||||
|
BMediaInput* input = sConsumer->BeginInput();
|
||||||
|
|
||||||
|
BMediaInput* filterInput = sFilter->BeginInput();
|
||||||
|
BMediaOutput* filterOutput = sFilter->BeginOutput();
|
||||||
|
|
||||||
|
assert(sFilter->Bind(filterInput, filterOutput) == B_OK);
|
||||||
|
|
||||||
|
assert(sProducer->Connect(output, filterInput) == B_OK);
|
||||||
|
assert(sFilter->Connect(filterOutput, input) == B_OK);
|
||||||
|
#ifdef DELAYED_MODE
|
||||||
|
snooze(SNOOZE_FOR);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(sFilter->Disconnect() == B_OK);
|
||||||
|
|
||||||
|
_DeleteClients();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
media_test()
|
||||||
|
{
|
||||||
|
printf("Testing Simple (1:1) Producer-Consumer configuration: ");
|
||||||
|
_ConsumerProducerTest();
|
||||||
|
_ProducerConsumerTest();
|
||||||
|
printf("OK\n");
|
||||||
|
|
||||||
|
printf("Testing Simple (1:1:1) Producer-Filter-Consumer configuration: ");
|
||||||
|
_ProducerFilterConsumerTest();
|
||||||
|
printf("OK\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2017, Dario Casalinuovo. All rights reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MEDIA_CLIENT_TEST
|
||||||
|
#define _MEDIA_CLIENT_TEST
|
||||||
|
|
||||||
|
void media_test();
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -1,133 +1,43 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016, Dario Casalinuovo. All rights reserved.
|
* Copyright 2016-2017, Dario Casalinuovo. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <SimpleMediaClient.h>
|
#include <Application.h>
|
||||||
#include <MediaConnection.h>
|
|
||||||
#include <SupportDefs.h>
|
|
||||||
|
|
||||||
#include <debug.h>
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef DEBUG
|
#include "MediaPlay.h"
|
||||||
#define DELAYED_MODE 1
|
#include "MediaTest.h"
|
||||||
#define SNOOZE_FOR 10000000
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static BSimpleMediaClient* sProducer = NULL;
|
void print_usage()
|
||||||
static BSimpleMediaClient* sConsumer = NULL;
|
|
||||||
static BSimpleMediaClient* sFilter = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
void _InitClients(bool hasFilter)
|
|
||||||
{
|
{
|
||||||
sProducer = new BSimpleMediaClient("MediaClientProducer");
|
printf("Usage:\n");
|
||||||
sConsumer = new BSimpleMediaClient("MediaClientConsumer");
|
printf(" media_client play <uri>\n");
|
||||||
|
printf(" media_client test\n");
|
||||||
if (hasFilter)
|
|
||||||
sFilter = new BSimpleMediaClient("MediaClientFilter");
|
|
||||||
else
|
|
||||||
sFilter = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void _DeleteClients()
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
delete sProducer;
|
if (argc < 2) {
|
||||||
delete sConsumer;
|
print_usage();
|
||||||
delete sFilter;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BApplication* app = new BApplication(
|
||||||
media_format _BuildRawAudioFormat()
|
"application/x-vnd.Haiku-media_client");
|
||||||
{
|
|
||||||
media_format format;
|
int ret = 0;
|
||||||
format.type = B_MEDIA_RAW_AUDIO;
|
if (strcmp(argv[1], "play") == 0) {
|
||||||
format.u.raw_audio = media_multi_audio_format::wildcard;
|
if (argc < 3) {
|
||||||
|
print_usage();
|
||||||
return format;
|
} else
|
||||||
}
|
ret = media_play(argv[2]);
|
||||||
|
} else if (strcmp(argv[1], "test") == 0)
|
||||||
|
media_test();
|
||||||
void _ConsumerProducerTest()
|
|
||||||
{
|
return ret;
|
||||||
_InitClients(false);
|
|
||||||
|
|
||||||
BSimpleMediaOutput* output = sProducer->BeginOutput();
|
|
||||||
BSimpleMediaInput* input = sConsumer->BeginInput();
|
|
||||||
|
|
||||||
output->SetAcceptedFormat(_BuildRawAudioFormat());
|
|
||||||
input->SetAcceptedFormat(_BuildRawAudioFormat());
|
|
||||||
|
|
||||||
assert(sConsumer->Connect(input, output) == B_OK);
|
|
||||||
|
|
||||||
#ifdef DELAYED_MODE
|
|
||||||
snooze(SNOOZE_FOR);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
assert(sConsumer->Disconnect() == B_OK);
|
|
||||||
|
|
||||||
_DeleteClients();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void _ProducerConsumerTest()
|
|
||||||
{
|
|
||||||
_InitClients(false);
|
|
||||||
|
|
||||||
BMediaOutput* output = sProducer->BeginOutput();
|
|
||||||
BMediaInput* input = sConsumer->BeginInput();
|
|
||||||
|
|
||||||
assert(sProducer->Connect(output, input) == B_OK);
|
|
||||||
|
|
||||||
#ifdef DELAYED_MODE
|
|
||||||
snooze(SNOOZE_FOR);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
assert(sProducer->Disconnect() == B_OK);
|
|
||||||
|
|
||||||
_DeleteClients();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void _ProducerFilterConsumerTest()
|
|
||||||
{
|
|
||||||
_InitClients(true);
|
|
||||||
|
|
||||||
BMediaOutput* output = sProducer->BeginOutput();
|
|
||||||
BMediaInput* input = sConsumer->BeginInput();
|
|
||||||
|
|
||||||
BMediaInput* filterInput = sFilter->BeginInput();
|
|
||||||
BMediaOutput* filterOutput = sFilter->BeginOutput();
|
|
||||||
|
|
||||||
assert(sFilter->Bind(filterInput, filterOutput) == B_OK);
|
|
||||||
|
|
||||||
assert(sProducer->Connect(output, filterInput) == B_OK);
|
|
||||||
assert(sFilter->Connect(filterOutput, input) == B_OK);
|
|
||||||
#ifdef DELAYED_MODE
|
|
||||||
snooze(SNOOZE_FOR);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
assert(sFilter->Disconnect() == B_OK);
|
|
||||||
|
|
||||||
_DeleteClients();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
printf("Testing Simple (1:1) Producer-Consumer configuration: ");
|
|
||||||
_ConsumerProducerTest();
|
|
||||||
_ProducerConsumerTest();
|
|
||||||
printf("OK\n");
|
|
||||||
|
|
||||||
printf("Testing Simple (1:1:1) Producer-Filter-Consumer configuration: ");
|
|
||||||
_ProducerFilterConsumerTest();
|
|
||||||
printf("OK\n");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user