AudioMedia_ios.h

  1. //
  2. //  AudioMedia_ios.h
  3. //  mmsplayer
  4. //
  5. //  Created by Weiny on 12-4-4.
  6. //  Copyright (c) 2012年 Weiny Zhou. All rights reserved.
  7. //
  8. #ifndef mmsplayer_AudioMedia_ios_h
  9. #define mmsplayer_AudioMedia_ios_h
  10. #include "wdef.h"
  11. typedef void* wAudio;
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. wAudio audio_open(int sample,int nchannles,int bits,int nFrameSize);//初始化声音接口
  17. int audio_play(wAudio audio);//播放
  18. int audio_pause(wAudio audio);
  19. int audio_wirte(wAudio audio,unsigned char* pcm,size_t count,int64_t dts);//写入音频数据
  20. int audio_stop(wAudio audio);//停止
  21. int audio_close(wAudio audio);//关闭
  22. #ifdef __cplusplus
  23. };
  24. #endif
  25. #endif

AudioMedia_ios.c

    1. //
    2. //  AudioMedia_ios.cpp
    3. //  mmsplayer
    4. //
    5. //  Created by Weiny on 12-4-4.
    6. //  Copyright (c) 2012年 Weiny Zhou. All rights reserved.
    7. //
    8. #include "AudioMedia_ios.h"
    9. #include <AudioToolbox/AudioQueue.h>
    10. #include "system/thread.h"
    11. #include "base/wlist.h"
    12. #include "system/lx_lock.h"
    13. #define AUDIO_LIST_COUNT 3
    14. #define AUDIO_BUFFER_SECONDS  1
    15. typedef struct WAudio_Ios
    16. {
    17. int playRequested;
    18. int framesize;
    19. Wlock mlock,mdecdonelock,mqueuelock;
    20. wlist_t mAudiolist;//声音队列
    21. wlist_func mlistfunc;
    22. AudioQueueRef queue;//player list
    23. AudioQueueBufferRef mBuffers[AUDIO_LIST_COUNT];
    24. AudioStreamBasicDescription mDataFormat;
    25. AudioQueueBufferRef emptyAudioBuffer;//空音频队列
    26. }WAudio_Ios;
    27. typedef struct
    28. {
    29. void* data;
    30. size_t size;
    31. int64_t dst;
    32. }WAudio_item;
    33. void wAudio_CallBack(void * in,AudioQueueRef intq,AudioQueueBufferRef outQB);
    34. void wAudtio_fillAudioBuffer(WAudio_Ios* audio,AudioQueueBufferRef buffer);
    35. static inline void waudio_free_back(void* lpvoid,wlist_item_ptr data)
    36. {
    37. WAudio_item* item;
    38. INTOFUNC();
    39. if(!data){PRINTF_ERROR_VALUE(ERROR_INITPARAM);goto error_lab;}
    40. item=(WAudio_item*)data;
    41. SAFE_FREE(item->data);
    42. SAFE_FREE(item);
    43. error_lab:
    44. EXITFUNC();
    45. }
    46. wAudio audio_open(int sample,int nchannles,int bits,int nFrameSize)
    47. {
    48. WAudio_Ios* ptr=NULL;
    49. uint32_t err=0;
    50. int i=0;
    51. INTOFUNC();
    52. ptr=WOS_MALLOC_(WAudio_Ios, 1);
    53. if(!ptr){
    54. PRINTF_ERROR_VALUE(ERROR_NEWMEM);
    55. goto error_lab;
    56. }
    57. memset(ptr,0,sizeof(WAudio_Ios));
    58. ptr->mDataFormat.mSampleRate=sample;//设置采样率
    59. ptr->mDataFormat.mChannelsPerFrame=nchannles;
    60. ptr->mDataFormat.mBitsPerChannel=bits;
    61. ptr->mDataFormat.mFormatID=kAudioFormatLinearPCM;//设置数据格式
    62. ptr->mDataFormat.mFormatFlags=kLinearPCMFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
    63. ptr->mDataFormat.mFramesPerPacket=1;
    64. ptr->mDataFormat.mBytesPerFrame=
    65. ptr->mDataFormat.mBitsPerChannel/
    66. ptr->mDataFormat.mChannelsPerFrame;
    67. ptr->mDataFormat.mBytesPerPacket=
    68. ptr->mDataFormat.mBytesPerFrame*ptr->mDataFormat.
    69. mFramesPerPacket;
    70. err=AudioQueueNewOutput(&ptr->mDataFormat,wAudio_CallBack, ptr, NULL,
    71. NULL/*kCFRunLoopCommonModes*/, 0
    72. , &ptr->queue);
    73. if(err){
    74. WERROR_A("init audio output error,sample=%d,channles=%d,bits=%d.\n",sample,nchannles,bits);
    75. goto error_lab;
    76. }
    77. for (i=0;i<AUDIO_LIST_COUNT;++i)
    78. {
    79. err=AudioQueueAllocateBufferWithPacketDescriptions(ptr->queue,
    80. bits*AUDIO_BUFFER_SECONDS/8,
    81. sample*AUDIO_BUFFER_SECONDS/nFrameSize+1,ptr->mBuffers+i);
    82. if(err){
    83. WERROR_A("can't allocate audio queue buffer: %d",err);
    84. goto error_lab;
    85. }
    86. }
    87. ptr->mlock=lx_lock_init();
    88. ptr->mdecdonelock=lx_lock_init();
    89. ptr->mqueuelock=lx_lock_init();
    90. ptr->mlistfunc=wlist_getfunc();
    91. ptr->mAudiolist.free=waudio_free_back;
    92. ptr->framesize=nFrameSize;
    93. #if 1
    94. err=AudioQueueStart(ptr->queue,NULL);
    95. if(err){
    96. WERROR_A("Error: Audio queue failed to start: %d", err);
    97. goto error_lab;
    98. }
    99. ptr->playRequested=1;
    100. WDEBUG_OUT("Started Audio queue.",NULL);
    101. #endif
    102. #if 0
    103. agc.FrameCount = FRAME_COUNT;
    104. bufferSize = agc.FrameCount * agc.mDataFormat.mBytesPerFrame;
    105. for (i=0; i<AUDIO_BUFFERS; i++)
    106. {
    107. err = AudioQueueAllocateBuffer(agc.queue,bufferSize,&agc.mBuffers[i]);
    108. if(err) return err;
    109. AQBufferCallback(&agc,agc.queue,agc.mBuffers[i]);
    110. }
    111. err = AudioQueueStart(agc.queue,NULL);
    112. if(err) return err;
    113. while (agc.playPtr<agc.sampleLen)
    114. {
    115. select(NULL,NULL,NULL,NULL,1.0);
    116. }
    117. #endif
    118. error_lab:
    119. if(err){
    120. audio_close(ptr);
    121. SAFE_FREE(ptr);
    122. }
    123. EXITFUNC();
    124. return (wAudio)ptr;
    125. }
    126. int audio_play(wAudio audio)
    127. {
    128. int nResult=0;
    129. WAudio_Ios* ptr=NULL;
    130. INTOFUNC();
    131. if(!audio){
    132. WERROR_A("input audio is null",NULL);
    133. nResult=ERROR_INITPARAM;
    134. goto error_lab;
    135. }
    136. ptr=(WAudio_Ios*)audio;
    137. if(ptr->playRequested==0||ptr->playRequested==2)
    138. {WERROR_A("state is %d",ptr->playRequested);goto error_lab;}
    139. ptr->playRequested=1;
    140. AudioQueueStart(ptr->queue,NULL);
    141. error_lab:
    142. EXITFUNC();
    143. return nResult;
    144. }
    145. int audio_pause(wAudio audio)
    146. {
    147. int nResult=0;
    148. WAudio_Ios* ptr=NULL;
    149. INTOFUNC();
    150. if(!audio){
    151. WERROR_A("input audio is null",NULL);
    152. nResult=ERROR_INITPARAM;
    153. goto error_lab;
    154. }
    155. ptr=(WAudio_Ios*)audio;
    156. if(1!=ptr->playRequested)
    157. {WERROR_A("state is %d",ptr->playRequested);goto error_lab;}
    158. ptr->playRequested=2;
    159. AudioQueuePause(ptr->queue);
    160. error_lab:
    161. EXITFUNC();
    162. return nResult;
    163. }
    164. int audio_wirte(wAudio audio,unsigned char* pcm,size_t count,int64_t dst)
    165. {
    166. int nResult=0;
    167. WAudio_Ios* ptr=NULL;
    168. WAudio_item* item=NULL;
    169. INTOFUNC();
    170. if(!audio){
    171. WERROR_A("input audio is null",NULL);
    172. nResult=ERROR_INITPARAM;
    173. goto error_lab;
    174. }
    175. ptr=(WAudio_Ios*)audio;
    176. item=WOS_MALLOC_(WAudio_item,1);
    177. if(!item){
    178. nResult=ERROR_NEWMEM;PRINTF_ERROR_VALUE(nResult);goto error_lab;
    179. }
    180. item->data=pcm;
    181. item->size=count;
    182. item->dst=dst;
    183. lx_lock(ptr->mqueuelock);
    184. //先加入队列
    185. ptr->mlistfunc.push_back(&ptr->mAudiolist,item);
    186. //
    187. if(ptr->emptyAudioBuffer)
    188. wAudtio_fillAudioBuffer(ptr,ptr->emptyAudioBuffer);//填充空buffer
    189. lx_unlock(ptr->mqueuelock);
    190. error_lab:
    191. EXITFUNC();
    192. return nResult;
    193. }
    194. int audio_stop(wAudio audio)
    195. {
    196. int nResult=0;
    197. WAudio_Ios* ptr=NULL;
    198. INTOFUNC();
    199. if(!audio){
    200. WERROR_A("input audio is null",NULL);
    201. nResult=ERROR_INITPARAM;
    202. goto error_lab;
    203. }
    204. ptr=(WAudio_Ios*)audio;
    205. AudioQueueStop(ptr->queue,false);
    206. ptr->playRequested=0;
    207. error_lab:
    208. EXITFUNC();
    209. return nResult;
    210. }
    211. int audio_close(wAudio audio)
    212. {
    213. int nResult=0;
    214. WAudio_Ios* ptr=NULL;
    215. INTOFUNC();
    216. if(!audio){
    217. WERROR_A("input audio is null.",NULL);
    218. nResult=ERROR_INITPARAM;
    219. goto error_lab;
    220. }
    221. ptr=(WAudio_Ios*)audio;
    222. ptr->mlistfunc.clear(&ptr->mAudiolist);//清空播放队列
    223. lx_lock_free(ptr->mqueuelock);
    224. lx_lock_free(ptr->mdecdonelock);
    225. lx_lock_free(ptr->mlock);
    226. AudioQueueDispose(ptr->queue,false);
    227. SAFE_FREE(ptr);
    228. error_lab:
    229. EXITFUNC();
    230. return nResult;
    231. }
    232. #if 0
    233. void AQBufferCallback( void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB)
    234. {
    235. AQCallbackStruct *agc;
    236. short *coreAudiobuffer;
    237. short sample;
    238. int i;
    239. agc=(AQCallbackStruct *) in;
    240. coreAudiobuffer =(short*) outQB->mAudioData;
    241. printf("Sync:%i / %i \n",agc->playPtr,agc->sampleLen);
    242. if (agc->FrameCount >0)
    243. {
    244. outQB->mAudioDataByteSize = 4*agc->FrameCount;
    245. for (i=0; i<agc->FrameCount*2; i++)
    246. {
    247. if(agc->playPtr > agc->sampleLen || agc->playPtr<0)
    248. {
    249. sample =0;
    250. }
    251. else
    252. {
    253. sample = (agc->pcmBuffer[agc->playPtr]);
    254. }
    255. coreAudiobuffer[i] = sample;
    256. coreAudiobuffer[i+1] = sample;
    257. agc->playPtr++;
    258. }
    259. AudioQueueEnqueueBuffer(inQ,outQB,0,NULL);
    260. }
    261. }
    262. #endif
    263. void wAudtio_fillAudioBuffer(WAudio_Ios* audio,AudioQueueBufferRef buffer)
    264. {
    265. AudioTimeStamp bufferStartTime;
    266. uint32_t err;
    267. INTOFUNC();
    268. buffer->mAudioDataByteSize=0;
    269. buffer->mPacketDescriptionCount=0;
    270. if(audio->mAudiolist.size<=0){
    271. WERROR_A("Warning: No audio packets in queue.",NULL);
    272. audio->emptyAudioBuffer=buffer;
    273. goto error_lab;
    274. }
    275. audio->emptyAudioBuffer=NULL;
    276. while(audio->mAudiolist.size&&buffer->mPacketDescriptionCount <
    277. buffer->mPacketDescriptionCapacity)
    278. {
    279. wlist_item* item=audio->mlistfunc.pop_front(&audio->mAudiolist);
    280. WAudio_item* data=(WAudio_item*)item->data;
    281. if(buffer->mAudioDataBytesCapacity -
    282. buffer->mAudioDataByteSize >=data->size)
    283. {
    284. if(buffer->mAudioDataBytesCapacity==0)
    285. {
    286. bufferStartTime.mSampleTime=data->dst*audio->framesize;
    287. bufferStartTime.mFlags=kAudioTimeStampSampleTimeValid;
    288. }
    289. memcpy((uint8_t *)buffer->mAudioData + buffer->mAudioDataByteSize, data->data, data->size);
    290. buffer->mPacketDescriptions[buffer->mPacketDescriptionCount].mStartOffset = buffer->mAudioDataByteSize;
    291. buffer->mPacketDescriptions[buffer->mPacketDescriptionCount].mDataByteSize = data->size;
    292. buffer->mPacketDescriptions[buffer->mPacketDescriptionCount].mVariableFramesInPacket = audio->framesize;
    293. buffer->mAudioDataByteSize += data->size;
    294. ++buffer->mPacketDescriptionCount;
    295. lx_lock(audio->mqueuelock);
    296. audio->mlistfunc.remove(&audio->mAudiolist,0);
    297. lx_unlock(audio->mqueuelock);
    298. }
    299. else
    300. break;
    301. }
    302. if(buffer->mPacketDescriptionCount>0)
    303. {
    304. if(err=AudioQueueEnqueueBufferWithParameters(audio->queue,
    305. buffer,0,NULL,0,0,0,NULL,&bufferStartTime,NULL))
    306. WERROR_A("Error enqueuing audio buffer: %d", err);
    307. //decodelock
    308. lx_lock(audio->mdecdonelock);
    309. if(!audio->playRequested&&audio->mAudiolist.size==0){
    310. if(err=AudioQueueStop(audio->queue,false))
    311. WERROR_A("Error: Failed to stop audio queue: %d", err);
    312. else
    313. WERROR_A("Stopped audio queue",NULL);
    314. }
    315. lx_unlock(audio->mdecdonelock);
    316. //decodeunlock
    317. }
    318. error_lab:
    319. EXITFUNC();
    320. }
    321. void wAudio_CallBack(void * in,AudioQueueRef intq,AudioQueueBufferRef buffer)
    322. {
    323. wAudtio_fillAudioBuffer((WAudio_Ios*)in,buffer);
    324. }

