Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/symb_aps_dev.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 of the License, or
8 * (at your option) 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjmedia-audiodev/audiodev_imp.h>
20 #include <pjmedia-audiodev/errno.h>
21 #include <pjmedia/alaw_ulaw.h>
22 #include <pjmedia/resample.h>
23 #include <pjmedia/stereo.h>
24 #include <pj/assert.h>
25 #include <pj/log.h>
26 #include <pj/math.h>
27 #include <pj/os.h>
28 #include <pj/string.h>
29
30 #if PJMEDIA_AUDIO_DEV_HAS_SYMB_APS
31
32 #include <e32msgqueue.h>
33 #include <sounddevice.h>
34 #include <APSClientSession.h>
35 #include <pjmedia-codec/amr_helper.h>
36
37 /* Pack/unpack G.729 frame of S60 DSP codec, taken from:
38 * http://wiki.forum.nokia.com/index.php/TSS000776_-_Payload_conversion_for_G.729_audio_format
39 */
40 #include "s60_g729_bitstream.h"
41
42
43 #define THIS_FILE "symb_aps_dev.c"
44 #define BITS_PER_SAMPLE 16
45
46
47 #if 1
48 # define TRACE_(st) PJ_LOG(3, st)
49 #else
50 # define TRACE_(st)
51 #endif
52
53
54 /* App UID to open global APS queues to communicate with the APS server. */
55 extern TPtrC APP_UID;
56
57 /* APS G.711 frame length */
58 static pj_uint8_t aps_g711_frame_len;
59
60
61 /* APS factory */
62 struct aps_factory
63 {
64 pjmedia_aud_dev_factory base;
65 pj_pool_t *pool;
66 pj_pool_factory *pf;
67 pjmedia_aud_dev_info dev_info;
68 };
69
70
71 /* Forward declaration of CPjAudioEngine */
72 class CPjAudioEngine;
73
74
75 /* APS stream. */
76 struct aps_stream
77 {
78 // Base
79 pjmedia_aud_stream base; /**< Base class. */
80
81 // Pool
82 pj_pool_t *pool; /**< Memory pool. */
83
84 // Common settings.
85 pjmedia_aud_param param; /**< Stream param. */
86 pjmedia_aud_rec_cb rec_cb; /**< Record callback. */
87 pjmedia_aud_play_cb play_cb; /**< Playback callback. */
88 void *user_data; /**< Application data. */
89
90 // Audio engine
91 CPjAudioEngine *engine; /**< Internal engine. */
92
93 pj_timestamp ts_play; /**< Playback timestamp.*/
94 pj_timestamp ts_rec; /**< Record timestamp. */
95
96 pj_int16_t *play_buf; /**< Playback buffer. */
97 pj_uint16_t play_buf_len; /**< Playback buffer length. */
98 pj_uint16_t play_buf_start; /**< Playback buffer start index. */
99 pj_int16_t *rec_buf; /**< Record buffer. */
100 pj_uint16_t rec_buf_len; /**< Record buffer length. */
101 void *strm_data; /**< Stream data. */
102
103 /* Resampling is needed, in case audio device is opened with clock rate
104 * other than 8kHz (only for PCM format).
105 */
106 pjmedia_resample *play_resample; /**< Resampler for playback. */
107 pjmedia_resample *rec_resample; /**< Resampler for recording */
108 pj_uint16_t resample_factor; /**< Resample factor, requested
109 clock rate / 8000 */
110
111 /* When stream is working in PCM format, where the samples may need to be
112 * resampled from/to different clock rate and/or channel count, PCM buffer
113 * is needed to perform such resampling operations.
114 */
115 pj_int16_t *pcm_buf; /**< PCM buffer. */
116 };
117
118
119 /* Prototypes */
120 static pj_status_t factory_init(pjmedia_aud_dev_factory *f);
121 static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f);
122 static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f);
123 static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f);
124 static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
125 unsigned index,
126 pjmedia_aud_dev_info *info);
127 static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
128 unsigned index,
129 pjmedia_aud_param *param);
130 static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
131 const pjmedia_aud_param *param,
132 pjmedia_aud_rec_cb rec_cb,
133 pjmedia_aud_play_cb play_cb,
134 void *user_data,
135 pjmedia_aud_stream **p_aud_strm);
136
137 static pj_status_t stream_get_param(pjmedia_aud_stream *strm,
138 pjmedia_aud_param *param);
139 static pj_status_t stream_get_cap(pjmedia_aud_stream *strm,
140 pjmedia_aud_dev_cap cap,
141 void *value);
142 static pj_status_t stream_set_cap(pjmedia_aud_stream *strm,
143 pjmedia_aud_dev_cap cap,
144 const void *value);
145 static pj_status_t stream_start(pjmedia_aud_stream *strm);
146 static pj_status_t stream_stop(pjmedia_aud_stream *strm);
147 static pj_status_t stream_destroy(pjmedia_aud_stream *strm);
148
149
150 /* Operations */
151 static pjmedia_aud_dev_factory_op factory_op =
152 {
153 &factory_init,
154 &factory_destroy,
155 &factory_get_dev_count,
156 &factory_get_dev_info,
157 &factory_default_param,
158 &factory_create_stream,
159 &factory_refresh
160 };
161
162 static pjmedia_aud_stream_op stream_op =
163 {
164 &stream_get_param,
165 &stream_get_cap,
166 &stream_set_cap,
167 &stream_start,
168 &stream_stop,
169 &stream_destroy
170 };
171
172
173 /****************************************************************************
174 * Internal APS Engine
175 */
176
177 /*
178 * Utility: print sound device error
179 */
180 static void snd_perror(const char *title, TInt rc)
181 {
182 PJ_LOG(1,(THIS_FILE, "%s (error code=%d)", title, rc));
183 }
184
185 /*
186 * Utility: wait for specified time.
187 */
188 static void snd_wait(unsigned ms)
189 {
190 TTime start, now;
191
192 start.UniversalTime();
193 do {
194 pj_symbianos_poll(-1, ms);
195 now.UniversalTime();
196 } while (now.MicroSecondsFrom(start) < ms * 1000);
197 }
198
199 typedef void(*PjAudioCallback)(TAPSCommBuffer &buf, void *user_data);
200
201 /**
202 * Abstract class for handler of callbacks from APS client.
203 */
204 class MQueueHandlerObserver
205 {
206 public:
207 MQueueHandlerObserver(PjAudioCallback RecCb_, PjAudioCallback PlayCb_,
208 void *UserData_)
209 : RecCb(RecCb_), PlayCb(PlayCb_), UserData(UserData_)
210 {}
211
212 virtual void InputStreamInitialized(const TInt aStatus) = 0;
213 virtual void OutputStreamInitialized(const TInt aStatus) = 0;
214 virtual void NotifyError(const TInt aError) = 0;
215
216 public:
217 PjAudioCallback RecCb;
218 PjAudioCallback PlayCb;
219 void *UserData;
220 };
221
222 /**
223 * Handler for communication and data queue.
224 */
225 class CQueueHandler : public CActive
226 {
227 public:
228 // Types of queue handler
229 enum TQueueHandlerType {
230 ERecordCommQueue,
231 EPlayCommQueue,
232 ERecordQueue,
233 EPlayQueue
234 };
235
236 // The order corresponds to the APS Server state, do not change!
237 enum TState {
238 EAPSPlayerInitialize = 1,
239 EAPSRecorderInitialize = 2,
240 EAPSPlayData = 3,
241 EAPSRecordData = 4,
242 EAPSPlayerInitComplete = 5,
243 EAPSRecorderInitComplete = 6
244 };
245
246 static CQueueHandler* NewL(MQueueHandlerObserver* aObserver,
247 RMsgQueue<TAPSCommBuffer>* aQ,
248 RMsgQueue<TAPSCommBuffer>* aWriteQ,
249 TQueueHandlerType aType)
250 {
251 CQueueHandler* self = new (ELeave) CQueueHandler(aObserver, aQ, aWriteQ,
252 aType);
253 CleanupStack::PushL(self);
254 self->ConstructL();
255 CleanupStack::Pop(self);
256 return self;
257 }
258
259 // Destructor
260 ~CQueueHandler() { Cancel(); }
261
262 // Start listening queue event
263 void Start() {
264 iQ->NotifyDataAvailable(iStatus);
265 SetActive();
266 }
267
268 private:
269 // Constructor
270 CQueueHandler(MQueueHandlerObserver* aObserver,
271 RMsgQueue<TAPSCommBuffer>* aQ,
272 RMsgQueue<TAPSCommBuffer>* aWriteQ,
273 TQueueHandlerType aType)
274 : CActive(CActive::EPriorityHigh),
275 iQ(aQ), iWriteQ(aWriteQ), iObserver(aObserver), iType(aType)
276 {
277 CActiveScheduler::Add(this);
278
279 // use lower priority for comm queues
280 if ((iType == ERecordCommQueue) || (iType == EPlayCommQueue))
281 SetPriority(CActive::EPriorityStandard);
282 }
283
284 // Second phase constructor
285 void ConstructL() {}
286
287 // Inherited from CActive
288 void DoCancel() { iQ->CancelDataAvailable(); }
289
290 void RunL() {
291 if (iStatus != KErrNone) {
292 iObserver->NotifyError(iStatus.Int());
293 return;
294 }
295
296 TAPSCommBuffer buffer;
297 TInt ret = iQ->Receive(buffer);
298
299 if (ret != KErrNone) {
300 iObserver->NotifyError(ret);
301 return;
302 }
303
304 switch (iType) {
305 case ERecordQueue:
306 if (buffer.iCommand == EAPSRecordData) {
307 iObserver->RecCb(buffer, iObserver->UserData);
308 } else {
309 iObserver->NotifyError(buffer.iStatus);
310 }
311 break;
312
313 // Callbacks from the APS main thread
314 case EPlayCommQueue:
315 switch (buffer.iCommand) {
316 case EAPSPlayData:
317 if (buffer.iStatus == KErrUnderflow) {
318 iObserver->PlayCb(buffer, iObserver->UserData);
319 iWriteQ->Send(buffer);
320 }
321 break;
322 case EAPSPlayerInitialize:
323 iObserver->NotifyError(buffer.iStatus);
324 break;
325 case EAPSPlayerInitComplete:
326 iObserver->OutputStreamInitialized(buffer.iStatus);
327 break;
328 case EAPSRecorderInitComplete:
329 iObserver->InputStreamInitialized(buffer.iStatus);
330 break;
331 default:
332 iObserver->NotifyError(buffer.iStatus);
333 break;
334 }
335 break;
336
337 // Callbacks from the APS recorder thread
338 case ERecordCommQueue:
339 switch (buffer.iCommand) {
340 // The APS recorder thread will only report errors
341 // through this handler. All other callbacks will be
342 // sent from the APS main thread through EPlayCommQueue
343 case EAPSRecorderInitialize:
344 case EAPSRecordData:
345 default:
346 iObserver->NotifyError(buffer.iStatus);
347 break;
348 }
349 break;
350
351 default:
352 break;
353 }
354
355 // issue next request
356 iQ->NotifyDataAvailable(iStatus);
357 SetActive();
358 }
359
360 TInt RunError(TInt) {
361 return 0;
362 }
363
364 // Data
365 RMsgQueue<TAPSCommBuffer> *iQ; // (not owned)
366 RMsgQueue<TAPSCommBuffer> *iWriteQ; // (not owned)
367 MQueueHandlerObserver *iObserver; // (not owned)
368 TQueueHandlerType iType;
369 };
370
371 /*
372 * Audio setting for CPjAudioEngine.
373 */
374 class CPjAudioSetting
375 {
376 public:
377 TFourCC fourcc;
378 TAPSCodecMode mode;
379 TBool plc;
380 TBool vad;
381 TBool cng;
382 TBool loudspk;
383 };
384
385 /*
386 * Implementation: Symbian Input & Output Stream.
387 */
388 class CPjAudioEngine : public CBase, MQueueHandlerObserver
389 {
390 public:
391 enum State
392 {
393 STATE_NULL,
394 STATE_INITIALIZING,
395 STATE_READY,
396 STATE_STREAMING,
397 STATE_PENDING_STOP
398 };
399
400 ~CPjAudioEngine();
401
402 static CPjAudioEngine *NewL(struct aps_stream *parent_strm,
403 PjAudioCallback rec_cb,
404 PjAudioCallback play_cb,
405 void *user_data,
406 const CPjAudioSetting &setting);
407
408 TInt StartL();
409 void Stop();
410
411 TInt ActivateSpeaker(TBool active);
412
413 TInt SetVolume(TInt vol) { return iSession.SetVolume(vol); }
414 TInt GetVolume() { return iSession.Volume(); }
415 TInt GetMaxVolume() { return iSession.MaxVolume(); }
416
417 TInt SetGain(TInt gain) { return iSession.SetGain(gain); }
418 TInt GetGain() { return iSession.Gain(); }
419 TInt GetMaxGain() { return iSession.MaxGain(); }
420
421 private:
422 CPjAudioEngine(struct aps_stream *parent_strm,
423 PjAudioCallback rec_cb,
424 PjAudioCallback play_cb,
425 void *user_data,
426 const CPjAudioSetting &setting);
427 void ConstructL();
428
429 TInt InitPlayL();
430 TInt InitRecL();
431 TInt StartStreamL();
432 void Deinit();
433
434 // Inherited from MQueueHandlerObserver
435 virtual void InputStreamInitialized(const TInt aStatus);
436 virtual void OutputStreamInitialized(const TInt aStatus);
437 virtual void NotifyError(const TInt aError);
438
439 TBool session_opened;
440 State state_;
441 struct aps_stream *parentStrm_;
442 CPjAudioSetting setting_;
443
444 RAPSSession iSession;
445 TAPSInitSettings iPlaySettings;
446 TAPSInitSettings iRecSettings;
447
448 RMsgQueue<TAPSCommBuffer> iReadQ;
449 RMsgQueue<TAPSCommBuffer> iReadCommQ;
450 TBool readq_opened;
451 RMsgQueue<TAPSCommBuffer> iWriteQ;
452 RMsgQueue<TAPSCommBuffer> iWriteCommQ;
453 TBool writeq_opened;
454
455 CQueueHandler *iPlayCommHandler;
456 CQueueHandler *iRecCommHandler;
457 CQueueHandler *iRecHandler;
458 };
459
460
461 CPjAudioEngine* CPjAudioEngine::NewL(struct aps_stream *parent_strm,
462 PjAudioCallback rec_cb,
463 PjAudioCallback play_cb,
464 void *user_data,
465 const CPjAudioSetting &setting)
466 {
467 CPjAudioEngine* self = new (ELeave) CPjAudioEngine(parent_strm,
468 rec_cb, play_cb,
469 user_data,
470 setting);
471 CleanupStack::PushL(self);
472 self->ConstructL();
473 CleanupStack::Pop(self);
474 return self;
475 }
476
477 CPjAudioEngine::CPjAudioEngine(struct aps_stream *parent_strm,
478 PjAudioCallback rec_cb,
479 PjAudioCallback play_cb,
480 void *user_data,
481 const CPjAudioSetting &setting)
482 : MQueueHandlerObserver(rec_cb, play_cb, user_data),
483 session_opened(EFalse),
484 state_(STATE_NULL),
485 parentStrm_(parent_strm),
486 setting_(setting),
487 readq_opened(EFalse),
488 writeq_opened(EFalse),
489 iPlayCommHandler(0),
490 iRecCommHandler(0),
491 iRecHandler(0)
492 {
493 }
494
495 CPjAudioEngine::~CPjAudioEngine()
496 {
497 Deinit();
498
499 TRACE_((THIS_FILE, "Sound device destroyed"));
500 }
501
502 TInt CPjAudioEngine::InitPlayL()
503 {
504 TInt err = iSession.InitializePlayer(iPlaySettings);
505 if (err != KErrNone) {
506 Deinit();
507 snd_perror("Failed to initialize player", err);
508 return err;
509 }
510
511 // Open message queues for the output stream
512 TBuf<128> buf2 = iPlaySettings.iGlobal;
513 buf2.Append(_L("PlayQueue"));
514 TBuf<128> buf3 = iPlaySettings.iGlobal;
515 buf3.Append(_L("PlayCommQueue"));
516
517 while (iWriteQ.OpenGlobal(buf2))
518 User::After(10);
519 while (iWriteCommQ.OpenGlobal(buf3))
520 User::After(10);
521
522 writeq_opened = ETrue;
523
524 // Construct message queue handler
525 iPlayCommHandler = CQueueHandler::NewL(this, &iWriteCommQ, &iWriteQ,
526 CQueueHandler::EPlayCommQueue);
527
528 // Start observing APS callbacks on output stream message queue
529 iPlayCommHandler->Start();
530
531 return 0;
532 }
533
534 TInt CPjAudioEngine::InitRecL()
535 {
536 // Initialize input stream device
537 TInt err = iSession.InitializeRecorder(iRecSettings);
538 if (err != KErrNone && err != KErrAlreadyExists) {
539 Deinit();
540 snd_perror("Failed to initialize recorder", err);
541 return err;
542 }
543
544 TBuf<128> buf1 = iRecSettings.iGlobal;
545 buf1.Append(_L("RecordQueue"));
546 TBuf<128> buf4 = iRecSettings.iGlobal;
547 buf4.Append(_L("RecordCommQueue"));
548
549 // Must wait for APS thread to finish creating message queues
550 // before we can open and use them.
551 while (iReadQ.OpenGlobal(buf1))
552 User::After(10);
553 while (iReadCommQ.OpenGlobal(buf4))
554 User::After(10);
555
556 readq_opened = ETrue;
557
558 // Construct message queue handlers
559 iRecHandler = CQueueHandler::NewL(this, &iReadQ, NULL,
560 CQueueHandler::ERecordQueue);
561 iRecCommHandler = CQueueHandler::NewL(this, &iReadCommQ, NULL,
562 CQueueHandler::ERecordCommQueue);
563
564 // Start observing APS callbacks from on input stream message queue
565 iRecHandler->Start();
566 iRecCommHandler->Start();
567
568 return 0;
569 }
570
571 TInt CPjAudioEngine::StartL()
572 {
573 if (state_ == STATE_READY)
574 return StartStreamL();
575
576 PJ_ASSERT_RETURN(state_ == STATE_NULL, PJMEDIA_EAUD_INVOP);
577
578 if (!session_opened) {
579 TInt err = iSession.Connect();
580 if (err != KErrNone)
581 return err;
582 session_opened = ETrue;
583 }
584
585 // Even if only capturer are opened, playback thread of APS Server need
586 // to be run(?). Since some messages will be delivered via play comm queue.
587 state_ = STATE_INITIALIZING;
588
589 return InitPlayL();
590 }
591
592 void CPjAudioEngine::Stop()
593 {
594 if (state_ == STATE_STREAMING) {
595 iSession.Stop();
596 state_ = STATE_READY;
597 TRACE_((THIS_FILE, "Sound device stopped"));
598 } else if (state_ == STATE_INITIALIZING) {
599 // Initialization is on progress, so let's set the state to
600 // STATE_PENDING_STOP to prevent it starting the stream.
601 state_ = STATE_PENDING_STOP;
602
603 // Then wait until initialization done.
604 while (state_ != STATE_READY && state_ != STATE_NULL)
605 pj_symbianos_poll(-1, 100);
606 }
607 }
608
609 void CPjAudioEngine::ConstructL()
610 {
611 // Recorder settings
612 iRecSettings.iFourCC = setting_.fourcc;
613 iRecSettings.iGlobal = APP_UID;
614 iRecSettings.iPriority = TMdaPriority(100);
615 iRecSettings.iPreference = TMdaPriorityPreference(0x05210001);
616 iRecSettings.iSettings.iChannels = EMMFMono;
617 iRecSettings.iSettings.iSampleRate = EMMFSampleRate8000Hz;
618
619 // Player settings
620 iPlaySettings.iFourCC = setting_.fourcc;
621 iPlaySettings.iGlobal = APP_UID;
622 iPlaySettings.iPriority = TMdaPriority(100);
623 iPlaySettings.iPreference = TMdaPriorityPreference(0x05220001);
624 iPlaySettings.iSettings.iChannels = EMMFMono;
625 iPlaySettings.iSettings.iSampleRate = EMMFSampleRate8000Hz;
626 iPlaySettings.iSettings.iVolume = 0;
627
628 User::LeaveIfError(iSession.Connect());
629 session_opened = ETrue;
630 }
631
632 TInt CPjAudioEngine::StartStreamL()
633 {
634 pj_assert(state_==STATE_READY || state_==STATE_INITIALIZING);
635
636 iSession.SetCng(setting_.cng);
637 iSession.SetVadMode(setting_.vad);
638 iSession.SetPlc(setting_.plc);
639 iSession.SetEncoderMode(setting_.mode);
640 iSession.SetDecoderMode(setting_.mode);
641 iSession.ActivateLoudspeaker(setting_.loudspk);
642
643 // Not only capture
644 if (parentStrm_->param.dir != PJMEDIA_DIR_CAPTURE) {
645 iSession.Write();
646 TRACE_((THIS_FILE, "Player started"));
647 }
648
649 // Not only playback
650 if (parentStrm_->param.dir != PJMEDIA_DIR_PLAYBACK) {
651 iSession.Read();
652 TRACE_((THIS_FILE, "Recorder started"));
653 }
654
655 state_ = STATE_STREAMING;
656
657 return 0;
658 }
659
660 void CPjAudioEngine::Deinit()
661 {
662 Stop();
663
664 delete iRecHandler;
665 delete iPlayCommHandler;
666 delete iRecCommHandler;
667
668 if (session_opened) {
669 enum { APS_CLOSE_WAIT_TIME = 200 }; /* in msecs */
670
671 // On some devices, immediate closing after stopping may cause
672 // APS server panic KERN-EXEC 0, so let's wait for sometime before
673 // closing the client session.
674 snd_wait(APS_CLOSE_WAIT_TIME);
675
676 iSession.Close();
677 session_opened = EFalse;
678 }
679
680 if (readq_opened) {
681 iReadQ.Close();
682 iReadCommQ.Close();
683 readq_opened = EFalse;
684 }
685
686 if (writeq_opened) {
687 iWriteQ.Close();
688 iWriteCommQ.Close();
689 writeq_opened = EFalse;
690 }
691
692 state_ = STATE_NULL;
693 }
694
695 void CPjAudioEngine::InputStreamInitialized(const TInt aStatus)
696 {
697 TRACE_((THIS_FILE, "Recorder initialized, err=%d", aStatus));
698
699 if (aStatus == KErrNone) {
700 // Don't start the stream since Stop() has been requested.
701 if (state_ != STATE_PENDING_STOP) {
702 StartStreamL();
703 } else {
704 state_ = STATE_READY;
705 }
706 } else {
707 Deinit();
708 }
709 }
710
711 void CPjAudioEngine::OutputStreamInitialized(const TInt aStatus)
712 {
713 TRACE_((THIS_FILE, "Player initialized, err=%d", aStatus));
714
715 if (aStatus == KErrNone) {
716 if (parentStrm_->param.dir == PJMEDIA_DIR_PLAYBACK) {
717 // Don't start the stream since Stop() has been requested.
718 if (state_ != STATE_PENDING_STOP) {
719 StartStreamL();
720 } else {
721 state_ = STATE_READY;
722 }
723 } else
724 InitRecL();
725 } else {
726 Deinit();
727 }
728 }
729
730 void CPjAudioEngine::NotifyError(const TInt aError)
731 {
732 Deinit();
733 snd_perror("Error from CQueueHandler", aError);
734 }
735
736 TInt CPjAudioEngine::ActivateSpeaker(TBool active)
737 {
738 if (state_ == STATE_READY || state_ == STATE_STREAMING) {
739 iSession.ActivateLoudspeaker(active);
740 TRACE_((THIS_FILE, "Loudspeaker turned %s", (active? "on":"off")));
741 return KErrNone;
742 }
743 return KErrNotReady;
744 }
745
746 /****************************************************************************
747 * Internal APS callbacks for PCM format
748 */
749
750 static void RecCbPcm(TAPSCommBuffer &buf, void *user_data)
751 {
752 struct aps_stream *strm = (struct aps_stream*) user_data;
753
754 /* Buffer has to contain normal speech. */
755 pj_assert(buf.iBuffer[0] == 1 && buf.iBuffer[1] == 0);
756
757 /* Detect the recorder G.711 frame size, player frame size will follow
758 * this recorder frame size.
759 */
760 if (aps_g711_frame_len == 0) {
761 aps_g711_frame_len = buf.iBuffer.Length() < 160? 80 : 160;
762 TRACE_((THIS_FILE, "Detected APS G.711 frame size = %u samples",
763 aps_g711_frame_len));
764 }
765
766 /* Decode APS buffer (coded in G.711) and put the PCM result into rec_buf.
767 * Whenever rec_buf is full, call parent stream callback.
768 */
769 unsigned samples_processed = 0;
770
771 while (samples_processed < aps_g711_frame_len) {
772 unsigned samples_to_process;
773 unsigned samples_req;
774
775 samples_to_process = aps_g711_frame_len - samples_processed;
776 samples_req = (strm->param.samples_per_frame /
777 strm->param.channel_count /
778 strm->resample_factor) -
779 strm->rec_buf_len;
780 if (samples_to_process > samples_req)
781 samples_to_process = samples_req;
782
783 pjmedia_ulaw_decode(&strm->rec_buf[strm->rec_buf_len],
784 buf.iBuffer.Ptr() + 2 + samples_processed,
785 samples_to_process);
786
787 strm->rec_buf_len += samples_to_process;
788 samples_processed += samples_to_process;
789
790 /* Buffer is full, time to call parent callback */
791 if (strm->rec_buf_len == strm->param.samples_per_frame /
792 strm->param.channel_count /
793 strm->resample_factor)
794 {
795 pjmedia_frame f;
796
797 /* Need to resample clock rate? */
798 if (strm->rec_resample) {
799 unsigned resampled = 0;
800
801 while (resampled < strm->rec_buf_len) {
802 pjmedia_resample_run(strm->rec_resample,
803 &strm->rec_buf[resampled],
804 strm->pcm_buf +
805 resampled * strm->resample_factor);
806 resampled += 80;
807 }
808 f.buf = strm->pcm_buf;
809 } else {
810 f.buf = strm->rec_buf;
811 }
812
813 /* Need to convert channel count? */
814 if (strm->param.channel_count != 1) {
815 pjmedia_convert_channel_1ton((pj_int16_t*)f.buf,
816 (pj_int16_t*)f.buf,
817 strm->param.channel_count,
818 strm->param.samples_per_frame /
819 strm->param.channel_count,
820 0);
821 }
822
823 /* Call parent callback */
824 f.type = PJMEDIA_FRAME_TYPE_AUDIO;
825 f.size = strm->param.samples_per_frame << 1;
826 strm->rec_cb(strm->user_data, &f);
827 strm->rec_buf_len = 0;
828 }
829 }
830 }
831
832 static void PlayCbPcm(TAPSCommBuffer &buf, void *user_data)
833 {
834 struct aps_stream *strm = (struct aps_stream*) user_data;
835 unsigned g711_frame_len = aps_g711_frame_len;
836
837 /* Init buffer attributes and header. */
838 buf.iCommand = CQueueHandler::EAPSPlayData;
839 buf.iStatus = 0;
840 buf.iBuffer.Zero();
841 buf.iBuffer.Append(1);
842 buf.iBuffer.Append(0);
843
844 /* Assume frame size is 10ms if frame size hasn't been known. */
845 if (g711_frame_len == 0)
846 g711_frame_len = 80;
847
848 /* Call parent stream callback to get PCM samples to play,
849 * encode the PCM samples into G.711 and put it into APS buffer.
850 */
851 unsigned samples_processed = 0;
852
853 while (samples_processed < g711_frame_len) {
854 /* Need more samples to play, time to call parent callback */
855 if (strm->play_buf_len == 0) {
856 pjmedia_frame f;
857 unsigned samples_got;
858
859 f.size = strm->param.samples_per_frame << 1;
860 if (strm->play_resample || strm->param.channel_count != 1)
861 f.buf = strm->pcm_buf;
862 else
863 f.buf = strm->play_buf;
864
865 /* Call parent callback */
866 strm->play_cb(strm->user_data, &f);
867 if (f.type != PJMEDIA_FRAME_TYPE_AUDIO) {
868 pjmedia_zero_samples((pj_int16_t*)f.buf,
869 strm->param.samples_per_frame);
870 }
871
872 samples_got = strm->param.samples_per_frame /
873 strm->param.channel_count /
874 strm->resample_factor;
875
876 /* Need to convert channel count? */
877 if (strm->param.channel_count != 1) {
878 pjmedia_convert_channel_nto1((pj_int16_t*)f.buf,
879 (pj_int16_t*)f.buf,
880 strm->param.channel_count,
881 strm->param.samples_per_frame,
882 PJ_FALSE,
883 0);
884 }
885
886 /* Need to resample clock rate? */
887 if (strm->play_resample) {
888 unsigned resampled = 0;
889
890 while (resampled < samples_got)
891 {
892 pjmedia_resample_run(strm->play_resample,
893 strm->pcm_buf +
894 resampled * strm->resample_factor,
895 &strm->play_buf[resampled]);
896 resampled += 80;
897 }
898 }
899
900 strm->play_buf_len = samples_got;
901 strm->play_buf_start = 0;
902 }
903
904 unsigned tmp;
905
906 tmp = PJ_MIN(strm->play_buf_len, g711_frame_len - samples_processed);
907 pjmedia_ulaw_encode((pj_uint8_t*)&strm->play_buf[strm->play_buf_start],
908 &strm->play_buf[strm->play_buf_start],
909 tmp);
910 buf.iBuffer.Append((TUint8*)&strm->play_buf[strm->play_buf_start], tmp);
911 samples_processed += tmp;
912 strm->play_buf_len -= tmp;
913 strm->play_buf_start += tmp;
914 }
915 }
916
917 /****************************************************************************
918 * Internal APS callbacks for non-PCM format
919 */
920
921 static void RecCb(TAPSCommBuffer &buf, void *user_data)
922 {
923 struct aps_stream *strm = (struct aps_stream*) user_data;
924 pjmedia_frame_ext *frame = (pjmedia_frame_ext*) strm->rec_buf;
925
926 switch(strm->param.ext_fmt.id) {
927 case PJMEDIA_FORMAT_AMR:
928 {
929 const pj_uint8_t *p = (const pj_uint8_t*)buf.iBuffer.Ptr() + 1;
930 unsigned len = buf.iBuffer.Length() - 1;
931
932 pjmedia_frame_ext_append_subframe(frame, p, len << 3, 160);
933 if (frame->samples_cnt == strm->param.samples_per_frame) {
934 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
935 strm->rec_cb(strm->user_data, (pjmedia_frame*)frame);
936 frame->samples_cnt = 0;
937 frame->subframe_cnt = 0;
938 }
939 }
940 break;
941
942 case PJMEDIA_FORMAT_G729:
943 {
944 /* Check if we got a normal or SID frame. */
945 if (buf.iBuffer[0] != 0 || buf.iBuffer[1] != 0) {
946 enum { NORMAL_LEN = 22, SID_LEN = 8 };
947 TBitStream *bitstream = (TBitStream*)strm->strm_data;
948 unsigned src_len = buf.iBuffer.Length()- 2;
949
950 pj_assert(src_len == NORMAL_LEN || src_len == SID_LEN);
951
952 const TDesC8& p = bitstream->CompressG729Frame(
953 buf.iBuffer.Right(src_len),
954 src_len == SID_LEN);
955
956 pjmedia_frame_ext_append_subframe(frame, p.Ptr(),
957 p.Length() << 3, 80);
958 } else { /* We got null frame. */
959 pjmedia_frame_ext_append_subframe(frame, NULL, 0, 80);
960 }
961
962 if (frame->samples_cnt == strm->param.samples_per_frame) {
963 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
964 strm->rec_cb(strm->user_data, (pjmedia_frame*)frame);
965 frame->samples_cnt = 0;
966 frame->subframe_cnt = 0;
967 }
968 }
969 break;
970
971 case PJMEDIA_FORMAT_ILBC:
972 {
973 unsigned samples_got;
974
975 samples_got =
976 strm->param.ext_fmt.det.aud.avg_bps == 15200? 160 : 240;
977
978 /* Check if we got a normal frame. */
979 if (buf.iBuffer[0] == 1 && buf.iBuffer[1] == 0) {
980 const pj_uint8_t *p = (const pj_uint8_t*)buf.iBuffer.Ptr() + 2;
981 unsigned len = buf.iBuffer.Length() - 2;
982
983 pjmedia_frame_ext_append_subframe(frame, p, len << 3,
984 samples_got);
985 } else { /* We got null frame. */
986 pjmedia_frame_ext_append_subframe(frame, NULL, 0, samples_got);
987 }
988
989 if (frame->samples_cnt == strm->param.samples_per_frame) {
990 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
991 strm->rec_cb(strm->user_data, (pjmedia_frame*)frame);
992 frame->samples_cnt = 0;
993 frame->subframe_cnt = 0;
994 }
995 }
996 break;
997
998 case PJMEDIA_FORMAT_PCMU:
999 case PJMEDIA_FORMAT_PCMA:
1000 {
1001 unsigned samples_processed = 0;
1002
1003 /* Make sure it is normal frame. */
1004 pj_assert(buf.iBuffer[0] == 1 && buf.iBuffer[1] == 0);
1005
1006 /* Detect the recorder G.711 frame size, player frame size will
1007 * follow this recorder frame size.
1008 */
1009 if (aps_g711_frame_len == 0) {
1010 aps_g711_frame_len = buf.iBuffer.Length() < 160? 80 : 160;
1011 TRACE_((THIS_FILE, "Detected APS G.711 frame size = %u samples",
1012 aps_g711_frame_len));
1013 }
1014
1015 /* Convert APS buffer format into pjmedia_frame_ext. Whenever
1016 * samples count in the frame is equal to stream's samples per
1017 * frame, call parent stream callback.
1018 */
1019 while (samples_processed < aps_g711_frame_len) {
1020 unsigned tmp;
1021 const pj_uint8_t *pb = (const pj_uint8_t*)buf.iBuffer.Ptr() +
1022 2 + samples_processed;
1023
1024 tmp = PJ_MIN(strm->param.samples_per_frame - frame->samples_cnt,
1025 aps_g711_frame_len - samples_processed);
1026
1027 pjmedia_frame_ext_append_subframe(frame, pb, tmp << 3, tmp);
1028 samples_processed += tmp;
1029
1030 if (frame->samples_cnt == strm->param.samples_per_frame) {
1031 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
1032 strm->rec_cb(strm->user_data, (pjmedia_frame*)frame);
1033 frame->samples_cnt = 0;
1034 frame->subframe_cnt = 0;
1035 }
1036 }
1037 }
1038 break;
1039
1040 default:
1041 break;
1042 }
1043 }
1044
1045 static void PlayCb(TAPSCommBuffer &buf, void *user_data)
1046 {
1047 struct aps_stream *strm = (struct aps_stream*) user_data;
1048 pjmedia_frame_ext *frame = (pjmedia_frame_ext*) strm->play_buf;
1049
1050 /* Init buffer attributes and header. */
1051 buf.iCommand = CQueueHandler::EAPSPlayData;
1052 buf.iStatus = 0;
1053 buf.iBuffer.Zero();
1054
1055 switch(strm->param.ext_fmt.id) {
1056 case PJMEDIA_FORMAT_AMR:
1057 {
1058 if (frame->samples_cnt == 0) {
1059 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
1060 strm->play_cb(strm->user_data, (pjmedia_frame*)frame);
1061 pj_assert(frame->base.type==PJMEDIA_FRAME_TYPE_EXTENDED ||
1062 frame->base.type==PJMEDIA_FRAME_TYPE_NONE);
1063 }
1064
1065 if (frame->base.type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1066 pjmedia_frame_ext_subframe *sf;
1067 unsigned samples_cnt;
1068
1069 sf = pjmedia_frame_ext_get_subframe(frame, 0);
1070 samples_cnt = frame->samples_cnt / frame->subframe_cnt;
1071
1072 if (sf->data && sf->bitlen) {
1073 /* AMR header for APS is one byte, the format (may be!):
1074 * 0xxxxy00, where xxxx:frame type, y:not sure.
1075 */
1076 unsigned len = (sf->bitlen+7)>>3;
1077 enum {SID_FT = 8 };
1078 pj_uint8_t amr_header = 4, ft = SID_FT;
1079
1080 if (len >= pjmedia_codec_amrnb_framelen[0])
1081 ft = pjmedia_codec_amr_get_mode2(PJ_TRUE, len);
1082
1083 amr_header |= ft << 3;
1084 buf.iBuffer.Append(amr_header);
1085
1086 buf.iBuffer.Append((TUint8*)sf->data, len);
1087 } else {
1088 enum {NO_DATA_FT = 15 };
1089 pj_uint8_t amr_header = 4 | (NO_DATA_FT << 3);
1090
1091 buf.iBuffer.Append(amr_header);
1092 }
1093
1094 pjmedia_frame_ext_pop_subframes(frame, 1);
1095
1096 } else { /* PJMEDIA_FRAME_TYPE_NONE */
1097 enum {NO_DATA_FT = 15 };
1098 pj_uint8_t amr_header = 4 | (NO_DATA_FT << 3);
1099
1100 buf.iBuffer.Append(amr_header);
1101
1102 frame->samples_cnt = 0;
1103 frame->subframe_cnt = 0;
1104 }
1105 }
1106 break;
1107
1108 case PJMEDIA_FORMAT_G729:
1109 {
1110 if (frame->samples_cnt == 0) {
1111 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
1112 strm->play_cb(strm->user_data, (pjmedia_frame*)frame);
1113 pj_assert(frame->base.type==PJMEDIA_FRAME_TYPE_EXTENDED ||
1114 frame->base.type==PJMEDIA_FRAME_TYPE_NONE);
1115 }
1116
1117 if (frame->base.type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1118 pjmedia_frame_ext_subframe *sf;
1119 unsigned samples_cnt;
1120
1121 sf = pjmedia_frame_ext_get_subframe(frame, 0);
1122 samples_cnt = frame->samples_cnt / frame->subframe_cnt;
1123
1124 if (sf->data && sf->bitlen) {
1125 enum { NORMAL_LEN = 10, SID_LEN = 2 };
1126 pj_bool_t sid_frame = ((sf->bitlen >> 3) == SID_LEN);
1127 TBitStream *bitstream = (TBitStream*)strm->strm_data;
1128 const TPtrC8 src(sf->data, sf->bitlen>>3);
1129 const TDesC8 &dst = bitstream->ExpandG729Frame(src,
1130 sid_frame);
1131 if (sid_frame) {
1132 buf.iBuffer.Append(2);
1133 buf.iBuffer.Append(0);
1134 } else {
1135 buf.iBuffer.Append(1);
1136 buf.iBuffer.Append(0);
1137 }
1138 buf.iBuffer.Append(dst);
1139 } else {
1140 buf.iBuffer.Append(2);
1141 buf.iBuffer.Append(0);
1142 buf.iBuffer.AppendFill(0, 22);
1143 }
1144
1145 pjmedia_frame_ext_pop_subframes(frame, 1);
1146
1147 } else { /* PJMEDIA_FRAME_TYPE_NONE */
1148 buf.iBuffer.Append(2);
1149 buf.iBuffer.Append(0);
1150 buf.iBuffer.AppendFill(0, 22);
1151
1152 frame->samples_cnt = 0;
1153 frame->subframe_cnt = 0;
1154 }
1155 }
1156 break;
1157
1158 case PJMEDIA_FORMAT_ILBC:
1159 {
1160 if (frame->samples_cnt == 0) {
1161 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
1162 strm->play_cb(strm->user_data, (pjmedia_frame*)frame);
1163 pj_assert(frame->base.type==PJMEDIA_FRAME_TYPE_EXTENDED ||
1164 frame->base.type==PJMEDIA_FRAME_TYPE_NONE);
1165 }
1166
1167 if (frame->base.type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1168 pjmedia_frame_ext_subframe *sf;
1169 unsigned samples_cnt;
1170
1171 sf = pjmedia_frame_ext_get_subframe(frame, 0);
1172 samples_cnt = frame->samples_cnt / frame->subframe_cnt;
1173
1174 pj_assert((strm->param.ext_fmt.det.aud.avg_bps == 15200 &&
1175 samples_cnt == 160) ||
1176 (strm->param.ext_fmt.det.aud.avg_bps != 15200 &&
1177 samples_cnt == 240));
1178
1179 if (sf->data && sf->bitlen) {
1180 buf.iBuffer.Append(1);
1181 buf.iBuffer.Append(0);
1182 buf.iBuffer.Append((TUint8*)sf->data, sf->bitlen>>3);
1183 } else {
1184 buf.iBuffer.Append(0);
1185 buf.iBuffer.Append(0);
1186 }
1187
1188 pjmedia_frame_ext_pop_subframes(frame, 1);
1189
1190 } else { /* PJMEDIA_FRAME_TYPE_NONE */
1191 buf.iBuffer.Append(0);
1192 buf.iBuffer.Append(0);
1193
1194 frame->samples_cnt = 0;
1195 frame->subframe_cnt = 0;
1196 }
1197 }
1198 break;
1199
1200 case PJMEDIA_FORMAT_PCMU:
1201 case PJMEDIA_FORMAT_PCMA:
1202 {
1203 unsigned samples_ready = 0;
1204 unsigned samples_req = aps_g711_frame_len;
1205
1206 /* Assume frame size is 10ms if frame size hasn't been known. */
1207 if (samples_req == 0)
1208 samples_req = 80;
1209
1210 buf.iBuffer.Append(1);
1211 buf.iBuffer.Append(0);
1212
1213 /* Call parent stream callback to get samples to play. */
1214 while (samples_ready < samples_req) {
1215 if (frame->samples_cnt == 0) {
1216 frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
1217 strm->play_cb(strm->user_data, (pjmedia_frame*)frame);
1218 pj_assert(frame->base.type==PJMEDIA_FRAME_TYPE_EXTENDED ||
1219 frame->base.type==PJMEDIA_FRAME_TYPE_NONE);
1220 }
1221
1222 if (frame->base.type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1223 pjmedia_frame_ext_subframe *sf;
1224 unsigned samples_cnt;
1225
1226 sf = pjmedia_frame_ext_get_subframe(frame, 0);
1227 samples_cnt = frame->samples_cnt / frame->subframe_cnt;
1228 if (sf->data && sf->bitlen) {
1229 buf.iBuffer.Append((TUint8*)sf->data, sf->bitlen>>3);
1230 } else {
1231 pj_uint8_t silc;
1232 silc = (strm->param.ext_fmt.id==PJMEDIA_FORMAT_PCMU)?
1233 pjmedia_linear2ulaw(0) : pjmedia_linear2alaw(0);
1234 buf.iBuffer.AppendFill(silc, samples_cnt);
1235 }
1236 samples_ready += samples_cnt;
1237
1238 pjmedia_frame_ext_pop_subframes(frame, 1);
1239
1240 } else { /* PJMEDIA_FRAME_TYPE_NONE */
1241 pj_uint8_t silc;
1242
1243 silc = (strm->param.ext_fmt.id==PJMEDIA_FORMAT_PCMU)?
1244 pjmedia_linear2ulaw(0) : pjmedia_linear2alaw(0);
1245 buf.iBuffer.AppendFill(silc, samples_req - samples_ready);
1246
1247 samples_ready = samples_req;
1248 frame->samples_cnt = 0;
1249 frame->subframe_cnt = 0;
1250 }
1251 }
1252 }
1253 break;
1254
1255 default:
1256 break;
1257 }
1258 }
1259
1260
1261 /****************************************************************************
1262 * Factory operations
1263 */
1264
1265 /*
1266 * C compatible declaration of APS factory.
1267 */
1268 PJ_BEGIN_DECL
1269 PJ_DECL(pjmedia_aud_dev_factory*) pjmedia_aps_factory(pj_pool_factory *pf);
1270 PJ_END_DECL
1271
1272 /*
1273 * Init APS audio driver.
1274 */
1275 PJ_DEF(pjmedia_aud_dev_factory*) pjmedia_aps_factory(pj_pool_factory *pf)
1276 {
1277 struct aps_factory *f;
1278 pj_pool_t *pool;
1279
1280 pool = pj_pool_create(pf, "APS", 1000, 1000, NULL);
1281 f = PJ_POOL_ZALLOC_T(pool, struct aps_factory);
1282 f->pf = pf;
1283 f->pool = pool;
1284 f->base.op = &factory_op;
1285
1286 return &f->base;
1287 }
1288
1289 /* API: init factory */
1290 static pj_status_t factory_init(pjmedia_aud_dev_factory *f)
1291 {
1292 struct aps_factory *af = (struct aps_factory*)f;
1293
1294 pj_ansi_strcpy(af->dev_info.name, "S60 APS");
1295 af->dev_info.default_samples_per_sec = 8000;
1296 af->dev_info.caps = PJMEDIA_AUD_DEV_CAP_EXT_FORMAT |
1297 //PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING |
1298 PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING |
1299 PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE |
1300 PJMEDIA_AUD_DEV_CAP_VAD |
1301 PJMEDIA_AUD_DEV_CAP_CNG;
1302 af->dev_info.routes = PJMEDIA_AUD_DEV_ROUTE_EARPIECE |
1303 PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER;
1304 af->dev_info.input_count = 1;
1305 af->dev_info.output_count = 1;
1306
1307 /* Enumerate codecs by trying to initialize each codec and examining
1308 * the error code. Consider the following:
1309 * - not possible to reinitialize the same APS session with
1310 * different settings,
1311 * - closing APS session and trying to immediately reconnect may fail,
1312 * clients should wait ~5s before attempting to reconnect.
1313 */
1314
1315 unsigned i, fmt_cnt = 0;
1316 pj_bool_t g711_supported = PJ_FALSE;
1317
1318 /* Do not change the order! */
1319 TFourCC fourcc[] = {
1320 TFourCC(KMCPFourCCIdAMRNB),
1321 TFourCC(KMCPFourCCIdG711),
1322 TFourCC(KMCPFourCCIdG729),
1323 TFourCC(KMCPFourCCIdILBC)
1324 };
1325
1326 for (i = 0; i < PJ_ARRAY_SIZE(fourcc); ++i) {
1327 pj_bool_t supported = PJ_FALSE;
1328 unsigned retry_cnt = 0;
1329 enum { MAX_RETRY = 3 };
1330
1331 #if (PJMEDIA_AUDIO_DEV_SYMB_APS_DETECTS_CODEC == 0)
1332 /* Codec detection is disabled */
1333 supported = PJ_TRUE;
1334 #elif (PJMEDIA_AUDIO_DEV_SYMB_APS_DETECTS_CODEC == 1)
1335 /* Minimal codec detection, AMR-NB and G.711 only */
1336 if (i > 1) {
1337 /* If G.711 has been checked, skip G.729 and iLBC checks */
1338 retry_cnt = MAX_RETRY;
1339 supported = g711_supported;
1340 }
1341 #endif
1342
1343 while (!supported && ++retry_cnt <= MAX_RETRY) {
1344 RAPSSession iSession;
1345 TAPSInitSettings iPlaySettings;
1346 TAPSInitSettings iRecSettings;
1347 TInt err;
1348
1349 // Recorder settings
1350 iRecSettings.iGlobal = APP_UID;
1351 iRecSettings.iPriority = TMdaPriority(100);
1352 iRecSettings.iPreference = TMdaPriorityPreference(0x05210001);
1353 iRecSettings.iSettings.iChannels = EMMFMono;
1354 iRecSettings.iSettings.iSampleRate = EMMFSampleRate8000Hz;
1355
1356 // Player settings
1357 iPlaySettings.iGlobal = APP_UID;
1358 iPlaySettings.iPriority = TMdaPriority(100);
1359 iPlaySettings.iPreference = TMdaPriorityPreference(0x05220001);
1360 iPlaySettings.iSettings.iChannels = EMMFMono;
1361 iPlaySettings.iSettings.iSampleRate = EMMFSampleRate8000Hz;
1362
1363 iRecSettings.iFourCC = iPlaySettings.iFourCC = fourcc[i];
1364
1365 err = iSession.Connect();
1366 if (err == KErrNone)
1367 err = iSession.InitializePlayer(iPlaySettings);
1368 if (err == KErrNone)
1369 err = iSession.InitializeRecorder(iRecSettings);
1370
1371 // On some devices, immediate closing causes APS Server panic,
1372 // e.g: N95, so let's just wait for some time before closing.
1373 enum { APS_CLOSE_WAIT_TIME = 200 }; /* in msecs */
1374 snd_wait(APS_CLOSE_WAIT_TIME);
1375
1376 iSession.Close();
1377
1378 if (err == KErrNone) {
1379 /* All fine, stop retyring */
1380 supported = PJ_TRUE;
1381 } else if (err == KErrAlreadyExists && retry_cnt < MAX_RETRY) {
1382 /* Seems that the previous session is still arround,
1383 * let's wait before retrying.
1384 */
1385 enum { RETRY_WAIT_TIME = 3000 }; /* in msecs */
1386 snd_wait(RETRY_WAIT_TIME);
1387 } else {
1388 /* Seems that this format is not supported */
1389 retry_cnt = MAX_RETRY;
1390 }
1391 }
1392
1393 if (supported) {
1394 pjmedia_format ext_fmt;
1395
1396 switch(i) {
1397 case 0: /* AMRNB */
1398 pjmedia_format_init_audio(&ext_fmt, PJMEDIA_FORMAT_AMR,
1399 8000, 1, 16, 20, 7400, 12200);
1400 af->dev_info.ext_fmt[fmt_cnt] = ext_fmt;
1401 //af->dev_info.ext_fmt[fmt_cnt].vad = PJ_TRUE;
1402 ++fmt_cnt;
1403 break;
1404 case 1: /* G.711 */
1405 pjmedia_format_init_audio(&ext_fmt, PJMEDIA_FORMAT_PCMU,
1406 8000, 1, 16, 20, 64000, 64000);
1407 af->dev_info.ext_fmt[fmt_cnt] = ext_fmt;
1408 //af->dev_info.ext_fmt[fmt_cnt].vad = PJ_FALSE;
1409 ++fmt_cnt;
1410 pjmedia_format_init_audio(&ext_fmt, PJMEDIA_FORMAT_PCMA,
1411 8000, 1, 16, 20, 64000, 64000);
1412 af->dev_info.ext_fmt[fmt_cnt] = ext_fmt;
1413 //af->dev_info.ext_fmt[fmt_cnt].vad = PJ_FALSE;
1414 ++fmt_cnt;
1415 g711_supported = PJ_TRUE;
1416 break;
1417 case 2: /* G.729 */
1418 pjmedia_format_init_audio(&ext_fmt, PJMEDIA_FORMAT_G729,
1419 8000, 1, 16, 20, 8000, 8000);
1420 af->dev_info.ext_fmt[fmt_cnt] = ext_fmt;
1421 //af->dev_info.ext_fmt[fmt_cnt].vad = PJ_FALSE;
1422 ++fmt_cnt;
1423 break;
1424 case 3: /* iLBC */
1425 pjmedia_format_init_audio(&ext_fmt, PJMEDIA_FORMAT_ILBC,
1426 8000, 1, 16, 30, 13333, 15200);
1427 af->dev_info.ext_fmt[fmt_cnt] = ext_fmt;
1428 //af->dev_info.ext_fmt[fmt_cnt].vad = PJ_TRUE;
1429 ++fmt_cnt;
1430 break;
1431 }
1432 }
1433 }
1434
1435 af->dev_info.ext_fmt_cnt = fmt_cnt;
1436
1437 PJ_LOG(4, (THIS_FILE, "APS initialized"));
1438
1439 return PJ_SUCCESS;
1440 }
1441
1442 /* API: destroy factory */
1443 static pj_status_t factory_destroy(pjmedia_aud_dev_factory *f)
1444 {
1445 struct aps_factory *af = (struct aps_factory*)f;
1446 pj_pool_t *pool = af->pool;
1447
1448 af->pool = NULL;
1449 pj_pool_release(pool);
1450
1451 PJ_LOG(4, (THIS_FILE, "APS destroyed"));
1452
1453 return PJ_SUCCESS;
1454 }
1455
1456 /* API: refresh the device list */
1457 static pj_status_t factory_refresh(pjmedia_aud_dev_factory *f)
1458 {
1459 PJ_UNUSED_ARG(f);
1460 return PJ_ENOTSUP;
1461 }
1462
1463 /* API: get number of devices */
1464 static unsigned factory_get_dev_count(pjmedia_aud_dev_factory *f)
1465 {
1466 PJ_UNUSED_ARG(f);
1467 return 1;
1468 }
1469
1470 /* API: get device info */
1471 static pj_status_t factory_get_dev_info(pjmedia_aud_dev_factory *f,
1472 unsigned index,
1473 pjmedia_aud_dev_info *info)
1474 {
1475 struct aps_factory *af = (struct aps_factory*)f;
1476
1477 PJ_ASSERT_RETURN(index == 0, PJMEDIA_EAUD_INVDEV);
1478
1479 pj_memcpy(info, &af->dev_info, sizeof(*info));
1480
1481 return PJ_SUCCESS;
1482 }
1483
1484 /* API: create default device parameter */
1485 static pj_status_t factory_default_param(pjmedia_aud_dev_factory *f,
1486 unsigned index,
1487 pjmedia_aud_param *param)
1488 {
1489 struct aps_factory *af = (struct aps_factory*)f;
1490
1491 PJ_ASSERT_RETURN(index == 0, PJMEDIA_EAUD_INVDEV);
1492
1493 pj_bzero(param, sizeof(*param));
1494 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
1495 param->rec_id = index;
1496 param->play_id = index;
1497 param->clock_rate = af->dev_info.default_samples_per_sec;
1498 param->channel_count = 1;
1499 param->samples_per_frame = af->dev_info.default_samples_per_sec * 20 / 1000;
1500 param->bits_per_sample = BITS_PER_SAMPLE;
1501 param->flags = PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE;
1502 param->output_route = PJMEDIA_AUD_DEV_ROUTE_EARPIECE;
1503
1504 return PJ_SUCCESS;
1505 }
1506
1507
1508 /* API: create stream */
1509 static pj_status_t factory_create_stream(pjmedia_aud_dev_factory *f,
1510 const pjmedia_aud_param *param,
1511 pjmedia_aud_rec_cb rec_cb,
1512 pjmedia_aud_play_cb play_cb,
1513 void *user_data,
1514 pjmedia_aud_stream **p_aud_strm)
1515 {
1516 struct aps_factory *af = (struct aps_factory*)f;
1517 pj_pool_t *pool;
1518 struct aps_stream *strm;
1519
1520 CPjAudioSetting aps_setting;
1521 PjAudioCallback aps_rec_cb;
1522 PjAudioCallback aps_play_cb;
1523
1524 /* Can only support 16bits per sample */
1525 PJ_ASSERT_RETURN(param->bits_per_sample == BITS_PER_SAMPLE, PJ_EINVAL);
1526
1527 /* Supported clock rates:
1528 * - for non-PCM format: 8kHz
1529 * - for PCM format: 8kHz and 16kHz
1530 */
1531 PJ_ASSERT_RETURN(param->clock_rate == 8000 ||
1532 (param->clock_rate == 16000 &&
1533 param->ext_fmt.id == PJMEDIA_FORMAT_L16),
1534 PJ_EINVAL);
1535
1536 /* Supported channels number:
1537 * - for non-PCM format: mono
1538 * - for PCM format: mono and stereo
1539 */
1540 PJ_ASSERT_RETURN(param->channel_count == 1 ||
1541 (param->channel_count == 2 &&
1542 param->ext_fmt.id == PJMEDIA_FORMAT_L16),
1543 PJ_EINVAL);
1544
1545 /* Create and Initialize stream descriptor */
1546 pool = pj_pool_create(af->pf, "aps-dev", 1000, 1000, NULL);
1547 PJ_ASSERT_RETURN(pool, PJ_ENOMEM);
1548
1549 strm = PJ_POOL_ZALLOC_T(pool, struct aps_stream);
1550 strm->pool = pool;
1551 strm->param = *param;
1552
1553 if (strm->param.flags & PJMEDIA_AUD_DEV_CAP_EXT_FORMAT == 0)
1554 strm->param.ext_fmt.id = PJMEDIA_FORMAT_L16;
1555
1556 /* Set audio engine fourcc. */
1557 switch(strm->param.ext_fmt.id) {
1558 case PJMEDIA_FORMAT_L16:
1559 case PJMEDIA_FORMAT_PCMU:
1560 case PJMEDIA_FORMAT_PCMA:
1561 aps_setting.fourcc = TFourCC(KMCPFourCCIdG711);
1562 break;
1563 case PJMEDIA_FORMAT_AMR:
1564 aps_setting.fourcc = TFourCC(KMCPFourCCIdAMRNB);
1565 break;
1566 case PJMEDIA_FORMAT_G729:
1567 aps_setting.fourcc = TFourCC(KMCPFourCCIdG729);
1568 break;
1569 case PJMEDIA_FORMAT_ILBC:
1570 aps_setting.fourcc = TFourCC(KMCPFourCCIdILBC);
1571 break;
1572 default:
1573 aps_setting.fourcc = 0;
1574 break;
1575 }
1576
1577 /* Set audio engine mode. */
1578 if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_AMR)
1579 {
1580 aps_setting.mode = (TAPSCodecMode)strm->param.ext_fmt.det.aud.avg_bps;
1581 }
1582 else if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_PCMU ||
1583 strm->param.ext_fmt.id == PJMEDIA_FORMAT_L16 ||
1584 (strm->param.ext_fmt.id == PJMEDIA_FORMAT_ILBC &&
1585 strm->param.ext_fmt.det.aud.avg_bps != 15200))
1586 {
1587 aps_setting.mode = EULawOr30ms;
1588 }
1589 else if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_PCMA ||
1590 (strm->param.ext_fmt.id == PJMEDIA_FORMAT_ILBC &&
1591 strm->param.ext_fmt.det.aud.avg_bps == 15200))
1592 {
1593 aps_setting.mode = EALawOr20ms;
1594 }
1595
1596 /* Disable VAD on L16, G711, and also G729 (G729's VAD potentially
1597 * causes noise?).
1598 */
1599 if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_PCMU ||
1600 strm->param.ext_fmt.id == PJMEDIA_FORMAT_PCMA ||
1601 strm->param.ext_fmt.id == PJMEDIA_FORMAT_L16 ||
1602 strm->param.ext_fmt.id == PJMEDIA_FORMAT_G729)
1603 {
1604 aps_setting.vad = EFalse;
1605 } else {
1606 aps_setting.vad = (strm->param.flags & PJMEDIA_AUD_DEV_CAP_VAD) &&
1607 strm->param.vad_enabled;
1608 }
1609
1610 /* Set other audio engine attributes. */
1611 aps_setting.plc = (strm->param.flags & PJMEDIA_AUD_DEV_CAP_PLC) &&
1612 strm->param.plc_enabled;
1613 aps_setting.cng = aps_setting.vad;
1614 aps_setting.loudspk =
1615 strm->param.output_route==PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER;
1616
1617 /* Set audio engine callbacks. */
1618 if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_L16) {
1619 aps_play_cb = &PlayCbPcm;
1620 aps_rec_cb = &RecCbPcm;
1621 } else {
1622 aps_play_cb = &PlayCb;
1623 aps_rec_cb = &RecCb;
1624 }
1625
1626 strm->rec_cb = rec_cb;
1627 strm->play_cb = play_cb;
1628 strm->user_data = user_data;
1629 strm->resample_factor = strm->param.clock_rate / 8000;
1630
1631 /* play_buf size is samples per frame scaled in to 8kHz mono. */
1632 strm->play_buf = (pj_int16_t*)pj_pool_zalloc(
1633 pool,
1634 (strm->param.samples_per_frame /
1635 strm->resample_factor /
1636 strm->param.channel_count) << 1);
1637 strm->play_buf_len = 0;
1638 strm->play_buf_start = 0;
1639
1640 /* rec_buf size is samples per frame scaled in to 8kHz mono. */
1641 strm->rec_buf = (pj_int16_t*)pj_pool_zalloc(
1642 pool,
1643 (strm->param.samples_per_frame /
1644 strm->resample_factor /
1645 strm->param.channel_count) << 1);
1646 strm->rec_buf_len = 0;
1647
1648 if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_G729) {
1649 TBitStream *g729_bitstream = new TBitStream;
1650
1651 PJ_ASSERT_RETURN(g729_bitstream, PJ_ENOMEM);
1652 strm->strm_data = (void*)g729_bitstream;
1653 }
1654
1655 /* Init resampler when format is PCM and clock rate is not 8kHz */
1656 if (strm->param.clock_rate != 8000 &&
1657 strm->param.ext_fmt.id == PJMEDIA_FORMAT_L16)
1658 {
1659 pj_status_t status;
1660
1661 if (strm->param.dir & PJMEDIA_DIR_CAPTURE) {
1662 /* Create resample for recorder */
1663 status = pjmedia_resample_create( pool, PJ_TRUE, PJ_FALSE, 1,
1664 8000,
1665 strm->param.clock_rate,
1666 80,
1667 &strm->rec_resample);
1668 if (status != PJ_SUCCESS)
1669 return status;
1670 }
1671
1672 if (strm->param.dir & PJMEDIA_DIR_PLAYBACK) {
1673 /* Create resample for player */
1674 status = pjmedia_resample_create( pool, PJ_TRUE, PJ_FALSE, 1,
1675 strm->param.clock_rate,
1676 8000,
1677 80 * strm->resample_factor,
1678 &strm->play_resample);
1679 if (status != PJ_SUCCESS)
1680 return status;
1681 }
1682 }
1683
1684 /* Create PCM buffer, when the clock rate is not 8kHz or not mono */
1685 if (strm->param.ext_fmt.id == PJMEDIA_FORMAT_L16 &&
1686 (strm->resample_factor > 1 || strm->param.channel_count != 1))
1687 {
1688 strm->pcm_buf = (pj_int16_t*)pj_pool_zalloc(pool,
1689 strm->param.samples_per_frame << 1);
1690 }
1691
1692
1693 /* Create the audio engine. */
1694 TRAPD(err, strm->engine = CPjAudioEngine::NewL(strm,
1695 aps_rec_cb, aps_play_cb,
1696 strm, aps_setting));
1697 if (err != KErrNone) {
1698 pj_pool_release(pool);
1699 return PJ_RETURN_OS_ERROR(err);
1700 }
1701
1702 /* Apply output volume setting if specified */
1703 if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
1704 stream_set_cap(&strm->base, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
1705 &param->output_vol);
1706 }
1707
1708 /* Done */
1709 strm->base.op = &stream_op;
1710 *p_aud_strm = &strm->base;
1711
1712 return PJ_SUCCESS;
1713 }
1714
1715 /* API: Get stream info. */
1716 static pj_status_t stream_get_param(pjmedia_aud_stream *s,
1717 pjmedia_aud_param *pi)
1718 {
1719 struct aps_stream *strm = (struct aps_stream*)s;
1720
1721 PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
1722
1723 pj_memcpy(pi, &strm->param, sizeof(*pi));
1724
1725 /* Update the output volume setting */
1726 if (stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
1727 &pi->output_vol) == PJ_SUCCESS)
1728 {
1729 pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING;
1730 }
1731
1732 return PJ_SUCCESS;
1733 }
1734
1735 /* API: get capability */
1736 static pj_status_t stream_get_cap(pjmedia_aud_stream *s,
1737 pjmedia_aud_dev_cap cap,
1738 void *pval)
1739 {
1740 struct aps_stream *strm = (struct aps_stream*)s;
1741 pj_status_t status = PJ_ENOTSUP;
1742
1743 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
1744
1745 switch (cap) {
1746 case PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE:
1747 if (strm->param.dir & PJMEDIA_DIR_PLAYBACK) {
1748 *(pjmedia_aud_dev_route*)pval = strm->param.output_route;
1749 status = PJ_SUCCESS;
1750 }
1751 break;
1752
1753 /* There is a case that GetMaxGain() stucks, e.g: in N95. */
1754 /*
1755 case PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING:
1756 if (strm->param.dir & PJMEDIA_DIR_CAPTURE) {
1757 PJ_ASSERT_RETURN(strm->engine, PJ_EINVAL);
1758
1759 TInt max_gain = strm->engine->GetMaxGain();
1760 TInt gain = strm->engine->GetGain();
1761
1762 if (max_gain > 0 && gain >= 0) {
1763 *(unsigned*)pval = gain * 100 / max_gain;
1764 status = PJ_SUCCESS;
1765 } else {
1766 status = PJMEDIA_EAUD_NOTREADY;
1767 }
1768 }
1769 break;
1770 */
1771
1772 case PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING:
1773 if (strm->param.dir & PJMEDIA_DIR_PLAYBACK) {
1774 PJ_ASSERT_RETURN(strm->engine, PJ_EINVAL);
1775
1776 TInt max_vol = strm->engine->GetMaxVolume();
1777 TInt vol = strm->engine->GetVolume();
1778
1779 if (max_vol > 0 && vol >= 0) {
1780 *(unsigned*)pval = vol * 100 / max_vol;
1781 status = PJ_SUCCESS;
1782 } else {
1783 status = PJMEDIA_EAUD_NOTREADY;
1784 }
1785 }
1786 break;
1787 default:
1788 break;
1789 }
1790
1791 return status;
1792 }
1793
1794 /* API: set capability */
1795 static pj_status_t stream_set_cap(pjmedia_aud_stream *s,
1796 pjmedia_aud_dev_cap cap,
1797 const void *pval)
1798 {
1799 struct aps_stream *strm = (struct aps_stream*)s;
1800 pj_status_t status = PJ_ENOTSUP;
1801
1802 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
1803
1804 switch (cap) {
1805 case PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE:
1806 if (strm->param.dir & PJMEDIA_DIR_PLAYBACK) {
1807 pjmedia_aud_dev_route r = *(const pjmedia_aud_dev_route*)pval;
1808 TInt err;
1809
1810 PJ_ASSERT_RETURN(strm->engine, PJ_EINVAL);
1811
1812 switch (r) {
1813 case PJMEDIA_AUD_DEV_ROUTE_DEFAULT:
1814 case PJMEDIA_AUD_DEV_ROUTE_EARPIECE:
1815 err = strm->engine->ActivateSpeaker(EFalse);
1816 status = (err==KErrNone)? PJ_SUCCESS:PJ_RETURN_OS_ERROR(err);
1817 break;
1818 case PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER:
1819 err = strm->engine->ActivateSpeaker(ETrue);
1820 status = (err==KErrNone)? PJ_SUCCESS:PJ_RETURN_OS_ERROR(err);
1821 break;
1822 default:
1823 status = PJ_EINVAL;
1824 break;
1825 }
1826 if (status == PJ_SUCCESS)
1827 strm->param.output_route = r;
1828 }
1829 break;
1830
1831 /* There is a case that GetMaxGain() stucks, e.g: in N95. */
1832 /*
1833 case PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING:
1834 if (strm->param.dir & PJMEDIA_DIR_CAPTURE) {
1835 PJ_ASSERT_RETURN(strm->engine, PJ_EINVAL);
1836
1837 TInt max_gain = strm->engine->GetMaxGain();
1838 if (max_gain > 0) {
1839 TInt gain, err;
1840
1841 gain = *(unsigned*)pval * max_gain / 100;
1842 err = strm->engine->SetGain(gain);
1843 status = (err==KErrNone)? PJ_SUCCESS:PJ_RETURN_OS_ERROR(err);
1844 } else {
1845 status = PJMEDIA_EAUD_NOTREADY;
1846 }
1847 if (status == PJ_SUCCESS)
1848 strm->param.input_vol = *(unsigned*)pval;
1849 }
1850 break;
1851 */
1852
1853 case PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING:
1854 if (strm->param.dir & PJMEDIA_DIR_PLAYBACK) {
1855 PJ_ASSERT_RETURN(strm->engine, PJ_EINVAL);
1856
1857 TInt max_vol = strm->engine->GetMaxVolume();
1858 if (max_vol > 0) {
1859 TInt vol, err;
1860
1861 vol = *(unsigned*)pval * max_vol / 100;
1862 err = strm->engine->SetVolume(vol);
1863 status = (err==KErrNone)? PJ_SUCCESS:PJ_RETURN_OS_ERROR(err);
1864 } else {
1865 status = PJMEDIA_EAUD_NOTREADY;
1866 }
1867 if (status == PJ_SUCCESS)
1868 strm->param.output_vol = *(unsigned*)pval;
1869 }
1870 break;
1871 default:
1872 break;
1873 }
1874
1875 return status;
1876 }
1877
1878 /* API: Start stream. */
1879 static pj_status_t stream_start(pjmedia_aud_stream *strm)
1880 {
1881 struct aps_stream *stream = (struct aps_stream*)strm;
1882
1883 PJ_ASSERT_RETURN(stream, PJ_EINVAL);
1884
1885 if (stream->engine) {
1886 TInt err = stream->engine->StartL();
1887 if (err != KErrNone)
1888 return PJ_RETURN_OS_ERROR(err);
1889 }
1890
1891 return PJ_SUCCESS;
1892 }
1893
1894 /* API: Stop stream. */
1895 static pj_status_t stream_stop(pjmedia_aud_stream *strm)
1896 {
1897 struct aps_stream *stream = (struct aps_stream*)strm;
1898
1899 PJ_ASSERT_RETURN(stream, PJ_EINVAL);
1900
1901 if (stream->engine) {
1902 stream->engine->Stop();
1903 }
1904
1905 return PJ_SUCCESS;
1906 }
1907
1908
1909 /* API: Destroy stream. */
1910 static pj_status_t stream_destroy(pjmedia_aud_stream *strm)
1911 {
1912 struct aps_stream *stream = (struct aps_stream*)strm;
1913
1914 PJ_ASSERT_RETURN(stream, PJ_EINVAL);
1915
1916 stream_stop(strm);
1917
1918 delete stream->engine;
1919 stream->engine = NULL;
1920
1921 if (stream->param.ext_fmt.id == PJMEDIA_FORMAT_G729) {
1922 TBitStream *g729_bitstream = (TBitStream*)stream->strm_data;
1923 stream->strm_data = NULL;
1924 delete g729_bitstream;
1925 }
1926
1927 pj_pool_t *pool;
1928 pool = stream->pool;
1929 if (pool) {
1930 stream->pool = NULL;
1931 pj_pool_release(pool);
1932 }
1933
1934 return PJ_SUCCESS;
1935 }
1936
1937 #endif // PJMEDIA_AUDIO_DEV_HAS_SYMB_APS
1938