xref: /haiku/src/bin/media_client/MediaPlay.cpp (revision 1a3518cf757c2da8006753f83962da5935bbc82b)
1 /*
2  * Copyright 2017, Dario Casalinuovo. All rights reserved.
3  * Copyright 2005, Marcus Overhagen, marcus@overhagen.de. All rights reserved.
4  * Copyright 2005, Jérôme Duval. All rights reserved.
5  * Distributed under the terms of the MIT License.
6  */
7 
8 #include "MediaPlay.h"
9 
10 #include <Entry.h>
11 #include <MediaFile.h>
12 #include <MediaTrack.h>
13 #include <OS.h>
14 #include <SoundPlayer.h>
15 #include <Url.h>
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 
22 thread_id reader = -1;
23 sem_id finished = -1;
24 BMediaTrack* playTrack;
25 media_format playFormat;
26 BSoundPlayer* player = 0;
27 volatile bool interrupt = false;
28 
29 
30 void
31 play_buffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format)
32 {
33 	int64 frames = 0;
34 
35 	// Use your feeling, Obi-Wan, and find him you will.
36 	playTrack->ReadFrames(buffer, &frames);
37 
38 	if (frames <=0) {
39 		player->SetHasData(false);
40 		release_sem(finished);
41 	}
42 }
43 
44 
45 void
46 keyb_int(int)
47 {
48 	// Are you threatening me, Master Jedi?
49 	interrupt = true;
50 	release_sem(finished);
51 }
52 
53 
54 int media_play(const char* uri)
55 {
56 	BUrl url;
57 	entry_ref ref;
58 	BMediaFile* playFile;
59 
60 	if (get_ref_for_path(uri, &ref) != B_OK) {
61 		url.SetUrlString(uri);
62 		if (url.IsValid()) {
63 			playFile = new BMediaFile(url);
64 		} else
65 			return 2;
66 	} else
67 		playFile = new BMediaFile(&ref);
68 
69 	if (playFile->InitCheck() != B_OK) {
70 		delete playFile;
71 		return 2;
72 	}
73 
74 	for (int i = 0; i < playFile->CountTracks(); i++) {
75 		BMediaTrack* track = playFile->TrackAt(i);
76 		if (track != NULL) {
77 			playFormat.type = B_MEDIA_RAW_AUDIO;
78 			if ((track->DecodedFormat(&playFormat) == B_OK)
79 				&& (playFormat.type == B_MEDIA_RAW_AUDIO)) {
80 				playTrack = track;
81 				break;
82 			}
83 			playFile->ReleaseTrack(track);
84 		}
85 	}
86 
87 	// Good relations with the Wookiees, I have.
88 	signal(SIGINT, keyb_int);
89 
90 	finished = create_sem(0, "finish wait");
91 
92 	printf("Playing file...\n");
93 
94 	// Execute Plan 66!
95 	player = new BSoundPlayer(&playFormat.u.raw_audio, "playfile", play_buffer);
96 	player->SetVolume(1.0f);
97 
98 	// Join me, Padmé and together we can rule this galaxy.
99 	player->SetHasData(true);
100 	player->Start();
101 
102 	acquire_sem(finished);
103 
104 	if (interrupt == true) {
105 		// Once more, the Sith will rule the galaxy.
106 		printf("Interrupted\n");
107 		player->Stop();
108 		kill_thread(reader);
109 	}
110 
111 	printf("Playback finished.\n");
112 
113 	delete player;
114 	delete playFile;
115 
116 	return 0;
117 }
118