OMXPlayerAudio.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (C) 2005-2008 Team XBMC
  3. * http://www.xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, write to
  17. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. */
  21. #if (defined HAVE_CONFIG_H) && (!defined WIN32)
  22. #include "config.h"
  23. #elif defined(_WIN32)
  24. #include "system.h"
  25. #endif
  26. #include "OMXPlayerAudio.h"
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include "linux/XMemUtils.h"
  30. OMXPlayerAudio::OMXPlayerAudio()
  31. {
  32. m_open = false;
  33. m_stream_id = -1;
  34. m_pStream = NULL;
  35. m_av_clock = NULL;
  36. m_omx_reader = NULL;
  37. m_decoder = NULL;
  38. m_flush = false;
  39. m_flush_requested = false;
  40. m_cached_size = 0;
  41. m_pAudioCodec = NULL;
  42. m_player_error = true;
  43. m_CurrentVolume = 0.0f;
  44. m_amplification = 0;
  45. m_mute = false;
  46. pthread_cond_init(&m_packet_cond, NULL);
  47. pthread_cond_init(&m_audio_cond, NULL);
  48. pthread_mutex_init(&m_lock, NULL);
  49. pthread_mutex_init(&m_lock_decoder, NULL);
  50. }
  51. OMXPlayerAudio::~OMXPlayerAudio()
  52. {
  53. Close();
  54. pthread_cond_destroy(&m_audio_cond);
  55. pthread_cond_destroy(&m_packet_cond);
  56. pthread_mutex_destroy(&m_lock);
  57. pthread_mutex_destroy(&m_lock_decoder);
  58. }
  59. void OMXPlayerAudio::Lock()
  60. {
  61. if(m_config.use_thread)
  62. pthread_mutex_lock(&m_lock);
  63. }
  64. void OMXPlayerAudio::UnLock()
  65. {
  66. if(m_config.use_thread)
  67. pthread_mutex_unlock(&m_lock);
  68. }
  69. void OMXPlayerAudio::LockDecoder()
  70. {
  71. if(m_config.use_thread)
  72. pthread_mutex_lock(&m_lock_decoder);
  73. }
  74. void OMXPlayerAudio::UnLockDecoder()
  75. {
  76. if(m_config.use_thread)
  77. pthread_mutex_unlock(&m_lock_decoder);
  78. }
  79. bool OMXPlayerAudio::Open(OMXClock *av_clock, const OMXAudioConfig &config, OMXReader *omx_reader)
  80. {
  81. if(ThreadHandle())
  82. Close();
  83. if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllAvFormat.Load() || !av_clock)
  84. return false;
  85. m_dllAvFormat.av_register_all();
  86. m_config = config;
  87. m_av_clock = av_clock;
  88. m_omx_reader = omx_reader;
  89. m_passthrough = false;
  90. m_hw_decode = false;
  91. m_iCurrentPts = DVD_NOPTS_VALUE;
  92. m_bAbort = false;
  93. m_flush = false;
  94. m_flush_requested = false;
  95. m_cached_size = 0;
  96. m_pAudioCodec = NULL;
  97. m_player_error = OpenAudioCodec();
  98. if(!m_player_error)
  99. {
  100. Close();
  101. return false;
  102. }
  103. m_player_error = OpenDecoder();
  104. if(!m_player_error)
  105. {
  106. Close();
  107. return false;
  108. }
  109. if(m_config.use_thread)
  110. Create();
  111. m_open = true;
  112. return true;
  113. }
  114. bool OMXPlayerAudio::Close()
  115. {
  116. m_bAbort = true;
  117. Flush();
  118. if(ThreadHandle())
  119. {
  120. Lock();
  121. pthread_cond_broadcast(&m_packet_cond);
  122. UnLock();
  123. StopThread();
  124. }
  125. CloseDecoder();
  126. CloseAudioCodec();
  127. m_open = false;
  128. m_stream_id = -1;
  129. m_iCurrentPts = DVD_NOPTS_VALUE;
  130. m_pStream = NULL;
  131. m_dllAvUtil.Unload();
  132. m_dllAvCodec.Unload();
  133. m_dllAvFormat.Unload();
  134. return true;
  135. }
  136. bool OMXPlayerAudio::Decode(OMXPacket *pkt)
  137. {
  138. if(!pkt)
  139. return false;
  140. /* last decoder reinit went wrong */
  141. if(!m_decoder || !m_pAudioCodec)
  142. return true;
  143. if(!m_omx_reader->IsActive(OMXSTREAM_AUDIO, pkt->stream_index))
  144. return true;
  145. int channels = pkt->hints.channels;
  146. unsigned int old_bitrate = m_config.hints.bitrate;
  147. unsigned int new_bitrate = pkt->hints.bitrate;
  148. /* only check bitrate changes on AV_CODEC_ID_DTS, AV_CODEC_ID_AC3, AV_CODEC_ID_EAC3 */
  149. if(m_config.hints.codec != AV_CODEC_ID_DTS && m_config.hints.codec != AV_CODEC_ID_AC3 && m_config.hints.codec != AV_CODEC_ID_EAC3)
  150. {
  151. new_bitrate = old_bitrate = 0;
  152. }
  153. // for passthrough we only care about the codec and the samplerate
  154. bool minor_change = channels != m_config.hints.channels ||
  155. pkt->hints.bitspersample != m_config.hints.bitspersample ||
  156. old_bitrate != new_bitrate;
  157. if(pkt->hints.codec != m_config.hints.codec ||
  158. pkt->hints.samplerate != m_config.hints.samplerate ||
  159. (!m_passthrough && minor_change))
  160. {
  161. printf("C : %d %d %d %d %d\n", m_config.hints.codec, m_config.hints.channels, m_config.hints.samplerate, m_config.hints.bitrate, m_config.hints.bitspersample);
  162. printf("N : %d %d %d %d %d\n", pkt->hints.codec, channels, pkt->hints.samplerate, pkt->hints.bitrate, pkt->hints.bitspersample);
  163. CloseDecoder();
  164. CloseAudioCodec();
  165. m_config.hints = pkt->hints;
  166. m_player_error = OpenAudioCodec();
  167. if(!m_player_error)
  168. return false;
  169. m_player_error = OpenDecoder();
  170. if(!m_player_error)
  171. return false;
  172. }
  173. CLog::Log(LOGINFO, "CDVDPlayerAudio::Decode dts:%.0f pts:%.0f size:%d", pkt->dts, pkt->pts, pkt->size);
  174. if(pkt->pts != DVD_NOPTS_VALUE)
  175. m_iCurrentPts = pkt->pts;
  176. else if(pkt->dts != DVD_NOPTS_VALUE)
  177. m_iCurrentPts = pkt->dts;
  178. const uint8_t *data_dec = pkt->data;
  179. int data_len = pkt->size;
  180. if(!m_passthrough && !m_hw_decode)
  181. {
  182. double dts = pkt->dts, pts=pkt->pts;
  183. while(data_len > 0)
  184. {
  185. int len = m_pAudioCodec->Decode((BYTE *)data_dec, data_len, dts, pts);
  186. if( (len < 0) || (len > data_len) )
  187. {
  188. m_pAudioCodec->Reset();
  189. break;
  190. }
  191. data_dec+= len;
  192. data_len -= len;
  193. uint8_t *decoded;
  194. int decoded_size = m_pAudioCodec->GetData(&decoded, dts, pts);
  195. if(decoded_size <=0)
  196. continue;
  197. while((int) m_decoder->GetSpace() < decoded_size)
  198. {
  199. OMXClock::OMXSleep(10);
  200. if(m_flush_requested) return true;
  201. }
  202. int ret = 0;
  203. ret = m_decoder->AddPackets(decoded, decoded_size, dts, pts, m_pAudioCodec->GetFrameSize());
  204. if(ret != decoded_size)
  205. {
  206. printf("error ret %d decoded_size %d\n", ret, decoded_size);
  207. }
  208. }
  209. }
  210. else
  211. {
  212. while((int) m_decoder->GetSpace() < pkt->size)
  213. {
  214. OMXClock::OMXSleep(10);
  215. if(m_flush_requested) return true;
  216. }
  217. m_decoder->AddPackets(pkt->data, pkt->size, pkt->dts, pkt->pts, 0);
  218. }
  219. return true;
  220. }
  221. void OMXPlayerAudio::Process()
  222. {
  223. OMXPacket *omx_pkt = NULL;
  224. while(true)
  225. {
  226. Lock();
  227. if(!(m_bStop || m_bAbort) && m_packets.empty())
  228. pthread_cond_wait(&m_packet_cond, &m_lock);
  229. if (m_bStop || m_bAbort)
  230. {
  231. UnLock();
  232. break;
  233. }
  234. if(m_flush && omx_pkt)
  235. {
  236. OMXReader::FreePacket(omx_pkt);
  237. omx_pkt = NULL;
  238. m_flush = false;
  239. }
  240. else if(!omx_pkt && !m_packets.empty())
  241. {
  242. omx_pkt = m_packets.front();
  243. if (omx_pkt)
  244. {
  245. m_cached_size -= omx_pkt->size;
  246. }
  247. else
  248. {
  249. assert(m_cached_size == 0);
  250. SubmitEOSInternal();
  251. }
  252. m_packets.pop_front();
  253. }
  254. UnLock();
  255. LockDecoder();
  256. if(m_flush && omx_pkt)
  257. {
  258. OMXReader::FreePacket(omx_pkt);
  259. omx_pkt = NULL;
  260. m_flush = false;
  261. }
  262. else if(omx_pkt && Decode(omx_pkt))
  263. {
  264. OMXReader::FreePacket(omx_pkt);
  265. omx_pkt = NULL;
  266. }
  267. UnLockDecoder();
  268. }
  269. if(omx_pkt)
  270. OMXReader::FreePacket(omx_pkt);
  271. }
  272. void OMXPlayerAudio::Flush()
  273. {
  274. m_flush_requested = true;
  275. Lock();
  276. LockDecoder();
  277. if(m_pAudioCodec)
  278. m_pAudioCodec->Reset();
  279. m_flush_requested = false;
  280. m_flush = true;
  281. while (!m_packets.empty())
  282. {
  283. OMXPacket *pkt = m_packets.front();
  284. m_packets.pop_front();
  285. OMXReader::FreePacket(pkt);
  286. }
  287. m_iCurrentPts = DVD_NOPTS_VALUE;
  288. m_cached_size = 0;
  289. if(m_decoder)
  290. m_decoder->Flush();
  291. UnLockDecoder();
  292. UnLock();
  293. }
  294. bool OMXPlayerAudio::AddPacket(OMXPacket *pkt)
  295. {
  296. bool ret = false;
  297. if(!pkt)
  298. return ret;
  299. if(m_bStop || m_bAbort)
  300. return ret;
  301. if((m_cached_size + pkt->size) < m_config.queue_size * 1024 * 1024)
  302. {
  303. Lock();
  304. m_cached_size += pkt->size;
  305. m_packets.push_back(pkt);
  306. UnLock();
  307. ret = true;
  308. pthread_cond_broadcast(&m_packet_cond);
  309. }
  310. return ret;
  311. }
  312. bool OMXPlayerAudio::OpenAudioCodec()
  313. {
  314. m_pAudioCodec = new COMXAudioCodecOMX();
  315. if(!m_pAudioCodec->Open(m_config.hints, m_config.layout))
  316. {
  317. delete m_pAudioCodec; m_pAudioCodec = NULL;
  318. return false;
  319. }
  320. return true;
  321. }
  322. void OMXPlayerAudio::CloseAudioCodec()
  323. {
  324. if(m_pAudioCodec)
  325. delete m_pAudioCodec;
  326. m_pAudioCodec = NULL;
  327. }
  328. bool OMXPlayerAudio::IsPassthrough(COMXStreamInfo hints)
  329. {
  330. if(m_config.device == "omx:local")
  331. return false;
  332. bool passthrough = false;
  333. if(hints.codec == AV_CODEC_ID_AC3)
  334. {
  335. passthrough = true;
  336. }
  337. if(hints.codec == AV_CODEC_ID_EAC3)
  338. {
  339. passthrough = true;
  340. }
  341. if(hints.codec == AV_CODEC_ID_DTS)
  342. {
  343. passthrough = true;
  344. }
  345. return passthrough;
  346. }
  347. bool OMXPlayerAudio::OpenDecoder()
  348. {
  349. bool bAudioRenderOpen = false;
  350. m_decoder = new COMXAudio();
  351. if(m_config.passthrough)
  352. m_passthrough = IsPassthrough(m_config.hints);
  353. if(!m_passthrough && m_config.hwdecode)
  354. m_hw_decode = COMXAudio::HWDecode(m_config.hints.codec);
  355. if(m_passthrough)
  356. m_hw_decode = false;
  357. bAudioRenderOpen = m_decoder->Initialize(m_av_clock, m_config, m_pAudioCodec->GetChannelMap(), m_pAudioCodec->GetBitsPerSample());
  358. m_codec_name = m_omx_reader->GetCodecName(OMXSTREAM_AUDIO);
  359. if(!bAudioRenderOpen)
  360. {
  361. delete m_decoder;
  362. m_decoder = NULL;
  363. return false;
  364. }
  365. else
  366. {
  367. if(m_passthrough)
  368. {
  369. printf("Audio codec %s passthrough channels %d samplerate %d bitspersample %d\n",
  370. m_codec_name.c_str(), m_config.hints.channels, m_config.hints.samplerate, m_config.hints.bitspersample);
  371. }
  372. else
  373. {
  374. printf("Audio codec %s channels %d samplerate %d bitspersample %d\n",
  375. m_codec_name.c_str(), m_config.hints.channels, m_config.hints.samplerate, m_config.hints.bitspersample);
  376. }
  377. }
  378. // setup current volume settings
  379. m_decoder->SetVolume(m_CurrentVolume);
  380. m_decoder->SetMute(m_mute);
  381. m_decoder->SetDynamicRangeCompression(m_amplification);
  382. return true;
  383. }
  384. bool OMXPlayerAudio::CloseDecoder()
  385. {
  386. if(m_decoder)
  387. delete m_decoder;
  388. m_decoder = NULL;
  389. return true;
  390. }
  391. double OMXPlayerAudio::GetDelay()
  392. {
  393. if(m_decoder)
  394. return m_decoder->GetDelay();
  395. else
  396. return 0;
  397. }
  398. double OMXPlayerAudio::GetCacheTime()
  399. {
  400. if(m_decoder)
  401. return m_decoder->GetCacheTime();
  402. else
  403. return 0;
  404. }
  405. double OMXPlayerAudio::GetCacheTotal()
  406. {
  407. if(m_decoder)
  408. return m_decoder->GetCacheTotal();
  409. else
  410. return 0;
  411. }
  412. void OMXPlayerAudio::SubmitEOS()
  413. {
  414. Lock();
  415. m_packets.push_back(nullptr);
  416. UnLock();
  417. pthread_cond_broadcast(&m_packet_cond);
  418. }
  419. void OMXPlayerAudio::SubmitEOSInternal()
  420. {
  421. if(m_decoder)
  422. m_decoder->SubmitEOS();
  423. }
  424. bool OMXPlayerAudio::IsEOS()
  425. {
  426. return m_packets.empty() && (!m_decoder || m_decoder->IsEOS());
  427. }