Mercurial > pt1.oyama
annotate libdlna-0.2.3/src/containers.c @ 129:4f6d9621ee00
add multi session streaming & add depending librarys.
- libupnp-1.6.6
- libdlna-0.2.3
author | Naoya OYAMA <naoya.oyama@gmail.com> |
---|---|
date | Sun, 10 Oct 2010 15:33:18 +0900 |
parents | |
children |
rev | line source |
---|---|
129
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
1 #include <string.h> |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
2 #include <unistd.h> |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
3 #include <sys/types.h> |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
4 #include <sys/stat.h> |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
5 #include <fcntl.h> |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
6 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
7 #include <ffmpeg/avformat.h> |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
8 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
9 #include "containers.h" |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
10 #include "profiles.h" |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
11 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
12 #define MPEG_PACK_HEADER 0xba |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
13 #define MPEG_TS_SYNC_CODE 0x47 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
14 #define MPEG_TS_PACKET_LENGTH 188 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
15 #define MPEG_TS_PACKET_LENGTH_DLNA 192 /* prepends 4 bytes to TS packet */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
16 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
17 static const struct { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
18 const char *name; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
19 const dlna_container_type_t type; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
20 } avf_format_mapping[] = { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
21 { "image2", CT_IMAGE }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
22 { "asf", CT_ASF }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
23 { "amr", CT_AMR }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
24 { "aac", CT_AAC }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
25 { "ac3", CT_AC3 }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
26 { "mp3", CT_MP3 }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
27 { "wav", CT_WAV }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
28 { "mov,mp4,m4a,3gp,3g2,mj2", CT_MOV }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
29 { "mpeg", CT_FF_MPEG }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
30 { "mpegts", CT_FF_MPEG_TS }, |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
31 { NULL, 0 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
32 }; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
33 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
34 static dlna_container_type_t |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
35 mpeg_find_container_type (const char *filename) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
36 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
37 unsigned char buffer[2*MPEG_TS_PACKET_LENGTH_DLNA+1]; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
38 int fd, i; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
39 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
40 /* read file header */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
41 fd = open (filename, O_RDONLY); |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
42 read (fd, buffer, 2 * MPEG_TS_PACKET_LENGTH_DLNA); |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
43 close (fd); |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
44 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
45 /* check for MPEG-TS container */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
46 for (i = 0; i < MPEG_TS_PACKET_LENGTH; i++) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
47 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
48 if (buffer[i] == MPEG_TS_SYNC_CODE) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
49 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
50 if (buffer[i + MPEG_TS_PACKET_LENGTH] == MPEG_TS_SYNC_CODE) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
51 return CT_MPEG_TRANSPORT_STREAM; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
52 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
53 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
54 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
55 /* check for DLNA specific MPEG-TS container */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
56 for (i = 0; i < MPEG_TS_PACKET_LENGTH_DLNA; i++) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
57 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
58 if (buffer[i] == MPEG_TS_SYNC_CODE) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
59 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
60 if (buffer[i + MPEG_TS_PACKET_LENGTH_DLNA] == MPEG_TS_SYNC_CODE) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
61 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
62 if (buffer[i] == 0x00 && buffer [i+1] == 0x00 && |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
63 buffer [i+2] == 0x00 && buffer [i+3] == 0x00) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
64 return CT_MPEG_TRANSPORT_STREAM_DLNA_NO_TS; /* empty timestamp */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
65 else |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
66 return CT_MPEG_TRANSPORT_STREAM_DLNA; /* valid timestamp */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
67 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
68 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
69 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
70 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
71 /* check for MPEG-PS and MPEG-(P)ES container */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
72 if (buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0x01) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
73 return (buffer[3] == MPEG_PACK_HEADER) ? |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
74 CT_MPEG_PROGRAM_STREAM : CT_MPEG_ELEMENTARY_STREAM; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
75 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
76 return CT_UNKNOWN; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
77 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
78 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
79 static dlna_container_type_t |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
80 mov_find_container_type (const char *filename) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
81 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
82 if (!filename) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
83 return CT_UNKNOWN; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
84 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
85 if (!strcasecmp (get_file_extension (filename), "3gp") || |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
86 !strcasecmp (get_file_extension (filename), "3gpp") || |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
87 !strcasecmp (get_file_extension (filename), "3g2")) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
88 return CT_3GP; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
89 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
90 return CT_MP4; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
91 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
92 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
93 dlna_container_type_t |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
94 stream_get_container (AVFormatContext *ctx) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
95 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
96 int i; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
97 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
98 #ifdef HAVE_DEBUG |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
99 fprintf (stderr, "Found container: %s\n", ctx->iformat->name); |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
100 #endif /* HAVE_DEBUG */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
101 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
102 for (i = 0; avf_format_mapping[i].name; i++) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
103 if (!strcmp (ctx->iformat->name, avf_format_mapping[i].name)) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
104 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
105 switch (avf_format_mapping[i].type) |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
106 { |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
107 case CT_FF_MPEG: |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
108 case CT_FF_MPEG_TS: |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
109 return mpeg_find_container_type (ctx->filename); |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
110 case CT_MOV: |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
111 return mov_find_container_type (ctx->filename); |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
112 default: |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
113 return avf_format_mapping[i].type; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
114 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
115 } |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
116 |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
117 /* unknown or invalid container */ |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
118 return CT_UNKNOWN; |
4f6d9621ee00
add multi session streaming & add depending librarys.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff
changeset
|
119 } |