-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmedia_tool.c
2609 lines (2134 loc) · 77.2 KB
/
media_tool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* framework/media/media_tool.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef CONFIG_LIBUV_EXTENSION
#include <uv.h>
#include <uv_async_queue.h>
#endif
#include <media_api.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MEDIATOOL_MAX_CHAIN 16
#define MEDIATOOL_MAX_ARGC 16
#define MEDIATOOL_MAX_SIZE 2048
#define MEDIATOOL_PLAYER 1
#define MEDIATOOL_RECORDER 2
#define MEDIATOOL_CONTROLLER 3
#define MEDIATOOL_CONTROLLEE 4
#define MEDIATOOL_FOCUS 5
#define MEDIATOOL_UVPLAYER 6
#define MEDIATOOL_UVRECORDER 7
#define MEDIATOOL_UVFOCUS 8
#define MEDIATOOL_UVCONTROLLER 9
#define MEDIATOOL_UVCONTROLLEE 10
#define MEDIATOOL_POLICY 11
#define GET_ARG_FUNC(out_type, arg) \
static out_type get_##out_type##_arg(char* arg); \
static out_type get_##out_type##_arg(char* arg)
#define GET_ARG(out_type, arg) \
get_##out_type##_arg(arg)
#define CMD0(func) \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool); \
static int mediatool_cmd_##func(mediatool_t* mediatool, int argc, char** argv) \
{ \
(void)argc; \
(void)argv; \
return mediatool_cmd_##func##_exec(mediatool); \
} \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool)
#define CMD1(func, type1, arg1) \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1); \
static int mediatool_cmd_##func(mediatool_t* mediatool, int argc, char** argv) \
{ \
type1 arg1; \
arg1 = (argc > 1) ? GET_ARG(type1, argv[1]) : 0; \
return mediatool_cmd_##func##_exec(mediatool, arg1); \
} \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1)
#define CMD2(func, type1, arg1, type2, arg2) \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1, type2 arg2); \
static int mediatool_cmd_##func(mediatool_t* mediatool, int argc, char** argv) \
{ \
type1 arg1; \
type2 arg2; \
arg1 = (argc > 1) ? GET_ARG(type1, argv[1]) : 0; \
arg2 = (argc > 2) ? GET_ARG(type2, argv[2]) : 0; \
return mediatool_cmd_##func##_exec(mediatool, arg1, arg2); \
} \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1, type2 arg2)
#define CMD3(func, type1, arg1, type2, arg2, type3, arg3) \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1, type2 arg2, type3 arg3); \
static int mediatool_cmd_##func(mediatool_t* mediatool, int argc, char** argv) \
{ \
type1 arg1; \
type2 arg2; \
type3 arg3; \
arg1 = (argc > 1) ? GET_ARG(type1, argv[1]) : 0; \
arg2 = (argc > 2) ? GET_ARG(type2, argv[2]) : 0; \
arg3 = (argc > 3) ? GET_ARG(type3, argv[3]) : 0; \
return mediatool_cmd_##func##_exec(mediatool, arg1, arg2, arg3); \
} \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1, type2 arg2, type3 arg3)
#define CMD4(func, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1, type2 arg2, type3 arg3, type4 arg4); \
static int mediatool_cmd_##func(mediatool_t* mediatool, int argc, char** argv) \
{ \
type1 arg1; \
type2 arg2; \
type3 arg3; \
type4 arg4; \
arg1 = (argc > 1) ? GET_ARG(type1, argv[1]) : 0; \
arg2 = (argc > 2) ? GET_ARG(type2, argv[2]) : 0; \
arg3 = (argc > 3) ? GET_ARG(type3, argv[3]) : 0; \
arg4 = (argc > 4) ? GET_ARG(type4, argv[4]) : 0; \
return mediatool_cmd_##func##_exec(mediatool, arg1, arg2, arg3, arg4); \
} \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, type1 arg1, type2 arg2, type3 arg3, type4 arg4)
#define CMD8(func, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6, t7, a7, t8, a8) \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8); \
static int mediatool_cmd_##func(mediatool_t* mediatool, int argc, char** argv) \
{ \
t1 a1; \
t2 a2; \
t3 a3; \
t4 a4; \
t5 a5; \
t6 a6; \
t7 a7; \
t8 a8; \
a1 = (argc > 1) ? GET_ARG(t1, argv[1]) : 0; \
a2 = (argc > 2) ? GET_ARG(t2, argv[2]) : 0; \
a3 = (argc > 3) ? GET_ARG(t3, argv[3]) : 0; \
a4 = (argc > 4) ? GET_ARG(t4, argv[4]) : 0; \
a5 = (argc > 5) ? GET_ARG(t5, argv[5]) : 0; \
a6 = (argc > 6) ? GET_ARG(t6, argv[6]) : 0; \
a7 = (argc > 7) ? GET_ARG(t7, argv[7]) : 0; \
a8 = (argc > 8) ? GET_ARG(t8, argv[8]) : 0; \
return mediatool_cmd_##func##_exec(mediatool, a1, a2, a3, a4, a5, a6, a7, a8); \
} \
static int mediatool_cmd_##func##_exec(mediatool_t* mediatool, t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8)
/****************************************************************************
* Type Declarations
****************************************************************************/
typedef struct mediatool_chain_s {
int id;
int type;
void* handle;
void* extra;
pthread_t thread;
int fd;
bool start;
bool loop;
bool direct;
bool direct_connect;
char* buf;
int size;
#ifdef CONFIG_LIBUV_EXTENSION
void* data;
uv_pipe_t* pipe; /* pipe handle object */
uv_fs_t fs_req;
uv_write_t write_req;
#endif
} mediatool_chain_t;
typedef struct mediatool_s {
mediatool_chain_t chain[MEDIATOOL_MAX_CHAIN];
#ifdef CONFIG_LIBUV_EXTENSION
uv_loop_t loop;
uv_async_queue_t asyncq;
#endif
} mediatool_t;
typedef int (*mediatool_func)(mediatool_t* mediatool, int argc, char** argv);
typedef struct mediatool_cmd_s {
const char* cmd; /* The command text */
mediatool_func pfunc; /* Pointer to command handler */
const char* help; /* The help text */
} mediatool_cmd_t;
typedef char* string_t;
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
#ifdef CONFIG_LIBUV_EXTENSION
/* uv recorder buffer mode functions declaration */
static void mediatool_uv_recorder_alloc_cb(uv_handle_t* handle,
size_t suggested_size, uv_buf_t* buf);
static void mediatool_uv_recorder_write_cb(uv_fs_t* req);
static void mediatool_uv_recorder_read_cb(uv_stream_t* stream,
ssize_t nread, const uv_buf_t* buf);
static void mediatool_uv_recorder_connection_cb(void* cookie, int ret, void* obj);
/* uv player buffer mode functions declaration */
static void mediatool_uv_player_write_cb(uv_write_t* req, int status);
static void mediatool_uv_player_read_cb(uv_fs_t* req);
static void mediatool_uv_player_connection_cb(void* cookie, int ret, void* obj);
#endif
/****************************************************************************
* Private Function
****************************************************************************/
GET_ARG_FUNC(int, arg)
{
return strtol(arg, NULL, 0);
}
GET_ARG_FUNC(string_t, arg)
{
if (arg && !strlen(arg))
return NULL;
return arg;
}
static void mediatool_controller_callback(void* cookie, int event,
int ret, const char* extra)
{
mediatool_chain_t* chain = cookie;
printf("[%s][%d] id:%d, event:%s(%d) ret:%d extra:%s\n",
__func__, __LINE__, chain->id, media_event_get_name(event), event, ret, extra);
}
static void mediatool_controllee_callback(void* cookie, int event,
int ret, const char* extra)
{
mediatool_chain_t* chain = cookie;
printf("[%s][%d] id:%d, event:%s(%d) ret:%d extra:%s\n",
__func__, __LINE__, chain->id, media_event_get_name(event), event, ret, extra);
/* Assume we've done real work and notify the result. */
media_session_notify(chain->handle, event, 0, "fake");
}
static void mediatool_controllee_music_callback(void* cookie, int event,
int ret, const char* extra)
{
mediatool_chain_t* chain = cookie;
printf("[%s][%d] id:%d, event:%s(%d) ret:%d extra:%s\n",
__func__, __LINE__, chain->id, media_event_get_name(event), event, ret, extra);
/* Handle control message by mediatool api. */
switch (event) {
case MEDIA_EVENT_START:
ret = media_player_start(chain->handle);
break;
case MEDIA_EVENT_PAUSE:
ret = media_player_pause(chain->handle);
break;
case MEDIA_EVENT_STOP:
ret = media_player_stop(chain->handle);
break;
case MEDIA_EVENT_INCREASE_VOLUME:
ret = media_policy_decrease_stream_volume(MEDIA_STREAM_MUSIC);
break;
case MEDIA_EVENT_DECREASE_VOLUME:
ret = media_policy_increase_stream_volume(MEDIA_STREAM_MUSIC);
break;
default:
/* There is no song list in this demo. */
ret = -ENOSYS;
break;
}
/* XXX: Another implemention for some status control message,
* notify after received corresponding event from player's event callback */
media_session_notify(chain->handle, event, ret, NULL);
}
static void mediatool_event_callback(void* cookie, int event,
int ret, const char* extra)
{
mediatool_chain_t* chain = cookie;
media_metadata_t meta = { 0 };
printf("[%s][%d] id:%d, event:%s(%d) ret:%d extra:%s\n",
__func__, __LINE__, chain->id, media_event_get_name(event), event, ret, extra);
if (event == MEDIA_EVENT_STARTED) {
meta.flags = MEDIA_METAFLAG_STATE;
meta.state = 1;
media_session_update(chain->extra, &meta);
} else if (event == MEDIA_EVENT_STOPPED || event == MEDIA_EVENT_PAUSED || event == MEDIA_EVENT_COMPLETED) {
meta.flags = MEDIA_METAFLAG_STATE;
meta.state = 0;
media_session_update(chain->extra, &meta);
}
}
static void mediatool_takepic_callback(void* cookie, int event,
int ret, const char* extra)
{
struct mediatool_chain_s* chain = cookie;
if (event == MEDIA_EVENT_COMPLETED) {
media_recorder_finish_picture(chain->handle);
chain->handle = NULL;
chain->extra = NULL;
}
printf("[%s][%d] id:%d, event:%s(%d) ret:%d extra:%s\n",
__func__, __LINE__, chain->id, media_event_get_name(event), event, ret, extra);
}
static void mediatool_focus_callback(int suggestion, void* cookie)
{
mediatool_chain_t* chain = cookie;
char* str;
if (suggestion == MEDIA_FOCUS_PLAY) {
str = "MEDIA_FOCUS_PLAY";
} else if (suggestion == MEDIA_FOCUS_STOP) {
str = "MEDIA_FOCUS_STOP";
} else if (suggestion == MEDIA_FOCUS_PAUSE) {
str = "MEDIA_FOCUS_PAUSE";
} else if (suggestion == MEDIA_FOCUS_PLAY_BUT_SILENT) {
str = "MEDIA_FOCUS_PLAY_BUT_SILENT";
} else if (suggestion == MEDIA_FOCUS_PLAY_WITH_DUCK) {
str = "MEDIA_FOCUS_PLAY_WITH_DUCK";
} else if (suggestion == MEDIA_FOCUS_PLAY_WITH_KEEP) {
str = "MEDIA_FOCUS_PLAY_WITH_KEEP";
} else {
str = "UNKOWN";
}
printf("%s, id %d, suggestion %s, suggestion %d, line %d\n",
__func__, chain->id, str, suggestion, __LINE__);
}
static void mediatool_policy_callback(void* cookie, int number, const char* literal)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d number:%d value:%s\n",
__func__, chain->id, number, literal);
}
static void mediatool_display_metadata(int id, const media_metadata_t* data)
{
printf("id:%d f:%d st:%d vol:%d pos:%u dur:%u ttl:%s art:%s\n", id,
data->flags, data->state, data->volume, data->position, data->duration,
data->title, data->artist);
}
#ifdef CONFIG_LIBUV_EXTENSION
static void mediatool_uv_common_close_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
chain->handle = NULL;
chain->id = 0;
}
static void mediatool_uv_common_open_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_prepare_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_start_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_pause_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_stop_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
free(chain->buf);
chain->buf = NULL;
}
static void mediatool_uv_player_reset_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_get_playing_cb(void* cookie, int ret, int isplaying)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d val:%u\n", __func__, chain->id, ret, isplaying);
}
static void mediatool_uv_common_get_position_cb(void* cookie, int ret, unsigned position)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d val:%u\n", __func__, chain->id, ret, position);
}
static void mediatool_uv_common_get_duration_cb(void* cookie, int ret, unsigned duration)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d val:%u\n", __func__, chain->id, ret, duration);
}
static void mediatool_uv_common_get_latency_cb(void* cookie, int ret, unsigned latency)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d val:%u\n", __func__, chain->id, ret, latency);
}
static void mediatool_uv_common_get_volume_cb(void* cookie, int ret, float volume)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d val:%f\n", __func__, chain->id, ret, volume);
}
static void mediatool_uv_common_increase_volume_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_decrease_volume_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_set_volume_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_query_cb(void* cookie, int ret, void* object)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
mediatool_display_metadata(chain->id, object);
}
static void mediatool_uv_player_set_looping_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_common_seek_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_recorder_reset_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void media_uv_session_prev_song_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void media_uv_session_next_song_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_controllee_callback(void* cookie, int event,
int ret, const char* extra)
{
mediatool_chain_t* chain = cookie;
printf("[%s][%d] id:%d, event:%s(%d) ret:%d extra:%s\n",
__func__, __LINE__, chain->id, media_event_get_name(event), event, ret, extra);
/* Assume we've done real work and notify the result. */
media_uv_session_notify(chain->handle, event, 0, "fake", NULL, NULL);
}
static void mediatool_cmd_uv_policy_set_string_cb(void* cookie, int ret)
{
printf("[%s] name:%s ret:%d\n", __func__, (char*)cookie, ret);
free(cookie);
}
static void mediatool_cmd_uv_policy_include_cb(void* cookie, int ret)
{
printf("[%s] name:%s ret:%d\n", __func__, (char*)cookie, ret);
free(cookie);
}
static void mediatool_cmd_uv_policy_set_int_cb(void* cookie, int ret)
{
printf("[%s] name:%s ret:%d\n", __func__, (char*)cookie, ret);
free(cookie);
}
static void mediatool_cmd_uv_policy_contain_cb(void* cookie, int ret, int val)
{
printf("[%s] name:%s ret:%d val:%d\n", __func__, (char*)cookie, ret, val);
free(cookie);
}
static void mediatool_cmd_uv_policy_get_string_cb(void* cookie, int ret, const char* val)
{
printf("[%s] name:%s ret:%d val:%s\n", __func__, (char*)cookie, ret, val);
free(cookie);
}
static void mediatool_cmd_uv_policy_get_int_cb(void* cookie, int ret, int val)
{
printf("[%s] name:%s ret:%d val:%d\n", __func__, (char*)cookie, ret, val);
free(cookie);
}
static void mediatool_focus_suggest_cb(int suggest, void* cookie)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d suggest:%d\n", __func__, chain->id, suggest);
}
static void mediatool_cmd_uv_policy_increase_cb(void* cookie, int ret)
{
printf("[%s] name:%s ret:%d\n", __func__, (char*)cookie, ret);
free(cookie);
}
static void mediatool_cmd_uv_policy_exclude_cb(void* cookie, int ret)
{
printf("[%s] name:%s ret:%d\n", __func__, (char*)cookie, ret);
free(cookie);
}
static void mediatool_cmd_uv_policy_decrease_cb(void* cookie, int ret)
{
printf("[%s] name:%s ret:%d\n", __func__, (char*)cookie, ret);
free(cookie);
}
static void mediatool_uv_session_open_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_session_update_cb(void* cookie, int ret)
{
mediatool_chain_t* chain = cookie;
printf("[%s] id:%d ret:%d\n", __func__, chain->id, ret);
}
static void mediatool_uv_take_picture_complete_cb(void* cookie, int ret)
{
if (ret < 0)
printf("take pic failed!\n");
else
printf("take pic successed!\n");
}
static void mediatool_uv_recorder_alloc_cb(uv_handle_t* handle,
size_t suggested_size, uv_buf_t* buf)
{
mediatool_chain_t* chain = uv_handle_get_data(handle);
if (chain->buf) {
buf->base = NULL;
buf->len = 0;
return;
}
buf->base = malloc(MEDIATOOL_MAX_SIZE);
assert(buf->base);
buf->len = MEDIATOOL_MAX_SIZE;
}
static void mediatool_uv_recorder_write_cb(uv_fs_t* req)
{
mediatool_chain_t* chain = uv_req_get_data((uv_req_t*)req);
if (req->result < 0) {
printf("[%s][%d] Recorder write to file Failed: %s\n", __func__,
__LINE__, uv_strerror(req->result));
free(chain->buf);
chain->buf = NULL;
return;
}
free(chain->buf);
chain->buf = NULL;
uv_fs_req_cleanup(req);
}
static void mediatool_uv_recorder_read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
{
mediatool_chain_t* chain = uv_handle_get_data((uv_handle_t*)stream);
mediatool_t* mediatool = chain->data;
uv_fs_t close_req;
uv_buf_t iov;
if (nread == UV_ENOBUFS) {
usleep(1000);
return;
}
assert(nread <= MEDIATOOL_MAX_SIZE);
chain->buf = buf->base;
uv_req_set_data((uv_req_t*)&chain->fs_req, chain);
if (nread < 0) {
if (nread != UV_EOF)
printf("[%s][%d] Recorder read error %s\n", __func__, __LINE__, uv_err_name(nread));
uv_fs_close(&mediatool->loop, &close_req, chain->fd, NULL);
free(chain->buf);
chain->buf = NULL;
return;
}
iov = uv_buf_init(buf->base, nread);
uv_fs_write(&mediatool->loop, &chain->fs_req, chain->fd,
&iov, 1, -1, mediatool_uv_recorder_write_cb);
}
static void mediatool_uv_recorder_connection_cb(void* cookie, int ret, void* obj)
{
mediatool_chain_t* chain = cookie;
printf("[%s][%d] id:%d ret:%d obj:%p\n", __func__, __LINE__, chain->id, ret, obj);
if (!obj)
return;
uv_handle_set_data(obj, cookie);
uv_read_start(obj, mediatool_uv_recorder_alloc_cb, mediatool_uv_recorder_read_cb);
}
static void mediatool_uv_player_write_cb(uv_write_t* req, int status)
{
mediatool_chain_t* chain = uv_req_get_data((uv_req_t*)req);
mediatool_t* mediatool = chain->data;
uv_buf_t iov;
if (status < 0) {
printf("[%s][%d] Player write error: %s\n", __func__, __LINE__, uv_strerror(status));
free(chain->buf);
chain->buf = NULL;
return;
} else {
iov = uv_buf_init(chain->buf, chain->size);
uv_fs_read(&mediatool->loop, &chain->fs_req, chain->fd,
&iov, 1, -1, mediatool_uv_player_read_cb);
}
}
static void mediatool_uv_player_read_cb(uv_fs_t* req)
{
mediatool_chain_t* chain = uv_req_get_data((uv_req_t*)req);
mediatool_t* mediatool = chain->data;
uv_fs_t close_req;
uv_buf_t iov;
if (req->result < 0) {
printf("[%s][%d] Player Read error: %s\n", __func__, __LINE__,
uv_strerror(req->result));
free(chain->buf);
chain->buf = NULL;
return;
} else if (req->result == 0) {
printf("[%s][%d] Player read to end of file\n", __func__, __LINE__);
uv_fs_close(&mediatool->loop, &close_req, chain->fd, NULL);
} else {
iov = uv_buf_init(chain->buf, req->result);
uv_req_set_data((uv_req_t*)&chain->write_req, chain);
uv_write((uv_write_t*)&chain->write_req, (uv_stream_t*)chain->pipe,
&iov, 1, mediatool_uv_player_write_cb);
}
}
static void mediatool_uv_player_connection_cb(void* cookie, int ret, void* obj)
{
mediatool_chain_t* chain = cookie;
mediatool_t* mediatool = chain->data;
uv_buf_t iov;
printf("[%s][%d] id:%d ret:%d obj:%p\n", __func__, __LINE__, chain->id, ret, obj);
chain->size = MEDIATOOL_MAX_SIZE;
chain->buf = malloc(MEDIATOOL_MAX_SIZE);
assert(chain->buf);
chain->pipe = obj;
iov = uv_buf_init(chain->buf, chain->size);
uv_req_set_data((uv_req_t*)&chain->fs_req, chain);
uv_fs_read(&mediatool->loop, &chain->fs_req, chain->fd,
&iov, 1, -1, mediatool_uv_player_read_cb);
}
#endif /* CONFIG_LIBUV_EXTENSION */
static void mediatool_common_stop_thread(mediatool_chain_t* chain)
{
if (chain->thread) {
pthread_join(chain->thread, NULL);
free(chain->buf);
chain->thread = 0;
chain->buf = NULL;
}
if (chain->fd > 0) {
close(chain->fd);
chain->fd = -1;
}
}
static int mediatool_common_stop_inner(mediatool_chain_t* chain)
{
int ret = 0;
switch (chain->type) {
case MEDIATOOL_PLAYER:
ret = media_player_stop(chain->handle);
break;
case MEDIATOOL_RECORDER:
if (chain->direct) {
chain->direct_connect = false;
pthread_join(chain->thread, NULL);
}
ret = media_recorder_stop(chain->handle);
break;
#ifdef CONFIG_LIBUV_EXTENSION
case MEDIATOOL_UVPLAYER:
ret = media_uv_player_stop(chain->handle, mediatool_uv_common_stop_cb, chain);
break;
case MEDIATOOL_UVRECORDER:
ret = media_uv_recorder_stop(chain->handle, mediatool_uv_common_stop_cb, chain);
break;
case MEDIATOOL_UVCONTROLLER:
ret = media_uv_session_stop(chain->handle, mediatool_uv_common_stop_cb, chain);
break;
#endif
}
usleep(1000);
mediatool_common_stop_thread(chain);
return ret;
}
CMD1(player_open, string_t, stream_type)
{
long int i;
int ret;
for (i = 0; i < MEDIATOOL_MAX_CHAIN; i++) {
if (!mediatool->chain[i].handle)
break;
}
if (i == MEDIATOOL_MAX_CHAIN)
return -ENOMEM;
mediatool->chain[i].id = i;
mediatool->chain[i].handle = media_player_open(stream_type);
if (!mediatool->chain[i].handle) {
printf("media_player_open error\n");
return -EINVAL;
}
ret = media_player_set_event_callback(mediatool->chain[i].handle,
&mediatool->chain[i], mediatool_event_callback);
assert(!ret);
if (stream_type && !strcmp(stream_type, MEDIA_STREAM_MUSIC)) {
mediatool->chain[i].extra = media_session_register(
&mediatool->chain[i], mediatool_controllee_music_callback);
}
mediatool->chain[i].type = MEDIATOOL_PLAYER;
printf("player ID %ld\n", i);
return 0;
}
CMD1(recorder_open, string_t, stream_type)
{
long int i;
int ret;
for (i = 0; i < MEDIATOOL_MAX_CHAIN; i++) {
if (!mediatool->chain[i].handle)
break;
}
if (i == MEDIATOOL_MAX_CHAIN)
return -ENOMEM;
mediatool->chain[i].id = i;
mediatool->chain[i].handle = media_recorder_open(stream_type);
if (!mediatool->chain[i].handle) {
printf("media_recorder_open error\n");
return -EINVAL;
}
ret = media_recorder_set_event_callback(mediatool->chain[i].handle,
&mediatool->chain[i],
mediatool_event_callback);
assert(!ret);
mediatool->chain[i].type = MEDIATOOL_RECORDER;
printf("recorder ID %ld\n", i);
return 0;
}
CMD1(session_open, string_t, stream_type)
{
long int i;
int ret;
for (i = 0; i < MEDIATOOL_MAX_CHAIN; i++) {
if (!mediatool->chain[i].handle)
break;
}
if (i == MEDIATOOL_MAX_CHAIN)
return -ENOMEM;
mediatool->chain[i].id = i;
mediatool->chain[i].handle = media_session_open(stream_type);
if (!mediatool->chain[i].handle) {
printf("media_session_open error\n");
return -EINVAL;
}
ret = media_session_set_event_callback(mediatool->chain[i].handle,
&mediatool->chain[i], mediatool_controller_callback);
assert(!ret);
mediatool->chain[i].type = MEDIATOOL_CONTROLLER;
printf("session controller ID %ld\n", i);
return 0;
}
CMD1(session_register, string_t, param)
{
long int i;
(void)param;
for (i = 0; i < MEDIATOOL_MAX_CHAIN; i++) {
if (!mediatool->chain[i].handle)
break;
}
if (i == MEDIATOOL_MAX_CHAIN)
return -ENOMEM;
mediatool->chain[i].id = i;
mediatool->chain[i].handle = media_session_register(
&mediatool->chain[i], mediatool_controllee_callback);
if (!mediatool->chain[i].handle) {
printf("media_session_register error\n");
return -EINVAL;
}
mediatool->chain[i].type = MEDIATOOL_CONTROLLEE;
printf("session controllee ID %ld\n", i);
return 0;
}
CMD2(close, int, id, int, pending_stop)
{
int ret = -EINVAL;
if (id < 0 || id >= MEDIATOOL_MAX_CHAIN || !mediatool->chain[id].handle)
return -EINVAL;
switch (mediatool->chain[id].type) {
case MEDIATOOL_PLAYER:
if (!pending_stop)
mediatool_common_stop_inner(&mediatool->chain[id]);
ret = media_player_close(mediatool->chain[id].handle, pending_stop);
if (ret >= 0 && mediatool->chain[id].extra)
ret = media_session_unregister(mediatool->chain[id].extra);
break;
case MEDIATOOL_RECORDER:
mediatool_common_stop_inner(&mediatool->chain[id]);
ret = media_recorder_close(mediatool->chain[id].handle);
break;
case MEDIATOOL_CONTROLLER:
ret = media_session_close(mediatool->chain[id].handle);
break;
case MEDIATOOL_CONTROLLEE:
ret = media_session_unregister(mediatool->chain[id].handle);
break;
case MEDIATOOL_FOCUS:
ret = media_focus_abandon(mediatool->chain[id].handle);
break;
case MEDIATOOL_POLICY:
ret = media_policy_unsubscribe(mediatool->chain[id].handle);
break;
#ifdef CONFIG_LIBUV_EXTENSION
case MEDIATOOL_UVPLAYER:
if (!pending_stop)
mediatool_common_stop_inner(&mediatool->chain[id]);
ret = media_uv_player_close(mediatool->chain[id].handle, pending_stop, mediatool_uv_common_close_cb);
break;
case MEDIATOOL_UVRECORDER:
mediatool_common_stop_inner(&mediatool->chain[id]);
ret = media_uv_recorder_close(mediatool->chain[id].handle, mediatool_uv_common_close_cb);
break;
case MEDIATOOL_UVFOCUS:
ret = media_uv_focus_abandon(mediatool->chain[id].handle, mediatool_uv_common_close_cb);
break;
case MEDIATOOL_UVCONTROLLER:
ret = media_uv_session_close(mediatool->chain[id].handle, mediatool_uv_common_close_cb);
break;
case MEDIATOOL_UVCONTROLLEE:
ret = media_uv_session_unregister(mediatool->chain[id].handle, mediatool_uv_common_close_cb);
break;
#endif /* CONFIG_LIBUV_EXTENSION */
}
mediatool->chain[id].handle = NULL;
mediatool->chain[id].extra = NULL;
return ret;