来源:http://blog.csdn.net/tigerleap/article/details/7628105

AudioToolbox--AudioQueue实现流播放接口的更多相关文章

  1. 抛开flash,自己开发实现C++ RTMP直播流播放器

    抛开flash,自己开发实现C++ RTMP直播流播放器 众所周知,RTMP是以flash为客户端播放器的直播协议,主要应用在B/S形式的场景中.本人研究并用C++开发实现了RTMP直播流协议的播放器 ...

  2. JavaCV 学习(二):使用 JavaCV + FFmpeg 制作拉流播放器

    一.前言 在 Android 音视频开发学习思路 中,我们不断的学习和了解音视频相关的知识,随着知识点不断的学习,我们现在应该做的事情,就是将知识点不断的串联起来.这样才能得到更深层次的领悟.通过整理 ...

  3. C#推送RTMP到SRS通过VLC进行取流播放!!

    前面一篇文章简单的介绍了下如何利用SRS自带的播放地址进行观看RTMP直播流,也就是说是使用SRS的内置demo进行Test,但是进行视频直播肯定不可能使用那样的去开发,不开源的东西肯定不好用.由于在 ...

  4. QT | 聊聊QT与直播流播放——从QMediaPlayer到Qt-AV

    [原创文章,转载请注明来源,方便查看本文更新] 这段时间需要用QT开发一个播放直播流的功能,能够播放各种格式的直播流,并且CPU占用率不要太高(可以占用GPU),这些是我们的目标. 直播流推流的技术进 ...

  5. [JavaScript,Java,C#,C++,Ruby,Perl,PHP,Python][转]流式接口(Fluent interface)

    原文:https://en.m.wikipedia.org/wiki/Fluent_interface(英文,完整) 转载:https://zh.wikipedia.org/wiki/流式接口(中文, ...

  6. 用C#做一个 拉流播放器

    做拉流播放器第一个想到就是,.,..FFmpeg没错 我也是用强大的他它来做的.但是我用的不是  cmd 调用 而是用的强大的FFmpeg.AutoGen FFmpeg.AutoGen 这个是C# 一 ...

  7. c# 基于RTMP推流 PC+移动端 拉流播放

    网上关于直播相关的文章很多,但是讲解还是不够系统,对于刚刚接触直播开发的朋友实施起来会浪费不少时间.下面结合我自己的经验, 介绍一下直播方面的实战经验. 分成两个部分 第一部分是标题中介绍的基于RTM ...

  8. 利用Docker挂载Nginx-rtmp(服务器直播流分发)+FFmpeg(推流)+Vue.js结合Video.js(播放器流播放)来实现实时网络直播

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_75 众所周知,在视频直播领域,有不同的商家提供各种的商业解决方案,其中比较靠谱的服务商有阿里云直播,腾讯云直播,以及又拍云和网易云 ...

  9. iOS音频播放之AudioQueue(一):播放本地音乐

    AudioQueue简单介绍 AudioStreamer说明 AudioQueue具体解释 AudioQueue工作原理 AudioQueue主要接口 AudioQueueNewOutput Audi ...

随机推荐

  1. pandas连接多个表格concat()函数

    网易云课堂该课程链接地址 https://study.163.com/course/courseMain.htm?share=2&shareId=400000000398149&cou ...

  2. 小米手机root

    目录 概念 解锁流程 root流程 如何Root? 关于supersu 关于twrp 关于Magisk Manager ref: 申请开发板流程 线刷教程 小米手机root 概念 解锁: 使手机可以刷 ...

  3. 24V低压检测电路 - 低压检测电压(转)

    24V低压检测电路 - 低压检测电压 参考: ADC采样工作原理详解 使用单片机的ADC采集电阻的分压 问题: 当ADC采集两个电阻分压后的电压的时候,ADC转换出来的电压值和万用表量出来的不一样差异 ...

  4. 如何将业务代码写得像诗一样(使用注解+单例+工厂去掉一大波if和else判断)

    1.订单控制器,提供一个根据商品id和银行渠道id计算商品折后价格的接口: import org.springframework.web.bind.annotation.GetMapping; imp ...

  5. java多线程(一)创建线程的四种方式

    1.   什么是并发与并行 要想学习多线程,必须先理解什么是并发与并行 并行:指两个或多个事件在同一时刻发生(同时发生). 并发:指两个或多个事件在同一个时间段内发生. 2.   什么是进程.线程 进 ...

  6. SpringBoot入门-Redis(六)

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  7. osgb文件过大,可以通过Compressor=zlib对纹理进行压缩

    osg::ref_ptr<osgDB::ReaderWriter::Options> options = new osgDB::ReaderWriter::Options; options ...

  8. Angular8开发拼多多WebApp_汇总贴

    https://coding.imooc.com/class/336.html?mc_marking=b9f5e475d0cb8922d899d416f5b4433f&mc_channel=s ...

  9. Xshell连接SqlPlus无法使用退格、删除键

    问题:在使用xshell连接CentOS7,进入SQLPLUS进行命令操作时,如果输错了信息,无法进行退格键删除(显示“^H”),同样按删除键,显示“^[[3~”. 解决:网上查找了相关资料,可以通过 ...

  10. mysql 安装为服务 ,mysql.zip 安装为服务,mysql搬移迁移服务器安装为服务

    从服务器A打包到服务器B后,在服务器B中运行安装服务命令,可自定义服务名,一台服务器上可装N个MySql实例 mysqld --install MySQL_0001 --defaults-file=D ...