1
0

PlaybackBase2.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. #include "PlaybackBase2.h"
  2. PlaybackImpl::PlaybackImpl()
  3. {
  4. playback=0;
  5. playback_parameters=0;
  6. }
  7. PlaybackImpl::~PlaybackImpl()
  8. {
  9. // TODO: decide if we need playback->Release() or not
  10. if (playback_parameters)
  11. playback_parameters->Release();
  12. }
  13. void PlaybackImpl::Connect(PlaybackBase2 *playback, ifc_playback_parameters *playback_parameters)
  14. {
  15. this->playback = playback;
  16. // TODO: decide if we need playback->Retain() or not
  17. this->playback_parameters = playback_parameters;
  18. if (playback_parameters)
  19. playback_parameters->Retain();
  20. }
  21. /* ---------- */
  22. PlaybackBase2::PlaybackBase2()
  23. {
  24. out=0;
  25. implementation=0;
  26. filelocker=0;
  27. paused=false;
  28. last_position=0;
  29. output_pointers=0;
  30. exact_length=false;
  31. memset(&parameters, 0, sizeof(parameters));
  32. }
  33. PlaybackBase2::~PlaybackBase2()
  34. {
  35. /* out should have hopefully been close already. just in case */
  36. if (out)
  37. out->Release();
  38. out=0;
  39. if (filelocker)
  40. filelocker->Release();
  41. delete implementation;
  42. free(output_pointers);
  43. }
  44. ns_error_t PlaybackBase2::Initialize(api_service *service_manager, PlaybackImpl *implementation, nx_uri_t filename, ifc_player *player)
  45. {
  46. service_manager->GetService(&filelocker);
  47. this->implementation = implementation;
  48. ns_error_t ret = PlaybackBase::Initialize(filename, player);
  49. if (ret != NErr_Success)
  50. return ret;
  51. implementation->Connect(this, secondary_parameters);
  52. this->ifc_playback::Retain(); /* the thread needs to hold a reference to this object so that it doesn't disappear out from under us */
  53. NXThreadCreate(&playback_thread, PlayerThreadFunction, this);
  54. return NErr_Success;
  55. }
  56. nx_thread_return_t PlaybackBase2::PlayerThreadFunction(nx_thread_parameter_t param)
  57. {
  58. PlaybackBase2 *playback = (PlaybackBase2 *)param;
  59. NXThreadCurrentSetPriority(NX_THREAD_PRIORITY_PLAYBACK);
  60. nx_thread_return_t ret = playback->DecodeLoop();
  61. playback->ifc_playback::Release(); /* give up the reference that was acquired before spawning the thread */
  62. return ret;
  63. }
  64. int PlaybackBase2::Init()
  65. {
  66. if (filelocker)
  67. filelocker->WaitForReadInterruptable(filename, this);
  68. ns_error_t ret = implementation->Open(filename);
  69. if (ret != NErr_Success)
  70. return ret;
  71. ifc_metadata *metadata;
  72. if (implementation->GetMetadata(&metadata) == NErr_Success)
  73. {
  74. player->SetMetadata(metadata);
  75. metadata->Release();
  76. }
  77. else
  78. player->SetMetadata(0);
  79. player->SetSeekable(implementation->IsSeekable()?1:0);
  80. double length;
  81. ret = implementation->GetLength(&length, &exact_length);
  82. if (ret == NErr_Success)
  83. player->SetLength(length);
  84. return NErr_Success;
  85. }
  86. nx_thread_return_t PlaybackBase2::DecodeLoop()
  87. {
  88. player->OnLoaded(filename);
  89. int ret = Init();
  90. if (ret != NErr_Success)
  91. {
  92. implementation->Close();
  93. if (filelocker)
  94. filelocker->UnlockFile(filename);
  95. player->OnError(ret);
  96. return 0;
  97. }
  98. player->OnReady();
  99. /* wait for Play (or Stop to abort) */
  100. for (;;)
  101. {
  102. ns_error_t ret = Wake(WAKE_PLAY|WAKE_STOP|WAKE_INTERRUPT);
  103. if (ret == WAKE_PLAY)
  104. {
  105. break;
  106. }
  107. else if (ret == WAKE_STOP)
  108. {
  109. player->OnStopped();
  110. goto cleanup;
  111. }
  112. else if (ret == WAKE_INTERRUPT)
  113. {
  114. ns_error_t ret = Internal_Interrupt();
  115. if (ret != NErr_Success)
  116. {
  117. implementation->Close();
  118. player->OnError(ret);
  119. goto cleanup;
  120. }
  121. }
  122. }
  123. /* at this point, we know that PLAY is on */
  124. for (;;)
  125. {
  126. int ret = Check(WAKE_STOP|WAKE_PAUSE|WAKE_INTERRUPT);
  127. if (ret == WAKE_PAUSE)
  128. {
  129. if (out)
  130. out->Pause(1);
  131. paused=true;
  132. continue; /* continue in case there's another wake reason */
  133. }
  134. else if (ret== WAKE_UNPAUSE)
  135. {
  136. if (out)
  137. out->Pause(0);
  138. paused=false;
  139. continue; /* continue in case there's another wake reason */
  140. }
  141. else if (ret == WAKE_STOP)
  142. {
  143. if (out)
  144. {
  145. out->Stop();
  146. out->Release();
  147. out=0;
  148. }
  149. player->OnStopped();
  150. goto cleanup;
  151. }
  152. else if (ret == WAKE_INTERRUPT)
  153. {
  154. ns_error_t ret = Internal_Interrupt();
  155. if (ret != NErr_Success)
  156. {
  157. implementation->Close();
  158. player->OnError(ret);
  159. goto cleanup;
  160. }
  161. continue;
  162. }
  163. Agave_Seek *seek = PlaybackBase::GetSeek();
  164. if (seek)
  165. {
  166. ns_error_t seek_error;
  167. double new_position;
  168. ns_error_t ret = implementation->Seek(seek, &seek_error, &new_position);
  169. if (ret != NErr_Success)
  170. {
  171. player->OnError(ret);
  172. goto cleanup;
  173. }
  174. if (out)
  175. out->Flush(new_position);
  176. player->OnSeekComplete(seek_error, new_position);
  177. PlaybackBase::FreeSeek(seek);
  178. }
  179. ret = implementation->DecodeStep();
  180. if (ret == NErr_EndOfFile)
  181. {
  182. if (out)
  183. out->Done();
  184. PlaybackBase::OnStopPlaying();
  185. player->OnEndOfFile();
  186. ret = WaitForClose();
  187. if (out)
  188. out->Release();
  189. out=0;
  190. if (ret != NErr_True)
  191. goto cleanup;
  192. }
  193. else if (ret != NErr_Success)
  194. {
  195. if (out)
  196. {
  197. out->Done();
  198. out->Release();
  199. out=0;
  200. }
  201. if (ret != NErr_False)
  202. player->OnError(NErr_Error); // TODO: find better error code
  203. goto cleanup;
  204. }
  205. else
  206. {
  207. if (!exact_length)
  208. {
  209. double length;
  210. ret = implementation->GetLength(&length, &exact_length);
  211. if (ret == NErr_Success)
  212. player->SetLength(length);
  213. }
  214. }
  215. }
  216. cleanup:
  217. implementation->Close();
  218. if (filelocker)
  219. filelocker->UnlockFile(filename);
  220. return 0;
  221. }
  222. ns_error_t PlaybackBase2::WaitForClose()
  223. {
  224. if (!out)
  225. {
  226. player->OnClosed();
  227. return NErr_False;
  228. }
  229. else for (;;)
  230. {
  231. int ret = Wait(10, WAKE_PLAY|WAKE_KILL|WAKE_STOP);
  232. if (ret == WAKE_KILL)
  233. {
  234. player->OnClosed();
  235. return NErr_False;
  236. }
  237. else if (ret == WAKE_PLAY)
  238. {
  239. return NErr_True;
  240. }
  241. else if (ret == WAKE_STOP)
  242. {
  243. player->OnStopped();
  244. return NErr_False;
  245. }
  246. else
  247. {
  248. if (out->Playing() == NErr_True)
  249. player->SetPosition(last_position - out->Latency());
  250. else
  251. {
  252. player->SetPosition(last_position);
  253. player->OnClosed();
  254. return NErr_False;
  255. }
  256. }
  257. }
  258. }
  259. ns_error_t PlaybackBase2::OpenOutput(const ifc_audioout::Parameters *_parameters)
  260. {
  261. // if out is already set, it means that there was a change in parameters, so we'll start a new stream
  262. if (out)
  263. {
  264. // check to see that the parameters actually changed
  265. if (!memcmp(&parameters, _parameters, sizeof(parameters)))
  266. return NErr_Success;
  267. out->Done();
  268. out=0;
  269. }
  270. parameters = *_parameters;
  271. free(output_pointers);
  272. output_pointers = (const uint8_t **)malloc(parameters.audio.number_of_channels * sizeof(const uint8_t *));
  273. if (!output_pointers)
  274. return NErr_OutOfMemory;
  275. ns_error_t ret = output_service->AudioOpen(&parameters, player, secondary_parameters, &out);
  276. if (ret != NErr_Success)
  277. {
  278. player->OnError(ret);
  279. return ret;
  280. }
  281. if (paused)
  282. out->Pause(1);
  283. else
  284. out->Pause(0);
  285. return NErr_True;
  286. }
  287. int PlaybackBase2::OutputNonInterleaved(const void *decode_buffer, size_t decoded, double start_position)
  288. {
  289. int ret;
  290. size_t frames_written=0;
  291. const uint8_t **buffer = (const uint8_t **)decode_buffer;
  292. for (size_t c=0;c<parameters.audio.number_of_channels;c++)
  293. {
  294. output_pointers[c] = buffer[c];
  295. }
  296. while (decoded)
  297. {
  298. size_t to_write = out->CanWrite();
  299. if (to_write)
  300. {
  301. if (decoded < to_write)
  302. to_write = decoded;
  303. ret = out->Output(output_pointers, to_write);
  304. if (ret != NErr_Success)
  305. {
  306. out->Release();
  307. out=0;
  308. return ret;
  309. }
  310. decoded -= to_write;
  311. for (size_t c=0;c<parameters.audio.number_of_channels;c++)
  312. {
  313. output_pointers[c] += to_write/parameters.audio.number_of_channels;
  314. }
  315. frames_written += to_write/parameters.audio.number_of_channels;
  316. player->SetPosition(start_position + (double)frames_written/parameters.audio.sample_rate - out->Latency());
  317. }
  318. else
  319. {
  320. ns_error_t ret = OutputWait();
  321. if (ret != NErr_Success)
  322. return ret;
  323. }
  324. }
  325. return NErr_True;
  326. }
  327. int PlaybackBase2::Output(const void *decode_buffer, size_t decoded, double start_position)
  328. {
  329. int ret;
  330. size_t frames_written=0;
  331. const uint8_t *decode8 = (const uint8_t *)decode_buffer;
  332. size_t buffer_position=0;
  333. while (decoded)
  334. {
  335. size_t to_write = out->CanWrite();
  336. if (to_write)
  337. {
  338. if (decoded < to_write)
  339. to_write = decoded;
  340. ret = out->Output(&decode8[buffer_position], to_write);
  341. if (ret != NErr_Success)
  342. {
  343. out->Release();
  344. out=0;
  345. return ret;
  346. }
  347. decoded -= to_write;
  348. buffer_position += to_write;
  349. frames_written += to_write/parameters.audio.number_of_channels;
  350. player->SetPosition(start_position + (double)frames_written/parameters.audio.sample_rate - out->Latency());
  351. }
  352. else
  353. {
  354. ns_error_t ret = OutputWait();
  355. if (ret != NErr_Success)
  356. return ret;
  357. }
  358. }
  359. return NErr_True;
  360. }
  361. ns_error_t PlaybackBase2::OutputWait()
  362. {
  363. if (paused)
  364. {
  365. /* if we're paused, we need to sit and wait until we're eiter unpaused or stopped */
  366. for (;;)
  367. {
  368. int ret = Wake(WAKE_STOP|WAKE_PAUSE|WAKE_INTERRUPT);
  369. if (ret == WAKE_STOP)
  370. {
  371. out->Stop();
  372. out->Release();
  373. out=0;
  374. player->OnStopped();
  375. return NErr_False;
  376. }
  377. else if (ret == WAKE_UNPAUSE)
  378. {
  379. out->Pause(0);
  380. paused=false;
  381. break;
  382. }
  383. else if (ret == WAKE_PAUSE)
  384. {
  385. out->Pause(1);
  386. paused=true;
  387. }
  388. else if (PlaybackBase::PendingSeek())
  389. {
  390. return NErr_True;
  391. }
  392. else if (ret == WAKE_INTERRUPT)
  393. {
  394. ns_error_t ret = Internal_Interrupt();
  395. if (ret != NErr_Success)
  396. return ret;
  397. }
  398. }
  399. }
  400. else
  401. {
  402. int ret = Wait(10, WAKE_STOP);
  403. if (ret == WAKE_STOP)
  404. {
  405. out->Stop();
  406. out->Release();
  407. out=0;
  408. player->OnStopped();
  409. return NErr_False;
  410. }
  411. }
  412. return NErr_Success;
  413. }
  414. ns_error_t PlaybackBase2::Internal_Interrupt()
  415. {
  416. Agave_Seek resume_information;
  417. implementation->Interrupt(&resume_information);
  418. ns_error_t ret = filelocker->UnlockFile(filename);
  419. if (ret != NErr_Success)
  420. {
  421. implementation->Close();
  422. return ret;
  423. }
  424. PlaybackBase::OnInterrupted();
  425. if (filelocker)
  426. filelocker->WaitForReadInterruptable(filename, this);
  427. ret = implementation->Resume(&resume_information);
  428. if (ret != NErr_Success)
  429. return ret;
  430. ifc_metadata *metadata;
  431. if (implementation->GetMetadata(&metadata) == NErr_Success)
  432. {
  433. player->SetMetadata(metadata);
  434. metadata->Release();
  435. }
  436. return NErr_Success;
  437. }